Smuggler/Effects/PBREffect.fx

160 lines
3.9 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
// Transformation Matrices
2020-07-31 00:57:57 +00:00
float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldViewProjection;
float4x3 WorldInverseTranspose;
// Samplers
DECLARE_TEXTURE(BaseColorTexture, 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);
// Light Info
float3 LightPositions[4];
float3 LightColors[4];
// PBR Values
float3 Albedo;
float Metallic;
float Roughness;
float AO;
float3 EyePosition;
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.PositionWS = mul(input.Position, World).xyz;
output.TexCoord = input.TexCoord;
2020-08-01 20:43:04 +00:00
output.NormalWS = normalize(mul(input.Normal, (float3x3)WorldInverseTranspose));
2020-07-31 07:20:07 +00:00
output.Position = mul(input.Position, WorldViewProjection);
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
float4 None(PixelShaderInput input) : SV_TARGET
2020-07-31 07:20:07 +00:00
{
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);
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();
PixelShader = compile ps_3_0 None();
}
2020-07-31 00:57:57 +00:00
}