forked from scriptfoundry/WebGL2-Videos-Materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.atlases.js
150 lines (119 loc) · 3.71 KB
/
11.atlases.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
// tslint:disable: no-console
const vertexShaderSrc = `#version 300 es
#pragma vscode_glsllint_stage: vert
layout(location=0) in vec4 aPosition;
layout(location=1) in vec2 aTexCoord;
out vec2 vTexCoord;
void main()
{
vTexCoord = aTexCoord;
gl_Position = aPosition;
}`;
const fragmentShaderSrc = `#version 300 es
#pragma vscode_glsllint_stage: frag
precision mediump float;
uniform sampler2D uSampler;
in vec2 vTexCoord;
out vec4 fragColor;
void main()
{
fragColor = texture(uSampler, vTexCoord);
}`;
const gl = document.querySelector('canvas').getContext('webgl2');
const program = gl.createProgram();
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, vertexShaderSrc);
gl.compileShader(vertexShader);
gl.attachShader(program, vertexShader);
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, fragmentShaderSrc);
gl.compileShader(fragmentShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log(gl.getShaderInfoLog(vertexShader));
console.log(gl.getShaderInfoLog(fragmentShader));
}
gl.useProgram(program);
const positionData = new Float32Array([
// Quad 1
-1,0,
0,1,
-1,1,
-1,0,
0,0,
0,1,
// Quad 2
0,0,
1,1,
0,1,
0,0,
1,0,
1,1,
// Quad 3
-1,-1,
0,0,
-1,0,
-1,-1,
0,-1,
0,0,
// Quad 4
0,-1,
1,0,
0,0,
0,-1,
1,-1,
1,0,
]);
const loadAtlas = () => new Promise(resolve => {
const image = new Image();
image.src = './assets/atlas.png';
image.addEventListener('load', () => resolve(image));
});
const createUVLookup = async () => {
const file = await fetch('./assets/atlas.json');
const data = await file.json();
const w = 128 / 1024;
const h = 128 / 2048;
const hPadding = .25 / 1024;
const vPadding = .25 / 2048;
return (name) => {
if (!data[name]) return null;
const [u,v] = data[name];
return [
u + hPadding, v - vPadding + h,
u - hPadding + w, v + vPadding,
u + hPadding, v + vPadding,
u + hPadding, v - vPadding + h,
u - hPadding + w, v - vPadding + h,
u - hPadding + w, v + vPadding,
];
};
};
const main = async () => {
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positionData, gl.STATIC_DRAW);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
const texCoordData = new Float32Array(2 * 4 * 6);
const texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, texCoordData.byteLength, gl.DYNAMIC_DRAW);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
const image = await loadAtlas();
const getUVs = await createUVLookup();
texCoordData.set(getUVs('medievalTile_04'), 0);
texCoordData.set(getUVs('medievalTile_06'), 12);
texCoordData.set(getUVs('medievalTile_58'), 24);
texCoordData.set(getUVs('medievalTile_31'), 36);
gl.bufferSubData(gl.ARRAY_BUFFER, 0, texCoordData);
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1024, 2048, 0, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.drawArrays(gl.TRIANGLES, 0, 24);
};
main();