Skip to content

Commit 52abc43

Browse files
committed
WIP gldemo spin tiles
1 parent 5c63a12 commit 52abc43

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

cube/gldemo.frag.glsl

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ precision highp sampler2DArray;
77
uniform sampler2DArray sheet;
88

99
in float sheetLayer;
10+
in mat3 tileTransform;
1011

1112
out lowp vec4 color;
1213

1314
void main(void) {
14-
mediump vec2 tileAt = gl_PointCoord;
15+
mediump vec2 tileAt = (tileTransform * vec3(gl_PointCoord, 1)).xy;
1516

1617
color = texture(sheet, vec3(tileAt, sheetLayer));
1718
}

cube/gldemo.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export default async function demo({
111111

112112
const attr = {
113113
loc: mustGetAttr('loc'), // vec2
114+
spin: mustGetAttr('spin'), // float
114115
size: mustGetAttr('size'), // float
115116
tileID: mustGetAttr('tileID'), // uvec2
116117
};
@@ -193,8 +194,8 @@ export default async function demo({
193194
const H = Math.ceil(height / cellSize);
194195
const N = (W + 1) * H;
195196

196-
if (locData.length != 2 * N)
197-
locData = new Float32Array(2 * N)
197+
if (locData.length != 3 * N)
198+
locData = new Float32Array(3 * N)
198199
else
199200
locData.fill(0);
200201

@@ -205,9 +206,15 @@ export default async function demo({
205206

206207
for (let rand = 0xdeadbeefn, i = 0, j = 0, x = 0, y = 0; i < N;) {
207208
rand = rand * 6364136223846793005n + 1442695040888963407n;
208-
tileData[i++] = Number(rand >> 17n & 0xffffffffn) % maxTileID;
209+
const tileID = Number(rand >> 17n & 0xffffffffn) % maxTileID;
210+
211+
rand = rand * 6364136223846793005n + 1442695040888963407n;
212+
const spin = (Number(rand >> 17n & 0xffffffffn) % 0xffff) / 0x10000;
213+
214+
tileData[i++] = tileID;
209215
locData[j++] = x;
210216
locData[j++] = y;
217+
locData[j++] = spin;
211218
if (++x > W) x = 0, y++;
212219
}
213220
}
@@ -262,11 +269,15 @@ export default async function demo({
262269
// to gl.draw* if/as we scale out to multiple draw calls
263270

264271
gl.enableVertexAttribArray(attr.loc);
272+
gl.enableVertexAttribArray(attr.spin);
265273
gl.enableVertexAttribArray(attr.tileID);
266274
gl.vertexAttrib1f(attr.size, cellSize);
267275

276+
const locStride = 3 * locData.BYTES_PER_ELEMENT;
277+
268278
gl.bindBuffer(gl.ARRAY_BUFFER, locBuffer)
269-
gl.vertexAttribPointer(attr.loc, 2, gl.FLOAT, false, 0, 0);
279+
gl.vertexAttribPointer(attr.loc, 2, gl.FLOAT, false, locStride, 0);
280+
gl.vertexAttribPointer(attr.spin, 1, gl.FLOAT, false, locStride, 2 * locData.BYTES_PER_ELEMENT);
270281

271282
gl.bindBuffer(gl.ARRAY_BUFFER, tileBuffer)
272283
gl.vertexAttribIPointer(attr.tileID, 1, gl.UNSIGNED_SHORT, 0, 0);

cube/gldemo.vert.glsl

+20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#version 300 es
22

3+
#define TAU 6.283185307179586
4+
35
precision highp float;
46
precision highp int;
57

68
uniform mat4 perspective;
79

810
in vec2 loc;
11+
in float spin;
912
in float size;
1013
in uint tileID;
1114

1215
out float sheetLayer;
16+
out mat3 tileTransform;
1317

1418
void main(void) {
1519
gl_Position = perspective * vec4(
@@ -18,6 +22,22 @@ void main(void) {
1822
1.0 // w
1923
);
2024

25+
float theta = spin * TAU;
26+
float cost = cos(theta);
27+
float sint = sin(theta);
28+
29+
mat3 spinOffset = mat3(
30+
vec3(1.0, 0.0, 0.0),
31+
vec3(0.0, 1.0, 0.0),
32+
vec3(0.5, 0.5, 1.0)
33+
);
34+
35+
tileTransform = spinOffset * mat3(
36+
vec3(cost, -sint, 0.0),
37+
vec3(sint, cost, 0.0),
38+
vec3(0.0, 0.0, 1.0)
39+
) * inverse(spinOffset);
40+
2141
gl_PointSize = size;
2242

2343
sheetLayer = float(tileID);

0 commit comments

Comments
 (0)