浏览代码

definitions and properties for object space normal maps in standard and pbr materials, removed redundant bindOnlyWorldMatrix function, added normal matrix to shaders

Max Limper 7 年之前
父节点
当前提交
01f0b8313c

+ 18 - 7
src/Materials/PBR/babylon.pbrBaseMaterial.ts

@@ -54,6 +54,7 @@
         public TANGENT = false;
         public BUMP = false;
         public BUMPDIRECTUV = 0;
+        public OBJECTSPACE_NORMALMAP = false;
         public PARALLAX = false;
         public PARALLAXOCCLUSION = false;
         public NORMALXYSCALE = true;
@@ -344,6 +345,11 @@
         protected _useRadianceOverAlpha = true;
 
         /**
+         * Allows using an object space normal map (instead of tangent space).
+         */
+        protected _useObjectSpaceNormalMap = false;
+
+        /**
          * Allows using the bump map in parallax mode.
          */
         protected _useParallax = false;
@@ -815,9 +821,11 @@
                         else {
                             defines.PARALLAX = false;
                         }
+                        
+                        defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
                     } else {
                         defines.BUMP = false;
-                    }
+                    }                
 
                     var refractionTexture = this._getRefractionTexture();
                     if (refractionTexture && StandardMaterial.RefractionTextureEnabled) {
@@ -1054,7 +1062,7 @@
                     "vFogInfos", "vFogColor", "pointSize",
                     "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vMicroSurfaceSamplerInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
                     "mBones",
-                    "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "microSurfaceSamplerMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
+                    "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "normalMatrix", "microSurfaceSamplerMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
                     "vLightingIntensity",
                     "logarithmicDepthConstant",
                     "vSphericalX", "vSphericalY", "vSphericalZ",
@@ -1165,11 +1173,7 @@
 
             super.unbind();
         }
-
-        public bindOnlyWorldMatrix(world: Matrix): void {
-            this._activeEffect.setMatrix("world", world);
-        }
-
+        
         public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
             var scene = this.getScene();
 
@@ -1189,6 +1193,13 @@
             // Matrices
             this.bindOnlyWorldMatrix(world);
 
+            // Normal Matrix
+            if (defines.OBJECTSPACE_NORMALMAP)
+            {
+                var normalMatrix = world.toNormalMatrix();
+                this.bindOnlyNormalMatrix(normalMatrix);                
+            }
+
             let mustRebind = this._mustRebind(scene, effect, mesh.visibility);
 
             // Bones

+ 7 - 0
src/Materials/PBR/babylon.pbrMaterial.ts

@@ -318,6 +318,13 @@
         public useRadianceOverAlpha = true;
 
         /**
+         * Allows using an object space normal map (instead of tangent space).
+         */
+        @serialize()
+        @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+        public useObjectSpaceNormalMap = false;
+        
+        /**
          * Allows using the bump map in parallax mode.
          */
         @serialize()

+ 16 - 1
src/Materials/babylon.standardMaterial.ts

@@ -47,6 +47,7 @@ module BABYLON {
         public REFLECTIONFRESNELFROMSPECULAR = false;
         public LIGHTMAP = false;
         public LIGHTMAPDIRECTUV = 0;
+        public OBJECTSPACE_NORMALMAP = false;
         public USELIGHTMAPASSHADOWMAP = false;
         public REFLECTIONMAP_3D = false;
         public REFLECTIONMAP_SPHERICAL = false;
@@ -196,6 +197,11 @@ module BABYLON {
         @expandToProperty("_markAllSubMeshesAsLightsDirty")
         public disableLighting: boolean;
 
+        @serialize("useObjectSpaceNormalMap")
+        private _useObjectSpaceNormalMap = false;
+        @expandToProperty("_markAllSubMeshesAsTexturesDirty")
+        public useObjectSpaceNormalMap: boolean;
+
         @serialize("useParallax")
         private _useParallax = false;
         @expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -663,6 +669,8 @@ module BABYLON {
                             defines.PARALLAX = this._useParallax;
                             defines.PARALLAXOCCLUSION = this._useParallaxOcclusion;
                         }
+
+                        defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
                     } else {
                         defines.BUMP = false;
                     }
@@ -966,6 +974,13 @@ module BABYLON {
             // Matrices        
             this.bindOnlyWorldMatrix(world);
 
+            // Normal Matrix
+            if (defines.OBJECTSPACE_NORMALMAP)
+            {
+                var normalMatrix = world.toNormalMatrix();
+                this.bindOnlyNormalMatrix(normalMatrix);                
+            }
+
             let mustRebind = this._mustRebind(scene, effect, mesh.visibility);
 
             // Bones
@@ -1146,7 +1161,7 @@ module BABYLON {
                 // View
                 if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || this._reflectionTexture || this._refractionTexture) {
                     this.bindView(effect);
-                }
+                }              
 
                 // Fog
                 MaterialHelper.BindFogParameters(scene, mesh, effect);

+ 5 - 0
src/Shaders/ShadersInclude/bumpFragment.fx

@@ -25,5 +25,10 @@
 #endif
 
 #ifdef BUMP
+#ifdef OBJECTSPACE_NORMALMAP
+	normalW = normalize(texture2D(bumpSampler, vBumpUV).xyz  * 2.0 - 1.0);
+	normalW = normalize(mat3(normalMatrix) * normalW);	
+#else
 	normalW = perturbNormal(TBN, vBumpUV + uvOffset);
+#endif
 #endif

+ 4 - 0
src/Shaders/ShadersInclude/defaultFragmentDeclaration.fx

@@ -34,6 +34,10 @@ uniform vec2 vTangentSpaceParams;
 uniform mat4 view;
 #endif
 
+#ifdef OBJECTSPACE_NORMALMAP
+uniform mat4 normalMat;
+#endif
+
 #ifdef REFRACTION
 uniform vec4 vRefractionInfos;
 

+ 4 - 0
src/Shaders/ShadersInclude/pbrFragmentDeclaration.fx

@@ -46,6 +46,10 @@ uniform vec2 vMicroSurfaceSamplerInfos;
 uniform mat4 view;
 #endif
 
+#ifdef OBJECTSPACE_NORMALMAP
+uniform mat4 normalMatrix;
+#endif
+
 // Refraction
 #ifdef REFRACTION
     uniform vec4 vRefractionInfos;