forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance-triangle.js
105 lines (85 loc) · 2.26 KB
/
instance-triangle.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
/*
tags: instancing, basic
<p> In this example, it is shown how you can draw a bunch of triangles using the
instancing feature of regl. </p>
*/
const regl = require('../regl')({ extensions: ['angle_instanced_arrays'] })
var N = 10 // N triangles on the width, N triangles on the height.
var angle = []
for (var i = 0; i < N * N; i++) {
// generate random initial angle.
angle[i] = Math.random() * (2 * Math.PI)
}
// This buffer stores the angles of all
// the instanced triangles.
const angleBuffer = regl.buffer({
length: angle.length * 4,
type: 'float',
usage: 'dynamic'
})
const draw = regl({
frag: `
precision mediump float;
varying vec3 vColor;
void main() {
gl_FragColor = vec4(vColor, 1.0);
}`,
vert: `
precision mediump float;
attribute vec2 position;
// These three are instanced attributes.
attribute vec3 color;
attribute vec2 offset;
attribute float angle;
varying vec3 vColor;
void main() {
gl_Position = vec4(
cos(angle) * position.x + sin(angle) * position.y + offset.x,
-sin(angle) * position.x + cos(angle) * position.y + offset.y, 0, 1);
vColor = color;
}`,
attributes: {
position: [[0.0, -0.05], [-0.05, 0.0], [0.05, 0.05]],
offset: {
buffer: regl.buffer(
Array(N * N).fill().map((_, i) => {
var x = -1 + 2 * Math.floor(i / N) / N + 0.1
var y = -1 + 2 * (i % N) / N + 0.1
return [x, y]
})),
divisor: 1 // one separate offset for every triangle.
},
color: {
buffer: regl.buffer(
Array(N * N).fill().map((_, i) => {
var r = Math.floor(i / N) / N
var g = (i % N) / N
return [r, g, r * g + 0.2]
})),
divisor: 1 // one separate color for every triangle
},
angle: {
buffer: angleBuffer,
divisor: 1 // one separate angle for every triangle
}
},
depth: {
enable: false
},
// Every triangle is just three vertices.
// However, every such triangle are drawn N * N times,
// through instancing.
count: 3,
instances: N * N
})
regl.frame(function () {
regl.clear({
color: [0, 0, 0, 1]
})
// rotate the triangles every frame.
for (var i = 0; i < N * N; i++) {
angle[i] += 0.01
}
angleBuffer.subdata(angle)
draw()
})