Skip to content

Commit

Permalink
#48 Add shader & update lightmap render pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jun 20, 2023
1 parent b81e484 commit 544d0c9
Show file tree
Hide file tree
Showing 28 changed files with 770 additions and 58 deletions.
17 changes: 17 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/GLSL/LightmapDeferredFS.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
precision mediump float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2DArray uTexIndirect;
uniform sampler2DArray uTexDirectional;

in vec3 varTexCoord0;

layout(location = 0) out vec4 Indirect;
layout(location = 1) out vec4 Directional;

void main(void)
{
Indirect = texture(uTexIndirect, varTexCoord0) * 3.0;
Directional = texture(uTexDirectional, varTexCoord0) * 3.0;
}
27 changes: 27 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/HLSL/LightmapDeferredFS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Texture2DArray uTexIndirect : register(t0);
SamplerState uTexIndirectSampler : register(s0);

Texture2DArray uTexDirectional : register(t1);
SamplerState uTexDirectionalSampler : register(s1);

struct PS_INPUT
{
float4 pos : SV_POSITION;
float3 tex0 : LIGHTMAP;
};

struct PS_OUTPUT
{
float4 Indirect: SV_TARGET0;
float4 Directional: SV_TARGET1;
};

PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT output;

output.Indirect = uTexIndirect.Sample(uTexIndirectSampler, input.tex0) * 3.0;
output.Directional = uTexDirectional.Sample(uTexDirectionalSampler, input.tex0) * 3.0;

return output;
}
15 changes: 15 additions & 0 deletions Assets/BuiltIn/Shader/Lightmap/LightmapDeferred.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<shaderConfig name="LightmapDeferred" baseShader="SOLID">
<uniforms>
<vs>
<uniform name="uMvpMatrix" type="WORLD_VIEW_PROJECTION" value="0" float="16" matrix="true"/>
</vs>
<fs>
<uniform name="uTexIndirect" type="DEFAULT_VALUE" value="0" float="1" directX="false"/>
<uniform name="uTexDirectional" type="DEFAULT_VALUE" value="0" float="2" directX="false"/>
</fs>
</uniforms>
<customUI>
</customUI>
<shader type="GLSL" vs="GLSL/VS.glsl" fs="GLSL/LightmapDeferredFS.glsl"/>
<shader type="HLSL" vs="HLSL/VS.hlsl" fs="HLSL/LightmapDeferredFS.hlsl"/>
</shaderConfig>
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ vec3 SG(

vec3 envSpecColor = mix(indirectColor, vec3(1.0, 1.0, 1.0), visibility);
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor + specular * specularColor * envSpecColor + light.a * specularColor;

// Direction lighting
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor;

// Direction specular
color += specular * specularColor * envSpecColor;

// Point light specular
color += light.a * specularColor;

// IBL Ambient
color += indirectColor * diffuseColor * indirectMultiplier / PI;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const float PI = 3.1415926;

#include "LibSolverMetallic.glsl"
#if defined(ENABLE_SSR)
#include "../../../SSR/GLSL/LibSSR.glsl"
#endif
#include "../../../PostProcessing/GLSL/LibToneMapping.glsl"

vec3 SGLM(
const vec3 baseColor,
const float spec,
const float gloss,
const vec4 position,
const vec3 worldViewDir,
const vec3 worldLightDir,
const vec3 worldNormal,
const vec3 lightColor,
const vec4 light,
const vec3 indirect,
const float directMultiplier,
const float indirectMultiplier,
const float lightMultiplier)
{
// Roughness
float glossiness = max(gloss, 0.01);
float roughness = 1.0 - glossiness;

// Metallic
vec3 f0 = vec3(spec, spec, spec);
vec3 specularColor = f0;
float oneMinusSpecularStrength = 1.0 - spec;
float metallic = solveMetallic(baseColor.rgb, specularColor, oneMinusSpecularStrength);

// Color
f0 = vec3(0.04, 0.04, 0.04);
vec3 diffuseColor = baseColor.rgb;
specularColor = mix(f0, baseColor.rgb, metallic);

// Tone mapping
specularColor = sRGB(specularColor);
diffuseColor = sRGB(diffuseColor);
vec3 directionColor = sRGB(light.rgb);
vec3 indirectColor = sRGB(indirect.rgb);

// Specular
vec3 H = normalize(worldLightDir + worldViewDir);
float NdotE = max(0.0, dot(worldNormal, H));
float specular = pow(NdotE, 100.0f * glossiness) * spec;

vec3 envSpecColor = vec3(1.0, 1.0, 1.0);

// Direction lighting
vec3 color = (directionColor * lightMultiplier) * diffuseColor;

// Direction specular
color += specular * specularColor * envSpecColor;

// IBL Ambient
color += indirectColor * diffuseColor * indirectMultiplier / PI;

// IBL reflection
#if defined(ENABLE_SSR)
vec3 reflection = -normalize(reflect(worldViewDir, worldNormal));
color += sRGB(SSR(linearRGB(color), position, reflection, roughness)) * metallic * specularColor;
#endif

return color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ vec3 SG(
float specular = pow(NdotE, 100.0f * glossiness) * spec;
vec3 envSpecColor = mix(indirectColor, vec3(1.0, 1.0, 1.0), visibility);
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor + specular * specularColor * envSpecColor + light.a * specularColor;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor;
color += specular * specularColor * envSpecColor;
color += light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return color;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ vec3 SG(
float specular = pow(NdotE, 100.0f * glossiness) * spec;
vec3 envSpecColor = mix(indirectColor, vec3(1.0, 1.0, 1.0), visibility);
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor + specular * specularColor * envSpecColor + light.a * specularColor;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor;
color += specular * specularColor * envSpecColor;
color += light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return color;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ vec3 SG(
float specular = pow(NdotE, 100.0f * glossiness) * spec;
vec3 envSpecColor = mix(indirectColor, vec3(1.0, 1.0, 1.0), visibility);
vec3 directionalLight = NdotL * directionLightColor * visibility;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor + specular * specularColor * envSpecColor + light.a * specularColor;
vec3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor;
color += specular * specularColor * envSpecColor;
color += light.a * specularColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
vec3 reflection = -normalize(reflect(worldViewDir, worldNormal));
color += sRGB(SSR(linearRGB(color), position, reflection, roughness)) * metallic * specularColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
precision highp float;
precision highp sampler2D;
precision highp sampler2DArray;

uniform sampler2D uTexAlbedo;
uniform sampler2D uTexPosition;
uniform sampler2D uTexNormal;
uniform sampler2D uTexData;
uniform sampler2D uTexLight;
uniform sampler2D uTexIndirect;

#if defined(ENABLE_SSR)
uniform sampler2D uTexLastFrame;
#endif

uniform vec4 uCameraPosition;
uniform vec4 uLightDirection;
uniform vec4 uLightColor;
uniform vec3 uLightMultiplier;

#if defined(ENABLE_SSR)
uniform mat4 uView;
uniform mat4 uProjection;
#endif

in vec2 varTexCoord0;

out vec4 FragColor;

#include "LibSGLM.glsl"

void main(void)
{
vec3 albedo = texture(uTexAlbedo, varTexCoord0.xy).rgb;

vec4 posdepth = texture(uTexPosition, varTexCoord0.xy);
vec3 position = posdepth.xyz;

vec3 normal = texture(uTexNormal, varTexCoord0.xy).xyz;
vec3 data = texture(uTexData, varTexCoord0.xy).rgb;
vec4 light = texture(uTexLight, varTexCoord0.xy);
vec3 indirect = texture(uTexIndirect, varTexCoord0.xy).rgb;

vec3 v = uCameraPosition.xyz - position;
vec3 viewDir = normalize(v);

float directMul = uLightMultiplier.x;
float indirectMul = uLightMultiplier.y;
float lightMul = uLightMultiplier.z;

// lighting
vec3 color = SGLM(
albedo,
data.r,
data.g,
posdepth,
viewDir,
uLightDirection.xyz,
normal,
uLightColor.rgb * uLightColor.a,
light,
indirect,
directMul,
indirectMul,
lightMul);

FragColor = vec4(color, 1.0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// File Generated by Assets/BuildShader.py - source: [SGLightmapFS.d.glsl : _]
precision highp float;
precision highp sampler2D;
precision highp sampler2DArray;
uniform sampler2D uTexAlbedo;
uniform sampler2D uTexPosition;
uniform sampler2D uTexNormal;
uniform sampler2D uTexData;
uniform sampler2D uTexLight;
uniform sampler2D uTexIndirect;
uniform vec4 uCameraPosition;
uniform vec4 uLightDirection;
uniform vec4 uLightColor;
uniform vec3 uLightMultiplier;
in vec2 varTexCoord0;
out vec4 FragColor;
const float PI = 3.1415926;
const float MinReflectance = 0.04;
float getPerceivedBrightness(vec3 color)
{
return sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
}
float solveMetallic(vec3 diffuse, vec3 specular, float oneMinusSpecularStrength)
{
float specularBrightness = getPerceivedBrightness(specular);
float diffuseBrightness = getPerceivedBrightness(diffuse);
float a = MinReflectance;
float b = diffuseBrightness * oneMinusSpecularStrength / (1.0 - MinReflectance) + specularBrightness - 2.0 * MinReflectance;
float c = MinReflectance - specularBrightness;
float D = b * b - 4.0 * a * c;
return clamp((-b + sqrt(D)) / (2.0 * a), 0.0, 1.0);
}
const float gamma = 2.2;
const float invGamma = 1.0 / 2.2;
vec3 sRGB(vec3 color)
{
return pow(color, vec3(gamma));
}
vec3 linearRGB(vec3 color)
{
return pow(color, vec3(invGamma));
}
vec3 SGLM(
const vec3 baseColor,
const float spec,
const float gloss,
const vec4 position,
const vec3 worldViewDir,
const vec3 worldLightDir,
const vec3 worldNormal,
const vec3 lightColor,
const vec4 light,
const vec3 indirect,
const float directMultiplier,
const float indirectMultiplier,
const float lightMultiplier)
{
float glossiness = max(gloss, 0.01);
float roughness = 1.0 - glossiness;
vec3 f0 = vec3(spec, spec, spec);
vec3 specularColor = f0;
float oneMinusSpecularStrength = 1.0 - spec;
float metallic = solveMetallic(baseColor.rgb, specularColor, oneMinusSpecularStrength);
f0 = vec3(0.04, 0.04, 0.04);
vec3 diffuseColor = baseColor.rgb;
specularColor = mix(f0, baseColor.rgb, metallic);
specularColor = sRGB(specularColor);
diffuseColor = sRGB(diffuseColor);
vec3 directionColor = sRGB(light.rgb);
vec3 indirectColor = sRGB(indirect.rgb);
vec3 H = normalize(worldLightDir + worldViewDir);
float NdotE = max(0.0, dot(worldNormal, H));
float specular = pow(NdotE, 100.0f * glossiness) * spec;
vec3 envSpecColor = vec3(1.0, 1.0, 1.0);
vec3 color = (directionColor * lightMultiplier) * diffuseColor;
color += specular * specularColor * envSpecColor;
color += indirectColor * diffuseColor * indirectMultiplier / PI;
return color;
}
void main(void)
{
vec3 albedo = texture(uTexAlbedo, varTexCoord0.xy).rgb;
vec4 posdepth = texture(uTexPosition, varTexCoord0.xy);
vec3 position = posdepth.xyz;
vec3 normal = texture(uTexNormal, varTexCoord0.xy).xyz;
vec3 data = texture(uTexData, varTexCoord0.xy).rgb;
vec4 light = texture(uTexLight, varTexCoord0.xy);
vec3 indirect = texture(uTexIndirect, varTexCoord0.xy).rgb;
vec3 v = uCameraPosition.xyz - position;
vec3 viewDir = normalize(v);
float directMul = uLightMultiplier.x;
float indirectMul = uLightMultiplier.y;
float lightMul = uLightMultiplier.z;
vec3 color = SGLM(
albedo,
data.r,
data.g,
posdepth,
viewDir,
uLightDirection.xyz,
normal,
uLightColor.rgb * uLightColor.a,
light,
indirect,
directMul,
indirectMul,
lightMul);
FragColor = vec4(color, 1.0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ float3 SG(
float3 color = (directionalLight * directMultiplier + pointLightColor * lightMultiplier) * diffuseColor;

// Direction specular
color = color + specular * specularColor * envSpecColor;
color += specular * specularColor * envSpecColor;

// Point light specular
color = color + light.a * specularColor;
color += light.a * specularColor;

// IBL Ambient
color += indirectColor * diffuseColor * indirectMultiplier / PI;
Expand Down
Loading

0 comments on commit 544d0c9

Please sign in to comment.