-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNormalMapPS.hlsl
71 lines (59 loc) · 1.69 KB
/
NormalMapPS.hlsl
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
#include "ShaderIncludes.hlsli"
cbuffer ExternalData : register(b0)
{
Light lights[MAX_LIGHTS];
int lightCount;
float3 cameraPosition;
float shininess;
}
Texture2D diffuseTexture: register(t0);
Texture2D normalMap: register(t1);
SamplerState samplerOptions: register(s0);
float4 main( V2P_NormalMap input ) : SV_TARGET
{
float3 unpackedNormal = normalMap.Sample(samplerOptions, input.uv).rgb * 2 - 1;
input.normal = normalize(input.normal);
input.tangent = normalize(input.tangent);
float3 N = input.normal; // Must be normalized
float3 T = input.tangent; // Must be normalized
T = normalize(T - N * dot(T, N)); // Gram-Schmidt orthogonalization
float3 B = cross(T, N); // bi - tangent
float3x3 TBN = float3x3(T, B, N);
// order of the multiplication matters
input.normal = mul(unpackedNormal, TBN);
//return float4(input.normal, 1.f); TESTING
input.color = diffuseTexture.Sample(samplerOptions, input.uv) * input.color;
float3 finalLight = float3(0,0,0);
PixelData pixelData;
pixelData.normal = input.normal;
pixelData.worldPos = input.worldPos;
pixelData.shininess = shininess;
for (int i = 0; i < lightCount; i++)
{
// point light diffuse & spec
switch(lights[i].type)
{
case LIGHT_TYPE_POINT:
///
finalLight += PointLight(pixelData, cameraPosition, lights[i]);
///
break;
case LIGHT_TYPE_DIR:
///
finalLight += DirectionLight(pixelData, cameraPosition, lights[i]);
///
break;
case LIGHT_TYPE_SPOT:
///
finalLight += SpotLight(pixelData, cameraPosition, lights[i]);
///
break;
case LIGHT_TYPE_AMBIENT:
///
finalLight += AmbientLight(lights[i]);
///
break;
}
}
return float4(finalLight * (float3)input.color, 1);
}