-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglsl-dispersion.glsl
197 lines (153 loc) · 6.34 KB
/
glsl-dispersion.glsl
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
#ifndef PI
#define PI 3.1415926536
#define TWO_PI 6.2831853072
#endif
// #define RGBCMY 1
// #define REFR_INTEGRAL 1
#define HUE 2
#define HUE_NUM 1
// #define COS_HUE 1
#pragma glslify: hsv = require(glsl-hsv2rgb)
#pragma glslify: cnoise3 = require(glsl-noise/classic/3d)
#pragma glslify: snoise3 = require(glsl-noise/simplex/3d)
#pragma glslify: rotationMatrix = require(./rotation-matrix3)
#pragma glslify: hue2IOR = require(./dispersion-ray-direction)
// #pragma glslify: hue2IOR = require(./dispersion/hue-to-ior-exponential)
// #pragma glslify: hue2IOR = require(./dispersion/hue-to-ior-sigmoid)
// #pragma glslify: hue2IOR = require(./dispersion/hue-to-ior-polynomial)
vec3 nsin (in vec3 t) {
return 0.5 + 0.5 * sin(TWO_PI * t);
}
vec3 intRefract (in vec3 l, in vec3 n, in float r1, in float r2) {
float sqrt1minusC = sqrt(1.0 - dot(l, -n));
return 0.5 * (
(r2 * refract(l, n, r2)
- n * asin(sqrt1minusC * r2) / sqrt1minusC)
-
(r1 * refract(l, n, r1)
- n * asin(sqrt1minusC * r1) / sqrt1minusC));
}
vec3 refractColors (in vec3 nor, in vec3 eye, in float n2, in float n1, in vec3 lightColor) {
float between = amount;
float greenIOR = n2;
#ifdef RGBCMY
float redIORRatio = hue2IOR(0.0, greenIOR, n1, between);
float yellowIORRatio = hue2IOR(60.0, greenIOR, n1, between);
float greenIORRatio = hue2IOR(120.0, greenIOR, n1, between);
float cyanIORRatio = hue2IOR(180.0, greenIOR, n1, between);
float blueIORRatio = hue2IOR(240.0, greenIOR, n1, between);
float purpleIORRatio = hue2IOR(270.0, greenIOR, n1, between);
vec3 redRefract = refract(eye, nor, redIORRatio);
vec3 yellowRefract = refract(eye, nor, yellowIORRatio);
vec3 greenRefract = refract(eye, nor, greenIORRatio);
vec3 cyanRefract = refract(eye, nor, cyanIORRatio);
vec3 blueRefract = refract(eye, nor, blueIORRatio);
vec3 purpleRefract = refract(eye, nor, purpleIORRatio);
float r = scene(redRefract, redIORRatio).r * 0.5;
float y = dot(scene(yellowRefract, yellowIORRatio), vec3(2.0, 2.0, -1.0)) / 6.0;
float g = scene(greenRefract, greenIORRatio).g * 0.5;
float c = dot(scene(cyanRefract, cyanIORRatio), vec3(-1.0, 2.0, 2.0)) / 6.0;
float b = scene(blueRefract, blueIORRatio).b * 0.5;
float p = dot(scene(purpleRefract, purpleIORRatio), vec3(2.0, -1.0, 2.0)) / 6.0;
float R = r + (2.0*p + 2.0*y - c)/3.0;
float G = g + (2.0*y + 2.0*c - p)/3.0;
float B = b + (2.0*c + 2.0*p - y)/3.0;
#else
#ifdef HUE
const float hueStep = 1.0 / float(HUE_NUM);
vec3 color = vec3(0.);
vec3 variation = vec3(0);
for (int i = 0; i < HUE_NUM; i++) {
float hue = float(i) * hueStep;
float ior = hue2IOR(360.0 * hue, greenIOR, n1, between);
// variation += pow(0.5, float(i)) * cos(pow(2.0, float(i)) * variation + 0.1 * eye.yzx);
vec3 iorRefract = refract(eye + 0.1 * variation, nor, ior);
#ifdef COS_HUE
color += (0.5 + 0.5 * cos(TWO_PI * (hue + vec3(0, 0.33, 0.67)))) * scene(iorRefract, ior);
// Red / Teal
// color += (vec3(0.8, 0.5, 0.4) + vec3(0.2, 0.4, 0.2) * cos(TWO_PI * (vec3(2, 1, 0) * hue + vec3(0, 0.25, 0.25)))) * scene(iorRefract, ior);
// Simple
// color += (0.5 + 0.5 * cos(TWO_PI * (hue + vec3(0, 0.1, 0.2)))) * scene(iorRefract, ior);
// Simple 2
// color += (0.5 + 0.5 * cos(TWO_PI * (vec3(1.5) * hue + vec3(0, 0.1, 0.2)))) * scene(iorRefract, ior);
// Neon
// color += (0.5 + 0.5 * cos(TWO_PI * (vec3(2, 1, 0) * hue + vec3(0.5, 0.2, 0.25)))) * scene(iorRefract, ior);
// Something 1
// color += (vec3(0.5, 0.3, 0.5) + vec3(0.25, 0.5, 0.7) * cos(TWO_PI * (vec3(2, 1, 0.5) * hue + vec3(0.6, 0.1, 0.25)))) * scene(iorRefract, ior);
// RED CYAN
// color += mix(#FF0000, #00FFFF, hue) * scene(iorRefract, ior);
#else
// -- Get Hue for given ior --
vec3 thisColor = vec3(0);
// Index into Cosine palette
vec3 dI = vec3(dot(nor, eye));
vec3 mixI = vec3(dI);
// vec3 mixI = clamp(0.5 + 0.5 * sin(1.0 * dI + nor + vec3(0., 0.33, 0.67)), 0.0, 1.0);
mixI += 0.2 * hue;
// Cosine Palette based Hue
vec3 cosOffset = vec3(0, 0.33, 0.67);
// thisColor = vec3(1);
thisColor = 0.5 + 0.5 * cos(TWO_PI * (mixI + cosOffset));
// Secondary cosine palette warp
thisColor += 0.5 + 0.5 * cos(TWO_PI * (nor + eye + cosOffset - 0.2));
// thisColor *= 0.5;
// // HSV based hue
// thisColor += hsv(vec3(hue, 1.0, 1.0));
// // Composite color
// thisColor += #BBBB44 * (0.5 + 0.5 * sin(nor));
// thisColor += #44BBBB * (0.5 + 0.5 * sin(eye));
// thisColor += #BB44BB * (0.5 + 0.5 * sin(PI * dot(eye, nor)));
// thisColor *= 0.7;
// -- Apply Scene Coloring --
vec3 sceneResult = scene(iorRefract, ior);
thisColor *= sceneResult;
// color = thisColor;
color += thisColor;
#endif
}
color *= pow(hueStep, 1.0);
float R = color.r;
float G = color.g;
float B = color.b;
#else
#ifdef REFR_INTEGRAL
float r1 = hue2IOR(0.0, greenIOR, n1, between);
float r2 = hue2IOR(240.0, greenIOR, n1, between);
// float r1 = (n2 - amount) / n1;
// float r2 = (n2 + amount) / n1;
vec3 intRefracted = intRefract(eye, nor, r1, r2);
// intRefracted = 0.5 + 0.5 * cos(TWO_PI * (intRefracted + vec3(0, 0.33, 0.67)));
// intRefracted = 0.5 + 0.5 * cos(intRefracted);
vec3 color = scene(intRefracted, r1);
// vec3 color = intRefracted;
float R = color.r;
float G = color.g;
float B = color.b;
#else
float redIORRatio = hue2IOR(0.0, greenIOR, n1, between);
float greenIORRatio = hue2IOR(120.0, greenIOR, n1, between);
float blueIORRatio = hue2IOR(240.0, greenIOR, n1, between);
vec3 redRefract = refract(eye, nor, redIORRatio);
vec3 greenRefract = refract(eye, nor, greenIORRatio);
vec3 blueRefract = refract(eye, nor, blueIORRatio);
float r = scene(redRefract, redIORRatio).r;
float g = scene(greenRefract, greenIORRatio).g;
float b = scene(blueRefract, blueIORRatio).b;
float R = r;
float G = g;
float B = b;
#endif
#endif
#endif
return vec3(R, G, B) * lightColor;
}
vec3 refractColors (in vec3 nor, in vec3 eye, in float n2) {
return refractColors(nor, eye, n2, 1., vec3(1.0));
}
vec3 refractColors (in vec3 nor, in vec3 eye, in float n2, in float n1) {
return refractColors(nor, eye, n2, n1, vec3(1.0));
}
vec3 refractColors (in vec3 nor, in vec3 eye, in float n2, in vec3 lightColor) {
return refractColors(nor, eye, n2, 1., lightColor);
}
#pragma glslify: export(refractColors)