-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathcommonFunctions.glsl
More file actions
336 lines (308 loc) · 7.99 KB
/
Copy pathcommonFunctions.glsl
File metadata and controls
336 lines (308 loc) · 7.99 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#ifdef VERTEX_SHADER
vec2 GetLightMapCoordinates() {
vec2 lmCoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
return clamp((lmCoord - 0.03125) * 1.06667, 0.0, 1.0);
}
#endif
#if defined VERTEX_SHADER || defined VOXY_PATCH
vec3 GetSunVector() {
const vec2 sunRotationData = vec2(cos(sunPathRotation * 0.01745329251994), -sin(sunPathRotation * 0.01745329251994));
#ifdef OVERWORLD
float ang = fract(timeAngle - 0.25);
ang = (ang + (cos(ang * 3.14159265358979) * -0.5 + 0.5 - ang) / 3.0) * 6.28318530717959;
return normalize((gbufferModelView * vec4(vec3(-sin(ang), cos(ang) * sunRotationData) * 2000.0, 1.0)).xyz);
#elif defined END
float ang = 0.0;
return normalize((gbufferModelView * vec4(vec3(0.0, sunRotationData * 2000.0), 1.0)).xyz);
#else
return vec3(0.0);
#endif
}
#endif
float GetLuminance(vec3 color) {
return dot(color, vec3(0.299, 0.587, 0.114));
}
vec3 DoLuminanceCorrection(vec3 color) {
return color / (GetLuminance(color) + 0.0001);
}
vec3 DoReducedLuminanceCorrection(vec3 color, float reduceAmount) {
return color / mix(GetLuminance(color), 1.0, reduceAmount);
}
float GetBiasFactor(float NdotLM) {
float NdotLM2 = NdotLM * NdotLM;
return 1.25 * (1.0 - NdotLM2 * NdotLM2) / NdotLM;
}
float GetHorizonFactor(float XdotU) {
#ifdef SUN_MOON_HORIZON
float horizon = clamp((XdotU + 0.1) * 10.0, 0.0, 1.0);
horizon *= horizon;
return horizon * horizon * (3.0 - 2.0 * horizon);
#else
float horizon = min(XdotU + 1.0, 1.0);
horizon *= horizon;
horizon *= horizon;
return horizon * horizon;
#endif
}
bool CheckForColor(vec3 albedo, vec3 check) { // Thanks to Builderb0y
vec3 dif = albedo - check * 0.003921568;
return dif == clamp(dif, vec3(-0.001), vec3(0.001));
}
bool CheckForStick(vec3 albedo) {
return CheckForColor(albedo, vec3(40, 30, 11)) ||
CheckForColor(albedo, vec3(73, 54, 21)) ||
CheckForColor(albedo, vec3(104, 78, 30)) ||
CheckForColor(albedo, vec3(137, 103, 39));
}
float GetMaxColorDif(vec3 color) {
vec3 dif = abs(vec3(color.r - color.g, color.g - color.b, color.r - color.b));
return max(dif.r, max(dif.g, dif.b));
}
float Noise3D(vec3 p) {
p.z = fract(p.z) * 128.0;
float iz = floor(p.z);
float fz = fract(p.z);
vec2 a_off = vec2(23.0, 29.0) * (iz) / 128.0;
vec2 b_off = vec2(23.0, 29.0) * (iz + 1.0) / 128.0;
float a = texture2DLod(noisetex, p.xy + a_off, 0.0).r;
float b = texture2DLod(noisetex, p.xy + b_off, 0.0).r;
return mix(a, b, fz);
}
float GetSkyLightFactor(vec2 lmCoordM, vec3 shadowMult) {
#if defined OVERWORLD || defined END && MC_VERSION >= 12109
#if WORLD_SPACE_REFLECTIONS_INTERNAL == -1
float skyLightFactor = max(lmCoordM.y - 0.7, 0.0) * 3.33333;
skyLightFactor *= skyLightFactor;
#else
float skyLightFactor = lmCoordM.y;
#endif
#if defined GBUFFERS_WATER || defined DH_WATER
#if SHADOW_QUALITY > -1 && WATER_REFLECT_QUALITY >= 2 && WATER_MAT_QUALITY >= 2
skyLightFactor = max(skyLightFactor, dot(shadowMult, shadowMult) * 0.333333);
#endif
#endif
#elif defined END && MC_VERSION < 12109
float skyLightFactor = min(1.0, dot(shadowMult, shadowMult) * 0.333333);
#else
float skyLightFactor = 0.0;
#endif
#ifdef VOXY_TRANSLUCENT
// A bug seems to make glass in voxy chunks have low skylight
skyLightFactor = pow(skyLightFactor, 0.25);
#endif
return skyLightFactor;
}
float minOf(vec3 x) {
return min(x.x, min(x.y, x.z));
}
int minOf(ivec3 x) {
return min(x.x, min(x.y, x.z));
}
int min1(int x) {
return min(x, 1);
}
float min1(float x) {
return min(x, 1.0);
}
int max0(int x) {
return max(x, 0);
}
float max0(float x) {
return max(x, 0.0);
}
int clamp01(int x) {
return clamp(x, 0, 1);
}
float clamp01(float x) {
return clamp(x, 0.0, 1.0);
}
vec2 clamp01(vec2 x) {
return clamp(x, vec2(0.0), vec2(1.0));
}
vec3 clamp01(vec3 x) {
return clamp(x, vec3(0.0), vec3(1.0));
}
int pow2(int x) {
return x * x;
}
float pow2(float x) {
return x * x;
}
vec2 pow2(vec2 x) {
return x * x;
}
vec3 pow2(vec3 x) {
return x * x;
}
vec4 pow2(vec4 x) {
return x * x;
}
int pow3(int x) {
return pow2(x) * x;
}
float pow3(float x) {
return pow2(x) * x;
}
vec2 pow3(vec2 x) {
return pow2(x) * x;
}
vec3 pow3(vec3 x) {
return pow2(x) * x;
}
vec4 pow3(vec4 x) {
return pow2(x) * x;
}
float pow1_5(float x) { // Faster pow(x, 1.5) approximation (that isn't accurate at all) if x is between 0 and 1
return x - x * pow2(1.0 - x); // Thanks to SixthSurge
}
vec2 pow1_5(vec2 x) {
return x - x * pow2(1.0 - x);
}
vec3 pow1_5(vec3 x) {
return x - x * pow2(1.0 - x);
}
vec4 pow1_5(vec4 x) {
return x - x * pow2(1.0 - x);
}
float sqrt1(float x) { // Faster sqrt() approximation (that isn't accurate at all) if x is between 0 and 1
return x * (2.0 - x); // Thanks to Builderb0y
}
vec2 sqrt1(vec2 x) {
return x * (2.0 - x);
}
vec3 sqrt1(vec3 x) {
return x * (2.0 - x);
}
vec4 sqrt1(vec4 x) {
return x * (2.0 - x);
}
float sqrt2(float x) {
x = 1.0 - x;
x *= x;
x *= x;
return 1.0 - x;
}
vec2 sqrt2(vec2 x) {
x = 1.0 - x;
x *= x;
x *= x;
return 1.0 - x;
}
vec3 sqrt2(vec3 x) {
x = 1.0 - x;
x *= x;
x *= x;
return 1.0 - x;
}
vec4 sqrt2(vec4 x) {
x = 1.0 - x;
x *= x;
x *= x;
return 1.0 - x;
}
float sqrt3(float x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec2 sqrt3(vec2 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec3 sqrt3(vec3 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec4 sqrt3(vec4 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
float sqrt4(float x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec2 sqrt4(vec2 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec3 sqrt4(vec3 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
vec4 sqrt4(vec4 x) {
x = 1.0 - x;
x *= x;
x *= x;
x *= x;
x *= x;
return 1.0 - x;
}
float smoothstep1(float x) {
return x * x * (3.0 - 2.0 * x);
}
vec2 smoothstep1(vec2 x) {
return x * x * (3.0 - 2.0 * x);
}
vec3 smoothstep1(vec3 x) {
return x * x * (3.0 - 2.0 * x);
}
vec4 smoothstep1(vec4 x) {
return x * x * (3.0 - 2.0 * x);
}
float dot3(vec3 x) {
return dot(x, x);
}
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
void LinearToRGB(inout vec3 color) { // sRGB gamma encode vec3
const vec3 k = vec3(0.055);
color = mix((vec3(1.0) + k) * pow(color, vec3(1.0 / 2.4)) - k, 12.92 * color, lessThan(color, vec3(0.0031308)));
}
void LinearToRGB(inout float color) { // sRGB gamma encode float
const float k = (0.055);
color = (color < (0.0031308)) ? (12.92 * color) : ((1.0 + k) * pow(color, (1.0 / 2.4)) - k);
}
void RGBToLinear(inout vec3 color) { // sRGB gamma decode vec3
const vec3 k = vec3(0.055);
color = mix(pow((color + k) / (vec3(1.0) + k), vec3(2.4)), color / 12.92, lessThan(color, vec3(0.04045)));
}
void RGBToLinear(inout float color) { // sRGB gamma decode float
const float k = (0.055);
color = (color < (0.04045)) ? (color / 12.92) : pow((color + k) / (1.0 + k), 2.4);
}