Selaa lähdekoodia

Split of bumpFragmentFunctions for easier reusing

Popov72 5 vuotta sitten
vanhempi
commit
65d8183b8f

+ 7 - 1
src/Materials/Node/Blocks/Fragment/perturbNormalBlock.ts

@@ -12,6 +12,7 @@ import { Mesh } from '../../../../Meshes/mesh';
 import { Scene } from '../../../../scene';
 import { editableInPropertyPage, PropertyTypeForEdition } from "../../nodeMaterialDecorator";
 
+import "../../../../Shaders/ShadersInclude/bumpFragmentMainFunctions";
 import "../../../../Shaders/ShadersInclude/bumpFragmentFunctions";
 import "../../../../Shaders/ShadersInclude/bumpFragment";
 
@@ -163,12 +164,17 @@ export class PerturbNormalBlock extends NodeMaterialBlock {
             state.compilationString += `mat3 vTBN = mat3(tbnTangent, tbnBitangent, tbnNormal);\r\n`;
         }
 
+        state._emitFunctionFromInclude("bumpFragmentMainFunctions", comments, {
+            replaceStrings: [
+                tangentReplaceString,
+            ]
+        });
+
         state._emitFunctionFromInclude("bumpFragmentFunctions", comments, {
             replaceStrings: [
                 { search: /vBumpInfos.y/g, replace: replaceForBumpInfos},
                 { search: /vTangentSpaceParams/g, replace: this._tangentSpaceParameterName},
                 { search: /vPositionW/g, replace: worldPosition.associatedVariableName + ".xyz"},
-                tangentReplaceString
             ]
         });
 

+ 9 - 8
src/Materials/Node/Blocks/PBR/anisotropyBlock.ts

@@ -129,15 +129,16 @@ export class AnisotropyBlock extends NodeMaterialBlock {
             code += `mat3 vTBN = mat3(tbnTangent, tbnBitangent, tbnNormal);\r\n`;
         }
 
-        state._emitFunctionFromInclude("bumpFragmentFunctions", comments);
-
-        code += state._emitCodeFromInclude("bumpFragment", comments, {
+        code += `
+            #if defined(${worldTangent.isConnected ? "TANGENT" : "IGNORE"}) && defined(NORMAL)
+                mat3 TBN = vTBN;
+            #else
+                mat3 TBN = cotangent_frame(${worldNormal.associatedVariableName + ".xyz"}, ${"v_" + worldPosition.associatedVariableName + ".xyz"}, ${uv.isConnected ? uv.associatedVariableName : "vec2(0.)"}, vec2(1., 1.));
+            #endif\r\n`;
+
+        state._emitFunctionFromInclude("bumpFragmentMainFunctions", comments, {
             replaceStrings: [
-                { search: /vMainUV1/g, replace: uv.isConnected ? uv.associatedVariableName : "vec2(0.)"},
-                { search: /vPositionW/g, replace: "v_" + worldPosition.associatedVariableName + ".xyz"},
-                { search: /normalW=/g, replace: "NOTUSED=" },
-                { search: /normalW/g, replace: worldNormal.associatedVariableName + ".xyz" },
-                tangentReplaceString
+                tangentReplaceString,
             ]
         });
 

+ 1 - 49
src/Shaders/ShadersInclude/bumpFragmentFunctions.fx

@@ -1,52 +1,4 @@
-#if defined(BUMP) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
-	#if defined(TANGENT) && defined(NORMAL) 
-		varying mat3 vTBN;
-	#endif
-
-	#ifdef OBJECTSPACE_NORMALMAP
-		uniform mat4 normalMatrix;
-	#endif
-
-	vec3 perturbNormal(mat3 cotangentFrame, vec3 textureSample, float scale)
-	{
-		textureSample = textureSample * 2.0 - 1.0;
-
-		#ifdef NORMALXYSCALE
-			textureSample = normalize(textureSample * vec3(scale, scale, 1.0));
-		#endif
-
-		return normalize(cotangentFrame * textureSample);
-	}
-
-	// Thanks to http://www.thetenthplanet.de/archives/1180
-	mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv, vec2 tangentSpaceParams)
-	{
-		// flip the uv for the backface
-		uv = gl_FrontFacing ? uv : -uv;
-
-		// get edge vectors of the pixel triangle
-		vec3 dp1 = dFdx(p);
-		vec3 dp2 = dFdy(p);
-		vec2 duv1 = dFdx(uv);
-		vec2 duv2 = dFdy(uv);
-
-		// solve the linear system
-		vec3 dp2perp = cross(dp2, normal);
-		vec3 dp1perp = cross(normal, dp1);
-		vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
-		vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y;
-
-		// invert the tangent/bitangent if requested
-		tangent *= tangentSpaceParams.x;
-		bitangent *= tangentSpaceParams.y;
-
-		// construct a scale-invariant frame
-		float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent)));
-		return mat3(tangent * invmax, bitangent * invmax, normal);
-	}
-#endif
-
-#if defined(BUMP)
+#if defined(BUMP)
 	#if BUMPDIRECTUV == 1
 		#define vBumpUV vMainUV1
 	#elif BUMPDIRECTUV == 2

+ 47 - 0
src/Shaders/ShadersInclude/bumpFragmentMainFunctions.fx

@@ -0,0 +1,47 @@
+#if defined(BUMP) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
+	#if defined(TANGENT) && defined(NORMAL) 
+		varying mat3 vTBN;
+	#endif
+
+	#ifdef OBJECTSPACE_NORMALMAP
+		uniform mat4 normalMatrix;
+	#endif
+
+	vec3 perturbNormal(mat3 cotangentFrame, vec3 textureSample, float scale)
+	{
+		textureSample = textureSample * 2.0 - 1.0;
+
+		#ifdef NORMALXYSCALE
+			textureSample = normalize(textureSample * vec3(scale, scale, 1.0));
+		#endif
+
+		return normalize(cotangentFrame * textureSample);
+	}
+
+	// Thanks to http://www.thetenthplanet.de/archives/1180
+	mat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv, vec2 tangentSpaceParams)
+	{
+		// flip the uv for the backface
+		uv = gl_FrontFacing ? uv : -uv;
+
+		// get edge vectors of the pixel triangle
+		vec3 dp1 = dFdx(p);
+		vec3 dp2 = dFdy(p);
+		vec2 duv1 = dFdx(uv);
+		vec2 duv2 = dFdy(uv);
+
+		// solve the linear system
+		vec3 dp2perp = cross(dp2, normal);
+		vec3 dp1perp = cross(normal, dp1);
+		vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;
+		vec3 bitangent = dp2perp * duv1.y + dp1perp * duv2.y;
+
+		// invert the tangent/bitangent if requested
+		tangent *= tangentSpaceParams.x;
+		bitangent *= tangentSpaceParams.y;
+
+		// construct a scale-invariant frame
+		float invmax = inversesqrt(max(dot(tangent, tangent), dot(bitangent, bitangent)));
+		return mat3(tangent * invmax, bitangent * invmax, normal);
+	}
+#endif

+ 1 - 0
src/Shaders/default.fragment.fx

@@ -153,6 +153,7 @@ varying vec3 vDirectionW;
 
 #include<imageProcessingFunctions>
 
+#include<bumpFragmentMainFunctions>
 #include<bumpFragmentFunctions>
 #include<clipPlaneFragmentDeclaration>
 #include<logDepthDeclaration>

+ 1 - 0
src/Shaders/geometry.fragment.fx

@@ -44,6 +44,7 @@ uniform sampler2D diffuseSampler;
 #endif
 
 #include<mrtFragmentDeclaration>[RENDER_TARGET_COUNT]
+#include<bumpFragmentMainFunctions>
 #include<bumpFragmentFunctions>
 
 void main() {

+ 1 - 0
src/Shaders/pbr.fragment.fx

@@ -40,6 +40,7 @@ precision highp float;
 #include<pbrBRDFFunctions>
 #include<pbrDirectLightingFunctions>
 #include<pbrIBLFunctions>
+#include<bumpFragmentMainFunctions>
 #include<bumpFragmentFunctions>
 
 #ifdef REFLECTION