@i7r.tech/moveby 0.0.21-ywc → 0.21.2-ywc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -28,31 +28,282 @@ var CORE_ERRORS = {
28
28
  WIRE_GLYPH_METRIC: "A glyph of the font does not fit the track file: the atlas sides are powers of two, and each glyph rectangle is whole pixels."
29
29
  };
30
30
 
31
+ // ../engine-binding/src/shaders/backdrop.vert.glsl
32
+ var backdrop_vert_default = `// ── Sky image — vertex shader ─────────────────────────────────────────────────
33
+ //
34
+ // A screen-space quad: no projection and no camera. The buffer holds the four corners of a unit square,
35
+ // uploaded once. \`u_rect\` places that square on the screen for this frame.
36
+
37
+ precision highp float;
38
+
39
+ attribute vec2 a_corner; // (0,0) bottom-left … (1,1) top-right
40
+
41
+ uniform vec4 u_rect; // the image on screen, in NDC: (left, bottom, right, top)
42
+
43
+ varying vec2 v_uv;
44
+
45
+ // The far plane, as the cloud quad. The depth test is off for this draw, so z decides no pixel. It is at
46
+ // the far plane so that the quad claims no nearness it does not have: with the test left on, a field sliver
47
+ // above the horizon would be in front of it.
48
+ const float FAR_Z = 1.0;
49
+
50
+ void main() {
51
+ // The upload does not flip (no UNPACK_FLIP_Y_WEBGL), so texel row 0 is the image's TOP row: the top
52
+ // corner of the quad reads v = 0.
53
+ v_uv = vec2(a_corner.x, 1.0 - a_corner.y);
54
+ gl_Position = vec4(mix(u_rect.xy, u_rect.zw, a_corner), FAR_Z, 1.0);
55
+ }
56
+ `;
57
+
58
+ // ../engine-binding/src/shaders/backdrop.frag.glsl
59
+ var backdrop_frag_default = `// ── Sky image — fragment shader ───────────────────────────────────────────────
60
+
61
+ precision mediump float;
62
+
63
+ uniform sampler2D u_image; // premultiplied RGBA, so a transparent texel adds no colour to its neighbours
64
+
65
+ varying vec2 v_uv;
66
+
67
+ void main() {
68
+ gl_FragColor = texture2D(u_image, v_uv); // premultiplied: the blend is ONE, ONE_MINUS_SRC_ALPHA
69
+ }
70
+ `;
71
+
72
+ // ../engine-binding/src/utils.ts
73
+ var VERTS_PER_QUAD_INDEXED = 4;
74
+ var UINT16_INDEX_QUAD_CAP = Math.floor(65535 / VERTS_PER_QUAD_INDEXED);
75
+ function buildQuadIndices(maxQuads) {
76
+ const idx = new Uint16Array(maxQuads * 6);
77
+ for (let i = 0;i < maxQuads; i++) {
78
+ const v = i * VERTS_PER_QUAD_INDEXED;
79
+ idx.set([v, v + 1, v + 2, v, v + 2, v + 3], i * 6);
80
+ }
81
+ return idx;
82
+ }
83
+ function assertQuadCapacity(pass, maxQuads, code, message) {
84
+ if (maxQuads > UINT16_INDEX_QUAD_CAP) {
85
+ throw new MovebyError(code, `${message} (${pass} pass: ${maxQuads} quads exceeds the Uint16 index cap (${UINT16_INDEX_QUAD_CAP}))`);
86
+ }
87
+ }
88
+ function pixelSize(bitmap) {
89
+ if ("naturalWidth" in bitmap)
90
+ return { width: bitmap.naturalWidth, height: bitmap.naturalHeight };
91
+ if ("videoWidth" in bitmap)
92
+ return { width: bitmap.videoWidth, height: bitmap.videoHeight };
93
+ if ("displayWidth" in bitmap)
94
+ return { width: bitmap.displayWidth, height: bitmap.displayHeight };
95
+ return { width: bitmap.width, height: bitmap.height };
96
+ }
97
+ var isPowerOfTwo = (n) => n > 0 && (n & n - 1) === 0;
98
+ function assertMipmapable(code, message, what, bitmap) {
99
+ const { width, height } = pixelSize(bitmap);
100
+ if (!isPowerOfTwo(width) || !isPowerOfTwo(height)) {
101
+ throw new MovebyError(code, `${message} (${what}: ${width} × ${height})`);
102
+ }
103
+ }
104
+ function drawQuadRange(gl, startVertex, countVertex) {
105
+ if (startVertex % VERTS_PER_QUAD_INDEXED !== 0 || countVertex % VERTS_PER_QUAD_INDEXED !== 0) {
106
+ throw new Error(`non-quad-aligned range: start=${startVertex} count=${countVertex}`);
107
+ }
108
+ const quadStart = startVertex / VERTS_PER_QUAD_INDEXED;
109
+ const quadCount = countVertex / VERTS_PER_QUAD_INDEXED;
110
+ gl.drawElements(gl.TRIANGLES, quadCount * 6, gl.UNSIGNED_SHORT, quadStart * 6 * 2);
111
+ }
112
+ var CAMERA_PART_FLOATS = 3;
113
+ var CAMERA_PART_BYTES = CAMERA_PART_FLOATS * Float32Array.BYTES_PER_ELEMENT;
114
+
115
+ class CameraViews {
116
+ position;
117
+ forward;
118
+ memory;
119
+ address;
120
+ buffer;
121
+ constructor(engine) {
122
+ this.memory = engine.wasm_memory();
123
+ this.address = engine.camera_ptr();
124
+ this.buffer = this.memory.buffer;
125
+ this.position = new Float32Array(this.buffer, this.address, CAMERA_PART_FLOATS);
126
+ this.forward = new Float32Array(this.buffer, this.address + CAMERA_PART_BYTES, CAMERA_PART_FLOATS);
127
+ }
128
+ current() {
129
+ if (this.memory.buffer !== this.buffer) {
130
+ this.buffer = this.memory.buffer;
131
+ this.position = new Float32Array(this.buffer, this.address, CAMERA_PART_FLOATS);
132
+ this.forward = new Float32Array(this.buffer, this.address + CAMERA_PART_BYTES, CAMERA_PART_FLOATS);
133
+ }
134
+ return this;
135
+ }
136
+ }
137
+ function disableAttributes(gl, locations) {
138
+ for (let i = 0;i < locations.length; i++) {
139
+ const location = locations[i];
140
+ if (location >= 0)
141
+ gl.disableVertexAttribArray(location);
142
+ }
143
+ }
144
+ function compileShader(gl, type, source) {
145
+ const shader = gl.createShader(type);
146
+ gl.shaderSource(shader, source);
147
+ gl.compileShader(shader);
148
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
149
+ const log = gl.getShaderInfoLog(shader);
150
+ gl.deleteShader(shader);
151
+ throw new Error(`Shader compile error:
152
+ ${log}`);
153
+ }
154
+ return shader;
155
+ }
156
+ function uploadGridAtlas(gl, bitmap) {
157
+ const tex = gl.createTexture();
158
+ if (!tex)
159
+ throw new Error("createTexture failed");
160
+ gl.bindTexture(gl.TEXTURE_2D, tex);
161
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
162
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
163
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
164
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
165
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
166
+ return tex;
167
+ }
168
+ async function decodeImage(url, premultiplyAlpha) {
169
+ const response = await fetch(url);
170
+ if (!response.ok) {
171
+ throw new Error(`HTTP ${response.status} ${response.statusText}`);
172
+ }
173
+ return await createImageBitmap(await response.blob(), {
174
+ premultiplyAlpha,
175
+ colorSpaceConversion: "none"
176
+ });
177
+ }
178
+ function createProgram(gl, vsSource, fsSource) {
179
+ const vs = compileShader(gl, gl.VERTEX_SHADER, vsSource);
180
+ const fs = compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
181
+ const prog = gl.createProgram();
182
+ gl.attachShader(prog, vs);
183
+ gl.attachShader(prog, fs);
184
+ gl.linkProgram(prog);
185
+ if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
186
+ throw new Error(`Program link error:
187
+ ${gl.getProgramInfoLog(prog)}`);
188
+ }
189
+ gl.deleteShader(vs);
190
+ gl.deleteShader(fs);
191
+ return prog;
192
+ }
193
+
31
194
  // ../engine-binding/src/passes/backdrop.ts
195
+ var QUAD_CORNERS = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]);
196
+ var RECT = new Float32Array(4);
197
+ function skyImageRect(horizonRow, canvasWidth, canvasHeight, imageWidth, imageHeight, share, parallax) {
198
+ const drawnWidth = share * canvasHeight * imageWidth / imageHeight;
199
+ const halfWidth = drawnWidth / canvasWidth;
200
+ const bottom = horizonRow / canvasHeight * 2 - 1;
201
+ RECT[0] = parallax - halfWidth;
202
+ RECT[1] = bottom;
203
+ RECT[2] = parallax + halfWidth;
204
+ RECT[3] = bottom + share * 2;
205
+ return RECT;
206
+ }
207
+
32
208
  class BackdropPass {
33
209
  gl;
34
210
  ground;
35
211
  sky;
36
- constructor(gl, ground, sky) {
212
+ share;
213
+ image = null;
214
+ constructor(gl, ground, sky, share) {
37
215
  this.gl = gl;
38
216
  this.ground = ground;
39
217
  this.sky = sky;
218
+ this.share = share;
219
+ }
220
+ setSkyImage(source, code, message) {
221
+ const gl = this.gl;
222
+ const { width, height } = pixelSize(source);
223
+ const limit = gl.getParameter(gl.MAX_TEXTURE_SIZE);
224
+ if (width > limit || height > limit) {
225
+ throw new MovebyError(code, `${message} (sky image: ${width} × ${height}, this device takes up to ${limit})`);
226
+ }
227
+ const texture = gl.createTexture();
228
+ if (!texture)
229
+ throw new Error("createTexture failed");
230
+ gl.bindTexture(gl.TEXTURE_2D, texture);
231
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
232
+ try {
233
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
234
+ } finally {
235
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
236
+ }
237
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
238
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
239
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
240
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
241
+ const program = createProgram(gl, backdrop_vert_default, backdrop_frag_default);
242
+ gl.useProgram(program);
243
+ gl.uniform1i(gl.getUniformLocation(program, "u_image"), 0);
244
+ const cornerBuffer = gl.createBuffer();
245
+ gl.bindBuffer(gl.ARRAY_BUFFER, cornerBuffer);
246
+ gl.bufferData(gl.ARRAY_BUFFER, QUAD_CORNERS, gl.STATIC_DRAW);
247
+ const indexBuffer = gl.createBuffer();
248
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
249
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buildQuadIndices(1), gl.STATIC_DRAW);
250
+ const a_corner = gl.getAttribLocation(program, "a_corner");
251
+ this.image = {
252
+ program,
253
+ cornerBuffer,
254
+ indexBuffer,
255
+ texture,
256
+ a_corner,
257
+ locList: [a_corner],
258
+ u_rect: gl.getUniformLocation(program, "u_rect"),
259
+ width,
260
+ height
261
+ };
40
262
  }
41
263
  clearToGround() {
42
264
  const gl = this.gl;
43
- gl.clearColor(...this.ground, 1);
265
+ const ground = this.ground;
266
+ gl.clearColor(ground[0], ground[1], ground[2], 1);
44
267
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
45
268
  }
46
269
  fillSky(horizonRow, width, height) {
47
270
  const gl = this.gl;
48
271
  if (horizonRow >= height)
49
272
  return;
273
+ const sky = this.sky;
50
274
  gl.enable(gl.SCISSOR_TEST);
51
275
  gl.scissor(0, horizonRow, width, height - horizonRow);
52
- gl.clearColor(...this.sky, 1);
276
+ gl.clearColor(sky[0], sky[1], sky[2], 1);
53
277
  gl.clear(gl.COLOR_BUFFER_BIT);
54
278
  gl.disable(gl.SCISSOR_TEST);
55
279
  }
280
+ drawSkyImage(horizonRow, width, height, parallax) {
281
+ const gl = this.gl;
282
+ const image = this.image;
283
+ if (!image || horizonRow >= height)
284
+ return;
285
+ const rect = skyImageRect(horizonRow, width, height, image.width, image.height, this.share, parallax);
286
+ gl.enable(gl.SCISSOR_TEST);
287
+ gl.scissor(0, horizonRow, width, height - horizonRow);
288
+ gl.disable(gl.DEPTH_TEST);
289
+ gl.depthMask(false);
290
+ gl.enable(gl.BLEND);
291
+ gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
292
+ gl.useProgram(image.program);
293
+ gl.bindBuffer(gl.ARRAY_BUFFER, image.cornerBuffer);
294
+ gl.enableVertexAttribArray(image.a_corner);
295
+ gl.vertexAttribPointer(image.a_corner, 2, gl.FLOAT, false, 0, 0);
296
+ gl.uniform4fv(image.u_rect, rect);
297
+ gl.activeTexture(gl.TEXTURE0);
298
+ gl.bindTexture(gl.TEXTURE_2D, image.texture);
299
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, image.indexBuffer);
300
+ gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
301
+ disableAttributes(gl, image.locList);
302
+ gl.depthMask(true);
303
+ gl.enable(gl.DEPTH_TEST);
304
+ gl.disable(gl.BLEND);
305
+ gl.disable(gl.SCISSOR_TEST);
306
+ }
56
307
  }
57
308
 
58
309
  // ../engine-binding/src/shaders/field.vert.glsl
@@ -161,104 +412,22 @@ void main() {
161
412
  }
162
413
  `;
163
414
 
164
- // ../engine-binding/src/utils.ts
165
- var VERTS_PER_QUAD_INDEXED = 4;
166
- var UINT16_INDEX_QUAD_CAP = Math.floor(65535 / VERTS_PER_QUAD_INDEXED);
167
- function buildQuadIndices(maxQuads) {
168
- const idx = new Uint16Array(maxQuads * 6);
169
- for (let i = 0;i < maxQuads; i++) {
170
- const v = i * VERTS_PER_QUAD_INDEXED;
171
- idx.set([v, v + 1, v + 2, v, v + 2, v + 3], i * 6);
172
- }
173
- return idx;
174
- }
175
- function assertQuadCapacity(pass, maxQuads, code, message) {
176
- if (maxQuads > UINT16_INDEX_QUAD_CAP) {
177
- throw new MovebyError(code, `${message} (${pass} pass: ${maxQuads} quads exceeds the Uint16 index cap (${UINT16_INDEX_QUAD_CAP}))`);
178
- }
179
- }
180
- function pixelSize(bitmap) {
181
- if ("naturalWidth" in bitmap)
182
- return { width: bitmap.naturalWidth, height: bitmap.naturalHeight };
183
- if ("videoWidth" in bitmap)
184
- return { width: bitmap.videoWidth, height: bitmap.videoHeight };
185
- if ("displayWidth" in bitmap)
186
- return { width: bitmap.displayWidth, height: bitmap.displayHeight };
187
- return { width: bitmap.width, height: bitmap.height };
188
- }
189
- var isPowerOfTwo = (n) => n > 0 && (n & n - 1) === 0;
190
- function assertMipmapable(code, message, what, bitmap) {
191
- const { width, height } = pixelSize(bitmap);
192
- if (!isPowerOfTwo(width) || !isPowerOfTwo(height)) {
193
- throw new MovebyError(code, `${message} (${what}: ${width} × ${height})`);
194
- }
195
- }
196
- function quadRangeToIndexRange(startVertex, countVertex) {
197
- if (startVertex % VERTS_PER_QUAD_INDEXED !== 0 || countVertex % VERTS_PER_QUAD_INDEXED !== 0) {
198
- throw new Error(`non-quad-aligned range: start=${startVertex} count=${countVertex}`);
199
- }
200
- const quadStart = startVertex / VERTS_PER_QUAD_INDEXED;
201
- const quadCount = countVertex / VERTS_PER_QUAD_INDEXED;
202
- return { count: quadCount * 6, offset: quadStart * 6 * 2 };
203
- }
204
- function disableAttributes(gl, locations) {
205
- for (const location of Object.values(locations)) {
206
- if (location >= 0)
207
- gl.disableVertexAttribArray(location);
208
- }
209
- }
210
- function compileShader(gl, type, source) {
211
- const shader = gl.createShader(type);
212
- gl.shaderSource(shader, source);
213
- gl.compileShader(shader);
214
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
215
- const log = gl.getShaderInfoLog(shader);
216
- gl.deleteShader(shader);
217
- throw new Error(`Shader compile error:
218
- ${log}`);
219
- }
220
- return shader;
221
- }
222
- function uploadGridAtlas(gl, bitmap) {
223
- const tex = gl.createTexture();
224
- if (!tex)
225
- throw new Error("createTexture failed");
226
- gl.bindTexture(gl.TEXTURE_2D, tex);
227
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
228
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
229
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
230
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
231
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
232
- return tex;
233
- }
234
- async function decodeAtlas(url) {
235
- const response = await fetch(url);
236
- if (!response.ok) {
237
- throw new Error(`HTTP ${response.status} ${response.statusText}`);
238
- }
239
- return await createImageBitmap(await response.blob(), {
240
- premultiplyAlpha: "none",
241
- colorSpaceConversion: "none"
242
- });
243
- }
244
- function createProgram(gl, vsSource, fsSource) {
245
- const vs = compileShader(gl, gl.VERTEX_SHADER, vsSource);
246
- const fs = compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
247
- const prog = gl.createProgram();
248
- gl.attachShader(prog, vs);
249
- gl.attachShader(prog, fs);
250
- gl.linkProgram(prog);
251
- if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
252
- throw new Error(`Program link error:
253
- ${gl.getProgramInfoLog(prog)}`);
254
- }
255
- gl.deleteShader(vs);
256
- gl.deleteShader(fs);
257
- return prog;
258
- }
259
-
260
415
  // ../engine-binding/src/passes/field.ts
261
416
  var FIELD_FLOATS_PER_VERTEX = 5;
417
+ var FLOAT = 4;
418
+ var STRIDE = FIELD_FLOATS_PER_VERTEX * FLOAT;
419
+ function bindFloat(gl, loc, fieldIndex) {
420
+ if (loc < 0)
421
+ return;
422
+ gl.enableVertexAttribArray(loc);
423
+ gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE, fieldIndex * FLOAT);
424
+ }
425
+ function drawBucket(gl, startVertex, countVertex) {
426
+ if (startVertex + countVertex > UINT16_INDEX_QUAD_CAP * VERTS_PER_QUAD_INDEXED) {
427
+ throw new MovebyError("FIELD_CAPACITY", `${FIELD_ERRORS.FIELD_CAPACITY} (${startVertex + countVertex} vertices)`);
428
+ }
429
+ drawQuadRange(gl, startVertex, countVertex);
430
+ }
262
431
  var FIELD_ERRORS = {
263
432
  FIELD_RANGE: "A field span has a bad range: 0 ≤ start < end.",
264
433
  FIELD_END: "A field span ends past the end of the track.",
@@ -280,10 +449,12 @@ class FieldPass {
280
449
  fieldBuffer = null;
281
450
  fieldIndexBuffer = null;
282
451
  fieldLoc = null;
452
+ fieldLocList = [];
283
453
  fieldUni = null;
284
454
  fieldTextures = new Map;
285
455
  fieldAtlasProgram = null;
286
456
  fieldAtlasLoc = null;
457
+ fieldAtlasLocList = [];
287
458
  fieldAtlasUni = null;
288
459
  fieldAtlasCellRectLoc = null;
289
460
  fieldAtlas = null;
@@ -300,6 +471,7 @@ class FieldPass {
300
471
  a_u: gl.getAttribLocation(this.fieldProgram, "a_u"),
301
472
  a_v: gl.getAttribLocation(this.fieldProgram, "a_v")
302
473
  };
474
+ this.fieldLocList = Object.values(this.fieldLoc);
303
475
  this.fieldUni = {
304
476
  u_camPos: gl.getUniformLocation(this.fieldProgram, "u_camPos"),
305
477
  u_camForward: gl.getUniformLocation(this.fieldProgram, "u_camForward"),
@@ -318,6 +490,7 @@ class FieldPass {
318
490
  a_u: gl.getAttribLocation(this.fieldAtlasProgram, "a_u"),
319
491
  a_v: gl.getAttribLocation(this.fieldAtlasProgram, "a_v")
320
492
  };
493
+ this.fieldAtlasLocList = Object.values(this.fieldAtlasLoc);
321
494
  this.fieldAtlasUni = {
322
495
  u_camPos: gl.getUniformLocation(this.fieldAtlasProgram, "u_camPos"),
323
496
  u_camForward: gl.getUniformLocation(this.fieldAtlasProgram, "u_camForward"),
@@ -360,7 +533,7 @@ class FieldPass {
360
533
  return;
361
534
  this.fieldAtlas = uploadGridAtlas(this.gl, bitmap);
362
535
  }
363
- draw() {
536
+ draw(camera) {
364
537
  const gl = this.gl;
365
538
  if (!this.fieldProgram || !this.fieldLoc)
366
539
  return;
@@ -368,32 +541,19 @@ class FieldPass {
368
541
  gl.bindBuffer(gl.ARRAY_BUFFER, this.fieldBuffer);
369
542
  const view = this.engine.fill_field_and_view();
370
543
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
371
- const FLOAT = 4;
372
- const STRIDE = FIELD_FLOATS_PER_VERTEX * FLOAT;
373
- const bind = (loc, fieldIndex) => {
374
- if (loc < 0)
375
- return;
376
- gl.enableVertexAttribArray(loc);
377
- gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE, fieldIndex * FLOAT);
378
- };
379
- bind(this.fieldLoc.a_cornerX, 0);
380
- bind(this.fieldLoc.a_cornerY, 1);
381
- bind(this.fieldLoc.a_cornerZ, 2);
382
- bind(this.fieldLoc.a_u, 3);
383
- bind(this.fieldLoc.a_v, 4);
544
+ bindFloat(gl, this.fieldLoc.a_cornerX, 0);
545
+ bindFloat(gl, this.fieldLoc.a_cornerY, 1);
546
+ bindFloat(gl, this.fieldLoc.a_cornerZ, 2);
547
+ bindFloat(gl, this.fieldLoc.a_u, 3);
548
+ bindFloat(gl, this.fieldLoc.a_v, 4);
549
+ const aspect = this.engine.aspect();
384
550
  if (this.fieldUni) {
385
- const cam = this.engine.camera_basis();
386
- gl.uniform3fv(this.fieldUni.u_camPos, cam.subarray(0, 3));
387
- gl.uniform3fv(this.fieldUni.u_camForward, cam.subarray(3, 6));
388
- gl.uniform1f(this.fieldUni.u_aspect, this.engine.aspect());
551
+ const cam = camera.current();
552
+ gl.uniform3fv(this.fieldUni.u_camPos, cam.position);
553
+ gl.uniform3fv(this.fieldUni.u_camForward, cam.forward);
554
+ gl.uniform1f(this.fieldUni.u_aspect, aspect);
389
555
  }
390
556
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.fieldIndexBuffer);
391
- const drawRange = (startVertex, countVertex) => {
392
- if (startVertex + countVertex > UINT16_INDEX_QUAD_CAP * VERTS_PER_QUAD_INDEXED) {
393
- throw new MovebyError("FIELD_CAPACITY", `${FIELD_ERRORS.FIELD_CAPACITY} (${startVertex + countVertex} vertices)`);
394
- }
395
- return quadRangeToIndexRange(startVertex, countVertex);
396
- };
397
557
  const ranges = this.engine.field_draw_ranges();
398
558
  gl.activeTexture(gl.TEXTURE0);
399
559
  for (let i = 0;i + 2 < ranges.length; i += 3) {
@@ -404,39 +564,31 @@ class FieldPass {
404
564
  if (!tex)
405
565
  throw noImage(`no texture for texture_id ${id}`);
406
566
  gl.bindTexture(gl.TEXTURE_2D, tex);
407
- const { count: indexCount, offset } = drawRange(start, count);
408
- gl.drawElements(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, offset);
567
+ drawBucket(gl, start, count);
409
568
  }
410
- disableAttributes(gl, this.fieldLoc);
569
+ disableAttributes(gl, this.fieldLocList);
411
570
  const atlasRanges = this.engine.field_atlas_draw_ranges();
412
571
  if (this.fieldAtlasProgram && this.fieldAtlasLoc && atlasRanges.length > 0) {
413
572
  if (!this.fieldAtlas)
414
573
  throw noImage("the track has atlas-cell ground and field() has no atlas");
415
574
  gl.useProgram(this.fieldAtlasProgram);
416
- const bindAtlas = (loc, fieldIndex) => {
417
- if (loc < 0)
418
- return;
419
- gl.enableVertexAttribArray(loc);
420
- gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE, fieldIndex * FLOAT);
421
- };
422
- bindAtlas(this.fieldAtlasLoc.a_cornerX, 0);
423
- bindAtlas(this.fieldAtlasLoc.a_cornerY, 1);
424
- bindAtlas(this.fieldAtlasLoc.a_cornerZ, 2);
425
- bindAtlas(this.fieldAtlasLoc.a_u, 3);
426
- bindAtlas(this.fieldAtlasLoc.a_v, 4);
575
+ bindFloat(gl, this.fieldAtlasLoc.a_cornerX, 0);
576
+ bindFloat(gl, this.fieldAtlasLoc.a_cornerY, 1);
577
+ bindFloat(gl, this.fieldAtlasLoc.a_cornerZ, 2);
578
+ bindFloat(gl, this.fieldAtlasLoc.a_u, 3);
579
+ bindFloat(gl, this.fieldAtlasLoc.a_v, 4);
427
580
  if (this.fieldAtlasUni) {
428
- const cam = this.engine.camera_basis();
429
- gl.uniform3fv(this.fieldAtlasUni.u_camPos, cam.subarray(0, 3));
430
- gl.uniform3fv(this.fieldAtlasUni.u_camForward, cam.subarray(3, 6));
431
- gl.uniform1f(this.fieldAtlasUni.u_aspect, this.engine.aspect());
581
+ const cam = camera.current();
582
+ gl.uniform3fv(this.fieldAtlasUni.u_camPos, cam.position);
583
+ gl.uniform3fv(this.fieldAtlasUni.u_camForward, cam.forward);
584
+ gl.uniform1f(this.fieldAtlasUni.u_aspect, aspect);
432
585
  }
433
586
  gl.bindTexture(gl.TEXTURE_2D, this.fieldAtlas);
434
587
  for (let i = 0;i + 5 < atlasRanges.length; i += 6) {
435
588
  gl.uniform4f(this.fieldAtlasCellRectLoc, atlasRanges[i], atlasRanges[i + 1], atlasRanges[i + 2], atlasRanges[i + 3]);
436
- const { count: indexCount, offset } = drawRange(atlasRanges[i + 4], atlasRanges[i + 5]);
437
- gl.drawElements(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, offset);
589
+ drawBucket(gl, atlasRanges[i + 4], atlasRanges[i + 5]);
438
590
  }
439
- disableAttributes(gl, this.fieldAtlasLoc);
591
+ disableAttributes(gl, this.fieldAtlasLocList);
440
592
  }
441
593
  }
442
594
  }
@@ -464,7 +616,7 @@ attribute float a_s; // road-SURFACE arc-length (m) at this corner, RAW
464
616
  attribute vec3 a_divider; // PER-QUAD: (pattern 0=Dashed/1=Solid/2=Double, dashOn m, dashGap m)
465
617
  attribute vec3 a_color; // PER-QUAD: divider colour, RGB 0..1
466
618
 
467
- // Camera — Rust builds ONE camera per frame (Engine::camera_basis) and forwards it opaquely; JS computes
619
+ // Camera — Rust builds ONE camera per frame (Engine::fill_camera) and forwards it opaquely; JS computes
468
620
  // no camera math, exactly as it forwarded opaque pre-projected vertices before this RFC.
469
621
  uniform vec3 u_camPos; // camera eye, world-space (Camera::pos, camera.rs)
470
622
  uniform vec3 u_camForward; // unit look direction (Camera::forward)
@@ -551,6 +703,10 @@ varying float v_dashOn; // PER-QUAD dash painted length (m) — the per-span das
551
703
  varying float v_dashGap; // PER-QUAD dash gap length (m)
552
704
  varying vec3 v_color; // PER-QUAD divider colour (RGB 0..1)
553
705
 
706
+ // The colour of the road surface (RGB 0..1), one value for the whole road. passes/road.ts sets it once, when
707
+ // the pass is made. GL starts a uniform at 0, so a pass that did not set it would draw a black road.
708
+ uniform vec3 u_asphalt;
709
+
554
710
  // Lane grid: a boundary every LANE_WIDTH m across the road. DOUBLE_GAP = the offset (m) of each of a Double
555
711
  // line's two parallel stripes from the boundary (each is the single-line band shifted ±DOUBLE_GAP). Both MUST
556
712
  // equal the Rust consts.rs values (LANE_WIDTH / DOUBLE_GAP) — duplicated across Rust + GLSL, kept in sync by
@@ -570,7 +726,7 @@ const float HALF_BAND_W = 0.1;
570
726
  // floor()/mod()/min() are likewise branchless. Combining them avoids warp divergence.
571
727
 
572
728
  void main() {
573
- gl_FragColor = vec4(0.43, 0.43, 0.43, 1.0); // asphalt grey
729
+ gl_FragColor = vec4(u_asphalt, 1.0); // the asphalt; the divider paints over it at the end
574
730
 
575
731
  // Recover surface arc-length AND signed lateral, both perspective-correct: v_sScaled ∝ s/cz,
576
732
  // v_lateralScaled ∝ L/cz, and v_scale ∝ 1/cz are ALL affine in screen space, so each ratio is the
@@ -620,9 +776,9 @@ void main() {
620
776
  `;
621
777
 
622
778
  // ../engine-binding/src/passes/road.ts
623
- var FLOAT = 4;
779
+ var FLOAT2 = 4;
624
780
  var FLOATS_PER_VERTEX = 12;
625
- var STRIDE = FLOATS_PER_VERTEX * FLOAT;
781
+ var STRIDE2 = FLOATS_PER_VERTEX * FLOAT2;
626
782
  var ROAD_ERRORS = {
627
783
  LANE_COUNT: "A lane span's lane count must be from 1 to 8.",
628
784
  LANE_BOUNDS: "A lane span has a bad range: 0 ≤ start < end ≤ track length.",
@@ -630,8 +786,15 @@ var ROAD_ERRORS = {
630
786
  LANE_TAPER: "Two changes of road width are closer than the 30 m taper.",
631
787
  LANE_DASH: "A dashed divider needs dashOn and dashGap above 0.",
632
788
  LANE_COLOR: "A divider colour needs each part from 0 to 1.",
633
- ROAD_CAPACITY: "The track has more road quads in one frame than one draw can index."
789
+ ROAD_CAPACITY: "The track has more road quads in one frame than one draw can index.",
790
+ ROAD_ASPHALT: "The asphalt colour needs three parts, each from 0 to 1."
634
791
  };
792
+ function bindFloat2(gl, loc, size, fieldIndex) {
793
+ if (loc < 0)
794
+ return;
795
+ gl.enableVertexAttribArray(loc);
796
+ gl.vertexAttribPointer(loc, size, gl.FLOAT, false, STRIDE2, fieldIndex * FLOAT2);
797
+ }
635
798
 
636
799
  class RoadPass {
637
800
  gl;
@@ -640,8 +803,9 @@ class RoadPass {
640
803
  vertexBuffer;
641
804
  indexBuffer;
642
805
  loc;
806
+ locList;
643
807
  uni;
644
- constructor(gl, engine) {
808
+ constructor(gl, engine, asphalt) {
645
809
  this.gl = gl;
646
810
  this.program = createProgram(gl, road_vert_default, road_frag_default);
647
811
  this.vertexBuffer = gl.createBuffer();
@@ -655,6 +819,7 @@ class RoadPass {
655
819
  a_divider: gl.getAttribLocation(this.program, "a_divider"),
656
820
  a_color: gl.getAttribLocation(this.program, "a_color")
657
821
  };
822
+ this.locList = Object.values(this.loc);
658
823
  this.uni = {
659
824
  u_scaleNear: gl.getUniformLocation(this.program, "u_scaleNear"),
660
825
  u_scaleFar: gl.getUniformLocation(this.program, "u_scaleFar"),
@@ -662,7 +827,8 @@ class RoadPass {
662
827
  u_camForward: gl.getUniformLocation(this.program, "u_camForward"),
663
828
  u_aspect: gl.getUniformLocation(this.program, "u_aspect"),
664
829
  u_cameraDepth: gl.getUniformLocation(this.program, "u_cameraDepth"),
665
- u_dashAnchor: gl.getUniformLocation(this.program, "u_dashAnchor")
830
+ u_dashAnchor: gl.getUniformLocation(this.program, "u_dashAnchor"),
831
+ u_asphalt: gl.getUniformLocation(this.program, "u_asphalt")
666
832
  };
667
833
  this.engine = engine;
668
834
  gl.useProgram(this.program);
@@ -670,6 +836,7 @@ class RoadPass {
670
836
  gl.uniform1f(this.uni.u_scaleNear, bounds[0]);
671
837
  gl.uniform1f(this.uni.u_scaleFar, bounds[1]);
672
838
  gl.uniform1f(this.uni.u_cameraDepth, engine.camera_depth());
839
+ gl.uniform3f(this.uni.u_asphalt, asphalt[0], asphalt[1], asphalt[2]);
673
840
  gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
674
841
  gl.bufferData(gl.ARRAY_BUFFER, engine.max_draw_bytes(), gl.DYNAMIC_DRAW);
675
842
  const maxQuads = engine.max_draw_bytes() / 4 / FLOATS_PER_VERTEX / VERTS_PER_QUAD_INDEXED;
@@ -678,7 +845,7 @@ class RoadPass {
678
845
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
679
846
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, buildQuadIndices(maxQuads), gl.STATIC_DRAW);
680
847
  }
681
- draw() {
848
+ draw(camera) {
682
849
  const gl = this.gl;
683
850
  const engine = this.engine;
684
851
  gl.useProgram(this.program);
@@ -686,29 +853,23 @@ class RoadPass {
686
853
  const view = engine.fill_and_view();
687
854
  const floatCount = view.length;
688
855
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
689
- const bindAttr = (loc, fieldIndex, size) => {
690
- if (loc < 0)
691
- return;
692
- gl.enableVertexAttribArray(loc);
693
- gl.vertexAttribPointer(loc, size, gl.FLOAT, false, STRIDE, fieldIndex * FLOAT);
694
- };
695
- bindAttr(this.loc.a_cornerX, 0, 1);
696
- bindAttr(this.loc.a_cornerY, 1, 1);
697
- bindAttr(this.loc.a_cornerZ, 2, 1);
698
- bindAttr(this.loc.a_lateral, 3, 1);
699
- bindAttr(this.loc.a_half, 4, 1);
700
- bindAttr(this.loc.a_s, 5, 1);
701
- bindAttr(this.loc.a_divider, 6, 3);
702
- bindAttr(this.loc.a_color, 9, 3);
703
- const cam = engine.camera_basis();
704
- gl.uniform3fv(this.uni.u_camPos, cam.subarray(0, 3));
705
- gl.uniform3fv(this.uni.u_camForward, cam.subarray(3, 6));
856
+ bindFloat2(gl, this.loc.a_cornerX, 1, 0);
857
+ bindFloat2(gl, this.loc.a_cornerY, 1, 1);
858
+ bindFloat2(gl, this.loc.a_cornerZ, 1, 2);
859
+ bindFloat2(gl, this.loc.a_lateral, 1, 3);
860
+ bindFloat2(gl, this.loc.a_half, 1, 4);
861
+ bindFloat2(gl, this.loc.a_s, 1, 5);
862
+ bindFloat2(gl, this.loc.a_divider, 3, 6);
863
+ bindFloat2(gl, this.loc.a_color, 3, 9);
864
+ const cam = camera.current();
865
+ gl.uniform3fv(this.uni.u_camPos, cam.position);
866
+ gl.uniform3fv(this.uni.u_camForward, cam.forward);
706
867
  gl.uniform1f(this.uni.u_aspect, engine.aspect());
707
868
  gl.uniform1f(this.uni.u_dashAnchor, engine.dash_anchor());
708
869
  const quadCount = floatCount / FLOATS_PER_VERTEX / VERTS_PER_QUAD_INDEXED;
709
870
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
710
871
  gl.drawElements(gl.TRIANGLES, quadCount * 6, gl.UNSIGNED_SHORT, 0);
711
- disableAttributes(gl, this.loc);
872
+ disableAttributes(gl, this.locList);
712
873
  }
713
874
  }
714
875
  // ../engine-binding/src/shaders/scenery.vert.glsl
@@ -805,6 +966,14 @@ void main() {
805
966
 
806
967
  // ../engine-binding/src/passes/scenery.ts
807
968
  var SCENERY_FLOATS_PER_VERTEX = 5;
969
+ var FLOAT3 = 4;
970
+ var STRIDE3 = SCENERY_FLOATS_PER_VERTEX * FLOAT3;
971
+ function bindFloat3(gl, loc, fieldIndex) {
972
+ if (loc < 0)
973
+ return;
974
+ gl.enableVertexAttribArray(loc);
975
+ gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE3, fieldIndex * FLOAT3);
976
+ }
808
977
  var SCENERY_ERRORS = {
809
978
  SCENERY_ATLAS: "The scenery atlas grid is bad: cols and rows are at least 1, each pixel size is a whole number of cells, and the grid has at most 65,536 cells.",
810
979
  SCENERY_SPRITE: "A sprite is bad: its cell is outside the grid or declared twice, its cell block does not fit, or its size is not a positive finite number.",
@@ -827,6 +996,7 @@ class SceneryPass {
827
996
  sceneryBuffer = null;
828
997
  sceneryIndexBuffer = null;
829
998
  sceneryLoc = null;
999
+ sceneryLocList = [];
830
1000
  sceneryUni = null;
831
1001
  sceneryTexture = null;
832
1002
  constructor(gl, engine) {
@@ -842,6 +1012,7 @@ class SceneryPass {
842
1012
  a_u: gl.getAttribLocation(this.sceneryProgram, "a_u"),
843
1013
  a_v: gl.getAttribLocation(this.sceneryProgram, "a_v")
844
1014
  };
1015
+ this.sceneryLocList = Object.values(this.sceneryLoc);
845
1016
  this.sceneryUni = {
846
1017
  u_camPos: gl.getUniformLocation(this.sceneryProgram, "u_camPos"),
847
1018
  u_camForward: gl.getUniformLocation(this.sceneryProgram, "u_camForward"),
@@ -868,7 +1039,7 @@ class SceneryPass {
868
1039
  return;
869
1040
  this.sceneryTexture = uploadGridAtlas(this.gl, bitmap);
870
1041
  }
871
- draw() {
1042
+ draw(camera) {
872
1043
  const gl = this.gl;
873
1044
  if (!this.sceneryProgram || !this.sceneryLoc || !this.sceneryTexture)
874
1045
  return;
@@ -878,23 +1049,15 @@ class SceneryPass {
878
1049
  if (view.length === 0)
879
1050
  return;
880
1051
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
881
- const FLOAT2 = 4;
882
- const STRIDE2 = SCENERY_FLOATS_PER_VERTEX * FLOAT2;
883
- const bind = (loc, fieldIndex) => {
884
- if (loc < 0)
885
- return;
886
- gl.enableVertexAttribArray(loc);
887
- gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE2, fieldIndex * FLOAT2);
888
- };
889
- bind(this.sceneryLoc.a_cornerX, 0);
890
- bind(this.sceneryLoc.a_cornerY, 1);
891
- bind(this.sceneryLoc.a_cornerZ, 2);
892
- bind(this.sceneryLoc.a_u, 3);
893
- bind(this.sceneryLoc.a_v, 4);
1052
+ bindFloat3(gl, this.sceneryLoc.a_cornerX, 0);
1053
+ bindFloat3(gl, this.sceneryLoc.a_cornerY, 1);
1054
+ bindFloat3(gl, this.sceneryLoc.a_cornerZ, 2);
1055
+ bindFloat3(gl, this.sceneryLoc.a_u, 3);
1056
+ bindFloat3(gl, this.sceneryLoc.a_v, 4);
894
1057
  if (this.sceneryUni) {
895
- const cam = this.engine.camera_basis();
896
- gl.uniform3fv(this.sceneryUni.u_camPos, cam.subarray(0, 3));
897
- gl.uniform3fv(this.sceneryUni.u_camForward, cam.subarray(3, 6));
1058
+ const cam = camera.current();
1059
+ gl.uniform3fv(this.sceneryUni.u_camPos, cam.position);
1060
+ gl.uniform3fv(this.sceneryUni.u_camForward, cam.forward);
898
1061
  gl.uniform1f(this.sceneryUni.u_aspect, this.engine.aspect());
899
1062
  }
900
1063
  gl.enable(gl.BLEND);
@@ -905,7 +1068,7 @@ class SceneryPass {
905
1068
  const quadCount = view.length / SCENERY_FLOATS_PER_VERTEX / VERTS_PER_QUAD_INDEXED;
906
1069
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.sceneryIndexBuffer);
907
1070
  gl.drawElements(gl.TRIANGLES, quadCount * 6, gl.UNSIGNED_SHORT, 0);
908
- disableAttributes(gl, this.sceneryLoc);
1071
+ disableAttributes(gl, this.sceneryLocList);
909
1072
  gl.depthMask(true);
910
1073
  gl.disable(gl.BLEND);
911
1074
  }
@@ -939,8 +1102,8 @@ void main() {
939
1102
  var sky_frag_default = `// ── Sky pass — fragment shader ────────────────────────────────────────────────
940
1103
  //
941
1104
  // The whole cloud shape lives here (sky-cloud.md §4/§7): a small union of overlapping circular lobes,
942
- // combined with \`max\` — no \`if\`/\`else\` (feedback_shader_branching.md). Blended over the existing
943
- // sky-blue clear with the standard \`over\` operator; this shader never writes depth (depthMask(false),
1105
+ // combined with \`max\` — no \`if\`/\`else\` (feedback_shader_branching.md). Blended over what the band
1106
+ // holds (the sky colour or the sky image of backdrop()) with the standard \`over\` operator; this shader never writes depth (depthMask(false),
944
1107
  // set by passes/sky.ts).
945
1108
 
946
1109
  precision mediump float;
@@ -1001,9 +1164,9 @@ void main() {
1001
1164
  `;
1002
1165
 
1003
1166
  // ../engine-binding/src/passes/sky.ts
1004
- var FLOAT2 = 4;
1167
+ var FLOAT4 = 4;
1005
1168
  var FLOATS_PER_VERTEX2 = 2;
1006
- var STRIDE2 = FLOATS_PER_VERTEX2 * FLOAT2;
1169
+ var STRIDE4 = FLOATS_PER_VERTEX2 * FLOAT4;
1007
1170
  var QUAD_NDC = new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1]);
1008
1171
 
1009
1172
  class SkyPass {
@@ -1012,11 +1175,13 @@ class SkyPass {
1012
1175
  vertexBuffer;
1013
1176
  indexBuffer;
1014
1177
  loc;
1178
+ locList;
1015
1179
  uni;
1016
1180
  constructor(gl) {
1017
1181
  this.gl = gl;
1018
1182
  this.program = createProgram(gl, sky_vert_default, sky_frag_default);
1019
1183
  this.loc = { a_ndc: gl.getAttribLocation(this.program, "a_ndc") };
1184
+ this.locList = Object.values(this.loc);
1020
1185
  this.uni = {
1021
1186
  u_aspect: gl.getUniformLocation(this.program, "u_aspect"),
1022
1187
  u_skyOffset: gl.getUniformLocation(this.program, "u_skyOffset")
@@ -1040,12 +1205,12 @@ class SkyPass {
1040
1205
  gl.useProgram(this.program);
1041
1206
  gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
1042
1207
  gl.enableVertexAttribArray(this.loc.a_ndc);
1043
- gl.vertexAttribPointer(this.loc.a_ndc, 2, gl.FLOAT, false, STRIDE2, 0);
1208
+ gl.vertexAttribPointer(this.loc.a_ndc, 2, gl.FLOAT, false, STRIDE4, 0);
1044
1209
  gl.uniform1f(this.uni.u_aspect, engine.aspect());
1045
1210
  gl.uniform1f(this.uni.u_skyOffset, engine.sky_parallax());
1046
1211
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
1047
1212
  gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
1048
- disableAttributes(gl, this.loc);
1213
+ disableAttributes(gl, this.locList);
1049
1214
  gl.depthMask(true);
1050
1215
  gl.disable(gl.BLEND);
1051
1216
  gl.disable(gl.SCISSOR_TEST);
@@ -1117,6 +1282,14 @@ void main() {
1117
1282
 
1118
1283
  // ../engine-binding/src/passes/text.ts
1119
1284
  var TEXT_FLOATS_PER_VERTEX = 5;
1285
+ var FLOAT5 = 4;
1286
+ var STRIDE5 = TEXT_FLOATS_PER_VERTEX * FLOAT5;
1287
+ function bindFloat4(gl, loc, fieldIndex) {
1288
+ if (loc < 0)
1289
+ return;
1290
+ gl.enableVertexAttribArray(loc);
1291
+ gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE5, fieldIndex * FLOAT5);
1292
+ }
1120
1293
  var TEXT_ERRORS = {
1121
1294
  TEXT_FONT: "The font is bad: scaleW, scaleH and lineHeight are finite and above 0, and base is finite and 0 or more.",
1122
1295
  TEXT_GLYPH: "A glyph has a bad rectangle, a negative size, or an offset or advance that is not a finite number.",
@@ -1137,6 +1310,7 @@ class TextPass {
1137
1310
  textBuffer = null;
1138
1311
  textIndexBuffer = null;
1139
1312
  textLoc = null;
1313
+ textLocList = [];
1140
1314
  uColor = null;
1141
1315
  textTexture = null;
1142
1316
  constructor(gl, engine) {
@@ -1152,6 +1326,7 @@ class TextPass {
1152
1326
  a_uScaled: gl.getAttribLocation(this.textProgram, "a_uScaled"),
1153
1327
  a_vScaled: gl.getAttribLocation(this.textProgram, "a_vScaled")
1154
1328
  };
1329
+ this.textLocList = Object.values(this.textLoc);
1155
1330
  gl.useProgram(this.textProgram);
1156
1331
  gl.uniform1f(gl.getUniformLocation(this.textProgram, "u_scaleNear"), bounds[0]);
1157
1332
  gl.uniform1f(gl.getUniformLocation(this.textProgram, "u_scaleFar"), bounds[1]);
@@ -1194,19 +1369,11 @@ class TextPass {
1194
1369
  if (view.length === 0)
1195
1370
  return;
1196
1371
  gl.bufferSubData(gl.ARRAY_BUFFER, 0, view);
1197
- const FLOAT3 = 4;
1198
- const STRIDE3 = TEXT_FLOATS_PER_VERTEX * FLOAT3;
1199
- const bind = (loc, fieldIndex) => {
1200
- if (loc < 0)
1201
- return;
1202
- gl.enableVertexAttribArray(loc);
1203
- gl.vertexAttribPointer(loc, 1, gl.FLOAT, false, STRIDE3, fieldIndex * FLOAT3);
1204
- };
1205
- bind(this.textLoc.a_ndcX, 0);
1206
- bind(this.textLoc.a_ndcY, 1);
1207
- bind(this.textLoc.a_scale, 2);
1208
- bind(this.textLoc.a_uScaled, 3);
1209
- bind(this.textLoc.a_vScaled, 4);
1372
+ bindFloat4(gl, this.textLoc.a_ndcX, 0);
1373
+ bindFloat4(gl, this.textLoc.a_ndcY, 1);
1374
+ bindFloat4(gl, this.textLoc.a_scale, 2);
1375
+ bindFloat4(gl, this.textLoc.a_uScaled, 3);
1376
+ bindFloat4(gl, this.textLoc.a_vScaled, 4);
1210
1377
  gl.enable(gl.BLEND);
1211
1378
  gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
1212
1379
  gl.enable(gl.DEPTH_TEST);
@@ -1219,10 +1386,9 @@ class TextPass {
1219
1386
  const start = ranges[i];
1220
1387
  const count = ranges[i + 1];
1221
1388
  gl.uniform4f(this.uColor, ranges[i + 2], ranges[i + 3], ranges[i + 4], ranges[i + 5]);
1222
- const { count: indexCount, offset } = quadRangeToIndexRange(start, count);
1223
- gl.drawElements(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, offset);
1389
+ drawQuadRange(gl, start, count);
1224
1390
  }
1225
- disableAttributes(gl, this.textLoc);
1391
+ disableAttributes(gl, this.textLocList);
1226
1392
  gl.depthMask(true);
1227
1393
  gl.disable(gl.BLEND);
1228
1394
  }
@@ -1695,12 +1861,19 @@ function trackTypes(table) {
1695
1861
  // ../engine-binding/src/scene.ts
1696
1862
  function backdrop({
1697
1863
  ground = [0, 0, 0],
1698
- sky: skyColor = [1, 1, 1]
1864
+ sky: above = [1, 1, 1]
1699
1865
  } = {}) {
1700
- return { kind: "backdrop", ground, sky: skyColor };
1866
+ return { kind: "backdrop", ground, sky: above };
1701
1867
  }
1702
- function road2() {
1703
- return { kind: "road" };
1868
+ function readSky(input) {
1869
+ if (Array.isArray(input))
1870
+ return { color: input, share: 0.5 };
1871
+ const form = typeof input === "object" && input !== null && "image" in input ? input : { image: input };
1872
+ const { image, color = [1, 1, 1], share = 0.5 } = form;
1873
+ return { color, share, image };
1874
+ }
1875
+ function road2({ asphalt = [0.43, 0.43, 0.43] } = {}) {
1876
+ return { kind: "road", asphalt };
1704
1877
  }
1705
1878
  function field2(inputs) {
1706
1879
  return { kind: "field", ...inputs };
@@ -1720,7 +1893,10 @@ var SCENE_ERRORS = {
1720
1893
  LAYER_MISSING: "The scene lists a layer that the track does not declare.",
1721
1894
  WEBGL_UNSUPPORTED: "This browser gave no WebGL context for the canvas.",
1722
1895
  SCENE_NO_TRACK: "This scene draws an engine the page moves itself, so it takes no dynamic inputs: scene.draw({ … }) needs a track from loadTrack.",
1723
- SCENE_ATLAS: "An atlas the scene names as a URL could not be fetched or decoded."
1896
+ SCENE_ATLAS: "An image the scene names as a URL could not be fetched or decoded.",
1897
+ SCENE_IMAGE_SIZE: "An image the scene names is larger than the largest texture of this device.",
1898
+ SCENE_IMAGE_MISSING: "The sky of backdrop() is not a colour and holds no image.",
1899
+ SCENE_IMAGE_SHARE: "The share of a sky image is a number above 0 and at most 1."
1724
1900
  };
1725
1901
  var ERROR_MESSAGES = {
1726
1902
  ...CORE_ERRORS,
@@ -1774,6 +1950,29 @@ function checkImages(listed) {
1774
1950
  if (labels && !labels.atlas) {
1775
1951
  throw imageError("TEXT_IMAGE_MISSING", TEXT_ERRORS.TEXT_IMAGE_MISSING, "text() needs atlas");
1776
1952
  }
1953
+ checkSky(listed);
1954
+ }
1955
+ function checkSky(listed) {
1956
+ const input = listed.backdrop?.sky;
1957
+ if (input === undefined || Array.isArray(input))
1958
+ return;
1959
+ const { image, share } = readSky(input);
1960
+ const prototype = image ? Object.getPrototypeOf(image) : null;
1961
+ if (!image || prototype === Object.prototype || prototype === null) {
1962
+ throw sceneError2("SCENE_IMAGE_MISSING", "backdrop({ sky })");
1963
+ }
1964
+ if (!(typeof share === "number" && share > 0 && share <= 1)) {
1965
+ throw sceneError2("SCENE_IMAGE_SHARE", "backdrop({ sky: { share } })");
1966
+ }
1967
+ }
1968
+ function checkAsphalt(listed) {
1969
+ if (!listed.road)
1970
+ return;
1971
+ const asphalt = listed.road.asphalt;
1972
+ const ok = Array.isArray(asphalt) && asphalt.length === 3 && Array.from(asphalt).every((part) => typeof part === "number" && part >= 0 && part <= 1);
1973
+ if (!ok) {
1974
+ throw new MovebyError("ROAD_ASPHALT", `${ROAD_ERRORS.ROAD_ASPHALT} (road({ asphalt }))`);
1975
+ }
1777
1976
  }
1778
1977
 
1779
1978
  class LayeredScene {
@@ -1786,17 +1985,27 @@ class LayeredScene {
1786
1985
  sky = null;
1787
1986
  scenery = null;
1788
1987
  text = null;
1988
+ camera;
1789
1989
  ready;
1790
1990
  constructor(gl, engine, listed, track) {
1791
1991
  this.gl = gl;
1792
1992
  this.engine = engine;
1793
1993
  this.track = track;
1994
+ this.camera = new CameraViews(engine);
1794
1995
  const late = [];
1795
1996
  if (listed.backdrop) {
1796
- this.backdrop = new BackdropPass(gl, listed.backdrop.ground, listed.backdrop.sky);
1997
+ const { color, share, image } = readSky(listed.backdrop.sky);
1998
+ const backdrop2 = new BackdropPass(gl, listed.backdrop.ground, color, share);
1999
+ this.backdrop = backdrop2;
2000
+ const upload = (source) => backdrop2.setSkyImage(source, "SCENE_IMAGE_SIZE", SCENE_ERRORS.SCENE_IMAGE_SIZE);
2001
+ if (typeof image === "string") {
2002
+ late.push(this.lateImage(image, upload, "premultiply"));
2003
+ } else if (image !== undefined) {
2004
+ upload(image);
2005
+ }
1797
2006
  }
1798
2007
  if (listed.road)
1799
- this.road = new RoadPass(gl, engine);
2008
+ this.road = new RoadPass(gl, engine, listed.road.asphalt);
1800
2009
  if (listed.sky)
1801
2010
  this.sky = new SkyPass(gl);
1802
2011
  if (listed.field) {
@@ -1804,7 +2013,7 @@ class LayeredScene {
1804
2013
  this.field = field3;
1805
2014
  const atlas = listed.field.atlas;
1806
2015
  if (typeof atlas === "string") {
1807
- late.push(this.lateAtlas(atlas, (bitmap) => field3.setFieldAtlas(bitmap)));
2016
+ late.push(this.lateImage(atlas, (bitmap) => field3.setFieldAtlas(bitmap)));
1808
2017
  } else if (atlas !== undefined) {
1809
2018
  field3.setFieldAtlas(atlas);
1810
2019
  }
@@ -1817,7 +2026,7 @@ class LayeredScene {
1817
2026
  this.scenery = scenery3;
1818
2027
  const atlas = listed.scenery.atlas;
1819
2028
  if (typeof atlas === "string") {
1820
- late.push(this.lateAtlas(atlas, (bitmap) => scenery3.setSceneryAtlas(bitmap)));
2029
+ late.push(this.lateImage(atlas, (bitmap) => scenery3.setSceneryAtlas(bitmap)));
1821
2030
  } else {
1822
2031
  scenery3.setSceneryAtlas(atlas);
1823
2032
  }
@@ -1827,7 +2036,7 @@ class LayeredScene {
1827
2036
  this.text = text3;
1828
2037
  const atlas = listed.text.atlas;
1829
2038
  if (typeof atlas === "string") {
1830
- late.push(this.lateAtlas(atlas, (bitmap) => text3.setTextAtlas(bitmap)));
2039
+ late.push(this.lateImage(atlas, (bitmap) => text3.setTextAtlas(bitmap)));
1831
2040
  } else {
1832
2041
  text3.setTextAtlas(atlas);
1833
2042
  }
@@ -1836,8 +2045,8 @@ class LayeredScene {
1836
2045
  return;
1837
2046
  });
1838
2047
  }
1839
- lateAtlas(url, upload) {
1840
- return decodeAtlas(url).then(upload, (reason) => {
2048
+ lateImage(url, upload, premultiplyAlpha = "none") {
2049
+ return decodeImage(url, premultiplyAlpha).then(upload, (reason) => {
1841
2050
  throw sceneError2("SCENE_ATLAS", `${url}: ${reason instanceof Error ? reason.message : String(reason)}`);
1842
2051
  });
1843
2052
  }
@@ -1862,11 +2071,13 @@ class LayeredScene {
1862
2071
  gl.clear(gl.DEPTH_BUFFER_BIT);
1863
2072
  gl.enable(gl.DEPTH_TEST);
1864
2073
  gl.depthFunc(gl.LEQUAL);
1865
- this.road?.draw();
1866
- this.field?.draw();
2074
+ engine.fill_camera();
2075
+ this.road?.draw(this.camera);
2076
+ this.field?.draw(this.camera);
1867
2077
  this.backdrop?.fillSky(horizonRow, canvas.width, canvas.height);
2078
+ this.backdrop?.drawSkyImage(horizonRow, canvas.width, canvas.height, engine.sky_parallax());
1868
2079
  this.sky?.draw(engine, horizonRow, canvas.width, canvas.height);
1869
- this.scenery?.draw();
2080
+ this.scenery?.draw(this.camera);
1870
2081
  this.text?.draw();
1871
2082
  }
1872
2083
  reset() {
@@ -1879,6 +2090,7 @@ function createScene(canvas, source, layers) {
1879
2090
  const listed = listByKind(layers);
1880
2091
  checkDeclared(listed, engine.layers());
1881
2092
  checkImages(listed);
2093
+ checkAsphalt(listed);
1882
2094
  const gl = canvas.getContext("webgl", { alpha: false });
1883
2095
  if (!gl)
1884
2096
  throw sceneError2("WEBGL_UNSUPPORTED", "getContext('webgl')");