Repository navigation
Expand file tree
/
Copy pathLight.Spot.fx
More file actions
224 lines (176 loc) · 5.18 KB
/
Copy pathLight.Spot.fx
File metadata and controls
224 lines (176 loc) · 5.18 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// TITLE: Spot Light Shader
// AUTHOR: Yve Verstrepen
// DESCRIPTION:
// This shader will render a spot light based on the information
// from the GBuffer.
///////////////////////////////////////////////////////////////////////////
/////// Variables /////////////////////////////////////////
float4x4 WVP : WORLDVIEWPROJECTION;
float4x4 LVP;
float4x4 ViewInverse: VIEWINVERSE;
float4x4 ProjInverse;
float4x4 WorldIT : WORLDIT;
float3 CamPos : CAMERAPOS;
// Tweakables
float3 LightPosition;
float LightRadius;
float LightLength;
float3 LightColor;
float Brightness = 1.0f;
float DepthBias = 0.001f;
bool EnableShadowing = true;
/////// Textures & Samplers /////////////////////////////////////////
texture texMRT1;
sampler sampMRT1 = sampler_state
{
Texture = (texMRT1);
MagFilter = Linear;
MinFilter = Linear;
Mipfilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
texture texMRT2;
sampler sampMRT2 = sampler_state
{
Texture = (texMRT2);
MagFilter = Linear;
MinFilter = Linear;
Mipfilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
texture texFilter : TEXTURE0;
sampler sampFilter = sampler_state
{
Texture = (texFilter);
MagFilter = Linear;
MinFilter = Linear;
Mipfilter = Linear;
AddressU = Border;
AddressV = Border;
};
texture texLightDepth;
sampler sampLightDepth = sampler_state
{
Texture = (texLightDepth);
MagFilter = Linear;
MinFilter = Linear;
Mipfilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
/////// Structures /////////////////////////////////////////
struct a2v
{
float4 Pos : POSITION0;
};
struct v2p
{
float4 Pos : POSITION0;
float4 SPos : TEXCOORD0;
};
/////// Functions /////////////////////////////////////////
// Calculate the light filter
float3 CalcFilter(float3 WorldPos)
{
// Rotate points
float4x4 WorldInverse = transpose(WorldIT);
float3 PRot = mul(WorldPos, WorldInverse);
float3 POri = mul(LightPosition, WorldInverse);
// Get distance & exit if its behind the cone
float PDist = PRot.y - POri.y;
if (PDist > 0) return 0;
// Calculate radius at point
float PPos = PDist / LightLength;
float PRadius = lerp(0, LightRadius, PPos);
// Transform point to -1,1 range
float2 PNT = (PRot.xz - POri.xz) / PRadius;
// Bring to 0,1 for texture coordinates
float2 FUV = (PNT + 1) / 2;
return tex2D(sampFilter, FUV);
}
bool IsShadow(float4 WorldPos)
{
float4 LightingPos = mul(WorldPos, LVP);
LightingPos /= LightingPos.w;
float2 DepthMapPos;
DepthMapPos.x = LightingPos.x / 2.0f + 0.5f;
DepthMapPos.y = 1 - (LightingPos.y / 2.0f + 0.5f);
float4 DepthSample = tex2D(sampLightDepth, DepthMapPos);
float CurDepth = LightingPos.z;
//float ESM_K = 60.0f; // Range [0, 80]
//float ESM_MIN = -1.0f; // Range [0, -oo]
//float ESM_DIFFUSE_SCALE = 1.79f; // Range [1, 10]
//Calculate ESM
//float shadow = saturate(exp(max(ESM_MIN, ESM_K * (DepthSample - CurDepth))));
//Return Shadow
//float3 ReturnColor = 1.0f - (ESM_DIFFUSE_SCALE * (1.0f - shadow));
/*
float3 ReturnColor;
if
{
ReturnColor = saturate(CurDepth / 3.0f);
}
else
{
ReturnColor = 1;
}
*/
return (DepthSample.r <= CurDepth - DepthBias);
}
/////// Programs /////////////////////////////////////////
void VSApp(in a2v IN, out v2p OUT)
{
OUT.Pos = mul(IN.Pos, WVP);
OUT.SPos = OUT.Pos;
}
float4 PSApp(in v2p IN) : COLOR0
{
IN.SPos.xy /= IN.SPos.w;
float2 UV = 0.5f * (float2(IN.SPos.x, -IN.SPos.y) + 1);
// Get world position
float Depth = tex2D(sampMRT2, UV).a;
float4 Position;
Position.xy = IN.SPos.xy;
Position.z = Depth;
Position.w = 1.0f;
Position = mul(Position, ProjInverse);
Position = mul(Position, ViewInverse);
Position /= Position.w;
if (EnableShadowing && IsShadow(Position)) return float4(0, 0, 0, 1);
float3 LightVec = LightPosition - Position;
float Attenuation = saturate(1.0f - length(LightVec) / LightLength);
LightVec = normalize(LightVec);
float3 Diffuse = tex2D(sampMRT2, UV).rgb;
float3 Normal = tex2D(sampMRT1, UV).rgb;
float Specular = tex2D(sampMRT1, UV).a;
float3 DiffuseColor = Diffuse * max(0, dot(Normal, LightVec));
// Calculate specular if enabled
float SpecularColor = 0;
if (Specular > 0)
{
Specular = 255 * (1.0f - Specular);
float3 ReflectVec = normalize(reflect(-LightVec, Normal));
float3 DirToCam = normalize(CamPos - Position);
SpecularColor = pow(saturate(dot(ReflectVec, DirToCam)), Specular);
}
// Calculate light filter
float3 FilterColor = CalcFilter(Position);
return float4(Brightness * LightColor * Attenuation * FilterColor * (DiffuseColor + SpecularColor), 1);
}
/////// Techniques /////////////////////////////////////////
technique SolidObject
{
pass Pass0
{
VertexShader = compile vs_3_0 VSApp();
PixelShader = compile ps_3_0 PSApp();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
CullMode = CW;
ZEnable = False;
ZWriteEnable = False;
}
}