-
Notifications
You must be signed in to change notification settings - Fork 4
/
bluecyl.js
61 lines (61 loc) · 1.6 KB
/
bluecyl.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
const regl = require('regl')()
const mat4 = require('gl-mat4')
var rmat = []
const cyl = require('./bits/implicitcyl.js')
const normals = require('angle-normals')
const camera = require('./bits/camera.js')(regl, {
center: [0, 0, 0]
})
const drawcyl = 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), p.y, r*sin(theta)) +
vnormal*(1.0+cos(40.0*t+p.y));
}
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: cyl.positions,
normal: normals(cyl.cells, cyl.positions)
},
elements: cyl.cells,
uniforms: {
t: function(context, props){
return context.tick/1000
},
model: function(context, props){
var theta = context.tick/60
return mat4.rotateY(rmat, mat4.identity(rmat), theta)
}
},
primitive: "line loop"
})
regl.frame(() => {
regl.clear({
color: [0, 0, 0, 1]
})
camera(() => {
drawcyl()
})
})