Ver código fonte

Support for vertex paint and vertex alpha

David Catuhe 10 anos atrás
pai
commit
0865aa69ac

+ 22 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.js

@@ -1,6 +1,24 @@
 var BABYLON;
 (function (BABYLON) {
     (function (Internals) {
+        var checkColors4 = function (colors, count) {
+            // Check if color3 was used
+            if (colors.length === count * 3) {
+                var colors4 = [];
+                for (var index = 0; index < colors.length; index += 3) {
+                    var newIndex = (index / 3) * 4;
+                    colors4[newIndex] = colors[index];
+                    colors4[newIndex + 1] = colors[index + 1];
+                    colors4[newIndex + 2] = colors[index + 2];
+                    colors4[newIndex + 3] = 1.0;
+                }
+
+                return colors4;
+            }
+
+            return colors;
+        };
+
         var loadCubeTexture = function (rootUrl, parsedTexture, scene) {
             var texture = new BABYLON.CubeTexture(rootUrl + parsedTexture.name, scene);
 
@@ -650,6 +668,8 @@
             }
 
             // Geometry
+            mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
+
             if (parsedMesh.delayLoadingFile) {
                 mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
                 mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
@@ -810,7 +830,7 @@
             // colors
             var colors = parsedVertexData.colors;
             if (colors) {
-                vertexData.set(colors, BABYLON.VertexBuffer.ColorKind);
+                vertexData.set(checkColors4(colors, positions.length / 3), BABYLON.VertexBuffer.ColorKind);
             }
 
             // matricesIndices
@@ -914,7 +934,7 @@
                 }
 
                 if (parsedGeometry.colors) {
-                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, parsedGeometry.colors, false);
+                    mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, checkColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
                 }
 
                 if (parsedGeometry.matricesIndices) {

+ 22 - 2
Babylon/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -1,5 +1,23 @@
 module BABYLON.Internals {
 
+    var checkColors4 = (colors: number[], count:number): number[] => {
+        // Check if color3 was used
+        if (colors.length === count * 3) {
+            var colors4 = [];
+            for (var index = 0; index < colors.length; index += 3) {
+                var newIndex = (index / 3) * 4;
+                colors4[newIndex] = colors[index];
+                colors4[newIndex + 1] = colors[index + 1];
+                colors4[newIndex + 2] = colors[index + 2];
+                colors4[newIndex + 3] = 1.0;
+            }
+
+            return colors4;
+        } 
+
+        return colors
+    }
+
     var loadCubeTexture = (rootUrl, parsedTexture, scene) => {
         var texture = new BABYLON.CubeTexture(rootUrl + parsedTexture.name, scene);
 
@@ -659,6 +677,8 @@
         }
 
         // Geometry
+        mesh.hasVertexAlpha = parsedMesh.hasVertexAlpha;
+
         if (parsedMesh.delayLoadingFile) {
             mesh.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
             mesh.delayLoadingFile = rootUrl + parsedMesh.delayLoadingFile;
@@ -821,7 +841,7 @@
         // colors
         var colors = parsedVertexData.colors;
         if (colors) {
-            vertexData.set(colors, BABYLON.VertexBuffer.ColorKind);
+            vertexData.set(checkColors4(colors, positions.length / 3), BABYLON.VertexBuffer.ColorKind);
         }
 
         // matricesIndices
@@ -926,7 +946,7 @@
             }
 
             if (parsedGeometry.colors) {
-                mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, parsedGeometry.colors, false);
+                mesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, checkColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
             }
 
             if (parsedGeometry.matricesIndices) {

+ 4 - 0
Babylon/Materials/babylon.standardMaterial.js

@@ -321,6 +321,10 @@ var BABYLON;
                 if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
                     attribs.push(BABYLON.VertexBuffer.ColorKind);
                     defines.push("#define VERTEXCOLOR");
+
+                    if (mesh.hasVertexAlpha) {
+                        defines.push("#define VERTEXALPHA");
+                    }
                 }
                 if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
                     attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);

+ 4 - 0
Babylon/Materials/babylon.standardMaterial.ts

@@ -330,6 +330,10 @@
                 if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
                     attribs.push(BABYLON.VertexBuffer.ColorKind);
                     defines.push("#define VERTEXCOLOR");
+
+                    if (mesh.hasVertexAlpha) {
+                        defines.push("#define VERTEXALPHA");
+                    }
                 }
                 if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
                     attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);

+ 8 - 0
Babylon/Math/babylon.math.js

@@ -1743,6 +1743,14 @@
             return result;
         };
 
+        Matrix.Invert = function (source) {
+            var result = new Matrix();
+
+            source.invertToRef(result);
+
+            return result;
+        };
+
         Matrix.RotationXToRef = function (angle, result) {
             var s = Math.sin(angle);
             var c = Math.cos(angle);

Diferenças do arquivo suprimidas por serem muito extensas
+ 2354 - 2346
Babylon/Math/babylon.math.ts


+ 1 - 0
Babylon/Mesh/babylon.abstractMesh.js

@@ -29,6 +29,7 @@ var BABYLON;
             this.renderOutline = false;
             this.outlineColor = BABYLON.Color3.Red();
             this.outlineWidth = 0.02;
+            this.hasVertexAlpha = false;
             this.useOctreeForRenderingSelection = true;
             this.useOctreeForPicking = true;
             this.useOctreeForCollisions = true;

+ 1 - 0
Babylon/Mesh/babylon.abstractMesh.ts

@@ -50,6 +50,7 @@
         public renderOutline = false;
         public outlineColor = BABYLON.Color3.Red();
         public outlineWidth = 0.02;
+        public hasVertexAlpha = false;
 
         public useOctreeForRenderingSelection = true;
         public useOctreeForPicking = true;

+ 1 - 1
Babylon/Mesh/babylon.vertexBuffer.js

@@ -37,7 +37,7 @@
                     this._strideSize = 2;
                     break;
                 case VertexBuffer.ColorKind:
-                    this._strideSize = 3;
+                    this._strideSize = 4;
                     break;
                 case VertexBuffer.MatricesIndicesKind:
                     this._strideSize = 4;

+ 1 - 1
Babylon/Mesh/babylon.vertexBuffer.ts

@@ -46,7 +46,7 @@
                     this._strideSize = 2;
                     break;
                 case VertexBuffer.ColorKind:
-                    this._strideSize = 3;
+                    this._strideSize = 4;
                     break;
                 case VertexBuffer.MatricesIndicesKind:
                     this._strideSize = 4;

+ 1 - 1
Babylon/Rendering/babylon.renderingGroup.js

@@ -87,7 +87,7 @@
             var material = subMesh.getMaterial();
             var mesh = subMesh.getMesh();
 
-            if (material.needAlphaBlending() || mesh.visibility < 1.0) {
+            if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) {
                 if (material.alpha > 0 || mesh.visibility < 1.0) {
                     this._transparentSubMeshes.push(subMesh);
                 }

+ 1 - 1
Babylon/Rendering/babylon.renderingGroup.ts

@@ -91,7 +91,7 @@
             var material = subMesh.getMaterial();
             var mesh = subMesh.getMesh();
 
-            if (material.needAlphaBlending() || mesh.visibility < 1.0) { // Transparent
+            if (material.needAlphaBlending() || mesh.visibility < 1.0 || mesh.hasVertexAlpha) { // Transparent
                 if (material.alpha > 0 || mesh.visibility < 1.0) {
                     this._transparentSubMeshes.push(subMesh);
                 }

+ 6 - 2
Babylon/Shaders/default.fragment.fx

@@ -21,7 +21,7 @@ varying vec3 vPositionW;
 varying vec3 vNormalW;
 
 #ifdef VERTEXCOLOR
-varying vec3 vColor;
+varying vec4 vColor;
 #endif
 
 // Lights
@@ -473,7 +473,7 @@ void main(void) {
 	float alpha = vDiffuseColor.a;
 
 #ifdef VERTEXCOLOR
-	diffuseColor *= vColor;
+	baseColor.rgb *= vColor.rgb;
 #endif
 
 #ifdef DIFFUSE
@@ -661,6 +661,10 @@ void main(void) {
 
 #endif
 
+#ifdef VERTEXALPHA
+	alpha *= vColor.a;
+#endif
+
 #ifdef OPACITYFRESNEL
 	float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
 

+ 2 - 2
Babylon/Shaders/default.vertex.fx

@@ -12,7 +12,7 @@ attribute vec2 uv;
 attribute vec2 uv2;
 #endif
 #ifdef VERTEXCOLOR
-attribute vec3 color;
+attribute vec4 color;
 #endif
 #ifdef BONES
 attribute vec4 matricesIndices;
@@ -82,7 +82,7 @@ varying vec3 vPositionW;
 varying vec3 vNormalW;
 
 #ifdef VERTEXCOLOR
-varying vec3 vColor;
+varying vec4 vColor;
 #endif
 
 #ifdef CLIPPLANE

+ 6 - 2
Babylon/Shaders/legacydefault.fragment.fx

@@ -16,7 +16,7 @@ varying vec3 vPositionW;
 varying vec3 vNormalW;
 
 #ifdef VERTEXCOLOR
-varying vec3 vColor;
+varying vec4 vColor;
 #endif
 
 // Lights
@@ -353,7 +353,7 @@ void main(void) {
 	vec3 diffuseColor = vDiffuseColor.rgb;
 
 #ifdef VERTEXCOLOR
-	diffuseColor *= vColor;
+	baseColor.rgb *= vColor.rgb;
 #endif
 
 #ifdef DIFFUSE
@@ -516,6 +516,10 @@ void main(void) {
 #endif
 #endif
 
+#ifdef VERTEXALPHA
+	alpha *= vColor.a;
+#endif
+
 #ifdef OPACITYFRESNEL
 	float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);
 

+ 2 - 2
Babylon/Shaders/legacydefault.vertex.fx

@@ -19,7 +19,7 @@ attribute vec2 uv;
 attribute vec2 uv2;
 #endif
 #ifdef VERTEXCOLOR
-attribute vec3 color;
+attribute vec4 color;
 #endif
 #ifdef BONES
 attribute vec4 matricesIndices;
@@ -83,7 +83,7 @@ varying vec3 vPositionW;
 varying vec3 vNormalW;
 
 #ifdef VERTEXCOLOR
-varying vec3 vColor;
+varying vec4 vColor;
 #endif
 
 #ifdef CLIPPLANE

+ 3 - 0
Exporters/3ds Max/BabylonExport.Entities/BabylonMesh.cs

@@ -43,6 +43,9 @@ namespace BabylonExport.Entities
         public float[] colors { get; set; }
 
         [DataMember]
+        public bool hasVertexAlpha { get; set; }
+
+        [DataMember]
         public int[] matricesIndices { get; set; }
 
         [DataMember]

BIN
Exporters/3ds Max/Max2Babylon-0.8.2.zip


BIN
Exporters/3ds Max/Max2Babylon-0.8.3.zip


+ 15 - 9
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Material.cs

@@ -18,7 +18,7 @@ namespace Max2Babylon
 
             if (materialNode.NumSubMtls > 0)
             {
-                var babylonMultimaterial = new BabylonMultiMaterial {name = name, id = id};
+                var babylonMultimaterial = new BabylonMultiMaterial { name = name, id = id };
 
                 var guids = new List<string>();
 
@@ -56,7 +56,7 @@ namespace Max2Babylon
                 ambient = materialNode.GetAmbient(0, false).ToArray(),
                 diffuse = materialNode.GetDiffuse(0, false).ToArray(),
                 specular = materialNode.GetSpecular(0, false).Scale(materialNode.GetShinStr(0, false)),
-                specularPower = materialNode.GetShininess(0, false)*256,
+                specularPower = materialNode.GetShininess(0, false) * 256,
                 emissive =
                     materialNode.GetSelfIllumColorOn(0, false)
                         ? materialNode.GetSelfIllumColor(0, false).ToArray()
@@ -87,18 +87,24 @@ namespace Max2Babylon
                 if (fresnelParameters != null)
                 {
                     babylonMaterial.emissiveFresnelParameters = fresnelParameters;
-                    if (babylonMaterial.emissive[0] == 0 && 
+                    if (babylonMaterial.emissive[0] == 0 &&
                         babylonMaterial.emissive[1] == 0 &&
-                        babylonMaterial.emissive[2] == 0)
+                        babylonMaterial.emissive[2] == 0 &&
+                        babylonMaterial.emissiveTexture == null)
                     {
-                        babylonMaterial.emissive = new float[]{1, 1, 1};
+                        babylonMaterial.emissive = new float[] { 1, 1, 1 };
                     }
                 }
-                
+
                 babylonMaterial.opacityTexture = ExportTexture(stdMat, 6, out fresnelParameters, babylonScene, false, true);   // Opacity
                 if (fresnelParameters != null)
                 {
                     babylonMaterial.opacityFresnelParameters = fresnelParameters;
+                    if (babylonMaterial.alpha == 1 &&
+                         babylonMaterial.opacityTexture == null)
+                    {
+                        babylonMaterial.alpha = 0;
+                    }
                 }
 
                 babylonMaterial.bumpTexture = ExportTexture(stdMat, 8, out fresnelParameters, babylonScene);                   // Bump
@@ -111,19 +117,19 @@ namespace Max2Babylon
                     }
                     else
                     {
-                        babylonMaterial.reflectionFresnelParameters = fresnelParameters;                        
+                        babylonMaterial.reflectionFresnelParameters = fresnelParameters;
                     }
                 }
 
                 // Constraints
                 if (babylonMaterial.diffuseTexture != null)
                 {
-                    babylonMaterial.diffuse = new [] { 1.0f, 1.0f, 1.0f };
+                    babylonMaterial.diffuse = new[] { 1.0f, 1.0f, 1.0f };
                 }
 
                 if (babylonMaterial.emissiveTexture != null)
                 {
-                    babylonMaterial.emissive = new float[]{0, 0, 0};
+                    babylonMaterial.emissive = new float[] { 0, 0, 0 };
                 }
 
                 if (babylonMaterial.opacityTexture != null && babylonMaterial.diffuseTexture != null &&

+ 22 - 5
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

@@ -102,7 +102,7 @@ namespace Max2Babylon
 
                 if (mesh.NumVerts >= 65536)
                 {
-                    RaiseError(string.Format("Mesh {0} has too many vertices (more than 65535)", babylonMesh.name), 2);
+                    RaiseWarning(string.Format("Mesh {0} has tmore than 65536 vertices which means that it will require specific WebGL extension to be rendered. This may impact portability of your scene on low end devices.", babylonMesh.name), 2);
                 }
 
                 // Material
@@ -129,6 +129,8 @@ namespace Max2Babylon
 
                 var hasUV = mesh.NumTVerts > 0;
                 var hasUV2 = mesh.GetNumMapVerts(2) > 0;
+                var hasColor = mesh.NumVertCol > 0;
+                var hasAlpha = mesh.GetNumMapVerts(-2) > 0;
 
                 var optimizeVertices = meshNode.GetBoolProperty("babylonjs_optimizevertices");
 
@@ -151,9 +153,9 @@ namespace Max2Babylon
 
                 for (var face = 0; face < mesh.NumFaces; face++)
                 {
-                    indices.Add(CreateGlobalVertex(mesh, face, vx1, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
-                    indices.Add(CreateGlobalVertex(mesh, face, vx2, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
-                    indices.Add(CreateGlobalVertex(mesh, face, vx3, vertices, hasUV, hasUV2, vnorms, verticesAlreadyExported, skinContext));
+                    indices.Add(CreateGlobalVertex(mesh, face, vx1, vertices, hasUV, hasUV2, hasColor, hasAlpha, vnorms, verticesAlreadyExported, skinContext));
+                    indices.Add(CreateGlobalVertex(mesh, face, vx2, vertices, hasUV, hasUV2, hasColor, hasAlpha, vnorms, verticesAlreadyExported, skinContext));
+                    indices.Add(CreateGlobalVertex(mesh, face, vx3, vertices, hasUV, hasUV2, hasColor, hasAlpha, vnorms, verticesAlreadyExported, skinContext));
                     matIDs.Add(mesh.Faces[face].MatID % multiMatsCount);
                     CheckCancelled();
                 }
@@ -188,6 +190,12 @@ namespace Max2Babylon
                     babylonMesh.matricesIndices = vertices.Select(v => v.BonesIndices).ToArray();
                 }
 
+                if (hasColor)
+                {
+                    babylonMesh.colors = vertices.SelectMany(v => v.Color.ToArray()).ToArray();
+                    babylonMesh.hasVertexAlpha = hasAlpha;
+                }
+
                 // Submeshes
                 var sortedIndices = new List<int>();
                 var subMeshes = new List<BabylonSubMesh>();
@@ -360,7 +368,7 @@ namespace Max2Babylon
             }
         }
 
-        int CreateGlobalVertex(IMesh mesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, VNormal[] vnorms, List<GlobalVertex>[] verticesAlreadyExported, IISkinContextData skinContextData)
+        int CreateGlobalVertex(IMesh mesh, int face, int facePart, List<GlobalVertex> vertices, bool hasUV, bool hasUV2, bool hasColor, bool hasAlpha, VNormal[] vnorms, List<GlobalVertex>[] verticesAlreadyExported, IISkinContextData skinContextData)
         {
             var faceObject = mesh.Faces[face];
             var vertexIndex = (int)faceObject.V[facePart];
@@ -384,6 +392,15 @@ namespace Max2Babylon
                 vertex.UV2 = Loader.Global.Point2.Create(mesh.MapVerts(2)[tvertexIndex].X, mesh.MapVerts(2)[tvertexIndex].Y);
             }
 
+            if (hasColor)
+            {
+                var vertexColorIndex = (int)mesh.VcFace[face].T[facePart];
+                var vertexColor = mesh.VertCol[vertexColorIndex];
+                var alpha = hasAlpha ? mesh.MapVerts(-2)[vertexColorIndex].X : 1;
+
+                vertex.Color = new float[] { vertexColor.X, vertexColor.Y, vertexColor.Z, alpha};
+            }
+
             if (skinContextData != null)
             {
                 float weight0 = 0;

+ 6 - 0
Exporters/3ds Max/Max2Babylon/Exporter/GlobalVertex.cs

@@ -12,6 +12,7 @@ namespace Max2Babylon
         public IPoint2 UV2 { get; set; }
         public int BonesIndices { get; set; }
         public IPoint4 Weights { get; set; }
+        public float[] Color { get; set; }
 
         public override int GetHashCode()
         {
@@ -57,6 +58,11 @@ namespace Max2Babylon
                 return false;
             }
 
+            if (Color != null && !other.Color.IsAlmostEqualTo(Color, Tools.Epsilon))
+            {
+                return false;
+            }
+
             return other.BonesIndices == BonesIndices;
         }
     }

+ 1 - 0
Exporters/Blender/io_export_babylon.py

@@ -744,6 +744,7 @@ class Mesh(FCurveAnimatable):
                             self.colors.append(vertex_Color.r)
                             self.colors.append(vertex_Color.g)
                             self.colors.append(vertex_Color.b)
+							self.colors.append(1.0)
                         if hasSkeleton:
                             self.skeletonWeights.append(matricesWeights[0])
                             self.skeletonWeights.append(matricesWeights[1])

Diferenças do arquivo suprimidas por serem muito extensas
+ 41 - 8
babylon.2.0-alpha.debug.js


Diferenças do arquivo suprimidas por serem muito extensas
+ 12 - 12
babylon.2.0-alpha.js


+ 2 - 44
babylon.2.0.d.ts

@@ -1779,50 +1779,6 @@ declare module BABYLON {
     }
 }
 declare module BABYLON {
-    class ProceduralTexture extends Texture {
-        private _size;
-        public _generateMipMaps: boolean;
-        private _doNotChangeAspectRatio;
-        private _currentRefreshId;
-        private _refreshRate;
-        private _vertexBuffer;
-        private _indexBuffer;
-        private _effect;
-        private _vertexDeclaration;
-        private _vertexStrideSize;
-        private _uniforms;
-        private _samplers;
-        private _fragment;
-        private _textures;
-        private _floats;
-        private _floatsArrays;
-        private _colors3;
-        private _colors4;
-        private _vectors2;
-        private _vectors3;
-        private _matrices;
-        constructor(name: string, size: any, fragment: any, scene: Scene, generateMipMaps?: boolean);
-        public isReady(): boolean;
-        public resetRefreshCounter(): void;
-        public refreshRate : number;
-        public _shouldRender(): boolean;
-        public getRenderSize(): number;
-        public resize(size: any, generateMipMaps: any): void;
-        private _checkUniform(uniformName);
-        public setTexture(name: string, texture: Texture): ProceduralTexture;
-        public setFloat(name: string, value: number): ProceduralTexture;
-        public setFloats(name: string, value: number[]): ProceduralTexture;
-        public setColor3(name: string, value: Color3): ProceduralTexture;
-        public setColor4(name: string, value: Color4): ProceduralTexture;
-        public setVector2(name: string, value: Vector2): ProceduralTexture;
-        public setVector3(name: string, value: Vector3): ProceduralTexture;
-        public setMatrix(name: string, value: Matrix): ProceduralTexture;
-        public render(useCameraPostProcess?: boolean): void;
-        public clone(): ProceduralTexture;
-        public dispose(): void;
-    }
-}
-declare module BABYLON {
     class RenderTargetTexture extends Texture {
         public renderList: AbstractMesh[];
         public renderParticles: boolean;
@@ -2310,6 +2266,7 @@ declare module BABYLON {
         static IdentityToRef(result: Matrix): void;
         static Zero(): Matrix;
         static RotationX(angle: number): Matrix;
+        static Invert(source: Matrix): Matrix;
         static RotationXToRef(angle: number, result: Matrix): void;
         static RotationY(angle: number): Matrix;
         static RotationYToRef(angle: number, result: Matrix): void;
@@ -2433,6 +2390,7 @@ declare module BABYLON {
         public renderOutline: boolean;
         public outlineColor: Color3;
         public outlineWidth: number;
+        public hasVertexAlpha: boolean;
         public useOctreeForRenderingSelection: boolean;
         public useOctreeForPicking: boolean;
         public useOctreeForCollisions: boolean;