-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscape.fx
254 lines (218 loc) · 5.24 KB
/
scape.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
extern const texture TextureDiffuseGrass;
extern const texture TextureDiffuseRock;
extern const texture TextureDiffuseMud;
extern const texture TextureDiffuseCaustic;
extern const texture TextureDepthShadow;
extern const float4x4 World;
extern const float4x4 View;
extern const float4x4 Projection;
extern const float4x4 LightViewProj;
extern const float Wave;
sampler SamplerDiffuseGrass = sampler_state
{
Texture = TextureDiffuseGrass;
MinFilter = ANISOTROPIC;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SamplerDiffuseRock = sampler_state
{
Texture = TextureDiffuseRock;
MinFilter = ANISOTROPIC;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SamplerDiffuseMud = sampler_state
{
Texture = TextureDiffuseMud;
MinFilter = ANISOTROPIC;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler SamplerDiffuseCaustic = sampler_state
{
Texture = TextureDiffuseCaustic;
MinFilter = ANISOTROPIC;
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;
float2 Texcoord : TEXCOORD0;
};
struct VsOutput
{
float4 Position : POSITION0;
float4 ShadowPos : POSITION1;
float3 Normal : NORMAL;
float2 Texcoord : TEXCOORD0;
float Fog : BLENDWEIGHT0;
float Angle : BLENDWEIGHT1;
float Height : BLENDWEIGHT2;
};
struct PsInput
{
float4 ShadowPos : POSITION1;
float3 Normal : NORMAL;
float2 Texcoord : TEXCOORD0;
float Fog : BLENDWEIGHT0;
float Angle : BLENDWEIGHT1;
float Height : BLENDWEIGHT2;
};
static const float3 LightDirection = { 1, 2, 1 };
static const float4 FogColor = { 0.675, 0.875, 1, 1 };
static const float4 WaterColor = { 0, 0.125, 0.1, 1 };
static const float texelSize = 1.0 / 4096.0;
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 Vshader(VsInput In)
{
VsOutput Out = (VsOutput)0;
float4 worldPos = mul(World, In.Position);
float4 viewPos = mul(View, worldPos);
Out.Position = mul(Projection, viewPos);
Out.ShadowPos = mul(LightViewProj, worldPos);
Out.Normal = mul(World, In.Normal);
Out.Texcoord = In.Texcoord;
Out.Fog = saturate(1 / exp(viewPos.z * 0.0035));
Out.Angle = pow((In.Normal.y - 0.5) * 2, 2);
Out.Height = worldPos.y;
return Out;
}
float4 VshaderCaster(VsInput In) : POSITION
{
float4 worldPos = mul(World, In.Position);
float4 viewPos = mul(View, worldPos);
float4 Pos = mul(Projection, viewPos);
return Pos;
}
float4 CalcColor(PsInput In)
{
float4 grass = tex2D(SamplerDiffuseGrass, In.Texcoord);
float4 rock = tex2D(SamplerDiffuseRock, In.Texcoord);
float4 land = lerp(rock, grass, In.Angle);
float4 mud = 0.5 * tex2D(SamplerDiffuseMud, In.Texcoord);
float4 caustic = lerp(1, tex2D(SamplerDiffuseCaustic, 2 * In.Texcoord + Wave), smoothstep(-1, -2, In.Height));
float4 color = lerp(mud * caustic, land, smoothstep(-0.5, 0.5, In.Height));
float diffuse = dot(normalize(LightDirection), normalize(In.Normal)) * 0.5 + 0.5;
return color * diffuse;
}
float4 Pshader(PsInput In) : COLOR
{
return lerp(FogColor, CalcColor(In), In.Fog);
}
float4 PshaderCaster() : COLOR
{
return 0;
}
float4 PshaderReflect(PsInput In) : COLOR
{
clip(In.Height);
return lerp(FogColor, CalcColor(In), In.Fog);
}
float4 PshaderUnderwater(PsInput In) : COLOR
{
float d = smoothstep(0.9, 1, In.Fog);
return lerp(WaterColor, CalcColor(In), d);
}
float4 PshaderUnderwaterReflect(PsInput In) : COLOR
{
clip(-In.Height);
float d = smoothstep(0.9, 1, In.Fog);
return lerp(WaterColor, CalcColor(In), d);
}
float4 PshaderShadow(PsInput In) : COLOR
{
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;
}
float4 color = CalcColor(In) * (0.5 * shade + 0.5);
return lerp(FogColor, color, In.Fog);
}
technique Simple
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 Pshader();
}
}
technique Caster
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 VshaderCaster();
PixelShader = compile ps_3_0 PshaderCaster();
}
}
technique Reflect
{
pass Pass0
{
CullMode = CCW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 PshaderReflect();
}
}
technique Underwater
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 PshaderUnderwater();
}
}
technique UnderwaterReflect
{
pass Pass0
{
CullMode = CCW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 PshaderUnderwaterReflect();
}
}
technique Shadow
{
pass Pass0
{
CullMode = CW;
VertexShader = compile vs_3_0 Vshader();
PixelShader = compile ps_3_0 PshaderShadow();
}
}