-
Notifications
You must be signed in to change notification settings - Fork 2
/
connecting_cubes_shader.glsl
461 lines (399 loc) · 14.1 KB
/
connecting_cubes_shader.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
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
// Code adapted from http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/
// TouchDesigner version by exsstas https://github.com/exsstas/Raymarching-in-TD
layout (location = 0) out vec4 fragColor;
uniform vec2 uRes; // GLSL TOP resolution
// raymarcher parameters
uniform int uSteps; // the max steps before giving up
uniform float uMinDist; // the starting distance away from the eye
uniform float uMaxDist; // the max distance away from the eye to march before giving up
// scene parameters
uniform int uNum; // number of primitives in array
uniform samplerBuffer uSizes;
uniform samplerBuffer uCenters;
uniform float uSmoothK; // smooth distance for blending primitives
uniform vec4 uPlane; // XYZ position and size of a plane
uniform float uFrameWidth = 0.5;
uniform float uFrameSmooth = 0.1;
// Camera and color parameters
uniform vec4 uCamera; // XYZ position + Field of view for a camera
uniform vec3 uLookAt;
uniform vec3 uLight1Pos; // Light position
uniform vec3 uLight1Col; // Light color
uniform vec3 uAmbient;
uniform vec3 uDiffuse;
uniform vec3 uSpecular;
uniform float uShine; // Shininess coefficient
uniform vec3 uAO; // Ambient Occlusion intencity
uniform vec4 uShadow; // Soft shadow http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
//------------------------------------------------------------
// SDF functions - add below all primitives and blending functions you need
// http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
//------------------------------------------------------------
float sdSphere( vec3 p, float s )
{
return length(p)-s;
}
float sdPlane( vec3 p, vec4 n )
{
// n must be normalized
return dot( p, n.xyz ) + n.w;
}
float sdBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
float sdRoundBox( vec3 p, vec3 b, float r )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r;
}
float sdCapsule( vec3 p,
vec3 a, // offset of end a from center?
vec3 b, // offset of end b from center?
float r // radius
)
{
vec3 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
float opSmoothUnion( float d1, float d2, float k) {
float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
return mix( d2, d1, h ) - k*h*(1.0-h); }
float opSmoothSubtraction( float d1, float d2, float k ) {
float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
return mix( d2, -d1, h ) + k*h*(1.0-h); }
float opUnion( float d1, float d2 ) { return min(d1,d2); }
float opSubtraction( float d1, float d2 ) { return max(-d1,d2); }
float opIntersection( float d1, float d2 ) { return max(d1,d2); }
vec3 opCheapBendPos(in vec3 p, in float k )
{
float c = cos(k*p.x);
float s = sin(k*p.x);
mat2 m = mat2(c,-s,s,c);
return vec3(m*p.xy,p.z);
}
vec3 opTwistPos( in vec3 p, in float k )
{
float c = cos(k*p.y);
float s = sin(k*p.y);
mat2 m = mat2(c,-s,s,c);
return vec3(m*p.xz,p.y);
}
vec3 opRepPos( in vec3 p, in vec3 c )
{
return mod(p+0.5*c,c)-0.5*c;
}
//------------------------------------------------------------
// Describe your scene here
//------------------------------------------------------------
float quadFrameSDF(vec3 p, vec2 size, float thickness) {
// top left-right
float frame = sdCapsule(p,
vec3(size * vec2(-0.5, 0.5), 0),
vec3(size * vec2(0.5, 0.5), 0),
thickness);
// bottom left-right
frame = opSmoothUnion(frame, sdCapsule(p,
vec3(size * vec2(-0.5, -0.5), 0),
vec3(size * vec2(0.5, -0.5), 0),
thickness), uFrameSmooth);
// top-bottom left
frame = opSmoothUnion(frame, sdCapsule(p,
vec3(size * vec2(-0.5, 0.5), 0),
vec3(size * vec2(-0.5, -0.5), 0),
thickness), uFrameSmooth);
// top-bottom right
frame = opSmoothUnion(frame, sdCapsule(p,
vec3(size * vec2(0.5, 0.5), 0),
vec3(size * vec2(0.5, -0.5), 0),
thickness), uFrameSmooth);
return frame;
}
float boxFrameSDF(vec3 p, vec3 size, float thickness) {
// front quad frame
float frame = quadFrameSDF(
p - vec3(0, 0, 0.5 * size.z),
size.xy,
thickness);
// back quad frame
frame = opSmoothUnion(frame, quadFrameSDF(
p + vec3(0, 0, 0.5 * size.z),
size.xy,
thickness), uFrameSmooth);
// top left front-back
frame = opSmoothUnion(frame, sdCapsule(p,
size * vec3(-0.5, 0.5, -0.5),
size * vec3(-0.5, 0.5, 0.5),
thickness), uFrameSmooth);
// top right front-back
frame = opSmoothUnion(frame, sdCapsule(p,
size * vec3(0.5, 0.5, -0.5),
size * vec3(0.5, 0.5, 0.5),
thickness), uFrameSmooth);
// bottom left front-back
frame = opSmoothUnion(frame, sdCapsule(p,
size * vec3(-0.5, -0.5, -0.5),
size * vec3(-0.5, -0.5, 0.5),
thickness), uFrameSmooth);
// bottom right front-back
frame = opSmoothUnion(frame, sdCapsule(p,
size * vec3(0.5, -0.5, -0.5),
size * vec3(0.5, -0.5, 0.5),
thickness), uFrameSmooth);
return frame;
}
float sceneSDFInner(vec3 p)
{
//p = mod(p, vec3(12.0));
float scene = uMaxDist; // for empty start
//float scene = sdPlane(p, uPlane);
// p = opCheapBendPos(p, -0.1);
// p = opTwistPos(p, 0.2);
// p = opRepPos(p, vec3(0.1));
for (int i = 0; i < uNum-1; i++) {
vec3 center = texelFetchBuffer(uCenters, i).xyz;
vec4 sizeThick = texelFetchBuffer(uSizes, i);
vec3 size = sizeThick.xyz;
vec3 adjustedP = p - center;
float frame = boxFrameSDF(p - center, size, uFrameWidth * sizeThick.w);
scene = opSmoothUnion(scene, frame, uSmoothK);
}
return scene;
}
float sceneSDF(vec3 p)
{
float scene;
// scene = sceneSDFInner(p);
// float r = length(p.xz);
// float theta = atan(p.z, p.x);
//// theta *= 2.0;
// theta = mod(theta, radians(60));
//
// theta = cos(theta) * radians(60);
//
//
// p.x = r * cos(theta);
// p.z = r * sin(theta);
// scene = opSmoothUnion(scene, sceneSDFInner(p), uSmoothK);
scene = sceneSDFInner(p);
// scene = opSmoothUnion(scene,sceneSDFInner(
// p * vec3(-1, -1, -1)
// ), uSmoothK);
//
// scene = opSmoothUnion(scene,sceneSDFInner(
// p * vec3(-1, 1, -1)
// ), uSmoothK);
//
// scene = opSmoothUnion(scene,sceneSDFInner(
// p * vec3(1, 1, -1)
// ), uSmoothK);
// theta *= -1.0;
// p.x = r * cos(theta);
// p.z = r * sin(theta);
// scene = opSmoothUnion(scene, sceneSDFInner(p), uSmoothK);
float block = sdBox(p - vec3(0, 0, -16), vec3(30, 20, 8));
scene = opSmoothUnion(scene, block, uSmoothK*8);
return scene;
}
//------------------------------------------------------------
// Distance and direction
//------------------------------------------------------------
/**
* eye: the eye point, acting as the origin of the ray
* marchingDirection: the normalized direction to march in
* start: the starting distance away from the eye
* end: the max distance away from the eye to march before giving up
*/
float shortestDistanceToSurface(vec3 eye, vec3 marchingDirection, float start, float end) {
float depth = start;
for (int i = 0; i < uSteps; i++) {
float dist = sceneSDF(eye + depth * marchingDirection);
if (dist < start) {
return depth;
}
depth += dist;
if (depth >= end) {
return end;
}
}
return end;
}
/**
**
* Return the normalized direction to march in from the eye point for a single pixel.
*
* fieldOfView: vertical field of view in degrees
* size: resolution of the output image
* fragCoord: the x,y coordinate of the pixel in the output image
*/
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) {
vec2 xy = fragCoord - size / 2.0;
float z = size.y / tan(radians(fieldOfView) / 2.0);
return normalize(vec3(xy, -z));
}
//------------------------------------------------------------
// Normals
//------------------------------------------------------------
/**
* Using the gradient of the SDF, estimate the normal on the surface at point p.
*/
vec3 estimateNormal(vec3 p) {
return normalize(vec3(
sceneSDF(vec3(p.x + uMinDist, p.y, p.z)) - sceneSDF(vec3(p.x - uMinDist, p.y, p.z)),
sceneSDF(vec3(p.x, p.y + uMinDist, p.z)) - sceneSDF(vec3(p.x, p.y - uMinDist, p.z)),
sceneSDF(vec3(p.x, p.y, p.z + uMinDist)) - sceneSDF(vec3(p.x, p.y, p.z - uMinDist))
));
}
//------------------------------------------------------------
// Light + coloring + shadows
//------------------------------------------------------------
// compute ambient occlusion value at given position/normal
// Source - https://www.shadertoy.com/view/lsKcDD
float calcAO( in vec3 pos, in vec3 nor )
{
float occ = uAO.x;
float sca = uAO.y;
for( int i=0; i<uAO.z; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = sceneSDF( aopos );
occ += -(dd-hr)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );
}
// Soft shadow code from http://iquilezles.org/www/articles/rmshadows/rmshadows.htm
float softshadow(vec3 p, vec3 eye)
{
float mint = uShadow.x;
float maxt = uShadow.y;
float k = uShadow.z;
float res = 1.0;
float ph = 1e20;
for( float t=mint; t<maxt; )
{
float h = sceneSDF(p + eye*t);
if( h<0.001 )
return 0.0;
float y = h*h/(2.0*ph);
float d = sqrt(h*h-y*y);
res = min( res, k*d/max(0.0,t-y) );
ph = h;
t += h;
}
return res;
}
/**
* Lighting contribution of a single point light source via Phong illumination.
*
* The vec3 returned is the RGB color of the light's contribution.
*
* k_a: Ambient color
* k_d: Diffuse color
* k_s: Specular color
* alpha: Shininess coefficient
* p: position of point being lit
* eye: the position of the camera
* lightPos: the position of the light
* lightIntensity: color/intensity of the light
*
* See https://en.wikipedia.org/wiki/Phong_reflection_model#Description
*/
vec3 phongContribForLight(vec3 k_d, vec3 k_s, float alpha, vec3 p, vec3 eye,
vec3 lightPos, vec3 lightIntensity) {
vec3 N = estimateNormal(p);
vec3 L = normalize(lightPos - p);
vec3 V = normalize(eye - p);
vec3 R = normalize(reflect(-L, N));
float occ = calcAO(p, N); // Ambient occlusion
float dotLN = dot(L, N);
float dotRV = dot(R, V);
if (dotLN < 0.0) {
// Light not visible from this point on the surface, so add no color
return vec3(0.0);
}
if (dotRV < 0.0) {
// Light reflection in opposite direction as viewer, apply only diffuse
// component + AO
return lightIntensity * (k_d * dotLN) *sqrt(occ);
}
return lightIntensity * (k_d * dotLN + k_s * pow(dotRV, alpha)) *sqrt(occ);
}
/**
* Lighting via Phong illumination.
*
* The vec3 returned is the RGB color of that point after lighting is applied.
* k_a: Ambient color
* k_d: Diffuse color
* k_s: Specular color
* alpha: Shininess coefficient
* p: position of point being lit
* eye: the position of the camera
*
* See https://en.wikipedia.org/wiki/Phong_reflection_model#Description
*/
vec3 phongIllumination(vec3 k_a, vec3 k_d, vec3 k_s, float alpha, vec3 p, vec3 eye) {
vec3 ambientLight = 0.5 * vec3(1.0, 1.0, 1.0);
vec3 color = ambientLight * k_a;
color += phongContribForLight(k_d, k_s, alpha, p, eye,
uLight1Pos,
uLight1Col);
float shadow = softshadow( p, normalize(uLight1Pos-p)); // calc grayscale shadow
// // Example of hardcoded (second) light + shadow:
// vec3 light2Pos = vec3(2.0, 5.0, 2.0);
// vec3 light2Col = vec3(0.6, 0.6, 1.0);
// color += phongContribForLight(k_d, k_s, alpha, p, eye,
// light2Pos,
// light2Col);
// shadow *= softshadow( p, normalize(light2Pos-p)); // calc grayscale shadow 2
vec3 cshadow = color * shadow; // multiply shadows with color
color = mix(color, vec3(cshadow), uShadow.w); // mix color+shadows based on uShadow 4th value
return color;
}
//------------------------------------------------------------
// "Look at ..." matrix
//------------------------------------------------------------
/**
* Return a transform matrix that will transform a ray from view space
* to world coordinates, given the eye point, the camera target, and an up vector.
*
* This assumes that the center of the camera is aligned with the negative z axis in
* view space when calculating the ray marching direction. See rayDirection.
*/
mat4 viewMatrix(vec3 eye, vec3 center, vec3 up) {
// Based on gluLookAt man page
vec3 f = normalize(center - eye);
vec3 s = normalize(cross(f, up));
vec3 u = cross(s, f);
return mat4(
vec4(s, 0.0),
vec4(u, 0.0),
vec4(-f, 0.0),
vec4(0.0, 0.0, 0.0, 1)
);
}
//------------------------------------------------------------
// Put everything together
//------------------------------------------------------------
void main()
{
// setting camera(eye)
vec3 viewDir = rayDirection(uCamera.w, uRes.xy, gl_FragCoord.xy);
vec3 eye = uCamera.xyz;
mat4 viewToWorld = viewMatrix(eye, uLookAt, vec3(0.0, 1.0, 0.0));
vec3 worldDir = (viewToWorld * vec4(viewDir, 0.0)).xyz;
float dist = shortestDistanceToSurface(eye, worldDir, uMinDist, uMaxDist);
if (dist > uMaxDist - uMinDist) {
fragColor = vec4(0.0); // Didn't hit anything return transparent background
return;
}
// The closest point on the surface to the eyepoint along the view ray
vec3 p = eye + dist * worldDir;
// coloring (details at line 165)
vec3 color = phongIllumination(uAmbient, uDiffuse, uSpecular, uShine, p, eye);
// alpha set to 1.0, try change it to 0.0 instead:
fragColor = vec4(color, 1.0);
}