@holtjs/stage 0.8.0 → 0.10.0

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.
@@ -1,4 +1,4 @@
1
- import { Pt as SceneHostLike, ft as Container, yt as DisplayObject } from "./game-CEa0m4eX.js";
1
+ import { It as SceneHostLike, bt as DisplayObject, pt as Container } from "./game-BbgtwqyW.js";
2
2
  export interface Camera25DConfig {
3
3
  viewportWidth: number;
4
4
  viewportHeight: number;
@@ -91,6 +91,29 @@ export declare class Camera25D {
91
91
  lengthRatio: number;
92
92
  };
93
93
  }
94
+ export interface Shadow25DConfig {
95
+ enabled?: boolean;
96
+ /** World-space sun azimuth in radians (`default = -Math.PI * 0.72`). */
97
+ sunAzimuthRad?: number;
98
+ /** Sun elevation above ground in radians (`default = 0.78`, ~45°). */
99
+ sunElevationRad?: number;
100
+ /** Silhouette shadow fill RGB (`default = 0x0b1020`, cool dark slate shadow). */
101
+ color?: number;
102
+ /** Daytime peak shadow opacity (`default = 0.32`). */
103
+ alpha?: number;
104
+ /** Night darkness in `[0, 1]` (from `DayNightState.darkness`; smoothly fades sun shadows out at dusk/night). */
105
+ darkness?: number;
106
+ }
107
+ export interface BillboardOptions25D {
108
+ wz?: number;
109
+ depthBias?: number;
110
+ baseScaleX?: number;
111
+ baseScaleY?: number;
112
+ /** Cast an oblique sun silhouette shadow onto `shadowLayer` (`default = true`). */
113
+ castShadow?: boolean;
114
+ /** Per-billboard shadow opacity multiplier (`default = 1`). */
115
+ shadowAlpha?: number;
116
+ }
94
117
  export interface BillboardEntry {
95
118
  readonly node: DisplayObject;
96
119
  wx: number;
@@ -99,6 +122,14 @@ export interface BillboardEntry {
99
122
  baseScaleX: number;
100
123
  baseScaleY: number;
101
124
  depthBias: number;
125
+ castShadow: boolean;
126
+ shadowAlpha: number;
127
+ }
128
+ export interface MountStage25DOptions {
129
+ depth?: number;
130
+ trackId?: string;
131
+ /** Enable real-time oblique sun-cast silhouette shadows (`true` or `Shadow25DConfig`). */
132
+ shadows?: boolean | Shadow25DConfig;
102
133
  }
103
134
  export interface Stage25DRig {
104
135
  readonly camera: Camera25D;
@@ -106,26 +137,28 @@ export interface Stage25DRig {
106
137
  readonly root: Container;
107
138
  /** Ground container whose children (e.g. `ChunkedTilemap`) automatically rotate by `-yaw` and tilt by `sin(pitch)`. */
108
139
  readonly groundWorld: Container;
140
+ /** Ground-cast silhouette shadow container (`zIndex = 5`: strictly above `groundWorld` tiles and strictly below `billboardLayer`). */
141
+ readonly shadowLayer: Container;
109
142
  /** Upright billboard container whose registered sprites/rigs stay vertical facing the lens while sorting by 2.5D depth. */
110
143
  readonly billboardLayer: Container;
111
- registerBillboard(node: DisplayObject, wx: number, wy: number, opts?: {
112
- wz?: number;
113
- depthBias?: number;
114
- baseScaleX?: number;
115
- baseScaleY?: number;
116
- }): BillboardEntry;
144
+ registerBillboard(node: DisplayObject, wx: number, wy: number, opts?: BillboardOptions25D): BillboardEntry;
117
145
  removeBillboard(entryOrNode: BillboardEntry | DisplayObject): void;
118
- /** Advance `Camera25D.step(deltaMs)` and update the ground plane matrix + all registered upright billboards. */
146
+ /** Configure or update oblique sun-cast silhouette shadows across all billboards (`enabled` defaults to `true` when called). */
147
+ setSunShadow(cfg: Shadow25DConfig): void;
148
+ /** Sync sun shadow azimuth, elevation, and dusk/night fade directly from `DayNightState` (`DayNightCycle.step()` / `Lighting2DSystem.update()`). */
149
+ syncDayNight(state: {
150
+ sunAzimuthRad: number;
151
+ sunElevationRad: number;
152
+ darkness: number;
153
+ }): void;
154
+ /** Advance `Camera25D.step(deltaMs)` and update the ground plane matrix, ground silhouette shadows, and upright billboards. */
119
155
  update(deltaMs?: number): void;
120
156
  destroy(): void;
121
157
  }
122
158
  /**
123
159
  * Mount a complete *Don't Starve*-style 2.5D rotatable world rig onto a Stage `Scene`:
124
160
  * - Attach any flat 2D map (`MountedChunkedTilemap.container`, ground decals, farm plots) into `groundWorld`
125
- * - Register upright trees, tombstones, campfires, and `Rig2DView.container` characters via `registerBillboard()`
126
- * Works identically across WebGL2, WebGPU, Canvas2D, and Node 60fps Headless!
161
+ * - Register upright trees, tombstones, campfires, and `SpriteObject` / `Container` characters via `registerBillboard()`
162
+ * - Enable `{ shadows: true }` (or `rig.syncDayNight(state)`) for true oblique sun-cast silhouette shadows across WebGPU, WebGL2, Canvas2D & Headless!
127
163
  */
128
- export declare function mountStage25D(scene: SceneHostLike, camera: Camera25D, opts?: {
129
- depth?: number;
130
- trackId?: string;
131
- }): Stage25DRig;
164
+ export declare function mountStage25D(scene: SceneHostLike, camera: Camera25D, opts?: MountStage25DOptions): Stage25DRig;
@@ -1,3 +1,4 @@
1
+ import { u as TintModes } from "./types-CXsCvp1p.js";
1
2
  // src/projection.ts
2
3
  const TWO_PI = Math.PI * 2;
3
4
  function shortestAngleDiff(from, to) {
@@ -175,11 +176,36 @@ var Camera25D = class {
175
176
  return out;
176
177
  }
177
178
  };
179
+ function syncImageCloneToSource(clone, src, shadowColor) {
180
+ if (clone.texture !== src.texture || clone.frame !== src.frame) {
181
+ clone.texture = src.texture;
182
+ clone.frame = src.frame;
183
+ clone.width = src.width;
184
+ clone.height = src.height;
185
+ }
186
+ clone.customOrigin = src.customOrigin;
187
+ clone._displayOriginX = src.displayOriginX;
188
+ clone._displayOriginY = src.displayOriginY;
189
+ clone.originX = src.originX;
190
+ clone.originY = src.originY;
191
+ clone.flipX = src.flipX;
192
+ clone.flipY = src.flipY;
193
+ clone.isCropped = src.isCropped;
194
+ if (src.isCropped) {
195
+ clone.cropX = src.cropX;
196
+ clone.cropY = src.cropY;
197
+ clone.cropW = src.cropW;
198
+ clone.cropH = src.cropH;
199
+ }
200
+ clone.tint = shadowColor;
201
+ clone.isTinted = true;
202
+ clone.tintMode = TintModes.FILL;
203
+ }
178
204
  /**
179
205
  * Mount a complete *Don't Starve*-style 2.5D rotatable world rig onto a Stage `Scene`:
180
206
  * - Attach any flat 2D map (`MountedChunkedTilemap.container`, ground decals, farm plots) into `groundWorld`
181
- * - Register upright trees, tombstones, campfires, and `Rig2DView.container` characters via `registerBillboard()`
182
- * Works identically across WebGL2, WebGPU, Canvas2D, and Node 60fps Headless!
207
+ * - Register upright trees, tombstones, campfires, and `SpriteObject` / `Container` characters via `registerBillboard()`
208
+ * - Enable `{ shadows: true }` (or `rig.syncDayNight(state)`) for true oblique sun-cast silhouette shadows across WebGPU, WebGL2, Canvas2D & Headless!
183
209
  */
184
210
  function mountStage25D(scene, camera, opts = {}) {
185
211
  const root = scene.add.container(0, 0);
@@ -193,16 +219,80 @@ function mountStage25D(scene, camera, opts = {}) {
193
219
  yawStage.add(groundWorld);
194
220
  tiltStage.add(yawStage);
195
221
  root.add(tiltStage);
222
+ const shadowLayer = scene.make.container(0, 0);
223
+ shadowLayer.zIndex = 5;
224
+ root.add(shadowLayer);
196
225
  const billboardLayer = scene.make.container(0, 0);
197
226
  billboardLayer.zIndex = 10;
198
227
  root.add(billboardLayer);
228
+ const initialShadowCfg = typeof opts.shadows === "object" && opts.shadows !== null ? opts.shadows : { enabled: Boolean(opts.shadows) };
229
+ const shadowState = {
230
+ enabled: initialShadowCfg.enabled ?? Boolean(opts.shadows),
231
+ sunAzimuthRad: initialShadowCfg.sunAzimuthRad ?? -Math.PI * .72,
232
+ sunElevationRad: initialShadowCfg.sunElevationRad ?? .78,
233
+ color: initialShadowCfg.color ?? 725024,
234
+ alpha: initialShadowCfg.alpha ?? .32,
235
+ darkness: initialShadowCfg.darkness ?? 0
236
+ };
237
+ shadowLayer.visible = shadowState.enabled;
199
238
  const entries = [];
239
+ const shadowMap = /* @__PURE__ */ new Map();
200
240
  const projScratch = {
201
241
  x: 0,
202
242
  y: 0,
203
243
  depth: 0,
204
244
  scale: 1
205
245
  };
246
+ const groundFootScratch = {
247
+ x: 0,
248
+ y: 0,
249
+ depth: 0,
250
+ scale: 1
251
+ };
252
+ const sunVecScratch = {
253
+ dx: 0,
254
+ dy: 0,
255
+ lengthRatio: 1
256
+ };
257
+ const destroyShadowRecord = (entry) => {
258
+ const rec = shadowMap.get(entry);
259
+ if (!rec) return;
260
+ shadowMap.delete(entry);
261
+ rec.cancelDispose();
262
+ rec.shadowNode.destroy();
263
+ rec.childClones.length = 0;
264
+ };
265
+ const ensureShadowRecord = (entry) => {
266
+ const existing = shadowMap.get(entry);
267
+ if (existing) return existing;
268
+ const t = entry.node.type;
269
+ if (t === "Image" || t === "Sprite") {
270
+ const srcImg = entry.node;
271
+ const shImg = scene.make.image(0, 0, srcImg.texture.key, srcImg.frame.name);
272
+ shadowLayer.add(shImg);
273
+ const rec = {
274
+ kind: "image",
275
+ shadowNode: shImg,
276
+ childClones: [],
277
+ cancelDispose: entry.node.onDispose(() => destroyShadowRecord(entry))
278
+ };
279
+ shadowMap.set(entry, rec);
280
+ return rec;
281
+ }
282
+ if (t === "Container") {
283
+ const shCont = scene.make.container(0, 0);
284
+ shadowLayer.add(shCont);
285
+ const rec = {
286
+ kind: "container",
287
+ shadowNode: shCont,
288
+ childClones: [],
289
+ cancelDispose: entry.node.onDispose(() => destroyShadowRecord(entry))
290
+ };
291
+ shadowMap.set(entry, rec);
292
+ return rec;
293
+ }
294
+ return null;
295
+ };
206
296
  const sync = () => {
207
297
  tiltStage.x = camera.viewportWidth * .5;
208
298
  tiltStage.y = camera.viewportHeight * .5;
@@ -211,6 +301,14 @@ function mountStage25D(scene, camera, opts = {}) {
211
301
  yawStage.rotation = -camera.yaw;
212
302
  groundWorld.x = -camera.focusX;
213
303
  groundWorld.y = -camera.focusY;
304
+ const effShadowAlpha = shadowState.enabled ? shadowState.alpha * Math.max(0, 1 - shadowState.darkness / .85) : 0;
305
+ const drawShadows = effShadowAlpha > .005;
306
+ shadowLayer.visible = drawShadows;
307
+ if (drawShadows) camera.computeSunShadowVector(shadowState.sunAzimuthRad, shadowState.sunElevationRad, 1, sunVecScratch);
308
+ for (let i = entries.length - 1; i >= 0; i--) if (!entries[i].node.active) {
309
+ destroyShadowRecord(entries[i]);
310
+ entries.splice(i, 1);
311
+ }
214
312
  for (let i = 0; i < entries.length; i++) {
215
313
  const e = entries[i];
216
314
  camera.worldToScreen(e.wx, e.wy, e.wz, projScratch);
@@ -218,7 +316,89 @@ function mountStage25D(scene, camera, opts = {}) {
218
316
  e.node.y = projScratch.y;
219
317
  e.node.scaleX = e.baseScaleX * projScratch.scale;
220
318
  e.node.scaleY = e.baseScaleY * projScratch.scale;
221
- e.node.zIndex = Math.round((projScratch.depth + e.depthBias) * 100);
319
+ const zKey = Math.round((projScratch.depth + e.depthBias) * 100);
320
+ e.node.zIndex = zKey;
321
+ if (!drawShadows) continue;
322
+ if (!e.castShadow || !e.node.visible || e.node.alpha <= 0 || e.shadowAlpha <= 0) {
323
+ const rec = shadowMap.get(e);
324
+ if (rec) rec.shadowNode.visible = false;
325
+ continue;
326
+ }
327
+ const rec = ensureShadowRecord(e);
328
+ if (!rec) continue;
329
+ let footX = projScratch.x;
330
+ let footY = projScratch.y;
331
+ if (e.wz !== 0) {
332
+ camera.worldToScreen(e.wx, e.wy, 0, groundFootScratch);
333
+ footX = groundFootScratch.x + sunVecScratch.dx * e.wz;
334
+ footY = groundFootScratch.y + sunVecScratch.dy * e.wz;
335
+ }
336
+ const cx = -sunVecScratch.dx * e.baseScaleY;
337
+ const cy = -sunVecScratch.dy * e.baseScaleY;
338
+ const lenY = Math.hypot(cx, cy);
339
+ const shSkewX = lenY > 1e-5 ? Math.atan2(cx, cy) : 0;
340
+ const shScaleX = e.baseScaleX * projScratch.scale;
341
+ const nodeAlpha = Math.min(1, effShadowAlpha * e.shadowAlpha * e.node.alpha);
342
+ if (rec.kind === "image") {
343
+ const shImg = rec.shadowNode;
344
+ const srcImg = e.node;
345
+ if (srcImg.frame?.empty) {
346
+ shImg.visible = false;
347
+ continue;
348
+ }
349
+ syncImageCloneToSource(shImg, srcImg, shadowState.color);
350
+ shImg.visible = true;
351
+ shImg.x = footX;
352
+ shImg.y = footY;
353
+ shImg.scaleX = shScaleX;
354
+ shImg.scaleY = lenY;
355
+ shImg.rotation = 0;
356
+ shImg.skewX = shSkewX;
357
+ shImg.skewY = 0;
358
+ shImg.alpha = nodeAlpha;
359
+ shImg.zIndex = zKey;
360
+ } else {
361
+ const shCont = rec.shadowNode;
362
+ const srcCont = e.node;
363
+ shCont.visible = true;
364
+ shCont.x = footX;
365
+ shCont.y = footY;
366
+ shCont.scaleX = shScaleX;
367
+ shCont.scaleY = lenY;
368
+ shCont.rotation = 0;
369
+ shCont.skewX = shSkewX;
370
+ shCont.skewY = 0;
371
+ shCont.alpha = nodeAlpha;
372
+ shCont.zIndex = zKey;
373
+ const srcChildren = srcCont.list;
374
+ let used = 0;
375
+ for (let ci = 0; ci < srcChildren.length; ci++) {
376
+ const child = srcChildren[ci];
377
+ if (!child.active || !child.visible || child.alpha <= 0) continue;
378
+ if (child.type !== "Image" && child.type !== "Sprite") continue;
379
+ const srcChildImg = child;
380
+ if (srcChildImg.frame?.empty) continue;
381
+ let clone = rec.childClones[used];
382
+ if (!clone) {
383
+ clone = scene.make.image(0, 0, srcChildImg.texture.key, srcChildImg.frame.name);
384
+ shCont.add(clone);
385
+ rec.childClones.push(clone);
386
+ }
387
+ syncImageCloneToSource(clone, srcChildImg, shadowState.color);
388
+ clone.visible = true;
389
+ clone.x = srcChildImg.x;
390
+ clone.y = srcChildImg.y;
391
+ clone.scaleX = srcChildImg.scaleX;
392
+ clone.scaleY = srcChildImg.scaleY;
393
+ clone.rotation = srcChildImg.rotation;
394
+ clone.skewX = srcChildImg.skewX;
395
+ clone.skewY = srcChildImg.skewY;
396
+ clone.alpha = srcChildImg.alpha;
397
+ clone.zIndex = srcChildImg.zIndex;
398
+ used++;
399
+ }
400
+ for (let extra = used; extra < rec.childClones.length; extra++) rec.childClones[extra].visible = false;
401
+ }
222
402
  }
223
403
  };
224
404
  sync();
@@ -226,6 +406,7 @@ function mountStage25D(scene, camera, opts = {}) {
226
406
  camera,
227
407
  root,
228
408
  groundWorld,
409
+ shadowLayer,
229
410
  billboardLayer,
230
411
  registerBillboard(node, wx, wy, bOpts = {}) {
231
412
  node.removeFromDisplayList();
@@ -237,7 +418,9 @@ function mountStage25D(scene, camera, opts = {}) {
237
418
  wz: bOpts.wz ?? 0,
238
419
  baseScaleX: bOpts.baseScaleX ?? node.scaleX,
239
420
  baseScaleY: bOpts.baseScaleY ?? node.scaleY,
240
- depthBias: bOpts.depthBias ?? 0
421
+ depthBias: bOpts.depthBias ?? 0,
422
+ castShadow: bOpts.castShadow !== false,
423
+ shadowAlpha: bOpts.shadowAlpha ?? 1
241
424
  };
242
425
  entries.push(entry);
243
426
  return entry;
@@ -246,15 +429,34 @@ function mountStage25D(scene, camera, opts = {}) {
246
429
  const idx = entries.findIndex((e) => e === entryOrNode || e.node === entryOrNode);
247
430
  if (idx >= 0) {
248
431
  const [removed] = entries.splice(idx, 1);
432
+ destroyShadowRecord(removed);
249
433
  billboardLayer.remove(removed.node);
250
434
  }
251
435
  },
436
+ setSunShadow(cfg) {
437
+ shadowState.enabled = cfg.enabled ?? true;
438
+ if (cfg.sunAzimuthRad !== void 0) shadowState.sunAzimuthRad = cfg.sunAzimuthRad;
439
+ if (cfg.sunElevationRad !== void 0) shadowState.sunElevationRad = cfg.sunElevationRad;
440
+ if (cfg.color !== void 0) shadowState.color = cfg.color;
441
+ if (cfg.alpha !== void 0) shadowState.alpha = cfg.alpha;
442
+ if (cfg.darkness !== void 0) shadowState.darkness = cfg.darkness;
443
+ sync();
444
+ },
445
+ syncDayNight(state) {
446
+ shadowState.enabled = true;
447
+ shadowState.sunAzimuthRad = state.sunAzimuthRad;
448
+ shadowState.sunElevationRad = state.sunElevationRad;
449
+ shadowState.darkness = state.darkness;
450
+ sync();
451
+ },
252
452
  update(deltaMs = 0) {
253
453
  if (deltaMs > 0) camera.step(deltaMs);
254
454
  sync();
255
455
  },
256
456
  destroy() {
457
+ for (let i = 0; i < entries.length; i++) destroyShadowRecord(entries[i]);
257
458
  entries.length = 0;
459
+ shadowMap.clear();
258
460
  root.destroy();
259
461
  }
260
462
  };