-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob8.html
160 lines (130 loc) · 5.26 KB
/
blob8.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
<!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 #8. 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 #8">
<meta name="twitter:description" content="Meta blobs - Blob #8. 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/blob8.png">
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<title>Meta blobs - Blob #8</title>
<link rel="stylesheet" href="blob.css" type="text/css">
</head>
<body>
<div id="panel">
<p>#8</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;
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 displacement(vec3 p, float n) {
return sin(p.x * n) * cos(p.y * n + n) - sin(p.z + p.y * n);
}
float map (vec3 p) {
vec3 p1 = p;
p1.xz *= rotate(time * .3);
p1.yz *= rotate(time * .2);
float s1 = sphereSDF(p1, .6);
s1 += sin((p1.x + p1.y * p1.z) * (3.14 * 7.) - time * 1.5) * .025 - sin((p1.y + p1.y * p1.z) * (3.14 * 20.) - time) * .01;
p.z -= time * .2;
vec3 c = vec3(1.4, 1.4, 2.);
p = mod(p, c) - .5 * c;
float s2 = sphereSDF(p, .3);
s2 += sin((p.x + p.y * p.z) * 20. + time * 2.) * .03 + cos(length(p.x - p.y * p.z) * 65. - time) * .01;
return min(s1 * .28, s2 * .25);
}
float trace (vec3 ro, vec3 rd) {
float e = .0001;
float d = e * 2.;
float t = 0.;
for (int i = 0; i < 60; i++) {
if (d < e || t > 50.) break;
d = map(ro + rd * t);
t += d;
}
return t;
}
vec3 getNormal (vec3 p) {
float d = map(p);
vec2 e = vec2(.001, 0.);
return normalize(d - vec3(
map(p - e.xyy),
0.,
//map(p - e.xxy)));
map(p - e.yyx)));
}
void main (void) {
vec2 uv = (gl_FragCoord.xy / resolution.xy) * 2.0 - 1.0;
uv.x *= resolution.x / resolution.y;
vec3 color;
// camera
vec3 ro = vec3(0., 0., 3.);
vec3 rd = normalize(vec3(uv, -2.5));
float t = trace(ro, rd);
vec3 p = ro + rd * t;
// lighting
vec3 light = normalize(vec3(3., 2., 1.));
vec3 nor = getNormal(p);
float lOcclusion = .8;
float ambient = clamp(.5 + .5 * nor.y, 0., 1.) * .3;
float diffuse = clamp(dot(nor, light), 0., 1.) * .8;
vec3 half_way = normalize(-rd + light);
float specular = pow(clamp(dot(half_way, nor), 0.0, 1.0), 16.) * diffuse * .5;
color += (ambient + diffuse + specular) * lOcclusion;
float fog = 1. / (1. + t * t * .1);
color *= fog * 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>