-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob1.html
188 lines (154 loc) · 6.47 KB
/
blob1.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
181
182
183
184
185
186
187
188
<!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 #1. 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 #1">
<meta name="twitter:description" content="Meta blobs - Blob #1. 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/blob1.png">
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<title>Meta blobs - Blob #1</title>
<link rel="stylesheet" href="blob.css" type="text/css">
</head>
<body>
<div id="panel">
<p>#1</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 250
#define MAX_DIST 30.
#define EPSILON .001
mat2 rotate(float a) {
return mat2(cos(a), -sin(a), sin(a), cos(a));
}
float sphereSDF (vec3 p, vec3 c, float r) {
return length(c - p) - r;
}
float cubeSDF (vec3 p, vec3 c, vec3 dimensions, float borderRoundness) {
vec3 pos = abs(c - p) - dimensions;
return length(max(pos, 0.)) - borderRoundness + min(max(pos.x, max(pos.y, pos.z)), 0.);
}
float sceneSDF (vec3 p) {
float s1 = sphereSDF(p, vec3(0.), 1.);
s1 += sin(cubeSDF(fract(p * 8. + cos(time * .08)) - .5, vec3(0.), vec3(4.), 1. * sin(time * .05)) * 8. + (sin(time * .3) * .5 + .6)) * .02;
s1 += cos(dot(p.xy * 3., p.yx * 2.) * (sin(time * .5) * 2. + 3.)) * .002;
s1 += abs(max(p.z * p.y, 0.)) * 2.8;
s1 += (sin(p.x * 2. + dot(p.xy, p.yz) - (time * .5)) - cos(p.z + p.x * 2. - (time * .2))) * .2;
return s1 * .4;
}
float raymarch (vec3 ro, vec3 rd) {
float depth = 0.;
for (int i = 0; i < MAX_STEPS; i++) {
vec3 ray = ro + rd * depth;
float dist = sceneSDF(ray);
if (dist < EPSILON) break;
depth += dist;
if (depth >= MAX_DIST) break;
}
return depth;
}
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)));
}
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);
}
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 * .2);
vec3 rd = normalize(vec3(uv.x, uv.y, 3.));
rd.xz *= rotate(time * .2);
vec3 color = vec3(0.);
float d = raymarch(ro, rd);
vec3 ray = ro + rd * d;
if (d < MAX_DIST) {
vec3 light1 = vec3(2., 2., -4.);
light1.xz *= rotate(time);
float phong1 = getLight(light1, ray, rd, .8, Material(.7, .7, 1.2));
color += phong1;
vec3 light2 = vec3(5., 5., -8.);
light2.yz *= rotate(time);
float phong2 = getLight(light2, ray, rd, .5, Material(.3, .4, .4));
color += phong2;
color = mix(color, vec3(1.), .2);
color += mix(color, vec3(uv.y * .4, ray.y * .2, .3), .5);
color *= mix(color, vec3(uv.y * .4, ray.y * .2, .3), .8);
} else {
color = 1. - mix(color, vec3(1.), step(.8, max(abs(uv.x), abs(uv.y))));
color *= vec3(uv.y * .4, uv.y * .2, .3) * sin(max(abs(uv.x), abs(uv.y)) * 100. + time) * 5.;
}
color += vec3(1. - length(uv) * 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>