Backlog/engine/rend/shaders/lit_mesh.frag.hlsl

78 lines
1.9 KiB
HLSL

Texture2D<float4> Texture : register(t0, space2);
SamplerState Sampler : register(s0, space2);
cbuffer Uniforms : register(b0, space3)
{
float time;
float3 viewPos;
};
// from learnopengl.com
float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightColor)
{
// diffuse parameter
float3 lightDir = normalize(lightPos - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
float3 diffuse = diff * lightColor;
// specular parameter
float3 viewDir = normalize(fragPos - viewPos);
float3 reflectDir = reflect(-lightDir, normal);
float spec = 0.0;
float3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 30.0);
float3 specular = spec * lightColor * 2;
// attenuation
float maxDist = 5;
float cutoff1 = 2;
float cutoff2 = 4;
float cutoffFactor = 0.5;
float dist = length(lightPos - fragPos);
float attenuation = 1.0 / (dist * 2);
if (dist > cutoff1)
{
attenuation *= cutoffFactor;
}
if (dist > cutoff2)
{
attenuation *= cutoffFactor;
}
if (dist > maxDist)
{
attenuation = 0;
}
diffuse *= attenuation;
specular *= attenuation;
return diffuse + specular;
//return lightColor * 100 * attenuation;
}
float4 main(
float2 UV : TEXCOORD0,
float3 WorldPos: TEXCOORD1,
float3 Normal: TEXCOORD2
) : SV_Target0
{
float3 lightPos = float3(0, 20 + -4, -5);
float3 lightColor = float3(0.8, 0.8, 0.5);
float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) );
float4 s = Texture.Sample(Sampler, UV);
float alpha = s.w;
float3 col = s.xyz;
float3 c2 = col * ambient + col * BlinnPhong(Normal, WorldPos, lightPos, lightColor);
float4 rv = float4(pow(c2, 1.0 / 2.2), alpha);
return rv;
}