-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob3.html
180 lines (150 loc) · 6.13 KB
/
blob3.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Meta blobs - Blob #3. Made by Jose Blanco Perales.">
<meta name="author" content="Jose Blanco Perales">
<meta name="keywords" content="creativeCoding, javascript, glsl, motionDesign">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Meta blobs - Blob #3">
<meta name="twitter:description" content="Meta blobs - Blob #3. Made by Jose Blanco Perales.">
<meta name="twitter:creator" content="@blancoperales">
<meta name="twitter:site" content="@blancoperales">
<meta name="twitter:image" content="https://metablobs.joseblancoperales.com/imgs/big/blob3.png">
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<title>Meta blobs - Blob #3</title>
<link rel="stylesheet" href="blob.css" type="text/css">
</head>
<body>
<div id="panel">
<p>#3</p>
<a href="index.html">
<svg width="35" height="35" version="1.1" viewBox="0 0 13.229166 13.229167" xmlns="http://www.w3.org/2000/svg"><g transform="translate(0 -283.77)"><path transform="matrix(.26458 0 0 .26458 0 283.77)" d="m25 5l-20 15v25h5v-23l15-11 15 11v18h-15v-10h5v-5h-10v15h-5v5h25 5v-25l-20-15z" style="paint-order:normal"/></g></svg>
</a>
<a href="https://github.com/jblanper/metablobs" target="_blank">
<svg width="35" height="35" version="1.1" viewBox="0 0 13.229166 13.229167" xmlns="http://www.w3.org/2000/svg"><g transform="translate(0 -283.77)"><path d="m6.6208 286.68-1.2972 7.4083h1.3656l1.2968-7.4083z" style="paint-order:normal"/><path d="m4.433 287.69c-1.3008 0.75108-2.6017 1.502-3.9026 2.2531v0.58497c-0.013266 0.11065 0.018061 0.24834 0.004134 0.30954 1.2995 0.75025 2.599 1.5003 3.8985 2.2505v-1.4769c-0.70558-0.40737-1.4111-0.81477-2.1167-1.2222 0.70558-0.40737 1.4111-0.81478 2.1167-1.2221z" style="paint-order:normal"/><path d="m8.794 287.69c1.3008 0.75108 2.6017 1.502 3.9026 2.2531v0.58497c0.01327 0.11065-0.01806 0.24834-0.0041 0.30954-1.2995 0.75025-2.599 1.5003-3.8985 2.2505v-1.4769c0.70558-0.40737 1.4111-0.81477 2.1167-1.2222-0.70558-0.40737-1.4111-0.81478-2.1167-1.2221z" style="paint-order:normal"/></g></svg>
</a>
</div>
<canvas></canvas>
<script id="vs" type="notjs">
attribute vec4 position;
void main() {
gl_Position = position;
}
</script>
<script id="fs" type="notjs">
//precision mediump float;
precision highp float;
uniform float time;
uniform vec2 resolution;
#define MAX_STEPS 100
#define MAX_DIST 60.
#define EPSILON .001
mat2 rotate(float a) {
return mat2(cos(a), -sin(a), sin(a), cos(a));
}
float sphereSDF (vec3 p, float r) {
return length(p) - r;
}
float sceneSDF (vec3 p) {
float s = length(p - vec3(0.)) - .8;
s += sin(length((fract(p * 10.) - .5) * (sin(time * .4) * 3. + 4.2) * .2) * (cos(time * .1) * 1. + 2.) - time) * sin(time * .3) * .05;
s += sin((p.x * p.y + time * .08) * 10. * sin(time * .01) * 2.5) * .08;
return s * .8;
}
vec3 getNormal (vec3 p) {
float d = sceneSDF(p);
vec2 e = vec2(.01, 0.);
return normalize(d - vec3(
sceneSDF(p - e.xyy),
sceneSDF(p - e.yxy),
sceneSDF(p - e.yyx)));
}
float raymarch (vec3 ro, vec3 rd) {
float depth = 0.;
for (int i = 0; i < MAX_STEPS; i++) {
float dist = sceneSDF(ro + rd * depth);
if (dist < EPSILON) return depth;
depth += dist;
if (depth >= MAX_DIST) return 0.;
}
return 0.;
}
struct Material {
float ambient;
float diffuse;
float specular;
};
float getLight (vec3 lightPos, vec3 p, vec3 rd, float lightOcclusion, Material material) {
// https://www.shadertoy.com/view/ll2GW1
vec3 light = normalize(lightPos - p);
vec3 normal = getNormal(p);
// phong reflection
float ambient = clamp(.5 + .5 * normal.y, 0., 1.);
float diffuse = clamp(dot(normal, light), 0., 1.);
vec3 half_way = normalize(-rd + light);
float specular = pow(clamp(dot(half_way, normal), 0.0, 1.0), 16.);
return (ambient * material.ambient * lightOcclusion) +
(diffuse * material.diffuse * lightOcclusion) +
(diffuse * specular * material.specular * lightOcclusion);
}
vec3 getRayDir (vec2 uv, vec3 rayOrigin, vec3 lookat, float zoom) {
vec3 forward = normalize(lookat - rayOrigin);
vec3 right = normalize(cross(vec3(0., 1., 0.), forward));
vec3 up = cross(forward, right);
vec3 center = rayOrigin + forward * zoom;
vec3 intersection = center + uv.x * right + uv.y * up;
return normalize(intersection - rayOrigin);
}
void main (void) {
vec2 uv = (gl_FragCoord.xy / resolution.xy) * 2.0 - 1.0;
uv.x *= resolution.x / resolution.y;
// camera
vec3 ro = vec3(0., 0., -5.);
ro.xz *= rotate(time * .1);
vec3 rd = getRayDir(uv, ro, vec3(0., 0., 0.), 3.);
vec3 color = vec3(0.);
float d = raymarch(ro, rd);
vec3 p = ro + rd * d;
if (d > 0.) {
vec3 lightPos1 = vec3(8., 8., -5.);
vec3 lightPos2 = vec3(-12., -8., -10.);
lightPos2.xz *= rotate(time * .5);
float phong1 = getLight(lightPos1, p, rd, 1., Material(.1, .5, .7));
float phong2 = getLight(lightPos2, p, rd, .7, Material(.1, .6, 0.));
color += phong1;
color += phong2;
} else {
color += .05 / length(uv);
}
// gamma correction
color = pow(color, vec3(1.0/2.2));
gl_FragColor = vec4(color, 1.);
}
</script>
<script src="twgl.min.js"></script>
<script>
const gl = document.querySelector("canvas").getContext("webgl");
const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
const arrays = {
position: [-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0],
};
const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
function render(time) {
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
const uniforms = {
time: time * 0.001,
resolution: [gl.canvas.width, gl.canvas.height],
};
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, bufferInfo);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
</script>
</body>
</html>