-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhongPS.hlsl
More file actions
81 lines (68 loc) · 1.89 KB
/
Copy pathPhongPS.hlsl
File metadata and controls
81 lines (68 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
cbuffer LightBuffer : register(b2)
{
float3 lightDir;
float pad0;
float3 lightColor;
float pad1;
float3 ambientColor;
float pad2;
float3 viewPos;
float pad3;
};
cbuffer MaterialBuffer : register(b3)
{
float4 diffuseColor;
float4 specularColor;
float shininess;
float hasTexture;
float2 padding;
};
Texture2D colorTexture : register(t0);
SamplerState colorSampler : register(s0);
struct PixelInput
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD;
float3 worldPos : TEXCOORD1;
float3 worldNormal : TEXCOORD2;
};
float4 main(PixelInput input) : SV_TARGET
{
// Normalize vectors
float3 N = normalize(input.worldNormal);
float3 L = normalize(-lightDir);
float3 V = normalize(viewPos - input.worldPos);
float3 H = normalize(L + V);
// Get base color
float3 albedo = diffuseColor.rgb;
if (hasTexture > 0.5f)
{
float4 texColor = colorTexture.Sample(colorSampler, input.texcoord);
albedo = texColor.rgb; // Texture overrides diffuse color
}
// Calculate angles
float NdotL = max(dot(N, L), 0.0f);
float NdotH = max(dot(N, H), 0.0f);
// CLASSIC BLINN-PHONG LIGHTING (what assignment asks for)
//AMBIENT
float3 ambient = ambientColor * albedo;
//DIFFUSE
float3 diffuse = float3(0, 0, 0);
if (NdotL > 0.0f)
{
diffuse = albedo * lightColor * NdotL;
}
// SPECULAR
float3 specular = float3(0, 0, 0);
if (NdotL > 0.0f && NdotH > 0.0f)
{
//(N·H)^shininess
float specFactor = pow(NdotH, shininess);
specular = specularColor.rgb * lightColor * specFactor * specularColor.a;
}
//FINAL COLOR
float3 finalColor = ambient + diffuse + specular;
// Clamp to [0, 1] for display
finalColor = saturate(finalColor);
return float4(finalColor, diffuseColor.a);
}