Browse Source

Optimizing parallax a bit

Panagiotis Christopoulos Charitos 9 years ago
parent
commit
c8871b3d7f
4 changed files with 40 additions and 37 deletions
  1. 1 1
      sandbox/Main.cpp
  2. 12 17
      shaders/MsCommonFrag.glsl
  3. 21 8
      shaders/MsCommonVert.glsl
  4. 6 11
      tools/scene/ExporterMaterial.cpp

+ 1 - 1
sandbox/Main.cpp

@@ -10,7 +10,7 @@
 
 using namespace anki;
 
-#define PLAYER 1
+#define PLAYER 0
 #define MOUSE 1
 
 class MyApp : public App

+ 12 - 17
shaders/MsCommonFrag.glsl

@@ -24,6 +24,8 @@ layout(location = 0) in highp vec2 in_uv;
 layout(location = 1) in mediump vec3 in_normal;
 layout(location = 2) in mediump vec4 in_tangent;
 layout(location = 3) in mediump vec3 in_vertPosViewSpace;
+layout(location = 4) in mediump vec3 in_eyeTangentSpace; // Parallax
+layout(location = 5) in mediump vec3 in_normalTangentSpace; // Parallax
 #endif
 
 //
@@ -209,13 +211,9 @@ float readRFromTexture(in sampler2D tex, in highp vec2 texCoords)
 }
 #endif
 
-#define computeTextureCoordParalax_DEFINED
-vec2 computeTextureCoordParalax(in sampler2D heightMap,
-	in vec2 uv,
-	in vec3 posView,
-	in vec3 normal,
-	in vec4 tangent,
-	in float heightMapScale)
+#define computeTextureCoordParallax_DEFINED
+vec2 computeTextureCoordParallax(
+	in sampler2D heightMap, in vec2 uv, in float heightMapScale)
 {
 #if PASS == COLOR && LOD == 0
 	const uint MAX_SAMPLES = 25;
@@ -226,13 +224,8 @@ vec2 computeTextureCoordParalax(in sampler2D heightMap,
 	vec2 dPdx = dFdx(uv);
 	vec2 dPdy = dFdy(uv);
 
-	vec3 n = normal; // Assume that getNormal() is called
-	vec3 t = normalize(tangent.xyz);
-	vec3 b = cross(n, t) * tangent.w;
-	mat3 invTbn = transpose(mat3(t, b, n));
-
-	vec3 eyeTangentSpace = invTbn * posView;
-	vec3 normTangentSpace = invTbn * n;
+	vec3 eyeTangentSpace = in_eyeTangentSpace;
+	vec3 normTangentSpace = in_normalTangentSpace;
 
 	float parallaxLimit = -length(eyeTangentSpace.xy) / eyeTangentSpace.z;
 	parallaxLimit *= heightMapScale;
@@ -242,9 +235,11 @@ vec2 computeTextureCoordParalax(in sampler2D heightMap,
 
 	vec3 E = normalize(eyeTangentSpace);
 
-	float sampleCountf = mix(MAX_SAMPLES,
-		MIN_SAMPLES,
-		min(dot(E, normTangentSpace), posView.z / -MAX_EFFECTIVE_DISTANCE));
+	float sampleCountf =
+		mix(MAX_SAMPLES,
+			MIN_SAMPLES,
+			min(dot(E, normTangentSpace),
+				in_vertPosViewSpace.z / -MAX_EFFECTIVE_DISTANCE));
 
 	float stepSize = 1.0 / sampleCountf;
 

+ 21 - 8
shaders/MsCommonVert.glsl

@@ -6,7 +6,7 @@
 #include "shaders/MsFsCommon.glsl"
 
 //
-// Attributes
+// Input
 //
 layout(location = POSITION_LOCATION) in highp vec3 in_position;
 layout(location = TEXTURE_COORDINATE_LOCATION) in highp vec2 in_uv;
@@ -20,7 +20,7 @@ layout(location = TANGENT_LOCATION) in mediump vec4 in_tangent;
 #endif
 
 //
-// Varyings
+// Output
 //
 out gl_PerVertex
 {
@@ -35,13 +35,9 @@ layout(location = 1) out mediump vec3 out_normal;
 
 #if PASS == COLOR
 layout(location = 2) out mediump vec4 out_tangent;
-
-// For env mapping. AKA view vector
 layout(location = 3) out mediump vec3 out_vertPosViewSpace;
-#endif
-
-#if INSTANCE_ID_FRAGMENT_SHADER
-layout(location = 4) flat out uint out_instanceId;
+layout(location = 4) out mediump vec3 out_eyeTangentSpace; // Parallax
+layout(location = 5) out mediump vec3 out_normalTangentSpace; // Parallax
 #endif
 
 //==============================================================================
@@ -95,3 +91,20 @@ void writeVertPosViewSpace(in mat4 modelViewMat)
 	out_vertPosViewSpace = vec3(modelViewMat * vec4(in_position, 1.0));
 }
 #endif
+
+//==============================================================================
+#if PASS == COLOR
+#define writeParallax_DEFINED
+void writeParallax(in mat3 normalMat, in mat4 modelViewMat)
+{
+	vec3 n = normalMat * in_normal.xyz;
+	vec3 t = normalMat * in_tangent.xyz;
+	vec3 b = cross(n, t) * in_tangent.w;
+	mat3 invTbn = transpose(mat3(t, b, n));
+
+	writeVertPosViewSpace(modelViewMat);
+
+	out_eyeTangentSpace = invTbn * out_vertPosViewSpace;
+	out_normalTangentSpace = invTbn * n;
+}
+#endif

+ 6 - 11
tools/scene/ExporterMaterial.cpp

@@ -445,8 +445,11 @@ void Exporter::exportMaterial(const aiMaterial& mtl) const
 			R"(<operation>
 					<id>2</id>
 					<returnType>void</returnType>
-					<function>writeVertPosViewSpace</function>
-					<arguments><argument>anki_mv</argument></arguments>
+					<function>writeParallax</function>
+					<arguments>
+						<argument>anki_n</argument>
+						<argument>anki_mv</argument>
+					</arguments>
 				</operation>)");
 
 		materialStr = replaceAllString(materialStr,
@@ -467,20 +470,12 @@ void Exporter::exportMaterial(const aiMaterial& mtl) const
 		materialStr = replaceAllString(materialStr,
 			"%heightFunc%",
 			R"(<operation>
-					<id>3</id>
-					<returnType>vec3</returnType>
-					<function>getPositionViewSpace</function>
-				</operation>
-				<operation>
 					<id>4</id>
 					<returnType>vec2</returnType>
-					<function>computeTextureCoordParalax</function>
+					<function>computeTextureCoordParallax</function>
 					<arguments>
 						<argument>heightMap</argument>
 						<argument>out2</argument>
-						<argument>out3</argument>
-						<argument>out0</argument>
-						<argument>out1</argument>
 						<argument>heightMapScale</argument>
 					</arguments>
 				</operation>)");