소스 검색

Use always non uniform scaling for thin instances

Popov72 5 년 전
부모
커밋
9b55c22d02
5개의 변경된 파일28개의 추가작업 그리고 43개의 파일을 삭제
  1. 0 1
      src/Meshes/mesh.ts
  2. 0 29
      src/Meshes/thinInstanceMesh.ts
  3. 10 5
      src/Shaders/default.vertex.fx
  4. 9 4
      src/Shaders/pbr.vertex.fx
  5. 9 4
      src/Shaders/shadowMap.vertex.fx

+ 0 - 1
src/Meshes/mesh.ts

@@ -93,7 +93,6 @@ export class _InstancesBatch {
  **/
 class _ThinInstanceDataStorage {
     public instancesCount: number = 0;
-    public nonUniformScaling: boolean = false;
     public matrixBuffer: Nullable<Buffer> = null;
     public matrixBufferSize = 32 * 16; // let's start with a maximum of 32 thin instances
     public matrixData: Nullable<Float32Array>;

+ 0 - 29
src/Meshes/thinInstanceMesh.ts

@@ -3,16 +3,6 @@ import { Mesh, _InstancesBatch } from "../Meshes/mesh";
 import { VertexBuffer, Buffer } from './buffer';
 import { Matrix, Vector3, TmpVectors } from '../Maths/math.vector';
 
-const isNonUniform = (buffer: DeepImmutableObject<Float32Array>, i: number) => {
-    TmpVectors.Vector3[1].copyFromFloats(buffer[i * 16 + 0], buffer[i * 16 + 1], buffer[i * 16 + 2]);
-    TmpVectors.Vector3[0].x = TmpVectors.Vector3[1].lengthSquared(); // scale x squared
-    TmpVectors.Vector3[1].copyFromFloats(buffer[i * 16 + 4], buffer[i * 16 + 5], buffer[i * 16 + 6]);
-    TmpVectors.Vector3[0].y = TmpVectors.Vector3[1].lengthSquared(); // scale y squared
-    TmpVectors.Vector3[1].copyFromFloats(buffer[i * 16 + 8], buffer[i * 16 + 9], buffer[i * 16 + 10]);
-    TmpVectors.Vector3[0].z = TmpVectors.Vector3[1].lengthSquared(); // scale z squared
-    return TmpVectors.Vector3[0].isNonUniformWithinEpsilon(0.0001);
-};
-
 declare module "./mesh" {
     export interface Mesh {
         /**
@@ -133,13 +123,6 @@ Mesh.prototype.thinInstanceSetMatrixAt = function(index: number, matrix: DeepImm
 
     matrix.copyToArray(matrixData, index * 16);
 
-    if (!this.nonUniformScaling) {
-        this._thinInstanceDataStorage.nonUniformScaling = isNonUniform(matrix.m, 0);
-        if (this._thinInstanceDataStorage.nonUniformScaling) {
-            this._updateNonUniformScalingState(true);
-        }
-    }
-
     if (refresh) {
         this.thinInstanceBufferUpdated("matrix");
 
@@ -188,18 +171,6 @@ Mesh.prototype.thinInstanceSetBuffer = function(kind: string, buffer: Nullable<F
             this.setVerticesBuffer(matrixBuffer.createVertexBuffer("world2", 8, 4));
             this.setVerticesBuffer(matrixBuffer.createVertexBuffer("world3", 12, 4));
 
-            /*this._thinInstanceDataStorage.nonUniformScaling = false;
-
-            if (!this.ignoreNonUniformScaling) {
-                for (let i = 0; i < this._thinInstanceDataStorage.instancesCount && !this._thinInstanceDataStorage.nonUniformScaling; ++i) {
-                    this._thinInstanceDataStorage.nonUniformScaling = isNonUniform(buffer, i);
-                }
-
-                if (this._thinInstanceDataStorage.nonUniformScaling && !this.nonUniformScaling) {
-                    this._updateNonUniformScalingState(true);
-                }
-            }*/
-
             if (!this.doNotSyncBoundingInfo) {
                 this.thinInstanceRefreshBoundingInfo(false);
             }

+ 10 - 5
src/Shaders/default.vertex.fx

@@ -127,11 +127,16 @@ void main(void) {
 #ifdef NORMAL
 	mat3 normalWorld = mat3(finalWorld);
 
-	#ifdef NONUNIFORMSCALING
-		normalWorld = transposeMat3(inverseMat3(normalWorld));
-	#endif
-
-	vNormalW = normalize(normalWorld * normalUpdated);
+    #if defined(INSTANCES) && defined(THIN_INSTANCES)
+        vNormalW = normalUpdated / vec3(dot(normalWorld[0], normalWorld[0]), dot(normalWorld[1], normalWorld[1]), dot(normalWorld[2], normalWorld[2]));
+        vNormalW = normalize(normalWorld * vNormalW);
+    #else
+        #ifdef NONUNIFORMSCALING
+            normalWorld = transposeMat3(inverseMat3(normalWorld));
+        #endif
+
+        vNormalW = normalize(normalWorld * normalUpdated);
+    #endif
 #endif
 
 #define CUSTOM_VERTEX_UPDATE_WORLDPOS

+ 9 - 4
src/Shaders/pbr.vertex.fx

@@ -173,11 +173,16 @@ void main(void) {
 #ifdef NORMAL
     mat3 normalWorld = mat3(finalWorld);
 
-    #ifdef NONUNIFORMSCALING
-        normalWorld = transposeMat3(inverseMat3(normalWorld));
-    #endif
+    #if defined(INSTANCES) && defined(THIN_INSTANCES)
+        vNormalW = normalUpdated / vec3(dot(normalWorld[0], normalWorld[0]), dot(normalWorld[1], normalWorld[1]), dot(normalWorld[2], normalWorld[2]));
+        vNormalW = normalize(normalWorld * vNormalW);
+    #else
+        #ifdef NONUNIFORMSCALING
+            normalWorld = transposeMat3(inverseMat3(normalWorld));
+        #endif
 
-    vNormalW = normalize(normalWorld * normalUpdated);
+        vNormalW = normalize(normalWorld * normalUpdated);
+    #endif
 
     #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
         vec3 reflectionVector = vec3(reflectionMatrix * vec4(vNormalW, 0)).xyz;

+ 9 - 4
src/Shaders/shadowMap.vertex.fx

@@ -51,11 +51,16 @@ vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
 #ifdef NORMAL
     mat3 normWorldSM = mat3(finalWorld);
 
-    #ifdef NONUNIFORMSCALING
-        normWorldSM = transposeMat3(inverseMat3(normWorldSM));
+    #if defined(INSTANCES) && defined(THIN_INSTANCES)
+        vec3 vNormalW = normalUpdated / vec3(dot(normWorldSM[0], normWorldSM[0]), dot(normWorldSM[1], normWorldSM[1]), dot(normWorldSM[2], normWorldSM[2]));
+        vNormalW = normalize(normWorldSM * vNormalW);
+    #else
+        #ifdef NONUNIFORMSCALING
+            normWorldSM = transposeMat3(inverseMat3(normWorldSM));
+        #endif
+
+        vec3 vNormalW = normalize(normWorldSM * normalUpdated);
     #endif
-
-    vec3 vNormalW = normalize(normWorldSM * normalUpdated);
 #endif
 
 #include<shadowMapVertexNormalBias>