-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskyvol.frag
604 lines (471 loc) · 15.8 KB
/
skyvol.frag
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/**
* sky.frag
* Fragment shader for the sky
*/
// seed provided outside, better random
uniform float uSeed;
// extremely slow time
uniform float uSlowTime;
uniform float uTime;
//uniform int activeWarpColor; // texture warp active
varying vec2 vST; // texture coords
varying vec3 vMCPosition; // model coords
varying vec4 vECPosition; // eye coords (camera coordinates)
// shaded normal, light, eye vectors
varying vec3 Ns;
varying vec3 Ls;
varying vec3 Es;
uniform float uAmbient, uDiffuse, uSpecular;
uniform vec3 SpecularColor;
uniform float Shininess;
uniform int uOctaves;
// start & dimensions of this volume, adjusted to eye space
uniform vec3 uVolumeStart;
uniform vec3 uVolumeDimens;
// inverse of model view matrix
varying mat4 vModelViewMatrix_Inverse;
// eye coordinate lights
varying vec3 vECLight;
// density of clouds
uniform float uCloudDensity;
// size multiplier for ray cast steps to use
uniform float uRayCastStepSize;
// whether to use or ignore boundaries
uniform bool uIgnoreBounds;
// noise texture to sample from
uniform sampler2D uNoiseTexture;
// from S.O., what does dot() do? (dot product, yes), and fract() (fraction, yes)
// Random Poster: https://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl#4275343
// Detailed Sourcing: https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
// Supposedly from a 1998 Mathematical Statistics paper that has since been lost?
// Not Random, Hash Function (works for same X & Y)
float hash(vec2 co) {
// multiplies input by the seed
// then converts number from 2D to 1D (via dot product)
// arbitrary numbers 12.9898 and 78.233 chosen to avoid repitition
// ~~~ used to multiply co.st by seed (a float)
float t = dot(co.st, vec2(12.9898,78.233));
// then takes the sin of that number
// then multiplies by 43758.5453, which amplifies the error of the sin function (based on local implementation)
float u = sin(t) * 43758.5453123;
// then returns the fractional component of that number, focusing further on the error
// overall, this is a dubious hash function because sin() is platform specific, and may not be consistent
return fract(u);
}
// from S.O., what does dot() do? (dot product, yes), and fract() (fraction, yes)
// Random Poster: https://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl#4275343
// Detailed Sourcing: https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
// Supposedly from a 1998 Mathematical Statistics paper that has since been lost?
// Not Random, Hash Function (works for same X & Y)
float hash3(vec3 co) {
// multiplies input by the seed
// then converts number from 2D to 1D (via dot product)
// arbitrary numbers 12.9898 and 78.233 chosen to avoid repitition
// ~~~ used to multiply co.st by seed (a float)
float t = dot(co.xyz, vec3(12.9898,78.233,34.424242));
// then takes the sin of that number
// then multiplies by 43758.5453, which amplifies the error of the sin function (based on local implementation)
float u = sin(t) * 43758.5453123;
// then returns the fractional component of that number, focusing further on the error
// overall, this is a dubious hash function because sin() is platform specific, and may not be consistent
return fract(u);
}
// 1D noise function
// Based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
// referenced from: https://thebookofshaders.com/13/
float noise(vec2 st) {
vec2 i = floor(st);
vec2 f = fract(st);
// calculate 4 corners of a 2D tile
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
// calculate f^2 * (3.0 - 2.0f)
vec2 u = f * f * (3.0 - 2.0 * f);
// mix between a and b via u.x
float h = mix(a, b, u.x) +
// diff of c & a * u.y, multiplied by inverse of u.x
(c - a)* u.y * (1.0 - u.x) +
// add in diff of d - b * (u.x*u.y)
(d - b) * u.x * u.y;
return h;
}
// pow implementation
float pow(float base, int pow) {
float val = base;
if(pow == 0) {
return 1.0;
}
for(int x = 1; x < pow; x++) {
val*=base;
}
return val;
}
float weakFract(float x) {
int q = int(x);
return x;
}
float altHash(vec3 v) {
//return fract(v.x + uSeed) + fract(v.y + uSeed) + fract(v.z + uSeed);
float q = v.x - floor(v.x);
return v.x;
}
float gn3(vec3 v, float seed) {
float h = distance(v * 0.8198498492, v) * v.x * seed;
return (float(int(h * 10.0))/10.0) - float(int(h));
}
// 1D noise function
// Based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
// referenced from: https://thebookofshaders.com/13/
float noise3D(vec3 v) {
vec3 i = floor(v);
vec3 f = fract(v);
// calculate 8 corners of a 3D tile
/**/
float a = hash3(i);
float b = hash3(i + vec3(1.0, 0.0, 0.0));
float c = hash3(i + vec3(0.0, 1.0, 0.0));
float d = hash3(i + vec3(1.0, 1.0, 0.0));
float a2 = hash3(i + vec3(0.0, 0.0, 1.0));
float b2 = hash3(i + vec3(1.0, 0.0, 1.0));
float c2 = hash3(i + vec3(0.0, 1.0, 1.0));
float d2 = hash3(i + vec3(1.0, 1.0, 1.0));
/**/
// using janky alt hash
/*
float a = gn3(i,uSeed);
float b = gn3(i + vec3(1.0, 0.0, 0.0),uSeed);
float c = gn3(i + vec3(0.0, 1.0, 0.0),uSeed);
float d = gn3(i + vec3(1.0, 1.0, 0.0),uSeed);
float a2 = gn3(i + vec3(0.0, 0.0, 1.0),uSeed);
float b2 = gn3(i + vec3(1.0, 0.0, 1.0),uSeed);
float c2 = gn3(i + vec3(0.0, 1.0, 1.0),uSeed);
float d2 = gn3(i + vec3(1.0, 1.0, 1.0),uSeed);
/**/
// using noise texture instead
/*
float a = texture2D(uNoiseTexture, i.xz).r * 3.0;
float b = texture2D(uNoiseTexture, i.xz + vec2(1.0, 0.0)).r * 3.0;
float c = texture2D(uNoiseTexture, i.xz + vec2(0.0, 1.0)).r * 3.0;
float d = texture2D(uNoiseTexture, i.xz + vec2(1.0, 1.0)).r * 3.0;
float a2 = texture2D(uNoiseTexture, i.yz + vec2(1.0, 1.0)).b * 3.0;
float b2 = texture2D(uNoiseTexture, i.yz + vec2(2.0, 1.0)).b * 3.0;
float c2 = texture2D(uNoiseTexture, i.yz + vec2(1.0, 2.0)).b * 3.0;
float d2 = texture2D(uNoiseTexture, i.yz + vec2(2.0, 2.0)).b * 3.0;
/**/
// calculate f^2 * (3.0 - 2.0f)
vec3 u = f * f * (3.0 - 2.0 * f);
// mix between a and b via u.x
float h1 = mix(a, b, u.x) +
// diff of c & a * u.y, multiplied by inverse of u.x
(c - a)* u.y * (1.0 - u.x) +
// add in diff of d - b * (u.x*u.y)
(d - b) * u.x * u.y;
// mix between a and b via u.x
float h2 = mix(a2, b2, u.x) +
// diff of c & a * u.y, multiplied by inverse of u.x
(c2 - a2)* u.y * (1.0 - u.x) +
// add in diff of d - b * (u.x*u.y)
(d2 - b2) * u.x * u.y;
// mix with regards to z
float h = mix(h1, h2, u.z);
return h;
}
// Fractal Brownian Motion
float fbm(vec3 v) {
// number of iterations
int octaves = uOctaves;
// initial value
float value = 0.0;
// initial amp at half
float amplitude = 0.5;
// regular step to increase freqency by
float lacunarity = 2.0;
// amplitude modification
float gain = 0.5;
for(int x = 0; x < octaves; x++) {
// add noise of 'v' scaled by amplitude
value += amplitude * noise3D(v);
// scale frequency by lacunarity
v *= lacunarity;
// scale amplitude by gain
amplitude *= gain;
}
return value;
}
// replacement for the shaded normal from the vertex shader
// instead computes the normal with respect to the termination positions of 3 rays
vec3 ReplaceNormal;
// used to compute modification of lighting
float lightAlpha = 0.0;
// per fragment lighting in the FRAG shader
vec4 perFragmentLighting(vec4 color) {
vec3 Normal,Light,Eye;
Normal = normalize(ReplaceNormal);
Light = normalize(Ls);
Eye = normalize(Es);
vec4 ambient = uAmbient * color * lightAlpha;
float d = max(dot(Normal,Light), 0.0);
vec4 diffuse = uDiffuse * d * color;
float s = 0.0;
// only do specular if the light can see the point
if(dot(Normal,Light) > 0.0) {
vec3 ref = normalize(2.0 * Normal * dot(Normal,Light) - Light);
s = pow(max(dot(Eye,ref), 0.0), Shininess);
}
vec4 specular = uSpecular * s * vec4(SpecularColor,1.0) * lightAlpha;
return vec4(ambient.rgb + diffuse.rgb + specular.rgb, 1.0);
}
// determine that this point lies above and below the x,y,z positions that define this shape
// is within the volume
bool isWithinVolume(vec3 p) {
return (
// within x coordinate
p.x >= uVolumeStart.x && p.x <= (uVolumeStart.x + uVolumeDimens.x) &&
// within y coordinate
p.y >= uVolumeStart.y && p.y <= (uVolumeStart.y + uVolumeDimens.y) &&
// within z coordinate
p.z >= uVolumeStart.z && p.z <= (uVolumeStart.z + uVolumeDimens.z)
);
}
// TODO, this method is unused, wasn't the right direction and doesn't work with the current inputs vals
// but left for reference
//
// Fixed probe, the data being viewed is fixed within the volume in model coordinates
// This can be changed by rotations and such, but only on the object itself
// gets the color using a ray cast, without a definite volume boundary
// for model coordinates (local)
void getColor_ByRayCast_NoBound_UsingModelCoordinates() {
// changes the ray step size
// 0.025, 0.0125
float rayStepSizeMultiplier = 0.1;
// max # of steps that are allowed before stopping the ray cast
int maxSteps = 30;
// default is no color
vec4 color = vec4(0.0);
// calculate small ray that steps through at fixed increments
// TODO what's wrong with this?
vec3 rayStep = (normalize(vECPosition) * rayStepSizeMultiplier).xyz;
// progressive rayCast that steps through the volume
vec3 rayCastPos = vMCPosition;
// counts ray cast steps, used to exit out
int steps = 0;
// isWithinVolume(rayCastPos)
while(isWithinVolume(rayCastPos)) {
if(color.a >= 1.0) {
// color maxed out, exit
break;
}
if(steps > maxSteps) {
break;
}
// circle test
/*
// x^2 + y^2 + z^2 = r^2 (should be <= a certain radius to be within the sphere)
if(sqrt(pow(rayCastPos.x, 2) + pow(rayCastPos.y, 2) + pow(rayCastPos.z, 2)) <= 0.52) {
// shade in white
color = vec4(1.0);
break;
}
/**/
/**/
// get fractal brownian motion val
float f = fbm(rayCastPos.xyz + uSeed) * 2.0;
//float f = fbm(rayCastPos.xyz + fbm(rayCastPos.xyz + fbm(rayCastPos.xyz + uSlowTime + uSeed))) * 2.0;
// only apply if greater than 0.8
if(f > 0.8) {
// add color only if exceeds the threshold to display
vec4 tColor = mix(
vec4(0.0),
vec4(1.0),
(f - 0.8)
);
tColor *= 1.0;
color += tColor;
}
/**/
// step ray through the volume
rayCastPos += rayStep;
steps++;
}
/*
if(color.a == 0.0) {
// discard if nothing to display
discard;
}
/**/
// hold alpha
float alpha = 1.0;
// to give effect of looking at water
//color.r = 0.0;
// calc fragment lighting with color
color = perFragmentLighting(color);
// aply fragment color
gl_FragColor = vec4(color.rgb, alpha);
}
// Current Approach
//
// Gets the color for a fragment by performing a ray cast
// based on eye coordinates, and then converting back to model coordinates
// in order to preserve locality of values obtained
void getColor_ByRayCast_NoBound_UsingEyeCoordinates() {
// changes the ray step size
// 0.025, 0.0125
float rayStepSizeMultiplier = uRayCastStepSize;
// max # of steps that are allowed before stopping the ray cast
int maxSteps = 200; // 200
// default is no color (testing with slight coloring to track it)
vec4 color = vec4(0.0);
// calculate small ray that steps through at fixed increments
vec3 rayStep = (normalize(vECPosition) * rayStepSizeMultiplier).xyz;
// progressive rayCast that steps through the volume
vec4 rayCastPos = vECPosition;
// counts ray cast steps, used to exit out
int steps = 0;
// time and seed adjustment
float timeAndSeed = (uSlowTime * 0.5) + uSeed;
// convert point to model coordinates before we start
vec4 convertedPoint = vModelViewMatrix_Inverse * rayCastPos;
// TODO old ray tracing method for lighting
// setup 2 other converted ray casted points to trace through volume
// the end result of the 3 converted points will be used
// to compute the normal via the cross product of the 2 resultant vectors
//vec4 rc2,rc3;
//vec4 c2,c3;
//rc2 = vec4(rayCastPos.x+0.01, rayCastPos.y, rayCastPos.z, rayCastPos.w);
//rc3 = vec4(rayCastPos.x, rayCastPos.y, rayCastPos.z+0.01, rayCastPos.w);
// setup default colors for these as well
//c2 = c3 = vec4(0.0);
// isWithinVolume(rayCastPos)
// isWithinVolume(convertedPoint.xyz)
while(uIgnoreBounds || isWithinVolume(convertedPoint.xyz) || steps == 0) {
if(color.a >= 1.0) {
// color maxed out, exit
break;
}
if(steps > maxSteps) {
break;
}
/**/
// get fractal brownian motion val
float f = fbm(convertedPoint.xyz + timeAndSeed) * 2.0;
//float f = fbm(convertedPoint.xyz + fbm(convertedPoint.xyz + fbm(convertedPoint.xyz + uSlowTime * 0.5 + uSeed))) * 2.0;
// only apply if greater than 0.8
if(f > 0.8) {
// add color only if exceeds the threshold to display
vec4 tColor = mix(
vec4(0.0),
vec4(1.0),
(f - 0.8)
);
tColor *= uCloudDensity;
color += tColor;
}
// step ray through the volume
rayCastPos.xyz += rayStep;
// recalc next converted point
convertedPoint = vModelViewMatrix_Inverse * rayCastPos;
// Do again for Ray Cast #2
/*
if(c2.a < 1.0) {
vec4 cp2 = vModelViewMatrix_Inverse * rc2;
f = fbm(cp2.xyz + (uSlowTime * 0.5) + uSeed) * 2.0;
if(f > 0.8) {
vec4 tColor = mix(
vec4(0.0),
vec4(1.0),
(f - 0.8)
);
tColor *= 0.222;
c2 += tColor;
}
rc2.xyz += rayStep;
}
/**/
// Do again for Ray Cast #3
/*
if(c3.a < 1.0) {
vec4 cp3 = vModelViewMatrix_Inverse * rc3;
f = fbm(cp3.xyz + (uSlowTime * 0.5) + uSeed) * 2.0;
if(f > 0.8) {
vec4 tColor = mix(
vec4(0.0),
vec4(1.0),
(f - 0.8)
);
tColor *= 0.222;
c3 += tColor;
}
rc3.xyz += rayStep;
}
/**/
// bump our step count
steps++;
}
// color red if we are exceeding the ray length
// this is a debug feature
/*
if(steps > maxSteps) {
color = vec4(1.0, 0.0, 0.0, 1.0);
}
*/
if(color.a == 0.0) {
// discard if nothing to display
// no need to draw empty fragments
discard;
}
// compute cross product of resulting ray cast end positions
//vec3 v1 = (rayCastPos - rc2).xyz;
//vec3 v2 = (rayCastPos - rc3).xyz;
// calculate vector from final point rayCastPos to vECLight
// reduce to multiple of unit vector
vec3 vRL = normalize(vECLight - rayCastPos.xyz) * rayStepSizeMultiplier;
// apply fixed increments until we exit the volume
rayCastPos.xyz += vRL;
convertedPoint = vModelViewMatrix_Inverse * rayCastPos;
steps = 0;
while(uIgnoreBounds || isWithinVolume(convertedPoint.xyz)) {
if(lightAlpha >= 1.0) {
break;
}
if(steps > maxSteps) {
break;
}
// sum up the alphas using the same equation as above
float g = fbm(convertedPoint.xyz + timeAndSeed) * 2.0;
if(g > 0.8) {
float tAlpha = mix(
0.0,
1.0,
(g - 0.8)
) * uCloudDensity;
lightAlpha += tAlpha;
}
rayCastPos.xyz += vRL;
convertedPoint = vModelViewMatrix_Inverse * rayCastPos;
steps++;
}
// use the inverse of the alpha to apply a lighting change
// which will take effect in the 'perFragmentLighting' function
lightAlpha = 1.0 - lightAlpha;
// use vector from ray to light for normal
// playing around with local normal plus ray to light vector, not really that good
//ReplaceNormal = normalize(cross(v1,v2) + normalize(vECLight - rayCastPos.xyz));
ReplaceNormal = normalize(vECLight - rayCastPos.xyz);
// hold alpha
float alpha = color.a;
// calc fragment lighting with color
color = perFragmentLighting(color);
// aply fragment color
gl_FragColor = vec4(color.rgb, alpha);
}
void main() {
// this one does not help
//getColor_ByRayCast_NoBound_UsingModelCoordinates();
// this is the one to use
getColor_ByRayCast_NoBound_UsingEyeCoordinates();
//gl_FragColor = vec4(texture2D(uNoiseTexture, vECPosition.xz).rgb, 1.0);
}