Forráskód Böngészése

Merge pull request #2073 from Palmer-JC/master

Add vertex color & bones to LinesMesh
David Catuhe 8 éve
szülő
commit
aaa1a46449

+ 20 - 2
src/Materials/babylon.shaderMaterial.ts

@@ -175,6 +175,7 @@
 
             // Instances
             var defines = [];
+            var attribs = [];
             var fallbacks = new EffectFallbacks();
             if (useInstances) {
                 defines.push("#define INSTANCES");
@@ -184,12 +185,29 @@
                 defines.push(this._options.defines[index]);
             }
 
+            for (var index = 0; index < this._options.attributes.length; index++) {
+                attribs.push(this._options.attributes[index]);
+            }
+
+            if (mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
+                attribs.push(VertexBuffer.ColorKind);
+                defines.push("#define VERTEXCOLOR");
+            }
+            
             // Bones
             if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
+                attribs.push(VertexBuffer.MatricesIndicesKind);
+                attribs.push(VertexBuffer.MatricesWeightsKind);
+                if (mesh.numBoneInfluencers > 4) {
+                    attribs.push(VertexBuffer.MatricesIndicesExtraKind);
+                    attribs.push(VertexBuffer.MatricesWeightsExtraKind);
+                }
                 defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
                 defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
                 fallbacks.addCPUSkinningFallback(0, mesh);
-            }
+            } else {
+                defines.push("#define NUM_BONE_INFLUENCERS 0");
+            }  
 
             // Textures
             for (var name in this._textures) {
@@ -207,7 +225,7 @@
             var join = defines.join("\n");
 
             this._effect = engine.createEffect(this._shaderPath, <EffectCreationOptions>{
-                    attributes: this._options.attributes,
+                    attributes: attribs,
                     uniformsNames: this._options.uniforms,
                     uniformBuffersNames: this._options.uniformBuffers,
                     samplers: this._options.samplers,

+ 19 - 17
src/Mesh/babylon.linesMesh.ts

@@ -5,8 +5,6 @@ module BABYLON {
         public color = new Color3(1, 1, 1);
         public alpha = 1;
 
-        private _positionBuffer: { [key: string]: VertexBuffer } = {};
-
         /**
          * The intersection Threshold is the margin applied when intersection a segment of the LinesMesh with a Ray.
          * This margin is expressed in world space coordinates, so its value may vary.
@@ -36,23 +34,29 @@ module BABYLON {
         private _intersectionThreshold: number;
         private _colorShader: ShaderMaterial;
 
-        constructor(name: string, scene: Scene, parent: Node = null, source?: LinesMesh, doNotCloneChildren?: boolean) {
+        constructor(name: string, scene: Scene, parent: Node = null, source?: LinesMesh, doNotCloneChildren?: boolean, public useVertexColor? : boolean) {
             super(name, scene, parent, source, doNotCloneChildren);
 
             if (source) {
                 this.color = source.color.clone();
                 this.alpha = source.alpha;
+                this.useVertexColor = source.useVertexColor;
             }
 
             this._intersectionThreshold = 0.1;
-            this._colorShader = new ShaderMaterial("colorShader", scene, "color",
-                {
-                    attributes: [VertexBuffer.PositionKind],
-                    uniforms: ["worldViewProjection", "color"],
-                    needAlphaBlending: true
-                });
-
-            this._positionBuffer[VertexBuffer.PositionKind] = null;
+            
+            var options = {
+                attributes: [VertexBuffer.PositionKind],
+                uniforms: ["world", "viewProjection"],
+                needAlphaBlending: false,
+            };
+            
+            if (!useVertexColor) {
+                options.uniforms.push("color");
+                options.needAlphaBlending = true;
+            }
+
+            this._colorShader = new ShaderMaterial("colorShader", scene, "color", options);
         }
 
         /**
@@ -76,15 +80,13 @@ module BABYLON {
         }
 
         public _bind(subMesh: SubMesh, effect: Effect, fillMode: number): LinesMesh {
-            var engine = this.getScene().getEngine();
-
-            this._positionBuffer[VertexBuffer.PositionKind] = this._geometry.getVertexBuffer(VertexBuffer.PositionKind);
-
             // VBOs
-            engine.bindBuffers(this._positionBuffer, this._geometry.getIndexBuffer(), this._colorShader.getEffect());
+            this._geometry._bind(this._colorShader.getEffect() );
 
             // Color
-            this._colorShader.setColor4("color", this.color.toColor4(this.alpha));
+            if (!this.useVertexColor) {
+                this._colorShader.setColor4("color", this.color.toColor4(this.alpha));
+            }
             return this;
         }
 

+ 10 - 1
src/Shaders/color.fragment.fx

@@ -1,5 +1,14 @@
-uniform vec4 color;
+
+#ifdef VERTEXCOLOR
+varying vec4 vColor;
+#else
+uniform vec4 color;
+#endif
 
 void main(void) {
+#ifdef VERTEXCOLOR
+	gl_FragColor = vColor;
+#else
 	gl_FragColor = color;
+#endif
 }

+ 21 - 2
src/Shaders/color.vertex.fx

@@ -1,9 +1,28 @@
 // Attributes
 attribute vec3 position;
 
+#ifdef VERTEXCOLOR
+attribute vec4 color;
+#endif
+
+#include<bonesDeclaration>
+
 // Uniforms
-uniform mat4 worldViewProjection;
+uniform mat4 viewProjection;
+uniform mat4 world;
+
+// Output
+#ifdef VERTEXCOLOR
+varying vec4 vColor;
+#endif
 
 void main(void) {
-	gl_Position = worldViewProjection * vec4(position, 1.0);
+    mat4 finalWorld = world;
+#include<bonesVertex>
+	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
+
+#ifdef VERTEXCOLOR
+	// Vertex color
+	vColor = color;
+#endif
 }