Explorar el Código

Merge pull request #1999 from sebavan/Development

Auto Generate Normals in PBR if not present on the mesh
David Catuhe hace 8 años
padre
commit
4628795ebe

+ 9 - 8
src/Materials/babylon.pbrMaterial.ts

@@ -213,7 +213,7 @@
          * This as to be use with the overloadedReflectivityIntensity parameter.
          */
         @serializeAsColor3()
-        public overloadedReflectivity: Color3 = new BABYLON.Color3(0.3, 0.3, 0.3);
+        public overloadedReflectivity: Color3 = new BABYLON.Color3(0.0, 0.0, 0.0);
         
         /**
          * Debug Control indicating how much the overloaded reflectivity color is used against the default one.
@@ -349,7 +349,7 @@
         public reflectivityColor = new Color3(1, 1, 1);
 
         @serializeAsColor3("reflection")
-        public reflectionColor = new Color3(0.5, 0.5, 0.5);
+        public reflectionColor = new Color3(0.0, 0.0, 0.0);
 
         @serializeAsColor3("emissive")
         public emissiveColor = new Color3(0, 0, 0);
@@ -664,13 +664,12 @@
 
             var scene = this.getScene();
             var engine = scene.getEngine();
-            var needNormals = false;
             var needUVs = false;
 
             this._defines.reset();
 
             if (scene.lightsEnabled && !this.disableLighting) {
-                needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, true, this.maxSimultaneousLights) || needNormals;
+                MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, true, this.maxSimultaneousLights);
             }
 
             if (!this.checkReadyOnEveryCall) {
@@ -722,7 +721,6 @@
                         return false;
                     }
                     
-                    needNormals = true;
                     this._defines.REFLECTION = true;
 
                     if (this.reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE) {
@@ -764,7 +762,6 @@
 
                     if (this.reflectionTexture instanceof HDRCubeTexture && (<HDRCubeTexture>this.reflectionTexture)) {
                         this._defines.USESPHERICALFROMREFLECTIONMAP = true;
-                        needNormals = true;
 
                         if ((<HDRCubeTexture>this.reflectionTexture).isPMREM) {
                             this._defines.USEPMREMREFLECTION = true;
@@ -963,7 +960,6 @@
                         this._defines.EMISSIVEFRESNEL = true;
                     }
 
-                    needNormals = true;
                     this._defines.FRESNEL = true;
                 }
             }
@@ -986,7 +982,12 @@
 
             // Attribs
             if (mesh) {
-                if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
+                if (!mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
+                    mesh.createNormals(true);
+                    Tools.Warn("PBRMaterial: Normals have been created for the mesh: " + mesh.name);
+                }
+
+                if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
                     this._defines.NORMAL = true;
                     if (mesh.isVerticesDataPresent(VertexBuffer.TangentKind)) {
                         this._defines.TANGENT = true;

+ 87 - 1
src/Mesh/babylon.abstractMesh.ts

@@ -1,5 +1,5 @@
 module BABYLON {
-    export class AbstractMesh extends Node implements IDisposable, ICullable {
+    export class AbstractMesh extends Node implements IDisposable, ICullable, IGetSetVerticesData {
         // Statics
         private static _BILLBOARDMODE_NONE = 0;
         private static _BILLBOARDMODE_X = 1;
@@ -579,6 +579,73 @@
         public getVerticesData(kind: string): number[] | Float32Array {
             return null;
         }
+        /**
+         * Sets the vertex data of the mesh geometry for the requested `kind`.
+         * If the mesh has no geometry, a new Geometry object is set to the mesh and then passed this vertex data.  
+         * The `data` are either a numeric array either a Float32Array. 
+         * The parameter `updatable` is passed as is to the underlying Geometry object constructor (if initianilly none) or updater. 
+         * The parameter `stride` is an optional positive integer, it is usually automatically deducted from the `kind` (3 for positions or normals, 2 for UV, etc).  
+         * Note that a new underlying VertexBuffer object is created each call. 
+         * If the `kind` is the `PositionKind`, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed. 
+         *
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind  
+         * 
+         * Returns the Mesh.  
+         */
+        public setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean, stride?: number): Mesh {
+            return null;
+        }
+
+        /**
+         * Updates the existing vertex data of the mesh geometry for the requested `kind`.
+         * If the mesh has no geometry, it is simply returned as it is.  
+         * The `data` are either a numeric array either a Float32Array. 
+         * No new underlying VertexBuffer object is created. 
+         * If the `kind` is the `PositionKind` and if `updateExtends` is true, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.  
+         * If the parameter `makeItUnique` is true, a new global geometry is created from this positions and is set to the mesh.
+         *
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
+         * 
+         * Returns the Mesh.  
+         */
+        public updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): Mesh {
+            return null;
+        }
+
+        /**
+         * Sets the mesh indices.  
+         * Expects an array populated with integers or a typed array (Int32Array, Uint32Array, Uint16Array).
+         * If the mesh has no geometry, a new Geometry object is created and set to the mesh. 
+         * This method creates a new index buffer each call.  
+         * Returns the Mesh.  
+         */
+        public setIndices(indices: IndicesArray, totalVertices?: number): Mesh {
+            return null;
+        }
 
         /** Returns false by default, used by the class Mesh.  
          *  Returns a boolean
@@ -2102,6 +2169,25 @@
                 this._facetParameters = null;
             }
             return this;
+        }
+
+        /**
+         * Creates new normals data for the mesh.
+         * @param updatable.
+         */
+        public createNormals(updatable: boolean) {
+            var positions = this.getVerticesData(VertexBuffer.PositionKind);
+            var indices = this.getIndices();
+            var normals: number[] | Float32Array;
+            
+            if (this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
+                normals = this.getVerticesData(VertexBuffer.NormalKind);
+            } else {
+                normals = [];
+            }
+            
+            VertexData.ComputeNormals(positions, indices, normals);
+            this.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
         } 
 
     }

+ 77 - 0
src/Mesh/babylon.instancedMesh.ts

@@ -77,6 +77,83 @@
         }
 
         /**
+         * Sets the vertex data of the mesh geometry for the requested `kind`.
+         * If the mesh has no geometry, a new Geometry object is set to the mesh and then passed this vertex data.  
+         * The `data` are either a numeric array either a Float32Array. 
+         * The parameter `updatable` is passed as is to the underlying Geometry object constructor (if initianilly none) or updater. 
+         * The parameter `stride` is an optional positive integer, it is usually automatically deducted from the `kind` (3 for positions or normals, 2 for UV, etc).  
+         * Note that a new underlying VertexBuffer object is created each call. 
+         * If the `kind` is the `PositionKind`, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed. 
+         *
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind  
+         * 
+         * Returns the Mesh.  
+         */
+        public setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean, stride?: number): Mesh {
+            if (this.sourceMesh) {
+               this.sourceMesh.setVerticesData(kind, data, updatable, stride);
+            }
+            return this.sourceMesh;
+        }
+
+        /**
+         * Updates the existing vertex data of the mesh geometry for the requested `kind`.
+         * If the mesh has no geometry, it is simply returned as it is.  
+         * The `data` are either a numeric array either a Float32Array. 
+         * No new underlying VertexBuffer object is created. 
+         * If the `kind` is the `PositionKind` and if `updateExtends` is true, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.  
+         * If the parameter `makeItUnique` is true, a new global geometry is created from this positions and is set to the mesh.
+         *
+         * Possible `kind` values :
+         * - BABYLON.VertexBuffer.PositionKind
+         * - BABYLON.VertexBuffer.UVKind
+         * - BABYLON.VertexBuffer.UV2Kind
+         * - BABYLON.VertexBuffer.UV3Kind
+         * - BABYLON.VertexBuffer.UV4Kind
+         * - BABYLON.VertexBuffer.UV5Kind
+         * - BABYLON.VertexBuffer.UV6Kind
+         * - BABYLON.VertexBuffer.ColorKind
+         * - BABYLON.VertexBuffer.MatricesIndicesKind
+         * - BABYLON.VertexBuffer.MatricesIndicesExtraKind
+         * - BABYLON.VertexBuffer.MatricesWeightsKind
+         * - BABYLON.VertexBuffer.MatricesWeightsExtraKind
+         * 
+         * Returns the Mesh.  
+         */
+        public updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): Mesh {
+            if (this.sourceMesh) {
+               this.sourceMesh.updateVerticesData(kind, data, updateExtends, makeItUnique);
+            }
+            return this.sourceMesh;
+        }
+
+        /**
+         * Sets the mesh indices.  
+         * Expects an array populated with integers or a typed array (Int32Array, Uint32Array, Uint16Array).
+         * If the mesh has no geometry, a new Geometry object is created and set to the mesh. 
+         * This method creates a new index buffer each call.  
+         * Returns the Mesh.  
+         */
+        public setIndices(indices: IndicesArray, totalVertices?: number): Mesh {
+            if (this.sourceMesh) {
+               this.sourceMesh.setIndices(indices, totalVertices);
+            }
+            return this.sourceMesh;
+        }
+
+        /**
          * Boolean : True if the mesh owns the requested kind of data.
          */
         public isVerticesDataPresent(kind: string): boolean {

+ 0 - 15
src/Mesh/babylon.mesh.ts

@@ -803,21 +803,6 @@
             return this;
         }
 
-        public createNormals(updatable: boolean) {
-            var positions = this.getVerticesData(VertexBuffer.PositionKind);
-            var indices = this.getIndices();
-            var normals: number[] | Float32Array;
-            
-            if (this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
-                normals = this.getVerticesData(VertexBuffer.NormalKind);
-            } else {
-                normals = [];
-            }
-            
-            VertexData.ComputeNormals(positions, indices, normals);
-            this.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
-        }
-
         /**
          * Creates a un-shared specific occurence of the geometry for the mesh.  
          * Returns the Mesh.