Smuggler/Effects/PBREffect.fx

269 lines
7.8 KiB
Plaintext
Raw Normal View History

#include "Macros.fxh" //from FNA
2020-07-31 00:57:57 +00:00
static const float PI = 3.141592653589793;
2020-07-31 00:57:57 +00:00
// Samplers
DECLARE_TEXTURE(AlbedoTexture, 0);
DECLARE_TEXTURE(NormalTexture, 1);
DECLARE_TEXTURE(EmissionTexture, 2);
DECLARE_TEXTURE(OcclusionTexture, 3);
DECLARE_TEXTURE(MetallicRoughnessTexture, 4);
DECLARE_CUBEMAP(EnvDiffuseTexture, 8);
DECLARE_TEXTURE(BrdfLutTexture, 9);
DECLARE_CUBEMAP(EnvSpecularTexture, 10);
BEGIN_CONSTANTS
// PBR Values
float3 AlbedoValue _ps(c0) _cb(c0);
float MetallicValue _ps(c1) _cb(c1);
float RoughnessValue _ps(c2) _cb(c2);
float AO _ps(c3) _cb(c3);
// Light Info
float3 LightPositions[4] _ps(c4) _cb(c4);
float3 LightColors[4] _ps(c8) _cb(c8);
float3 EyePosition _ps(c12) _cb(c12);
float4x4 World _vs(c0) _cb(c16);
float4x4 WorldInverseTranspose _vs(c4) _cb(c20);
MATRIX_CONSTANTS
float4x4 WorldViewProjection _vs(c8) _cb(c0);
END_CONSTANTS
2020-07-31 00:57:57 +00:00
2020-07-31 07:20:07 +00:00
struct VertexShaderInput
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD0;
2020-07-31 07:20:07 +00:00
};
2020-07-31 00:57:57 +00:00
2020-07-31 07:20:07 +00:00
struct PixelShaderInput
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float3 PositionWS : TEXCOORD1;
float3 NormalWS : TEXCOORD2;
2020-07-31 07:20:07 +00:00
};
2020-07-31 00:57:57 +00:00
2020-07-31 07:20:07 +00:00
PixelShaderInput main_vs(VertexShaderInput input)
{
PixelShaderInput output;
2020-08-01 20:43:04 +00:00
output.TexCoord = input.TexCoord;
2020-08-03 23:27:59 +00:00
output.PositionWS = mul(input.Position, World).xyz;
output.NormalWS = mul(input.Normal, WorldInverseTranspose);
output.Position = mul(input.Position, WorldViewProjection);
2020-08-03 23:27:59 +00:00
2020-07-31 07:20:07 +00:00
return output;
}
2020-07-31 00:57:57 +00:00
float3 FresnelSchlick(float cosTheta, float3 F0)
2020-07-31 00:57:57 +00:00
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
2020-07-31 00:57:57 +00:00
float DistributionGGX(float3 N, float3 H, float roughness)
2020-07-31 00:57:57 +00:00
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH * NdotH;
2020-07-31 00:57:57 +00:00
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
2020-07-31 00:57:57 +00:00
return num / denom;
2020-07-31 00:57:57 +00:00
}
float GeometrySchlickGGX(float NdotV, float roughness)
2020-07-31 00:57:57 +00:00
{
float r = (roughness + 1.0);
float k = (r * r) / 8.0;
2020-07-31 00:57:57 +00:00
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
2020-07-31 00:57:57 +00:00
return num / denom;
2020-07-31 00:57:57 +00:00
}
float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
2020-07-31 07:20:07 +00:00
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
2020-07-31 07:20:07 +00:00
}
2020-07-31 00:57:57 +00:00
// The case where we have no texture maps for any PBR data
2020-08-03 23:27:59 +00:00
float4 None(PixelShaderInput input) : SV_TARGET0
2020-07-31 07:20:07 +00:00
{
float3 albedo = AlbedoValue;
float metallic = MetallicValue;
float roughness = RoughnessValue;
float3 N = normalize(input.NormalWS);
float3 V = normalize(EyePosition - input.PositionWS);
float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic);
float3 Lo = float3(0.0, 0.0, 0.0);
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - input.PositionWS;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO;
float3 color = ambient + Lo;
color = color / (color + float3(1.0, 1.0, 1.0));
float exposureConstant = 1.0 / 2.2;
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant));
return float4(color, 1.0);
}
float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET
{
float3 albedo = SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord).rgb;
float metallic = MetallicValue;
float roughness = RoughnessValue;
float3 N = normalize(input.NormalWS);
float3 V = normalize(EyePosition - input.PositionWS);
float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic);
float3 Lo = float3(0.0, 0.0, 0.0);
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - input.PositionWS;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO;
float3 color = ambient + Lo;
color = color / (color + float3(1.0, 1.0, 1.0));
float exposureConstant = 1.0 / 2.2;
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant));
return float4(color, 1.0);
}
float4 AlbedoMetallicRoughnessMapPS(PixelShaderInput input) : SV_TARGET
{
2020-08-03 23:27:59 +00:00
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float metallic = metallicRoughness.r;
float roughness = metallicRoughness.g;
float3 N = normalize(input.NormalWS);
float3 V = normalize(EyePosition - input.PositionWS);
2020-07-31 00:57:57 +00:00
2020-08-01 20:43:04 +00:00
float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic);
2020-08-01 20:43:04 +00:00
float3 Lo = float3(0.0, 0.0, 0.0);
2020-07-31 00:57:57 +00:00
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - input.PositionWS;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
2020-07-31 00:57:57 +00:00
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
2020-08-01 20:43:04 +00:00
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO;
float3 color = ambient + Lo;
color = color / (color + float3(1.0, 1.0, 1.0));
float exposureConstant = 1.0 / 2.2;
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant));
2020-07-31 07:20:07 +00:00
return float4(color, 1.0);
2020-07-31 00:57:57 +00:00
}
2020-07-31 07:20:07 +00:00
Technique PBR
2020-07-31 00:57:57 +00:00
{
Pass Pass1
{
VertexShader = compile vs_3_0 main_vs();
2020-08-03 23:27:59 +00:00
PixelShader = compile ps_3_0 AlbedoMetallicRoughnessMapPS();
}
2020-07-31 00:57:57 +00:00
}