-
Notifications
You must be signed in to change notification settings - Fork 4
/
swimmypuff.js
68 lines (62 loc) · 1.63 KB
/
swimmypuff.js
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
const regl = require('regl')()
const mat4 = require('gl-mat4')
var rmat = []
const skelly = require('./bits/skelly.json')
const normals = require('angle-normals')
const camera = require('./bits/camera')(regl, {
center: [0, 2.5, 0]
})
const drawskelly = regl({
frag: `
precision mediump float;
varying vec3 vnormal;
vec3 hsl2rgb(vec3 hsl) {
vec3 rgb = clamp( abs(mod(hsl.x*2.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
return hsl.z - hsl.y * (rgb-0.5)*(3.0-abs(2.0*hsl.y-1.0));
}
void main () {
gl_FragColor = vec4(hsl2rgb(abs(vnormal)), 1.0);
}`,
vert: `
precision mediump float;
uniform mat4 model, projection, view;
attribute vec3 position, normal;
varying vec3 vnormal;
uniform float t;
vec3 warp (vec3 p){
float r = length(p.zx);
float theta = atan(p.z, p.x);
return vec3 (r-cos(theta), r, r*sin(theta)) +
vnormal+(1.0+cos(40.0*t+p.z));
}
void main () {
vnormal = normal;
gl_Position = projection * view * model *
vec4(warp(position), 1.0);
gl_PointSize =
(64.0*(1.0+sin(t*20.0+length(position))))/gl_Position.w;
}`,
attributes: {
position: skelly.positions,
normal: normals(skelly.cells, skelly.positions)
},
elements: skelly.cells,
uniforms: {
t: function(context, props){
return context.tick/1000
},
model: function(context, props){
var theta = context.tick/60
return mat4.rotateX(rmat, mat4.identity(rmat), theta)
}
},
primitive: "triangles"
})
regl.frame(() => {
regl.clear({
color: [0, 0, 0, 1]
})
camera(() => {
drawskelly()
})
})