瀏覽代碼

[REFACTORING] Move GBuffer pass' normal to world space

Panagiotis Christopoulos Charitos 8 年之前
父節點
當前提交
e2fa9e8c21

+ 2 - 2
programs/FinalComposite.ankiprog

@@ -86,9 +86,9 @@ void main()
 	out_color += blueNoise / 255.0;
 #endif
 
-#if 0
+#if 1
 	{
-		out_color = vec3(textureLod(u_isRt, uv, 0.0).r);
+		out_color = vec3(textureLod(u_isRt, uv, 0.0).rgb);
 	}
 #endif
 

+ 2 - 2
programs/GBufferGeneric.ankiprog

@@ -23,7 +23,7 @@ http://www.anki3d.org/LICENSE
 		<shader type="vert">
 			<inputs>
 				<input name="mvp" type="mat4" instanced="1"/>
-				<input name="normalMat" type="mat3" instanced="1">
+				<input name="rotationMat" type="mat3" instanced="1">
 					<mutators>
 						<mutator name="PASS" values="0"/>
 					</mutators>
@@ -45,7 +45,7 @@ void main()
 #endif
 
 #if PASS == PASS_GB_FS
-	positionUvNormalTangent(mvp, normalMat);
+	positionUvNormalTangent(mvp, rotationMat);
 	
 #	if PARALLAX
 	parallax(modelViewMat);

+ 2 - 1
programs/LightShading.ankiprog

@@ -266,7 +266,8 @@ void main()
 
 	outC += specIndirect * specIndirectTerm + diffIndirect * diffCol;
 
-	out_color = outC;
+	//out_color = outC;
+	out_color = diffCol;
 #if 0
 	count = scount;
 	if(count == 0)

+ 3 - 3
shaders/GBufferCommonFrag.glsl

@@ -19,8 +19,8 @@ layout(location = 2) in mediump vec4 in_tangent;
 #if CALC_BITANGENT_IN_VERT
 layout(location = 3) in mediump vec3 in_bitangent;
 #endif
-layout(location = 4) in mediump vec3 in_vertPosViewSpace;
-layout(location = 5) in mediump vec3 in_eyeTangentSpace; // Parallax
+layout(location = 4) in mediump float in_distFromTheCamera; // Parallax
+layout(location = 5) in highp vec3 in_eyeTangentSpace; // Parallax
 layout(location = 6) in mediump vec3 in_normalTangentSpace; // Parallax
 #endif // PASS == PASS_GB_FS
 
@@ -90,7 +90,7 @@ vec2 computeTextureCoordParallax(in sampler2D heightMap, in vec2 uv, in float he
 	vec3 E = normalize(eyeTangentSpace);
 
 	float factor0 = -dot(E, normTangentSpace);
-	float factor1 = in_vertPosViewSpace.z / -MAX_EFFECTIVE_DISTANCE;
+	float factor1 = in_distFromTheCamera / -MAX_EFFECTIVE_DISTANCE;
 	float factor = (1.0 - factor0) * (1.0 - factor1);
 	float sampleCountf = mix(float(MIN_SAMPLES), float(MAX_SAMPLES), factor);
 

+ 19 - 15
shaders/GBufferCommonVert.glsl

@@ -48,8 +48,8 @@ layout(location = 2) out mediump vec4 out_tangent;
 #if CALC_BITANGENT_IN_VERT
 layout(location = 3) out mediump vec3 out_bitangent;
 #endif
-layout(location = 4) out mediump vec3 out_vertPosViewSpace;
-layout(location = 5) out mediump vec3 out_eyeTangentSpace; // Parallax
+layout(location = 4) out mediump float out_distFromTheCamera; // Parallax
+layout(location = 5) out highp vec3 out_eyeTangentSpace; // Parallax
 layout(location = 6) out mediump vec3 out_normalTangentSpace; // Parallax
 #endif
 
@@ -66,35 +66,39 @@ mediump vec4 g_tangent = in_tangent;
 //
 // Functions
 //
+
+// Common store function
 #if PASS == PASS_GB_FS
-void positionUvNormalTangent(mat4 mvp, mat3 normalMat)
+void positionUvNormalTangent(mat4 mvp, mat3 rotationMat)
 {
 	out_uv = g_uv;
 	gl_Position = mvp * vec4(g_position, 1.0);
 
-	out_normal = normalMat * g_normal.xyz;
-	out_tangent.xyz = normalMat * g_tangent.xyz;
+	out_normal = rotationMat * g_normal.xyz;
+	out_tangent.xyz = rotationMat * g_tangent.xyz;
 	out_tangent.w = g_tangent.w;
 
 #if CALC_BITANGENT_IN_VERT
 	out_bitangent = cross(out_normal, out_tangent.xyz) * out_tangent.w;
 #endif
 }
+#endif // PASS == PASS_GB_FS
 
-void parallax(in mat4 modelViewMat)
+// Store stuff for parallax mapping
+#if PASS == PASS_GB_FS
+void parallax(mat4 modelViewMat)
 {
-	vec3 n = out_normal;
-	vec3 t = out_tangent.xyz;
-#if CALC_BITANGENT_IN_VERT
-	vec3 b = out_bitangent;
-#else
+	vec3 n = in_normal;
+	vec3 t = in_tangent.xyz;
 	vec3 b = cross(n, t) * in_tangent.w;
-#endif
-	mat3 invTbn = transpose(mat3(t, b, n));
 
-	out_vertPosViewSpace = vec3(modelViewMat * vec4(g_position, 1.0));
+	mat3 normalMat = mat3(modelViewMat);
+	mat3 invTbn = transpose(normalMat * mat3(t, b, n));
+
+	vec3 viewPos = (modelViewMat * vec4(g_position, 1.0)).xyz;
+	out_distFromTheCamera = length(viewPos);
 
-	out_eyeTangentSpace = invTbn * out_vertPosViewSpace;
+	out_eyeTangentSpace = invTbn * viewPos;
 	out_normalTangentSpace = invTbn * n;
 }
 #endif // PASS == PASS_GB_FS

+ 5 - 2
src/anki/resource/Material.cpp

@@ -20,10 +20,13 @@ struct BuiltinVarInfo
 static const Array<BuiltinVarInfo, U(BuiltinMaterialVariableId::COUNT) - 1> BUILTIN_INFOS = {
 	{{"MODEL_VIEW_PROJECTION_MATRIX", ShaderVariableDataType::MAT4, true},
 		{"MODEL_VIEW_MATRIX", ShaderVariableDataType::MAT4, true},
+		{"MODEL_MATRIX", ShaderVariableDataType::MAT4, true},
 		{"VIEW_PROJECTION_MATRIX", ShaderVariableDataType::MAT4, false},
 		{"VIEW_MATRIX", ShaderVariableDataType::MAT4, false},
 		{"NORMAL_MATRIX", ShaderVariableDataType::MAT3, true},
-		{"CAMERA_ROTATION_MATRIX", ShaderVariableDataType::MAT3, false}}};
+		{"ROTATION_MATRIX", ShaderVariableDataType::MAT3, true},
+		{"CAMERA_ROTATION_MATRIX", ShaderVariableDataType::MAT3, false},
+		{"CAMERA_POSITION", ShaderVariableDataType::VEC3, false}}};
 
 MaterialVariable::MaterialVariable()
 {
@@ -297,7 +300,7 @@ Error Material::parseInputs(XmlElement inputsEl, Bool async)
 
 		if(!foundVar->acceptAllMutations(m_mutations))
 		{
-			ANKI_RESOURCE_LOGE("Variable \"%s\" is not needed by the material's mutations. Don't need it");
+			ANKI_RESOURCE_LOGE("Variable \"%s\" is not needed by the material's mutations");
 			return Error::USER_DATA;
 		}
 

+ 3 - 0
src/anki/resource/Material.h

@@ -33,10 +33,13 @@ enum class BuiltinMaterialVariableId : U8
 	NONE = 0,
 	MODEL_VIEW_PROJECTION_MATRIX,
 	MODEL_VIEW_MATRIX,
+	MODEL_MATRIX,
 	VIEW_PROJECTION_MATRIX,
 	VIEW_MATRIX,
 	NORMAL_MATRIX,
+	ROTATION_MATRIX,
 	CAMERA_ROTATION_MATRIX,
+	CAMERA_POSITION,
 	COUNT
 };
 

+ 41 - 2
src/anki/scene/RenderComponent.cpp

@@ -73,8 +73,24 @@ void RenderComponent::allocateAndSetupUniforms(
 		}
 		case ShaderVariableDataType::VEC3:
 		{
-			Vec3 val = mvar.getValue<Vec3>();
-			progVariant.writeShaderBlockMemory(progvar, &val, 1, uniformsBegin, uniformsEnd);
+			switch(mvar.getBuiltin())
+			{
+			case BuiltinMaterialVariableId::NONE:
+			{
+				Vec3 val = mvar.getValue<Vec3>();
+				progVariant.writeShaderBlockMemory(progvar, &val, 1, uniformsBegin, uniformsEnd);
+				break;
+			}
+			case BuiltinMaterialVariableId::CAMERA_POSITION:
+			{
+				Vec3 val = ctx.m_cameraTransform.getTranslationPart().xyz();
+				progVariant.writeShaderBlockMemory(progvar, &val, 1, uniformsBegin, uniformsEnd);
+				break;
+			}
+			default:
+				ANKI_ASSERT(0);
+			}
+
 			break;
 		}
 		case ShaderVariableDataType::VEC4:
@@ -111,6 +127,21 @@ void RenderComponent::allocateAndSetupUniforms(
 					progvar, &normMats[0], transforms.getSize(), uniformsBegin, uniformsEnd);
 				break;
 			}
+			case BuiltinMaterialVariableId::ROTATION_MATRIX:
+			{
+				ANKI_ASSERT(transforms.getSize() > 0);
+
+				DynamicArrayAuto<Mat3> rots(getFrameAllocator());
+				rots.create(transforms.getSize());
+
+				for(U i = 0; i < transforms.getSize(); i++)
+				{
+					rots[i] = transforms[i].getRotationPart();
+				}
+
+				progVariant.writeShaderBlockMemory(progvar, &rots[0], transforms.getSize(), uniformsBegin, uniformsEnd);
+				break;
+			}
 			case BuiltinMaterialVariableId::CAMERA_ROTATION_MATRIX:
 			{
 				Mat3 rot = ctx.m_cameraTransform.getRotationPart();
@@ -163,6 +194,14 @@ void RenderComponent::allocateAndSetupUniforms(
 				progVariant.writeShaderBlockMemory(progvar, &mv[0], transforms.getSize(), uniformsBegin, uniformsEnd);
 				break;
 			}
+			case BuiltinMaterialVariableId::MODEL_MATRIX:
+			{
+				ANKI_ASSERT(transforms.getSize() > 0);
+
+				progVariant.writeShaderBlockMemory(
+					progvar, &transforms[0], transforms.getSize(), uniformsBegin, uniformsEnd);
+				break;
+			}
 			case BuiltinMaterialVariableId::VIEW_PROJECTION_MATRIX:
 			{
 				ANKI_ASSERT(transforms.getSize() == 0 && "Cannot have transform");

+ 3 - 2
tools/scene/CMakeLists.txt

@@ -2,5 +2,6 @@ include_directories("../../thirdparty/assimp/include")
 
 add_definitions("-fexceptions")
 
-add_executable(ankisceneimp Main.cpp Common.cpp Exporter.cpp ExporterMesh.cpp ExporterMaterial.cpp)
-target_link_libraries(ankisceneimp ankiassimp)
+add_executable(sceneimp Main.cpp Common.cpp Exporter.cpp ExporterMesh.cpp ExporterMaterial.cpp)
+target_link_libraries(sceneimp ankiassimp)
+installExecutable(sceneimp)

+ 4 - 4
tools/scene/ExporterMaterial.cpp

@@ -22,8 +22,8 @@ const char* MATERIAL_TEMPLATE = R"(<?xml version="1.0" encoding="UTF-8" ?>
 	
 	<inputs>
 		<input shaderInput="mvp" builtin="MODEL_VIEW_PROJECTION_MATRIX"/>
-		<input shaderInput="normalMat" builtin="NORMAL_MATRIX"/>
-		%modelViewMat%
+		<input shaderInput="rotationMat" builtin="ROTATION_MATRIX"/>
+		%parallaxInput%
 	
 		%diff%
 		%spec%
@@ -247,7 +247,7 @@ void Exporter::exportMaterial(const aiMaterial& mtl) const
 					  "\t\t<input shaderInput=\"heightMapScale\" value=\"0.05\"/>");
 
 			xml = replaceAllString(
-				xml, "%modelViewMat%", "<input shaderInput=\"modelViewMat\" builtin=\"MODEL_VIEW_MATRIX\"/>");
+				xml, "%parallaxInput%", "<input shaderInput=\"modelViewMat\" builtin=\"MODEL_VIEW_MATRIX\"/>");
 
 			xml = replaceAllString(xml, "%parallaxMutator%", "1");
 		}
@@ -259,7 +259,7 @@ void Exporter::exportMaterial(const aiMaterial& mtl) const
 	else
 	{
 		xml = replaceAllString(xml, "%height%", "");
-		xml = replaceAllString(xml, "%modelViewMat%", "");
+		xml = replaceAllString(xml, "%parallaxInput%", "");
 		xml = replaceAllString(xml, "%parallaxMutator%", "0");
 	}