forked from regl-project/regl
/
benchmark.instances.ts
98 lines (75 loc) · 2.03 KB
/
benchmark.instances.ts
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
import { IAttributeBuffer, PipeGL, TAttribute, TUniform } from "../../src";
//准备N*N个数据
const N = 5;
const offset: number[][] = [], color: number[][] = [], angle: number[] = [];
for (let i = 0, len = N * N; i < len; i++) {
const x = -1 + 2 * Math.floor(i / N) / N + 0.1, y = -1 + 2 * (i % N) / N + 0.1;
offset.push([x, y]);
const r = Math.floor(i / N) / N, g = (i % N) / N, b = r * g + 0.2;
color.push(([r, g, b]));
const a = Math.random() * (2 * Math.PI);
angle[i] = a;
}
const RADIUS = 700;
const pipegl0 = new PipeGL({
width: 700,
height: 700,
extensions: ['ANGLE_instanced_arrays']
});
interface Attribute extends TAttribute {
position: number[][];
offset: IAttributeBuffer,
color: IAttributeBuffer,
angle: IAttributeBuffer,
}
const oBuf = pipegl0.buffer(angle);
const draw0 = pipegl0.compile<Attribute, TUniform>({
vert: `precision mediump float;
attribute vec2 position;
attribute vec3 color;
attribute vec2 offset;
attribute float angle;
varying vec3 vColor;
void main(){
vColor=color;
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);
}`,
frag: `precision mediump float;
varying vec3 vColor;
void main(){
gl_FragColor = vec4(vColor,1.0);
}`,
attributes: {
position: [[0.0, -0.05], [-0.05, 0.0], [0.05, 0.05]],
color: {
buffer: pipegl0.buffer(color),
divisor: 1,
},
offset: {
buffer: pipegl0.buffer(offset),
divisor: 1,
},
angle: {
buffer: oBuf,
divisor: 1,
}
},
status: {
DEPTH_TEST: true,
BLEND: true,
},
count: 3,
instances: N * N,
});
const anim = () => {
for (let i = 0; i < N * N; i++)
angle[i] += 0.01;
oBuf.subData(angle);
draw0.draw();
requestAnimationFrame(anim);
}
anim();