-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubo_instanced.js
186 lines (124 loc) · 4.33 KB
/
ubo_instanced.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Instanced rendering + UBO test
*/
var canvas = null;
var gl = null;
var mvp,proj,model,transformLoc,viewportSizeLoc, transformUBO;
var transformBuffer;
const NUM_INSTANCES = 1024;//maximum
function init() {
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
gl = canvas.getContext('webgl2', { antialias: false });
var isWebGL2 = !!gl;
if (isWebGL2 === false) {
console.error("WebGL2 context not supported");
return;
}
const quad_geom = new Float32Array( [
0.5, 0.5, 0.0,
-0.5, 0.5, 0.0,
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0
]);
const quad_indices = new Int8Array([0, 1, 2, 0, 2, 3]);
const vert = [
"#version 300 es",
"#define MAX_TRANSFORMS 1024",
"precision highp float; ",
"precision highp int;",
"layout(std140) uniform Transform {",
"mat4 mvp[MAX_TRANSFORMS];",
"}transform;",
"layout(location = 0) in vec3 pos;",
"void main(){",
"gl_Position = transform.mvp[gl_InstanceID] * vec4(pos,1.0);",
"}"
].join('\n');
const frag = [
"#version 300 es",
"precision highp float; ",
"precision highp int;",
"uniform vec2 viewportSize;",
"out vec4 oColor;",
"void main(){",
"vec2 texel = gl_FragCoord.xy / viewportSize;",
"oColor = vec4(texel.st,(texel.s),1);",
"}"
].join('\n');
var prog = createProgram(gl, vert, frag);
if (!prog == null) {
console.error("failed to create shader prog");
return;
}
var vao = gl.createVertexArray();
gl.bindVertexArray(vao);
var vertBuff = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,vertBuff);
gl.bufferData(gl.ARRAY_BUFFER,quad_geom,gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
var indexBuff = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuff);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, quad_indices, gl.STATIC_DRAW);
//Transform UBO:
//get location and bind
transformLoc = gl.getUniformLocation(prog,'Transform');
gl.uniformBlockBinding(prog,transformLoc,0);
viewportSizeLoc = gl.getUniformLocation(prog, "viewportSize");
transformUBO =gl.createBuffer();
gl.bindBuffer(gl.UNIFORM_BUFFER,transformUBO);
const w = gl.canvas.width;
const h = gl.canvas.height;
//math api ref: http://math.hws.edu/graphicsbook/c7/s1.html#webgl3d.1.2
proj = mat4.perspective( mat4.create(), 33 * 3.14 / 180, w/h, 1, 5000 );
model = mat4.create();
mat4.identity( model );
mvp = mat4.create();
transformBuffer = new Float32Array(NUM_INSTANCES * 16);
updateUBO(transformBuffer);
gl.bufferData(gl.UNIFORM_BUFFER,transformBuffer,gl.DYNAMIC_DRAW);
//gl.bindBuffer(gl.UNIFORM_BUFFER,null); keep it bound for updates
gl.bindBufferBase(gl.UNIFORM_BUFFER,0,transformUBO);
gl.useProgram(prog);
gl.uniform2fv(viewportSizeLoc,[window.innerWidth,window.innerHeight]);
window.addEventListener('resize', onResize);
requestAnimationFrame(render);
}
function updateUBO(buffer)
{
var offset = 0;
for (let index = 0; index < NUM_INSTANCES; index++) {
var x = Math.random() * 800 - 400;
var y = Math.random() * 800 - 400;
mat4.identity( model );
mat4.translate(model,model,[x,y,-2500]);
mat4.scale(model,model,[20,20,1]);
mat4.multiply( mvp, proj, model );
buffer.set(mvp,offset);
offset+=16;
}
}
function onResize()
{
mat4.identity(proj);
const w = window.innerWidth;
const h = window.innerHeight;
canvas.width = w;
canvas.height = h;
gl.viewport(0,0,w,h);
gl.uniform2fv(viewportSizeLoc,[w,h]);
proj = mat4.perspective(proj, 33 * 3.14 / 180, w/h, 1, 5000 );
}
function render()
{
updateUBO(transformBuffer);//update matrices
gl.bufferSubData(gl.UNIFORM_BUFFER,0,transformBuffer);
gl.clearColor(0,0,0,1);
gl.clear(gl.COLOR_BUFFER_BIT);
// gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_BYTE,0);
gl.drawElementsInstanced(gl.TRIANGLES,6,gl.UNSIGNED_BYTE,0,NUM_INSTANCES);
requestAnimationFrame(render);
}