-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.fxh
298 lines (237 loc) · 8.51 KB
/
map.fxh
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
////////////////////////////////////////////////////////////////
//
// Handy Mapping Functions
//
////////////////////////////////////////////////////////////////
#define MAP_FXH
////////////////////////////////////////////////////////////////
//
// Bias & Gain
//
////////////////////////////////////////////////////////////////
// Schlick's version of Bias & Gain w/ inversion on 0 to -1 for bias/gain values
// these functions expect an input in 0 - 1 range
float bias(float x, float control = 0.5)
{
float result = control > 0 ? (x / ((((1.0/control) - 2.0)*(1.0 - x))+1.0)) : 1-(x / ((((1.0/abs(control)) - 2.0)*(1.0 - x))+1.0));
return result;
}
float2 bias(float2 x, float2 control = 0.5)
{
return float2(bias(x.x, control.x), bias(x.y, control.y));
}
float3 bias(float3 x, float3 control = 0.5)
{
return float3(bias(x.x, control.x), bias(x.y, control.y), bias(x.z, control.z));
}
float gain(float x, float control = 0.5)
{
if (control > 0)
{if(x < 0.5) return bias(x * 2.0,control)/2.0; else return bias(x * 2.0 - 1.0,1.0 - control)/2.0 + 0.5; }
else if(x < 0.5) return 1-(bias(x * 2.0,-control)/2.0); else return 1-(bias(x * 2.0 - 1.0,1.0 + control)/2.0 + 0.5);
}
float2 gain(float2 x, float2 control = 0.5)
{
return float2(gain(x.x, control.x), gain(x.y, control.y));
}
float3 gain(float3 x, float3 control = 0.5)
{
return float3(gain(x.x, control.x), gain(x.y, control.y), gain(x.z, control.z));
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Bandpass Functions
//
////////////////////////////////////////////////////////////////
float smoothstep2 (float onMin, float onMax, float offMin, float offMax, float input)
{
input = smoothstep(onMin, onMax, input) * (1 - smoothstep(offMin, offMax, input));
return input;
}
float pulse(float input, float center, float slopeWidth, float centerWidth = 0.0)
{
centerWidth *= 0.5;
input = abs(input - center);
input -= centerWidth;
if(input > slopeWidth) return 0.0f;
input /= slopeWidth;
return 1.0 - input;
}
float cubicPulse(float input, float center, float width)
{
input = abs(input - center);
if(input > width) return 0.0f;
input /= width;
return 1.0f - input*input*(3.0f-2.0f*input);
}
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// Map Functions
//
////////////////////////////////////////////////////////////////
float map(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
float range = InMax - InMin;
float normalized = (Input - InMin) / range;
return OutMin + normalized * (OutMax - OutMin);
}
float mapClamp(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
float range = InMax - InMin;
float normalized = (Input - InMin) / range;
float output = OutMin + normalized * (OutMax - OutMin);
float minV = min(OutMin,OutMax);
float maxV = max(OutMin, OutMax);
output = min(max(output, minV), maxV);
return output ;
}
float mapWrap(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
float range = InMax - InMin;
float normalized = (Input - InMin) / range;
float output = OutMin + normalized * (OutMax - OutMin);
if (normalized < 0) normalized = 1 + normalized;
return OutMin + (normalized % 1) * (OutMax - OutMin);
}
float mapMirror(float Input, float InMin, float InMax, float OutMin, float OutMax)
{
float range = InMax - InMin;
float normalized = (Input - InMin) / range;
normalized = 1-2*abs(frac(normalized*.5)-.5);
float output = OutMin + (normalized % 1) * (OutMax - OutMin);
return output;
}
float2 map(float2 Input, float2 InMin, float2 InMax, float2 OutMin, float2 OutMax)
{
float2 vec;
vec.x = map( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = map( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
return vec;
}
float3 map(float3 Input, float3 InMin, float3 InMax, float3 OutMin, float3 OutMax)
{
float3 vec;
vec.x = map( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = map( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = map( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
return vec;
}
float4 map(float4 Input, float4 InMin, float4 InMax, float4 OutMin, float4 OutMax)
{
float4 vec;
vec.x = map( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = map( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = map( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
vec.w = map( Input.w, InMin.w, InMax.w, OutMin.w, OutMax.w);
return vec;
}
float2 mapClamp(float2 Input, float2 InMin, float2 InMax, float2 OutMin, float2 OutMax)
{
float2 vec;
vec.x = mapClamp( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapClamp( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
return vec;
}
float3 mapClamp(float3 Input, float3 InMin, float3 InMax, float3 OutMin, float3 OutMax)
{
float3 vec;
vec.x = mapClamp( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapClamp( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapClamp( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
return vec;
}
float4 mapClamp(float4 Input, float4 InMin, float4 InMax, float4 OutMin, float4 OutMax)
{
float4 vec;
vec.x = mapClamp( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapClamp( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapClamp( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
vec.w = mapClamp( Input.w, InMin.w, InMax.w, OutMin.w, OutMax.w);
return vec;
}
float2 mapWrap(float2 Input, float2 InMin, float2 InMax, float2 OutMin, float2 OutMax)
{
float2 vec;
vec.x = mapWrap( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapWrap( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
return vec;
}
float3 mapWrap(float3 Input, float3 InMin, float3 InMax, float3 OutMin, float3 OutMax)
{
float3 vec;
vec.x = mapWrap( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapWrap( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapWrap( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
return vec;
}
float4 mapWrap(float4 Input, float4 InMin, float4 InMax, float4 OutMin, float4 OutMax)
{
float4 vec;
vec.x = mapWrap( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapWrap( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapWrap( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
vec.w = mapWrap( Input.w, InMin.w, InMax.w, OutMin.w, OutMax.w);
return vec;
}
float2 mapMirror(float2 Input, float2 InMin, float2 InMax, float2 OutMin, float2 OutMax)
{
float2 vec;
vec.x = mapMirror( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapMirror( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
return vec;
}
float3 mapMirror(float3 Input, float3 InMin, float3 InMax, float3 OutMin, float3 OutMax)
{
float3 vec;
vec.x = mapMirror( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapMirror( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapMirror( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
return vec;
}
float4 mapMirror(float4 Input, float4 InMin, float4 InMax, float4 OutMin, float4 OutMax)
{
float4 vec;
vec.x = mapMirror( Input.x, InMin.x, InMax.x, OutMin.x, OutMax.x);
vec.y = mapMirror( Input.y, InMin.y, InMax.y, OutMin.y, OutMax.y);
vec.z = mapMirror( Input.z, InMin.z, InMax.z, OutMin.z, OutMax.z);
vec.w = mapMirror( Input.w, InMin.w, InMax.w, OutMin.w, OutMax.w);
return vec;
}
//TODO add interface for map types
// ACES ToneMap
// sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
static const float3x3 ACESInputMat =
{
{0.59719, 0.35458, 0.04823},
{0.07600, 0.90834, 0.01566},
{0.02840, 0.13383, 0.83777}
};
// ODT_SAT => XYZ => D60_2_D65 => sRGB
static const float3x3 ACESOutputMat =
{
{ 1.60475, -0.53108, -0.07367},
{-0.10208, 1.10813, -0.00605},
{-0.00327, -0.07276, 1.07602}
};
float3 RRTAndODTFit(float3 v)
{
float3 a = v * (v + 0.0245786f) - 0.000090537f;
float3 b = v * (0.983729f * v + 0.4329510f) + 0.238081f;
return a / b;
}
float3 ACESFitted(float3 color, float gamma = 1)
{
color = mul(ACESInputMat, color);
// Apply RRT and ODT
color = RRTAndODTFit(color);
color = mul(ACESOutputMat*gamma, color);
// Clamp to [0, 1]
color = saturate(color);
return color;
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//EOF