From 7eef51a27406bdfb24c29b4b1ff5f3303711ad59 Mon Sep 17 00:00:00 2001 From: bdysvik Date: Mon, 9 Jan 2017 14:22:59 +0100 Subject: [PATCH 1/2] updating the snowflake example so that it is compatible with latest glTF specification --- examples/js/loaders/GLTFLoader.js | 8 +- examples/models/gltf/snowflake/line_frag.glsl | 4 +- examples/models/gltf/snowflake/line_vert.glsl | 8 +- examples/models/gltf/snowflake/snowFlake.gltf | 53 ++++++++++++-- .../models/gltf/snowflakes/line_frag.glsl | 4 +- .../models/gltf/snowflakes/line_vert.glsl | 8 +- .../models/gltf/snowflakes/snowFlakes.gltf | 73 +++++++++++++++---- 7 files changed, 123 insertions(+), 35 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 78587542cd6b43..5c5a67496b9b05 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -1241,7 +1241,9 @@ THREE.GLTFLoader = ( function () { var attributes = primitive.attributes; - _each( attributes, function( attributeEntry, attributeId ) { + for ( var attributeId in attributes ) { + + var attributeEntry = attributes[ attributeId ]; if ( !attributeEntry ) { @@ -1257,13 +1259,15 @@ THREE.GLTFLoader = ( function () { geometry.addAttribute( 'position', bufferAttribute ); break; + case 'COLOR_0': + case 'COLOR0': case 'COLOR': geometry.addAttribute( 'color', bufferAttribute ); break; } - }); + }; if ( primitive.indices ) { diff --git a/examples/models/gltf/snowflake/line_frag.glsl b/examples/models/gltf/snowflake/line_frag.glsl index b3b1579fd29caa..de4f7893987708 100644 --- a/examples/models/gltf/snowflake/line_frag.glsl +++ b/examples/models/gltf/snowflake/line_frag.glsl @@ -1,8 +1,8 @@ precision highp float; -varying vec3 vertexColor; +varying vec3 v_color; void main(void) { - gl_FragColor = vec4(vertexColor.r, vertexColor.g, vertexColor.b, 1.0);; + gl_FragColor = vec4(v_color.r, v_color.g, v_color.b, 1.0);; } diff --git a/examples/models/gltf/snowflake/line_vert.glsl b/examples/models/gltf/snowflake/line_vert.glsl index f74e0094b086bc..478770c6319015 100644 --- a/examples/models/gltf/snowflake/line_vert.glsl +++ b/examples/models/gltf/snowflake/line_vert.glsl @@ -3,15 +3,13 @@ precision highp float; attribute vec3 position; attribute vec3 color; +varying vec3 v_color; + uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; -varying vec3 vertexColor; - void main(void) { - vec4 pos = modelViewMatrix * vec4(position,1.0); - vertexColor = vec3(color.x, color.y, color.y); + v_color = color; gl_Position = projectionMatrix * pos; - } \ No newline at end of file diff --git a/examples/models/gltf/snowflake/snowFlake.gltf b/examples/models/gltf/snowflake/snowFlake.gltf index ff8ed60eb1f0a7..97e399ad1d73af 100644 --- a/examples/models/gltf/snowflake/snowFlake.gltf +++ b/examples/models/gltf/snowflake/snowFlake.gltf @@ -7,6 +7,16 @@ "componentType": 5126, "count": 257, "type": "VEC3", + "max": [ + 250.0, + 250.0, + 0.0 + ], + "min": [ + -250.0, + -250.0, + 0.0 + ], "name": "VertexAccessor" }, "ColorAccessor": { @@ -16,9 +26,22 @@ "componentType": 5126, "count": 257, "type": "VEC3", + "max": [ + 0.999963164, + 0.99640733, + 1.0 + ], + "min": [ + 0.500137746, + 0.5041365, + 1.0 + ], "name": "ColorAccessor" } }, + "asset": { + "version": "1.1" + }, "buffers": { "Buffer": { "uri": "snowFlake.bin", @@ -58,7 +81,7 @@ { "attributes": { "POSITION": "VertexAccessor", - "COLOR": "ColorAccessor" + "COLOR_0": "ColorAccessor" }, "material": "meshMaterial", "mode": 1 @@ -79,8 +102,8 @@ "programs": { "meshProgram": { "attributes": [ - "VertexAccessor", - "ColorAccessor" + "a_position", + "a_color" ], "fragmentShader": "fragmentShader", "vertexShader": "vertexShader", @@ -112,14 +135,32 @@ "techniques": { "meshTechnique": { "parameters": { + "modelViewMatrixParameter": { + "type": 35676, + "semantic": "MODELVIEW" + }, + "projectionMatrixParameter": { + "type": 35676, + "semantic": "PROJECTION" + }, + "position": { + "type": 35665, + "semantic": "POSITION" + }, "color": { "type": 35665, - "semantic": "color" + "semantic": "COLOR_0" } }, - "attributes": {}, + "attributes": { + "a_position": "position", + "a_color": "color" + }, "program": "meshProgram", - "uniforms": {}, + "uniforms": { + "modelViewMatrix": "modelViewMatrixParameter", + "projectionMatrix": "projectionMatrixParameter" + }, "states": { "enable": [ 3042, diff --git a/examples/models/gltf/snowflakes/line_frag.glsl b/examples/models/gltf/snowflakes/line_frag.glsl index b3b1579fd29caa..de4f7893987708 100644 --- a/examples/models/gltf/snowflakes/line_frag.glsl +++ b/examples/models/gltf/snowflakes/line_frag.glsl @@ -1,8 +1,8 @@ precision highp float; -varying vec3 vertexColor; +varying vec3 v_color; void main(void) { - gl_FragColor = vec4(vertexColor.r, vertexColor.g, vertexColor.b, 1.0);; + gl_FragColor = vec4(v_color.r, v_color.g, v_color.b, 1.0);; } diff --git a/examples/models/gltf/snowflakes/line_vert.glsl b/examples/models/gltf/snowflakes/line_vert.glsl index f74e0094b086bc..478770c6319015 100644 --- a/examples/models/gltf/snowflakes/line_vert.glsl +++ b/examples/models/gltf/snowflakes/line_vert.glsl @@ -3,15 +3,13 @@ precision highp float; attribute vec3 position; attribute vec3 color; +varying vec3 v_color; + uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; -varying vec3 vertexColor; - void main(void) { - vec4 pos = modelViewMatrix * vec4(position,1.0); - vertexColor = vec3(color.x, color.y, color.y); + v_color = color; gl_Position = projectionMatrix * pos; - } \ No newline at end of file diff --git a/examples/models/gltf/snowflakes/snowFlakes.gltf b/examples/models/gltf/snowflakes/snowFlakes.gltf index a7096a43261946..f43cb1fcc8b7f0 100644 --- a/examples/models/gltf/snowflakes/snowFlakes.gltf +++ b/examples/models/gltf/snowflakes/snowFlakes.gltf @@ -7,16 +7,32 @@ "componentType": 5126, "count": 1376, "type": "VEC3", + "max": [ + 2300.0, + 2450.0, + 0.0 + ], + "min": [ + 0.0, + 0.0, + 0.0 + ], "name": "VertexAccessor" }, - "TriangleAccessor": { - "bufferView": "Lines", + "LineAccessor": { + "bufferView": "LineView", "byteOffset": 0, "byteStride": 2, "componentType": 5123, "count": 2720, "type": "SCALAR", - "name": "TriangleAccessor" + "max": [ + 1374.0 + ], + "min": [ + 0.0 + ], + "name": "LineAccessor" }, "ColorAccessor": { "bufferView": "ColorView", @@ -25,9 +41,22 @@ "componentType": 5126, "count": 1376, "type": "VEC3", + "max": [ + 0.999963164, + 0.999615431, + 1.0 + ], + "min": [ + 0.500137746, + 0.5002869, + 1.0 + ], "name": "ColorAccessor" } }, + "asset": { + "version": "1.1" + }, "buffers": { "Buffer": { "uri": "snowFlakes.bin", @@ -45,12 +74,12 @@ "target": 34962, "name": "VertexBuffer" }, - "Lines": { + "LineView": { "buffer": "Buffer", "byteOffset": 16512, "byteLength": 5440, - "target": 34962, - "name": "Lines" + "target": 34963, + "name": "LineView" }, "ColorView": { "buffer": "Buffer", @@ -74,9 +103,9 @@ { "attributes": { "POSITION": "VertexAccessor", - "COLOR": "ColorAccessor" + "COLOR_0": "ColorAccessor" }, - "indices": "TriangleAccessor", + "indices": "LineAccessor", "material": "meshMaterial", "mode": 1 } @@ -96,8 +125,8 @@ "programs": { "meshProgram": { "attributes": [ - "VertexAccessor", - "ColorAccessor" + "a_position", + "a_color" ], "fragmentShader": "fragmentShader", "vertexShader": "vertexShader", @@ -129,14 +158,32 @@ "techniques": { "meshTechnique": { "parameters": { + "modelViewMatrixParameter": { + "type": 35676, + "semantic": "MODELVIEW" + }, + "projectionMatrixParameter": { + "type": 35676, + "semantic": "PROJECTION" + }, + "position": { + "type": 35665, + "semantic": "POSITION" + }, "color": { "type": 35665, - "semantic": "color" + "semantic": "COLOR_0" } }, - "attributes": {}, + "attributes": { + "a_position": "position", + "a_color": "color" + }, "program": "meshProgram", - "uniforms": {}, + "uniforms": { + "modelViewMatrix": "modelViewMatrixParameter", + "projectionMatrix": "projectionMatrixParameter" + }, "states": { "enable": [ 3042, From abf63b05ade52fef9d65d78157d76829f50fb04a Mon Sep 17 00:00:00 2001 From: bdysvik Date: Tue, 10 Jan 2017 09:25:14 +0100 Subject: [PATCH 2/2] Proper prefixing of uniforms and attributes. Add 'color' to the attribreplaceTHREEShaderAttributes. --- examples/js/loaders/GLTFLoader.js | 9 ++++++++- examples/models/gltf/snowflake/line_vert.glsl | 14 +++++++------- examples/models/gltf/snowflake/snowFlake.gltf | 4 ++-- examples/models/gltf/snowflakes/line_vert.glsl | 14 +++++++------- examples/models/gltf/snowflakes/snowFlakes.gltf | 4 ++-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 10c8a4079c67c0..84757702e5c178 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -486,6 +486,13 @@ THREE.GLTFLoader = ( function () { shaderText = shaderText.replace( regEx, 'uv' ); break; + case 'COLOR_0': + case 'COLOR0': + case 'COLOR': + + shaderText = shaderText.replace( regEx, 'color' ); + break; + case "WEIGHT": shaderText = shaderText.replace( regEx, 'skinWeight' ); @@ -1758,4 +1765,4 @@ THREE.GLTFLoader = ( function () { return GLTFLoader; -} )(); +} )(); \ No newline at end of file diff --git a/examples/models/gltf/snowflake/line_vert.glsl b/examples/models/gltf/snowflake/line_vert.glsl index 478770c6319015..80ff31b224a75c 100644 --- a/examples/models/gltf/snowflake/line_vert.glsl +++ b/examples/models/gltf/snowflake/line_vert.glsl @@ -1,15 +1,15 @@ precision highp float; -attribute vec3 position; -attribute vec3 color; +attribute vec3 a_position; +attribute vec3 a_color; varying vec3 v_color; -uniform mat4 modelViewMatrix; -uniform mat4 projectionMatrix; +uniform mat4 u_modelViewMatrix; +uniform mat4 u_projectionMatrix; void main(void) { - vec4 pos = modelViewMatrix * vec4(position,1.0); - v_color = color; - gl_Position = projectionMatrix * pos; + vec4 pos = u_modelViewMatrix * vec4(position,1.0); + v_color = a_color; + gl_Position = u_projectionMatrix * pos; } \ No newline at end of file diff --git a/examples/models/gltf/snowflake/snowFlake.gltf b/examples/models/gltf/snowflake/snowFlake.gltf index 97e399ad1d73af..a5a4ce55a496cd 100644 --- a/examples/models/gltf/snowflake/snowFlake.gltf +++ b/examples/models/gltf/snowflake/snowFlake.gltf @@ -158,8 +158,8 @@ }, "program": "meshProgram", "uniforms": { - "modelViewMatrix": "modelViewMatrixParameter", - "projectionMatrix": "projectionMatrixParameter" + "u_modelViewMatrix": "modelViewMatrixParameter", + "u_projectionMatrix": "projectionMatrixParameter" }, "states": { "enable": [ diff --git a/examples/models/gltf/snowflakes/line_vert.glsl b/examples/models/gltf/snowflakes/line_vert.glsl index 478770c6319015..48239476456515 100644 --- a/examples/models/gltf/snowflakes/line_vert.glsl +++ b/examples/models/gltf/snowflakes/line_vert.glsl @@ -1,15 +1,15 @@ precision highp float; -attribute vec3 position; -attribute vec3 color; +attribute vec3 a_position; +attribute vec3 a_color; varying vec3 v_color; -uniform mat4 modelViewMatrix; -uniform mat4 projectionMatrix; +uniform mat4 u_modelViewMatrix; +uniform mat4 u_projectionMatrix; void main(void) { - vec4 pos = modelViewMatrix * vec4(position,1.0); - v_color = color; - gl_Position = projectionMatrix * pos; + vec4 pos = u_modelViewMatrix * vec4(a_position,1.0); + v_color = a_color; + gl_Position = u_projectionMatrix * pos; } \ No newline at end of file diff --git a/examples/models/gltf/snowflakes/snowFlakes.gltf b/examples/models/gltf/snowflakes/snowFlakes.gltf index f43cb1fcc8b7f0..066f363438418f 100644 --- a/examples/models/gltf/snowflakes/snowFlakes.gltf +++ b/examples/models/gltf/snowflakes/snowFlakes.gltf @@ -181,8 +181,8 @@ }, "program": "meshProgram", "uniforms": { - "modelViewMatrix": "modelViewMatrixParameter", - "projectionMatrix": "projectionMatrixParameter" + "u_modelViewMatrix": "modelViewMatrixParameter", + "u_projectionMatrix": "projectionMatrixParameter" }, "states": { "enable": [