-
Notifications
You must be signed in to change notification settings - Fork 1
/
Effect.fx
executable file
·245 lines (213 loc) · 7.18 KB
/
Effect.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
//--------------------------------------------------------------------------------------
// File: Effect.fx
//--------------------------------------------------------------------------------------
cbuffer cb0 {
float4x4 g_mWorldViewProj;
float3 g_vCamPos;
float g_fTime;
float g_fElapsedTime;
}
cbuffer cb2 {
float3 g_vBlockOffset = float3(0, 0, 0);
float g_vBlockActivationTime = 0;
}
cbuffer cb3 {
bool g_bNormalMapping = true;
bool g_bDetailTex = true;
bool g_bFog = true;
float4 g_vFogColor = float4(0.976, 0.996, 0.667, 0.0);
float3 g_vLightDir = float3(0, 1, 1);
}
cbuffer cb4 {
float g_fLoaded = 0.0;
}
SamplerState ssNearestClamp {
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
Filter = MIN_MAG_MIP_POINT;
};
SamplerState ssTrilinearClamp {
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
Filter = MIN_MAG_MIP_LINEAR;
};
SamplerState ssTrilinearRepeat {
AddressU = WRAP;
AddressV = WRAP;
AddressW = WRAP;
Filter = MIN_MAG_MIP_LINEAR;
};
#include "BlockGen.fxh"
struct VS_BLOCK_INPUT {
float3 Position : POSITION;
float3 Normal : NORMAL;
};
struct VS_BLOCK_OUTPUT {
float4 Position : SV_Position;
float3 Normal : NORMAL;
float3 LightDir : LIGHTDIR;
float3 ViewDir : VIEWDIR;
float3 WorldPos : WORLDPOS;
float Age : AGE;
};
Texture2D g_tDiffuseX;
Texture2D g_tDiffuseY;
Texture2D g_tDiffuseZ;
Texture2D g_tNormalX;
Texture2D g_tNormalY;
Texture2D g_tNormalZ;
Texture2D g_tBump;
Texture2D g_tDetail;
Texture2D g_tDetailNormals;
VS_BLOCK_OUTPUT Block_VS(VS_BLOCK_INPUT Input) {
VS_BLOCK_OUTPUT Output;
Output.Position = mul(float4(Input.Position, 1), g_mWorldViewProj);
Output.Normal = Input.Normal;
Output.WorldPos = Input.Position;
Output.Age = g_fTime - g_vBlockActivationTime;
Output.LightDir = normalize(g_vLightDir);
Output.ViewDir = normalize(g_vCamPos - Input.Position);
float3 N_abs = abs(Input.Normal);
float3 Tangent;
if (N_abs.x > N_abs.y && N_abs.x > N_abs.z) {
// x dominant
if (N_abs.y > N_abs.z) {
// other vertices of this primitive possibly y dominant
Tangent = float3(0, 0, 1);
} else {
// other vertices of this primitive possibly z dominant
Tangent = float3(0, 1, 0);
}
} else if (N_abs.y > N_abs.x && N_abs.y > N_abs.z) {
// y dominant
if (N_abs.x > N_abs.z) {
// other vertices of this primitive possibly x dominant
Tangent = float3(0, 0, 1);
} else {
// other vertices of this primitive possibly z dominant
Tangent = float3(1, 0, 0);
}
} else {
// z dominant
if (N_abs.x > N_abs.y) {
// other vertices of this primitive possibly x dominant
Tangent = float3(0, 1, 0);
} else {
// other vertices of this primitive possibly y dominant
Tangent = float3(1, 0, 0);
}
}
float3 Binormal = normalize(cross(Tangent, Input.Normal));
Tangent = normalize(cross(Input.Normal, Binormal));
float3x3 world_to_tangent = float3x3(Tangent, Binormal, Input.Normal);
Output.LightDir = mul(world_to_tangent, Output.LightDir);
Output.ViewDir = mul(world_to_tangent, Output.ViewDir);
return Output;
}
float4 Block_PS(VS_BLOCK_OUTPUT Input) : SV_Target {
const float2 texX = Input.WorldPos.yz*4;
const float2 texY = Input.WorldPos.xz*3;
const float2 texZ = Input.WorldPos.xy*4;
float3 colorX = g_tDiffuseX.Sample(ssTrilinearRepeat, texX);
float3 colorY = g_tDiffuseY.Sample(ssTrilinearRepeat, texY);
float3 colorZ = g_tDiffuseZ.Sample(ssTrilinearRepeat, texZ);
float3 normalX = g_tNormalX.Sample(ssTrilinearRepeat, texX)*2-1;
float3 normalY = g_tNormalY.Sample(ssTrilinearRepeat, texY)*2-1;
float3 normalZ = g_tNormalZ.Sample(ssTrilinearRepeat, texZ)*2-1;
if (g_bDetailTex) {
colorX *= g_tDetail.Sample(ssTrilinearRepeat, texX*7.97);
colorY *= g_tDetail.Sample(ssTrilinearRepeat, texY*7.95);
colorZ *= g_tDetail.Sample(ssTrilinearRepeat, texZ*7.98);
normalX += g_tDetailNormals.Sample(ssTrilinearRepeat, texX*5)*2-1;
normalY += g_tDetailNormals.Sample(ssTrilinearRepeat, texY*5)*2-1;
normalZ += g_tDetailNormals.Sample(ssTrilinearRepeat, texZ*5)*2-1;
}
float3 N = normalize(Input.Normal);
float3 blend_weights = abs(N) - 0.1;
blend_weights *= 12;
blend_weights = max(0, blend_weights);
blend_weights = pow(blend_weights, 5);
blend_weights /= dot(blend_weights, 1);
float3 color = blend_weights.x*colorX + blend_weights.y*colorY + blend_weights.z*colorZ;
float3 normal = blend_weights.x*normalX + blend_weights.y*normalY + blend_weights.z*normalZ;
if (g_bNormalMapping) {
N = normalize(normal + float3(0, 0, 1));
} else {
Input.LightDir = normalize(g_vLightDir);
Input.ViewDir = normalize(g_vCamPos - Input.WorldPos);
}
float3 L = normalize(Input.LightDir);
float3 H = normalize(Input.ViewDir + Input.LightDir);
float4 coeffs = lit(dot(N, L), abs(dot(N, H)), 32);
float intensity = dot(coeffs, float4(0.05, 0.5, 0.5, 0));
//color = float4(Input.Tangent*0.5+0.5, 0);
//color = float4(0.6 + 0.4*normalize(Input.Normal), 0);
//return intensity*float4(normalize(Input.Tangent)*0.5+0.5, 0);
color *= intensity;
// Fog
if (g_bFog) {
float depth = length(g_vCamPos - Input.WorldPos);
const float fFogStart = 1.0, fFogEnd = 7.5;
color = lerp(g_vFogColor.rgb, color, 1.f/exp(pow(depth * 0.2, 2)));
}
return float4(color, saturate(Input.Age*3));
}
RasterizerState rsWireframe {
FillMode = WIREFRAME;
//CullMode = NONE;
};
BlendState bsSrcAlphaBlendingAdd
{
BlendEnable[0] = TRUE;
SrcBlend = SRC_ALPHA;
DestBlend = INV_SRC_ALPHA;
BlendOp = ADD;
RenderTargetWriteMask[0] = 0x0F;
};
DepthStencilState dssEnableDepth
{
DepthEnable = TRUE;
DepthWriteMask = ALL;
DepthFunc = LESS;
};
technique10 RenderBlock {
pass P0 {
SetVertexShader(CompileShader(vs_4_0, Block_VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, Block_PS()));
SetDepthStencilState(dssEnableDepth, 0);
SetBlendState(bsSrcAlphaBlendingAdd, float4(0, 0, 0, 0), 0xFFFFFFFF);
SetRasterizerState(NULL);
}
}
float4 Loading_VS(float4 vPosition : POSITION, out float2 vScreenPos : SCREENPOS) : SV_Position
{
vScreenPos = vPosition.xy*0.5 + 0.5;
vScreenPos.y = 1 - vScreenPos.y;
return vPosition;
}
float4 Loading_PS(float4 vPosition : SV_Position, float2 vScreenPos : SCREENPOS) : SV_Target
{
if (vScreenPos.y >= 0.45 && vScreenPos.y <= 0.55) {
float x = vScreenPos.x*1.2-0.1;
if (x >= 0 && x <= g_fLoaded) {
float s = 1-sqrt(1-g_fLoaded);
return float4(s, s, s, 1);
}
if ((x >= -0.025 && x <= -0.0125) || (x >= 1.0125 && x <= 1.025)) return float4(1, 1, 1, 1);
}
return float4(0, 0, 0, 1);
}
technique10 LoadingScreen {
pass P0 {
SetVertexShader(CompileShader(vs_4_0, Loading_VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, Loading_PS()));
SetDepthStencilState(dssDisableDepthStencil, 0);
SetBlendState(NULL, float4(0, 0, 0, 0), 0xFFFFFFFF);
SetRasterizerState(NULL);
}
}
#include "postprocessing.fx"