-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregl-test.js
130 lines (116 loc) · 2.96 KB
/
regl-test.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
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
const canvasSketch = require('canvas-sketch');
const mat4 = require('gl-mat4');
const createRegl = require('regl');
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: 'webgl',
// Turn on MSAA
attributes: { antialias: true }
};
const sketch = ({ gl }) => {
const regl = createRegl({ gl });
// Setup REGL with our canvas context
const eye = [0, 0, 0];
const target = [0, 0, 0];
const view = mat4.lookAt(
[],
eye,
target,
[0, 1, 0]
);
const projection = mat4.perspective(
[],
Math.PI / 4,
regl.context('viewportWidth') / regl.context('viewportHeight'),
0.01,
1000.0
);
const drawTriangle = regl({
vert: `
precision mediump float;
uniform mat4 projection, view, model;
attribute vec3 position;
varying vec3 pos;
void main() {
pos = position;
gl_Position = projection * view * model * vec4(position, 1);
}
`,
frag: `
precision mediump float;
uniform vec4 color;
varying vec3 pos;
void main() {
gl_FragColor = mix(vec4(pos, 1.0), color, 0.7);
}
`,
attributes: {
position: [
[-0.5, +0.5, +0.5], [+0.5, +0.5, +0.5], [+0.5, -0.5, +0.5], [-0.5, -0.5, +0.5], // positive z face.
[+0.5, +0.5, +0.5], [+0.5, +0.5, -0.5], [+0.5, -0.5, -0.5], [+0.5, -0.5, +0.5], // positive x face
[+0.5, +0.5, -0.5], [-0.5, +0.5, -0.5], [-0.5, -0.5, -0.5], [+0.5, -0.5, -0.5], // negative z face
[-0.5, +0.5, -0.5], [-0.5, +0.5, +0.5], [-0.5, -0.5, +0.5], [-0.5, -0.5, -0.5], // negative x face.
[-0.5, +0.5, -0.5], [+0.5, +0.5, -0.5], [+0.5, +0.5, +0.5], [-0.5, +0.5, +0.5], // top face
[-0.5, -0.5, -0.5], [+0.5, -0.5, -0.5], [+0.5, -0.5, +0.5], [-0.5, -0.5, +0.5] // bottom face
],
},
uniforms: {
color: [1, 1, 1, 1],
view: ({tick}) => {
const t = 0.01 * tick
return mat4.lookAt(
[],
[5, 5, 5],
[0, 0.0, 0],
[0, 1, 0]
)
},
projection: ({viewportWidth, viewportHeight}) => mat4.perspective(
[],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
100
),
model: ({tick}) => {
const model = [];
const t = tick * 0.01;
mat4.identity(model);
mat4.fromYRotation(model, t * t);
mat4.translate(
model,
model,
[
2 * Math.sin(t),
1 * Math.cos(t),
0.5 * Math.sin(t),
]
);
return model;
},
},
count: 4*6,
});
// Regl GL draw commands
// ...
// Return the renderer function
return {
render ({ time }) {
// Update regl sizes
regl.poll();
// Clear back buffer
regl.clear({
color: [ 0, 0, 0, 1 ]
});
// Draw meshes to scene
drawTriangle();
},
unload () {
// Unload sketch for hot reloading
regl.destroy();
}
}
};
canvasSketch(sketch, settings);