-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferredLightPS.hlsl
More file actions
108 lines (83 loc) · 3.64 KB
/
Copy pathDeferredLightPS.hlsl
File metadata and controls
108 lines (83 loc) · 3.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Texture2D posTexture : register(t0);
Texture2D normalTexture : register(t1);
Texture2D worldPosTexture : register(t2);
Texture2D diffuseAlbedoTexture : register(t3);
Texture2D ambientMatTexture : register(t4);
Texture2D diffuseMatTexture : register(t5);
Texture2D specularMatTexture : register(t6);
Texture2D lightClipPos : register(t7);
Texture2D shadowMapTex : register(t8);
SamplerState wrapSampler : register(s0);
SamplerState clampSampler : register(s1);
cbuffer DirectionalLight : register(b0)
{
float4 color;
float3 direction;
float lightRange;
};
cbuffer pos : register(b1)
{
float3 cameraPosition;
}
struct PixelInput
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD;
};
struct LightResult
{
float4 diffuse;
float4 specular;
float4 color;
};
LightResult LightCalculation(float4 P, float3 N, float4 D, float4 S)
{
LightResult result = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
float4 V = { 1, 1, 1, 1 };
float3 E = normalize(cameraPosition - P.xyz);//cam to pixel
float diffuse = saturate(dot(N, direction)); //dot product of normal and direction
if (diffuse < 0.0f) //if backface
continue;
if (D.x > 0)
result.diffuse += diffuse * D;
else
result.diffuse += diffuse * V;
float3 R = normalize(reflect(-direction, N));
float specular = pow(saturate(dot(R, E)), 1);
if (S.x > 0)
result.specular += specular * S;
else
result.specular += specular * V;
return result;
}
float4 main(PixelInput input) : SV_Target
{
float3 normal = normalTexture.Sample(wrapSampler, input.tex).xyz;
float4 albedo = diffuseAlbedoTexture.Sample(wrapSampler, input.tex);
float4 worldPos = worldPosTexture.Sample(wrapSampler, input.tex);
//Material properties
float4 ambientMaterial = ambientMatTexture.Sample(wrapSampler, input.tex);
float4 diffuseMaterial = diffuseMatTexture.Sample(wrapSampler, input.tex);
float4 specularMaterial = specularMatTexture.Sample(wrapSampler, input.tex);
//Vertices from the lights perspective view
float4 lightClip = lightClipPos.Sample(wrapSampler, input.tex);
//shadow shite
lightClip.xyz /= lightClip.w; // Complete projection of textureCoords by doing division by w
float depth = lightClip.z; // Depth in the NDC-Space, Normalized Device Coordinates - Figure out what obscures what
float2 tx = float2(0.5f * lightClip.x + 0.5f, -0.5f * lightClip.y + 0.5); // [-1,1] => [0, 1] -> in d3d11 the y-axis is positive when pointing in clip-space.
// when working with UVs or texcoords the y-axis positive in HLSL y-axis is pointing downwards. invert here
float sm = shadowMapTex.Sample(wrapSampler, tx).r;
float shadow = (sm + 0.005f < depth) ? sm : 1.0f; // If sm + bias (closest) < clip-depth there is a primitive castings shadow.
// else - no shadows
if (diffuseMaterial.x < 0) // For the particles
{
return albedo;
}
LightResult lResult = LightCalculation(worldPos, normal, diffuseMaterial, specularMaterial); // The light calculations
float4 lighting = (lResult.diffuse + lResult.specular) * shadow;
float4 globalAmbient = { 0.4f, 0.4f, 0.4f, 1.0f };
float4 A = ambientMaterial;
float4 finalColor = albedo * (lighting + A) * globalAmbient;
return finalColor;
//return float4(shadow, shadow, shadow, 1);
}