瀏覽代碼

Check standardDerivatives capabilities and calculate normals if shader can't do it

Gary Hsu 8 年之前
父節點
當前提交
29c9c0a7aa
共有 3 個文件被更改,包括 13 次插入6 次删除
  1. 5 0
      src/Materials/babylon.pbrMaterial.ts
  2. 2 2
      src/Mesh/babylon.abstractMesh.ts
  3. 6 4
      src/Mesh/babylon.mesh.vertexData.ts

+ 5 - 0
src/Materials/babylon.pbrMaterial.ts

@@ -996,6 +996,11 @@
 
             // Attribs
             if (mesh) {
+                if (!scene.getEngine().getCaps().standardDerivatives && !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)) {

+ 2 - 2
src/Mesh/babylon.abstractMesh.ts

@@ -2190,8 +2190,8 @@
             } else {
                 normals = [];
             }
-            
-            VertexData.ComputeNormals(positions, indices, normals);
+
+            VertexData.ComputeNormals(positions, indices, normals, { useRightHandedSystem: this.getScene().useRightHandedSystem });
             this.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
         } 
 

+ 6 - 4
src/Mesh/babylon.mesh.vertexData.ts

@@ -2126,7 +2126,7 @@
          * bInfo : optional bounding info, required for facetPartitioning computation
          */
         public static ComputeNormals(positions: any, indices: any, normals: any,
-            options?: { facetNormals?: any, facetPositions?: any, facetPartitioning?: any, ratio?: number, bInfo?: any, bbSize?: Vector3, subDiv?: any}): void {
+            options?: { facetNormals?: any, facetPositions?: any, facetPartitioning?: any, ratio?: number, bInfo?: any, bbSize?: Vector3, subDiv?: any, useRightHandedSystem?: boolean }): void {
 
             // temporary scalar variables
             var index = 0;                      // facet index     
@@ -2152,10 +2152,12 @@
             var computeFacetNormals = false;
             var computeFacetPositions = false;
             var computeFacetPartitioning = false;
+            var faceNormalSign = 1;
             if (options) {
                 computeFacetNormals = (options.facetNormals) ? true : false;
                 computeFacetPositions = (options.facetPositions) ? true : false;
                 computeFacetPartitioning = (options.facetPartitioning) ? true : false;
+                faceNormalSign = (options.useRightHandedSystem === true) ? -1 : 1;
             }
 
             // facetPartitioning reinit if needed
@@ -2215,9 +2217,9 @@
                 p3p2z = positions[v3z] - positions[v2z];
 
                 // compute the face normal with the cross product
-                faceNormalx = p1p2y * p3p2z - p1p2z * p3p2y;
-                faceNormaly = p1p2z * p3p2x - p1p2x * p3p2z;
-                faceNormalz = p1p2x * p3p2y - p1p2y * p3p2x;
+                faceNormalx = faceNormalSign * (p1p2y * p3p2z - p1p2z * p3p2y);
+                faceNormaly = faceNormalSign * (p1p2z * p3p2x - p1p2x * p3p2z);
+                faceNormalz = faceNormalSign * (p1p2x * p3p2y - p1p2y * p3p2x);
                 // normalize this normal and store it in the array facetData
                 length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
                 length = (length === 0) ? 1.0 : length;