Skip to content

Commit 359f2ee

Browse files
author
Andrew Adamson
committed
Adding instanced drawing example code and asset
1 parent abba4d5 commit 359f2ee

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

13.instanceddrawing.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// tslint:disable: no-console
2+
const vertexShaderSrc = `#version 300 es
3+
#pragma vscode_glsllint_stage: vert
4+
5+
layout(location=0) in vec4 aPosition;
6+
layout(location=1) in vec3 aOffset;
7+
layout(location=2) in float aScale;
8+
layout(location=3) in vec4 aColor;
9+
layout(location=4) in vec2 aTexCoord;
10+
layout(location=5) in float aDepth;
11+
12+
out vec2 vTexCoord;
13+
out float vDepth;
14+
15+
void main()
16+
{
17+
vTexCoord = aTexCoord;
18+
vDepth = aDepth;
19+
gl_Position = vec4(aPosition.xyz * aScale + aOffset, 1.0);
20+
}`;
21+
22+
const fragmentShaderSrc = `#version 300 es
23+
#pragma vscode_glsllint_stage: frag
24+
25+
precision mediump float;
26+
27+
uniform mediump sampler2DArray uSampler;
28+
29+
in vec2 vTexCoord;
30+
in float vDepth;
31+
out vec4 fragColor;
32+
33+
void main()
34+
{
35+
fragColor = texture(uSampler, vec3(vTexCoord, vDepth));
36+
}`;
37+
38+
const gl = document.querySelector('canvas').getContext('webgl2');
39+
40+
const program = gl.createProgram();
41+
42+
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
43+
gl.shaderSource(vertexShader, vertexShaderSrc);
44+
gl.compileShader(vertexShader);
45+
gl.attachShader(program, vertexShader);
46+
47+
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
48+
gl.shaderSource(fragmentShader, fragmentShaderSrc);
49+
gl.compileShader(fragmentShader);
50+
gl.attachShader(program, fragmentShader);
51+
52+
gl.linkProgram(program);
53+
54+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
55+
console.log(gl.getShaderInfoLog(vertexShader));
56+
console.log(gl.getShaderInfoLog(fragmentShader));
57+
}
58+
gl.useProgram(program);
59+
60+
const loadImage = () => new Promise(resolve => {
61+
const image = new Image();
62+
image.src = './kittenstacked.png';
63+
image.addEventListener('load', () => resolve(image));
64+
});
65+
const modelData = new Float32Array([
66+
// position texCoord
67+
-1,-.7, 0,1,
68+
0,.8, .5,0,
69+
1,-.7, 1,1,
70+
]);
71+
const transformData = new Float32Array([
72+
// offset scale color depth
73+
-.2,.7, .7, 1,0,0, 1,
74+
.3,-.5, .4, 0,0,1, 0,
75+
]);
76+
77+
const main = async () => {
78+
const image = await loadImage();
79+
const texture = gl.createTexture();
80+
gl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);
81+
gl.texImage3D(gl.TEXTURE_2D_ARRAY, 0, gl.RGBA, 256,256,2, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
82+
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
83+
84+
const modelBuffer = gl.createBuffer();
85+
gl.bindBuffer(gl.ARRAY_BUFFER, modelBuffer);
86+
gl.bufferData(gl.ARRAY_BUFFER, modelData, gl.STATIC_DRAW);
87+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
88+
gl.vertexAttribPointer(4, 2, gl.FLOAT, false, 16, 8);
89+
gl.enableVertexAttribArray(0);
90+
gl.enableVertexAttribArray(4);
91+
92+
const transformBuffer = gl.createBuffer();
93+
gl.bindBuffer(gl.ARRAY_BUFFER, transformBuffer);
94+
gl.bufferData(gl.ARRAY_BUFFER, transformData, gl.STATIC_DRAW);
95+
96+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 28, 0);
97+
gl.vertexAttribPointer(2, 1, gl.FLOAT, false, 28, 8);
98+
gl.vertexAttribPointer(3, 3, gl.FLOAT, false, 28, 12);
99+
gl.vertexAttribPointer(5, 1, gl.FLOAT, false, 28, 24);
100+
101+
gl.vertexAttribDivisor(1,1);
102+
gl.vertexAttribDivisor(2,1);
103+
gl.vertexAttribDivisor(3,1);
104+
gl.vertexAttribDivisor(5,1);
105+
106+
gl.enableVertexAttribArray(1);
107+
gl.enableVertexAttribArray(2);
108+
gl.enableVertexAttribArray(3);
109+
gl.enableVertexAttribArray(5);
110+
111+
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 2);
112+
};
113+
114+
main();

assets/kittenstacked.png

173 KB
Loading

0 commit comments

Comments
 (0)