Repository navigation
Expand file tree
/
Copy pathNormalMap.fx
More file actions
275 lines (217 loc) · 5.12 KB
/
Copy pathNormalMap.fx
File metadata and controls
275 lines (217 loc) · 5.12 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
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
//#region Includes
#include "FXShaders/Common.fxh"
#include "FXShaders/Math.fxh"
//#endregion
//#region Preprocessor Directives
#ifndef NORMAL_MAP_WIDTH
#define NORMAL_MAP_WIDTH 1024
#endif
#ifndef NORMAL_MAP_HEIGHT
#define NORMAL_MAP_HEIGHT 1024
#endif
#ifndef NORMAL_MAP_TEXTURE
#define NORMAL_MAP_TEXTURE "NormalMap.png"
#endif
//#endregion
namespace FXShaders
{
//#region Constants
static const float2 NormalResolution = float2(NORMAL_MAP_WIDTH, NORMAL_MAP_HEIGHT);
static const float2 NormalPixelSize = 1.0 / NormalResolution;
static const float NormalAspectRatio = NormalResolution.x * NormalPixelSize.y;
static const float TranslationScale = 1.0;
static const float RotationScale = 0.2;
static const int AddressMode_Repeat = 0;
static const int AddressMode_Clip = 1;
static const int AddressMode_Stretch = 2;
static const float DistortionAmountScale = 100.0;
//#endregion
//#region Uniforms
FXSHADERS_WIP_WARNING();
uniform float DistortionAmount
<
ui_label = "Distortion Amount";
ui_tooltip =
"Determines the amount of distortion to apply to the image based on "
"normal map texture.\n"
"\nDefault: 1.0";
ui_type = "slider";
ui_min = -3.0;
ui_max = 3.0;
> = 1.0;
uniform float TextureScale
<
ui_label = "Texture Scale";
ui_tooltip =
"Scales the normal map texture on the screen.\n"
"\nDefault: 6.0";
ui_type = "slider";
ui_min = 0.0;
ui_max = 10.0;
> = 6.0;
uniform float ZScale
<
ui_label = "Z Scale";
ui_tooltip =
"Determines how much the Z axis of the normal map affects the "
"distortion.\n"
"\nDefault: 1.0";
ui_type = "slider";
ui_min = 0.0;
ui_max = 1.0;
> = 1.0;
uniform float2 Translation
<
ui_label = "Translation Speed";
ui_tooltip =
"Speed at which the normal map is moved.\n"
"\nDefault: 0.0 0.0";
ui_type = "slider";
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;
uniform float Rotation
<
ui_label = "Rotation Speed";
ui_tooltip =
"Speed at which the normal map is spinned.\n"
"\nDefault: 0.0";
ui_type = "slider";
ui_min = -1.0;
ui_max = 1.0;
> = 0.0;
uniform int AddressMode
<
ui_label = "Texture Address Mode";
ui_tooltip =
"Determines how out of bounds coordinates of the normal map texture"
"are rendered.\n"
"\nDefault: Repeat";
ui_type = "combo";
ui_items = "Repeat\0Clip\0Stretch\0";
> = AddressMode_Repeat;
uniform float Timer <source = "timer";>;
//#endregion
//#region Textures
texture BackBufferTex : COLOR;
sampler BackBuffer
{
Texture = BackBufferTex;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};
texture NormalTex <source = NORMAL_MAP_TEXTURE;>
{
Width = NormalResolution.x;
Height = NormalResolution.y;
};
sampler NormalRepeat
{
Texture = NormalTex;
AddressU = REPEAT;
AddressV = REPEAT;
};
sampler NormalClip
{
Texture = NormalTex;
AddressU = BORDER;
AddressV = BORDER;
};
sampler NormalStretch
{
Texture = NormalTex;
};
//#endregion
//#region Functions
float2 CorrectAspect(float2 uv)
{
// Correct the normal map aspect ratio in case it's not 1:1.
uv = CorrectAspectRatio(uv, NormalAspectRatio, 1.0);
// Correct the normal map aspecct ratio to cover the screen's aspect ratio.
uv = CorrectAspectRatio(
uv,
NormalAspectRatio,
GetAspectRatio());
return uv;
}
float2 ApplyScale(float2 uv)
{
// Apply the normal texture scale.
return ScaleCoord(uv, TextureScale);
}
float2 ApplyRotation(float2 uv)
{
// We'll apply the rotation with non-normalized coordinates, otherwise it
// breaks the aspect ratio.
uv *= NormalResolution;
uv = RotatePoint(
uv,
Rotation * Timer * RotationScale,
NormalResolution * 0.5);
uv *= NormalPixelSize;
return uv;
}
float2 ApplyTranslation(float2 uv)
{
// Apply translation.
const float2 offset =
float2(-Translation.x, Translation.y) *
Timer *
TranslationScale;
return uv + offset * NormalPixelSize;
}
float2 ApplyTransformations(float2 uv)
{
uv = CorrectAspect(uv);
uv = ApplyScale(uv);
uv = ApplyRotation(uv);
uv = ApplyTranslation(uv);
return uv;
}
float2 ReadNormalTexture(float2 uv)
{
uv = ApplyTransformations(uv);
// HACK:
//
// Using a switch breaks, probably because the SPIR-V code is attempting to
// optimize the call by changing the sampler dynamically, which HLSL does
// not support.
// TODO: Check if apply address modes programmatically is more efficient.
float4 normal = 0.0;
if (AddressMode == AddressMode_Repeat)
normal = tex2D(NormalRepeat, uv);
else if (AddressMode == AddressMode_Clip)
normal = tex2D(NormalClip, uv);
else if (AddressMode == AddressMode_Stretch)
normal = tex2D(NormalStretch, uv);
normal.xyz *= normal.a;
normal = normal * 2.0 - 1.0;
normal.xy *= lerp(1.0, normal.z, ZScale);
return normal.xy;
}
//#endregion
//#region Shaders
float4 MainPS(float4 p : SV_POSITION, float2 uv : TEXCOORD) : SV_TARGET
{
const float2 normal = ReadNormalTexture(uv);
const float amount = DistortionAmount * DistortionAmountScale;
const float2 ps = GetPixelSize() * amount;
uv += normal.xy * ps;
return tex2D(BackBuffer, uv);
}
//#endregion
//#region Techniques
technique NormalMap
<
ui_tooltip = "FXShaders - Distorts the image using a normal map texture.";
>
{
pass
{
VertexShader = ScreenVS;
PixelShader = MainPS;
}
}
//#endregion
} // Namespace.