Browse Source

Adding emission and pseudo-subsurface scattering support

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
4f3a504cc4

+ 6 - 5
shaders/IsLp.frag.glsl

@@ -78,17 +78,18 @@ void main()
 	vec3 normal;
 	vec3 diffCol;
 	vec3 specCol;
-	float specPower;
-	const float subsurface = 0.0;
+	float roughness;
+	float subsurface;
+	float emission;
 
 	readGBuffer(
 		u_msRt0, u_msRt1, u_msRt2,
-		in_texCoord, diffCol, normal, specCol, specPower);
+		in_texCoord, diffCol, normal, specCol, roughness, subsurface, emission);
 
-	float a2 = pow(max(EPSILON, specPower), 2.0);
+	float a2 = pow(max(EPSILON, roughness), 2.0);
 
 	// Ambient color
-	out_color = diffCol * u_lightingUniforms.sceneAmbientColor.rgb;
+	out_color = diffCol * u_lightingUniforms.sceneAmbientColor.rgb + emission;
 
 	// Get counts and offsets
 	uint k = calcClusterSplit(fragPos.z);

+ 5 - 4
shaders/MsCommonFrag.glsl

@@ -215,10 +215,11 @@ void writeRts(
 	in vec3 diffColor, // from 0 to 1
 	in vec3 normal,
 	in vec3 specularColor,
-	in float specularPower,
-	in float blurring)
+	in float roughness,
+	in float subsurface,
+	in float emission)
 {
-	writeGBuffer(diffColor, normal, specularColor, specularPower,
-		out_msRt0, out_msRt1, out_msRt2);
+	writeGBuffer(diffColor, normal, specularColor, roughness, subsurface,
+		emission, out_msRt0, out_msRt1, out_msRt2);
 }
 #endif

+ 20 - 13
shaders/Pack.glsl

@@ -92,34 +92,41 @@ void writeGBuffer(
 	in vec3 normal,
 	in vec3 specColor,
 	in float roughness,
-	out vec4 fai0,
-	out vec4 fai1,
-	out vec4 fai2)
+	in float subsurface,
+	in float emission,
+	out vec4 rt0,
+	out vec4 rt1,
+	out vec4 rt2)
 {
-	fai0 = vec4(diffColor, 0.0);
-	fai1 = vec4(specColor, roughness);
-	fai2 = vec4(normal * 0.5 + 0.5, 0.0);
+	rt0 = vec4(diffColor, packUnorm2ToUnorm1(vec2(subsurface, emission)));
+	rt1 = vec4(specColor, roughness);
+	rt2 = vec4(normal * 0.5 + 0.5, 0.0);
 }
 
 // Read from the G buffer
 void readGBuffer(
-	in sampler2D fai0,
-	in sampler2D fai1,
-	in sampler2D fai2,
+	in sampler2D rt0,
+	in sampler2D rt1,
+	in sampler2D rt2,
 	in vec2 texCoord,
 	out vec3 diffColor,
 	out vec3 normal,
 	out vec3 specColor,
-	out float roughness)
+	out float roughness,
+	out float subsurface,
+	out float emission)
 {
-	vec4 comp = textureLod(fai0, texCoord, 0.0);
+	vec4 comp = textureLod(rt0, texCoord, 0.0);
 	diffColor = comp.rgb;
+	vec2 tmp = unpackUnorm1ToUnorm2(comp.a);
+	subsurface = tmp.x;
+	emission = tmp.y;
 
-	comp = textureLod(fai1, texCoord, 0.0);
+	comp = textureLod(rt1, texCoord, 0.0);
 	specColor = comp.rgb;
 	roughness = comp.a;
 
-	normal = textureLod(fai2, texCoord, 0.0).rgb;
+	normal = textureLod(rt2, texCoord, 0.0).rgb;
 	normal = normalize(normal * 2.0 - 1.0);
 }
 

+ 14 - 0
tools/scene/Exporter.cpp

@@ -676,6 +676,20 @@ void Exporter::exportMaterial(
 	materialStr = replaceAllString(materialStr, "%diffuseMap%",
 		m_texrpath + diffTex);
 
+	// Subsurface
+	materialStr = replaceAllString(materialStr, "%subsurfaceInput%",
+		"<input><type>float</type><name>subsurface</name>"
+		"<const>1</const><value>0.0</value></input>");
+	materialStr = replaceAllString(materialStr, "%subsurfaceArg%",
+		"subsurface");
+
+	// Emission
+	materialStr = replaceAllString(materialStr, "%emissionInput%",
+		"<input><type>float</type><name>emission</name>"
+		"<const>1</const><value>0.0</value></input>");
+	materialStr = replaceAllString(materialStr, "%emissionArg%",
+		"emission");
+
 	// Replace texture extensions with .anki
 	materialStr = replaceAllString(materialStr, ".tga", ".ankitex");
 	materialStr = replaceAllString(materialStr, ".png", ".ankitex");

+ 5 - 3
tools/scene/templates/diffNormSpecFrag.h

@@ -8,11 +8,12 @@ R"(		<program>
 			<inputs>
 				%specularColorInput%
 				%specularPowerInput%
-				<input><type>float</type><name>uBlurring</name><value>0.0</value><const>1</const></input>
 				%diffuseColorInput%
 				%normalInput%
+				%subsurfaceInput%
+				%emissionInput%
 			</inputs>
-			
+
 			<operations>
 				<operation>
 					<id>0</id>
@@ -42,7 +43,8 @@ R"(		<program>
 						<argument>%normalArg%</argument>
 						<argument>%specularColorArg%</argument>
 						<argument>%specularPowerArg%</argument>
-						<argument>uBlurring</argument>
+						<argument>%subsurfaceArg%</argument>
+						<argument>%emissionArg%</argument>
 					</arguments>
 				</operation>
 			</operations>