-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtilemap.js
More file actions
261 lines (225 loc) · 7.23 KB
/
tilemap.js
File metadata and controls
261 lines (225 loc) · 7.23 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
class GLTilemap {
constructor(pos, size, res, img, tilemapData, tilesList,
shad = createShader(GLTilemap.vertShader, GLTilemap.fragShader),
rdr = window, canvas = _renderer) {
{ // Load parameters
this.pos = pos;
this.size = size;
this.res = res;
this.img = img;
this.tilesList = tilesList;
this.tilemapData = tilemapData;
this.shad = shad;
this.img.texture = canvas.getTexture(this.img);
this.rdr = rdr;
this.canvas = canvas;
this.gl = canvas.drawingContext;
}
{ // Load WebGL stuff
// Chrome can't load the texture properly without this for some reason otherwise it complains about the type and format not being valid
this.gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
// Load tilemap texture
this.loadTilemap();
// Load tileset texture
this.loadTiles(tilesList)
this.shad.bindShader();
}
}
loadTilemap() {
let tilemapTex = new Texture.T2D(0, this.gl.R16UI, this.res[0], this.res[1], 1, 0, this.gl.RED_INTEGER, this.gl.UNSIGNED_SHORT, this.tilemapData, this.gl);
tilemapTex.bind();
this.tilemap = new GLPixy(this.pos, this.size, tilemapTex, 1, null, false, false, this.rdr, this.canvas);
this.tilemap.setInterpolation(this.gl.NEAREST, this.gl.NEAREST);
}
loadTiles(tilesList) {
const coordsPerTile = 4;
let tilesData = new Uint16Array(tilesList.length * coordsPerTile);
for (const i in tilesList) {
const tile = tilesList[i];
for (let j = 0; j < tile.length; j++) {
const coord = tile[j];
const MAX_UNSIGNED_SHORT = 2 ** 16 - 1;
const uintCoord = Math.floor(coord * MAX_UNSIGNED_SHORT);
tilesData[i * coordsPerTile + j] = uintCoord;
}
}
let tilesTex = new Texture.T2D(0, this.gl.RGBA16UI, tilesList.length, 1, 2, 0, this.gl.RGBA_INTEGER, this.gl.UNSIGNED_SHORT, tilesData, this.gl);
tilesTex.bind();
this.tiles = new GLPixy([0, 0], [0, 0], tilesTex, 1, null, false, false, this.rdr, this.canvas);
this.tiles.setInterpolation(this.gl.NEAREST, this.gl.NEAREST);
}
display() {
shader(this.shad);
{
const cachedSamplers = this.shad.samplers;
this.shad.samplers = [];
this.gl.activeTexture(this.gl.TEXTURE0);
this.gl.bindTexture(this.gl.TEXTURE_2D, this.img.texture.glTex);
this.shad.setUniform("atlas", 0);
this.tilemap.img.activate();
this.tilemap.img.bind();
this.shad.setUniform("tilemap", 1);
this.tiles.img.activate();
this.tiles.img.bind();
this.shad.setUniform("tiles", 2);
this.rdr.rect(this.tilemap.off[0], this.tilemap.off[1], this.tilemap.size[0], this.tilemap.size[1]);
this.tilemap.img.unbind();
this.shad.samplers = cachedSamplers;
}
resetShader();
}
}
GLTilemap.vertShader = `#version 300 es
in vec3 aPosition;
in vec2 aTexCoord;
out vec2 vTexCoord;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
void main() {
vec4 positionVec4 = vec4(aPosition, 1.0);
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
vTexCoord = aTexCoord;
}
`;
GLTilemap.fragShader = `#version 300 es
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
in vec2 vTexCoord;
uniform sampler2D atlas;
uniform mediump usampler2D tilemap;
uniform mediump usampler2D tiles;
out vec4 fragColor;
void main() {
uint tile = texture(tilemap, vTexCoord).r;
if (tile == uint(0)) {
fragColor = vec4(0.0);
return;
}
const float MAX_UNSIGNED_SHORT = pow(2.0, 16.0) - 1.0;
uvec4 iCoords = texelFetch(tiles, ivec2(tile - uint(1), 0), 0);
vec4 fCoords = vec4(iCoords) / MAX_UNSIGNED_SHORT;
vec2 tileTexCoord = mod(vTexCoord * vec2(textureSize(tilemap, 0)), 1.0) + 1e-3;
fragColor = texture(atlas, fCoords.rg + tileTexCoord * fCoords.ba);
}
`;
// Overwrite p5.js setUniform function to work with different data formats
p5.Shader.prototype.setUniform = function (uniformName, data) {
const uniform = this.uniforms[uniformName];
if (!uniform) {
return;
}
const gl = this._renderer.GL;
if (uniform.isArray) {
if (
uniform._cachedData &&
this._renderer._arraysEqual(uniform._cachedData, data)
) {
return;
} else {
uniform._cachedData = data.slice(0);
}
} else if (uniform._cachedData && uniform._cachedData === data) {
return;
} else {
if (Array.isArray(data)) {
uniform._cachedData = data.slice(0);
} else {
uniform._cachedData = data;
}
}
const location = uniform.location;
this.useProgram();
switch (uniform.type) {
case gl.BOOL:
if (data === true) {
gl.uniform1i(location, 1);
} else {
gl.uniform1i(location, 0);
}
break;
case gl.INT:
if (uniform.size > 1) {
data.length && gl.uniform1iv(location, data);
} else {
gl.uniform1i(location, data);
}
break;
case gl.FLOAT:
if (uniform.size > 1) {
data.length && gl.uniform1fv(location, data);
} else {
gl.uniform1f(location, data);
}
break;
case gl.FLOAT_MAT3:
gl.uniformMatrix3fv(location, false, data);
break;
case gl.FLOAT_MAT4:
gl.uniformMatrix4fv(location, false, data);
break;
case gl.FLOAT_VEC2:
if (uniform.size > 1) {
data.length && gl.uniform2fv(location, data);
} else {
gl.uniform2f(location, data[0], data[1]);
}
break;
case gl.FLOAT_VEC3:
if (uniform.size > 1) {
data.length && gl.uniform3fv(location, data);
} else {
gl.uniform3f(location, data[0], data[1], data[2]);
}
break;
case gl.FLOAT_VEC4:
if (uniform.size > 1) {
data.length && gl.uniform4fv(location, data);
} else {
gl.uniform4f(location, data[0], data[1], data[2], data[3]);
}
break;
case gl.INT_VEC2:
if (uniform.size > 1) {
data.length && gl.uniform2iv(location, data);
} else {
gl.uniform2i(location, data[0], data[1]);
}
break;
case gl.INT_VEC3:
if (uniform.size > 1) {
data.length && gl.uniform3iv(location, data);
} else {
gl.uniform3i(location, data[0], data[1], data[2]);
}
break;
case gl.INT_VEC4:
if (uniform.size > 1) {
data.length && gl.uniform4iv(location, data);
} else {
gl.uniform4i(location, data[0], data[1], data[2], data[3]);
}
break;
case gl.SAMPLER_2D:
if (typeof data == 'number') {
gl.activeTexture(gl.TEXTURE0 + data);
gl.uniform1i(location, data);
}
else {
gl.activeTexture(gl.TEXTURE0 + uniform.samplerIndex);
uniform.texture = data instanceof p5.Texture ? data : this._renderer.getTexture(data);
gl.uniform1i(location, uniform.samplerIndex);
if (typeof uniform.texture === 'object' && uniform.texture.src.gifProperties) {
uniform.texture.src._animateGif(this._renderer._pInst);
}
}
break;
case gl.UNSIGNED_INT_SAMPLER_2D:
gl.activeTexture(gl.TEXTURE0 + data);
gl.uniform1i(location, data);
break;
//@todo complete all types
}
return this;
}