-
Notifications
You must be signed in to change notification settings - Fork 0
/
statue.fx
235 lines (191 loc) · 5.21 KB
/
statue.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
extern const texture TextureDiffuse;
extern const texture TextureNormal;
extern const texture TextureDepthShadow;
extern const float4x4 World;
extern const float4x4 View;
extern const float4x4 Projection;
extern const float4x4 LightViewProj;
extern const float3 CameraPosition;
extern const int ShadowTexSize;
sampler SamplerDiffuse = sampler_state
{
Texture = TextureDiffuse;
MinFilter = ANISOTROPIC;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SamplerNormal = sampler_state
{
Texture = TextureNormal;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SamplerDepthShadow = sampler_state
{
Texture = TextureDepthShadow;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = NONE;
AddressU = BORDER;
AddressV = BORDER;
BorderColor = 0xFFFFFFFF;
};
struct VsInput
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 Bitangent : BINORMAL;
float2 Texcoord : TEXCOORD0;
};
struct VsOutput
{
float4 Position : POSITION0;
float4 WorldPosition : POSITION1;
float4 ShadowPos : POSITION2;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 Bitangent : BINORMAL;
float2 Texcoord : TEXCOORD0;
float Fog : BLENDWEIGHT0;
};
struct PsInput
{
float4 WorldPosition : POSITION1;
float4 ShadowPos : POSITION2;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 Bitangent : BINORMAL;
float2 Texcoord : TEXCOORD0;
float Fog : BLENDWEIGHT0;
};
static const float3 LightDirection = { 1, 2, 1 };
static const float4 FogColor = { 0.675, 0.875, 1, 1 };
static const float4 SpecularColor = { 0.25, 0.25, 0.2, 1 };
static const float SpecularPower = 20;
static const float texelSize = 1.0 / ShadowTexSize;
static const float2 filterKernel[4] =
{
float2(0 * texelSize, 0 * texelSize),
float2(1 * texelSize, 0 * texelSize),
float2(0 * texelSize, 1 * texelSize),
float2(1 * texelSize, 1 * texelSize)
};
VsOutput VshaderSimple(VsInput In)
{
VsOutput Out = (VsOutput)0;
Out.WorldPosition = mul(World, In.Position);
float4 ViewPosition = mul(View, Out.WorldPosition);
Out.Position = mul(Projection, ViewPosition);
Out.Normal = normalize(mul(World, In.Normal));
Out.Texcoord = In.Texcoord;
Out.Fog = saturate(1 / exp(ViewPosition.z * 0.0035));
return Out;
}
float4 VshaderCaster(VsInput In) : POSITION
{
float4 WorldPosition = mul(World, In.Position);
float4 ViewPosition = mul(View, WorldPosition);
float4 Pos = mul(Projection, ViewPosition);
return Pos;
}
float4 PshaderSimple(PsInput In) : COLOR
{
float3 ViewDir = normalize(In.WorldPosition.xyz - CameraPosition);
float3 LightDir = normalize(LightDirection);
float3 ReflectLightDir = reflect(LightDir, In.Normal);
float4 specular = pow(max(dot(ReflectLightDir, ViewDir), 0), SpecularPower) * SpecularColor;
float diffuse = dot(LightDir, normalize(In.Normal)) * 0.5 + 0.5;
float4 color = tex2D(SamplerDiffuse, In.Texcoord);
color = pow(color, 2) * diffuse + specular;
return lerp(FogColor, color, In.Fog);
}
float4 PshaderCaster() : COLOR
{
return 0;
}
VsOutput Vshader(VsInput In)
{
VsOutput Out = (VsOutput)0;
Out.WorldPosition = mul(World, In.Position);
float4 ViewPosition = mul(View, Out.WorldPosition);
Out.Position = mul(Projection, ViewPosition);
Out.ShadowPos = mul(LightViewProj, Out.WorldPosition);
Out.Texcoord = In.Texcoord;
Out.Tangent = In.Tangent;
Out.Bitangent = In.Bitangent;
Out.Normal = In.Normal;
Out.Fog = saturate(1 / exp(ViewPosition.z * 0.0035));
return Out;
}
float4 Pshader(PsInput In) : COLOR
{
float3 normal = tex2D(SamplerNormal, In.Texcoord).xyz * 2 - 1;
float3 T = normalize(In.Tangent);
float3 B = normalize(In.Bitangent);
float3 N = normalize(In.Normal);
float3x3 TBN = transpose(float3x3(T, B, N));
normal = mul(TBN, normal);
normal = mul(World, normal);
normal = normalize(normal);
float3 LightDir = normalize(LightDirection);
float diffuse = dot(LightDir, normal) * 0.5 + 0.5;
float3 ViewDir = normalize(In.WorldPosition.xyz - CameraPosition);
float3 ReflectLightDir = reflect(LightDir, normal);
float4 specular = pow(max(dot(ReflectLightDir, ViewDir), 0), SpecularPower) * SpecularColor;
float4 color = tex2D(SamplerDiffuse, In.Texcoord);
float2 shadeUV = {
In.ShadowPos.x / In.ShadowPos.w * 0.5 + 0.5,
-In.ShadowPos.y / In.ShadowPos.w * 0.5 + 0.5
};
float pointDepth = (In.ShadowPos.z / In.ShadowPos.w) - 0.0025;
float shade = 0.0;
for (int i = 0; i < 4; i++)
{
float shadow = step(pointDepth, tex2D(SamplerDepthShadow, shadeUV + filterKernel[i]).r);
shade += shadow * 0.25;
}
color = shade * specular + (0.5 * shade + 0.5) * diffuse * pow(color, 2);
return lerp(FogColor, color, In.Fog);
}
technique Normal
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 Pshader();
}
}
technique Reflect
{
pass Pass0
{
CullMode = CCW;
VertexShader = compile vs_3_0 VshaderSimple();
PixelShader = compile ps_3_0 PshaderSimple();
}
}
technique Simple
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 VshaderSimple();
PixelShader = compile ps_3_0 PshaderSimple();
}
}
technique Caster
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 VshaderCaster();
PixelShader = compile ps_3_0 PshaderCaster();
}
}