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
|
VertexOut VS(VertexIn input)
{
VertexOut output;
output.posW = mul(float4(input.pos, 1.0f), World).xyz;
output.normalW = mul(input.normal, (float3x3)WorldInvTranspose);
output.posH = mul(float4(output.posW, 1.0f), ViewProjection);
output.tex = input.tex;
output.shadowPos = mul(float4(input.pos, 1.0f), ShadowTransform);
return output;
}
float4 PS(VertexOut input) : SV_TARGET
{
input.normalW = normalize(input.normalW);
float3 toEye = gEyePosW - input.posW;
// Cache the distance to the eye from this surface point.
float distToEye = length(toEye);
// Normalize.
toEye /= distToEye;
// Default to multiplicative identity.
float4 texColor = float4(1, 1, 1, 1);
texColor = Texture.Sample(Sample, input.tex);
// Start with a sum of zero.
float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
float4 spec = float4(0.0f, 0.0f, 0.0f, 0.0f);
// Sum the light contribution from each light source.
ComputeDirectionalLight(material, dirLight, input.normalW, toEye,
ambient, diffuse, spec);
float shadow = 1.0f;
shadow = CalcShadowFactor(SampleShadow, ShadowMap, input.shadowPos);
diffuse *= shadow;
spec *= shadow;
float4 A, D, S;
[unroll]
for (int i = 0; i < pointLightCount; i++)
{
ComputePointLight(material, pointLight[i], input.posW, input.normalW, toEye, A, D, S);
ambient += A;
diffuse += D;
spec += S;
}
// Modulate with late add.
float4 litColor = texColor*(ambient + diffuse) + spec;
// Common to take alpha from diffuse material and texture.
litColor.a = material.Diffuse.a * texColor.a;
return litColor;
}
|