-
Notifications
You must be signed in to change notification settings - Fork 0
/
shading.fx
368 lines (309 loc) · 10 KB
/
shading.fx
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
//matrix xCameraWorldViewProjection;
matrix xWorld;
//worldviewprojection from light source point of view
//matrix xLightWorldViewProjection;
matrix xCameraProjection;
matrix xCameraView;
matrix xLightProjection;
matrix xLightView;
float3 xLightPosition;
float xLightPower;
float xAmbientLight;
Texture xTex;
Texture xShadowMap;
Texture xCarLightTex;
sampler TextureSampler = sampler_state
{
texture = <xTex>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = mirror;
AddressV = mirror;
};
sampler ShadowMapSampler = sampler_state
{
texture = <xShadowMap>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = clamp;
AddressV = clamp;
};
sampler CarLightSampler = sampler_state
{
texture = <xCarLightTex>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = clamp;
AddressV = clamp;
};
float DotProduct(float3 lightPosition, float3 position3D, float3 normal)
{
float3 lightDirection = normalize(position3D - lightPosition);
return dot(-lightDirection, normal);
}
//ShadowedScene
struct ShadowedSceneVSInput
{
float4 Position : SV_POSITION0;
float3 Normal : NORMAL0;
float2 TexCoords : TEXCOORD0;
};
struct ShadowedSceneVSOutput
{
float4 Position : SV_POSITION0;
float4 PositionAsSeenByLight : TEXCOORD0;
float2 TexCoords : TEXCOORD1;
float3 Normal : TEXCOORD2;
float4 Position3D : TEXCOORD3;
};
struct ShadowedScenePSOutput
{
float4 Color : COLOR0;
};
//preshader matrix multiplication done only once by cpu
//compiler finds calculations that are the same for every vertex in input
//and sends them to cpu
matrix GetWorldViewProjection(matrix view, matrix projection)
{
matrix preViewProjection = mul(view, projection);
matrix preWorldViewProjection = mul(xWorld, preViewProjection);
return preWorldViewProjection;
}
ShadowedSceneVSOutput ShadowedSceneVertexShader(ShadowedSceneVSInput input)
{
ShadowedSceneVSOutput output = (ShadowedSceneVSOutput)0;
//get input v position as seen by the camera
output.Position = mul(input.Position, GetWorldViewProjection(xCameraView, xCameraProjection));
//get input v position as seen by the light source
output.PositionAsSeenByLight = mul(input.Position, GetWorldViewProjection(xLightView, xLightProjection));
/*output.Position = mul(input.Position, xCameraWorldViewProjection);*/
/*output.PositionAsSeenByLight = mul(input.Position, xLightWorldViewProjection);*/
//rotate Normal according to position in World
output.Normal = normalize(mul(input.Normal, (float3x3) xWorld));
output.TexCoords = input.TexCoords;
//transform position of vertex to real position
output.Position3D = mul(input.Position, xWorld);
return output;
}
ShadowedScenePSOutput ShadowedScenePixelShader(ShadowedSceneVSOutput input)
{
ShadowedScenePSOutput output = (ShadowedScenePSOutput)0;
//remap texCoords from [-1,1] to [0,1]
//if the value of texCoords was in [0,1] range before then it is in view of our light source
float2 projectedTexCoords;
projectedTexCoords[0] = input.PositionAsSeenByLight.x / input.PositionAsSeenByLight.w / 2.0f + 0.5f;
projectedTexCoords[1] = -input.PositionAsSeenByLight.y / input.PositionAsSeenByLight.w / 2.0f + 0.5f;
float diffuseLightFactor = 0;
//check if the value of texCoords was the same before clipping to [0,1] if yes then it is lighted
if ((saturate(projectedTexCoords).x == projectedTexCoords.x) && (saturate(projectedTexCoords).y == projectedTexCoords.y))
{
//get depth stored in shadow map i.e distance between pixel and light calculated before
float depthStoredInShadowMap = tex2D(ShadowMapSampler, projectedTexCoords).r;
//get distance calculated now
float realDistance = input.PositionAsSeenByLight.z / input.PositionAsSeenByLight.w;
//compare them, if realdistance - bias is smaller then it is not obscured by another object
//the smaller the bias the greater is the shadow and it is closer to lighted object
if ((realDistance - 0.003f) <= depthStoredInShadowMap)
{
//find direction of light(lightPos - position of pixel) and dotprod with normal
diffuseLightFactor = DotProduct(xLightPosition, input.Position3D, input.Normal);
diffuseLightFactor = saturate(diffuseLightFactor);
diffuseLightFactor *= xLightPower;
}
}
//sample the shape of light (0,1)
float lightTextureFactor = tex2D(CarLightSampler, projectedTexCoords).r;
diffuseLightFactor *= lightTextureFactor;
//light the base color
float4 baseColor = tex2D(TextureSampler, input.TexCoords);
output.Color = baseColor * (diffuseLightFactor + xAmbientLight);
return output;
}
technique ShadowedScene
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL ShadowedSceneVertexShader();
PixelShader = compile PS_SHADERMODEL ShadowedScenePixelShader();
}
};
//ShadowMap creates a depth map (the darker pixel is the closer to light source
struct ShadowMapVSInput
{
float4 Position : SV_POSITION0;
};
struct ShadowMapVSOutput
{
float4 Position : SV_POSITION0;
float4 Position2D : TEXCOORD0;
};
struct ShadowMapPSOutput
{
float4 Color : COLOR0;
};
ShadowMapVSOutput ShadowMapVertexShader(ShadowMapVSInput input)
{
ShadowMapVSOutput output = (ShadowMapVSOutput)0;
//get projection of input position as seen from the light source view
output.Position = mul(input.Position, GetWorldViewProjection(xLightView, xLightProjection));
/*output.Position = mul(input.Position, xLightWorldViewProjection);*/
output.Position2D = output.Position;
return output;
}
ShadowMapPSOutput ShadowMapPixelShader(ShadowMapVSOutput input)
{
ShadowMapPSOutput output = (ShadowMapPSOutput)0;
//z is distance between light source and pixel, w is normalizer
output.Color = input.Position2D.z / input.Position2D.w;
return output;
}
technique ShadowMap
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL ShadowMapVertexShader();
PixelShader = compile PS_SHADERMODEL ShadowMapPixelShader();
}
};
//TexturedShaded
struct TexturedShadedVSInput
{
float4 Position : SV_POSITION0;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
};
struct TexturedShadedVSOutput
{
float4 Position : SV_POSITION;
float3 Normal : TEXCOORD0;
float2 TexCoords : TEXCOORD1;
float3 Position3D : TEXCOORD2;
};
struct TexturedShadedPSOutput
{
float4 Color : COLOR0;
};
TexturedShadedVSOutput TexturedShadedVertexShader(TexturedShadedVSInput input)
{
TexturedShadedVSOutput output = (TexturedShadedVSOutput)0;
//transform vertices according to camera
output.Position = mul(input.Position, GetWorldViewProjection(xCameraView, xCameraProjection));
/*output.Position = mul(input.Position, xCameraWorldViewProjection);*/
output.TexCoords = input.TexCoords;
//we need to rotate the normals using World matrix
output.Normal = normalize(mul(input.Normal, (float3x3) xWorld));
//transform vertices according to their real position
output.Position3D = mul((float3)input.Position, (float3x3) xWorld);
return output;
}
TexturedShadedPSOutput TexturedShadedPixelShader(TexturedShadedVSOutput input)
{
TexturedShadedPSOutput output = (TexturedShadedPSOutput)0;
//compute direction of light(from lightPosition to input position in 3D, then dotProduct with normal
float diffuseLightFactor = DotProduct(xLightPosition, input.Position3D, input.Normal);
//set factor to [0,1] interval;
diffuseLightFactor = saturate(diffuseLightFactor);
diffuseLightFactor *= xLightPower;
float4 baseColor = tex2D(TextureSampler, input.TexCoords);
output.Color = baseColor * (diffuseLightFactor + xAmbientLight);
//output.Color = diffuseLightFactor;
return output;
}
technique TexturedShaded
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL TexturedShadedVertexShader();
PixelShader = compile PS_SHADERMODEL TexturedShadedPixelShader();
}
}
// Textured
struct TexturedVSInput
{
float4 Position : SV_POSITION0;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
};
struct TexturedVSOutput
{
float4 Position : SV_POSITION;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
};
struct TexturedPSOutput
{
float4 Color : COLOR0;
};
TexturedVSOutput TexturedVertexShader(TexturedVSInput input)
{
TexturedVSOutput output = (TexturedVSOutput)0;
output.Position = mul(input.Position, GetWorldViewProjection(xCameraView, xCameraProjection));
/*output.Position = mul(input.Position, xCameraWorldViewProjection);*/
output.TexCoords = input.TexCoords;
return output;
}
TexturedPSOutput TexturedPixelShader(TexturedVSOutput input)
{
TexturedPSOutput output = (TexturedPSOutput)0;
output.Color = tex2D(TextureSampler, input.TexCoords);
return output;
}
technique Textured
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL TexturedVertexShader();
PixelShader = compile PS_SHADERMODEL TexturedPixelShader();
}
}
// Simple //
struct SimpleVSInput
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
};
struct SimpleVSOutput
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
float3 Position3D : TEXCOORD0;
};
struct SimplePSOutput
{
float4 Color : COLOR0;
};
SimpleVSOutput SimpleVertexShader(SimpleVSInput input)
{
SimpleVSOutput output = (SimpleVSOutput)0;
output.Position = mul(input.Position, GetWorldViewProjection(xCameraView, xCameraProjection));
/*output.Position = mul(input.Position, xCameraWorldViewProjection);*/
output.Color = input.Color;
output.Position3D = (float3)input.Position;
return output;
}
SimplePSOutput SimplePixelShader(SimpleVSOutput input)
{
SimplePSOutput output = (SimplePSOutput)0;
output.Color = input.Color;
output.Color.rgb = input.Position3D.xyz;
return output;
}
technique Simple
{
pass Pass0
{
VertexShader = compile VS_SHADERMODEL SimpleVertexShader();
PixelShader = compile PS_SHADERMODEL SimplePixelShader();
}
}