@holtjs/stage 0.7.0 → 0.9.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 { Nt as SceneHostLike, dt as Container, vt as DisplayObject } from "./game-3L9wS-Bv.js";
1
+ import { Pt as SceneHostLike, ft as Container, yt as DisplayObject } from "./game-DnJYRLZP.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,83 @@ 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
+ syncImageCloneToSource(shImg, e.node, shadowState.color);
345
+ shImg.visible = true;
346
+ shImg.x = footX;
347
+ shImg.y = footY;
348
+ shImg.scaleX = shScaleX;
349
+ shImg.scaleY = lenY;
350
+ shImg.rotation = 0;
351
+ shImg.skewX = shSkewX;
352
+ shImg.skewY = 0;
353
+ shImg.alpha = nodeAlpha;
354
+ shImg.zIndex = zKey;
355
+ } else {
356
+ const shCont = rec.shadowNode;
357
+ const srcCont = e.node;
358
+ shCont.visible = true;
359
+ shCont.x = footX;
360
+ shCont.y = footY;
361
+ shCont.scaleX = shScaleX;
362
+ shCont.scaleY = lenY;
363
+ shCont.rotation = 0;
364
+ shCont.skewX = shSkewX;
365
+ shCont.skewY = 0;
366
+ shCont.alpha = nodeAlpha;
367
+ shCont.zIndex = zKey;
368
+ const srcChildren = srcCont.list;
369
+ let used = 0;
370
+ for (let ci = 0; ci < srcChildren.length; ci++) {
371
+ const child = srcChildren[ci];
372
+ if (!child.active || !child.visible || child.alpha <= 0) continue;
373
+ if (child.type !== "Image" && child.type !== "Sprite") continue;
374
+ const srcChildImg = child;
375
+ let clone = rec.childClones[used];
376
+ if (!clone) {
377
+ clone = scene.make.image(0, 0, srcChildImg.texture.key, srcChildImg.frame.name);
378
+ shCont.add(clone);
379
+ rec.childClones.push(clone);
380
+ }
381
+ syncImageCloneToSource(clone, srcChildImg, shadowState.color);
382
+ clone.visible = true;
383
+ clone.x = srcChildImg.x;
384
+ clone.y = srcChildImg.y;
385
+ clone.scaleX = srcChildImg.scaleX;
386
+ clone.scaleY = srcChildImg.scaleY;
387
+ clone.rotation = srcChildImg.rotation;
388
+ clone.skewX = srcChildImg.skewX;
389
+ clone.skewY = srcChildImg.skewY;
390
+ clone.alpha = srcChildImg.alpha;
391
+ clone.zIndex = srcChildImg.zIndex;
392
+ used++;
393
+ }
394
+ for (let extra = used; extra < rec.childClones.length; extra++) rec.childClones[extra].visible = false;
395
+ }
222
396
  }
223
397
  };
224
398
  sync();
@@ -226,6 +400,7 @@ function mountStage25D(scene, camera, opts = {}) {
226
400
  camera,
227
401
  root,
228
402
  groundWorld,
403
+ shadowLayer,
229
404
  billboardLayer,
230
405
  registerBillboard(node, wx, wy, bOpts = {}) {
231
406
  node.removeFromDisplayList();
@@ -237,7 +412,9 @@ function mountStage25D(scene, camera, opts = {}) {
237
412
  wz: bOpts.wz ?? 0,
238
413
  baseScaleX: bOpts.baseScaleX ?? node.scaleX,
239
414
  baseScaleY: bOpts.baseScaleY ?? node.scaleY,
240
- depthBias: bOpts.depthBias ?? 0
415
+ depthBias: bOpts.depthBias ?? 0,
416
+ castShadow: bOpts.castShadow !== false,
417
+ shadowAlpha: bOpts.shadowAlpha ?? 1
241
418
  };
242
419
  entries.push(entry);
243
420
  return entry;
@@ -246,15 +423,34 @@ function mountStage25D(scene, camera, opts = {}) {
246
423
  const idx = entries.findIndex((e) => e === entryOrNode || e.node === entryOrNode);
247
424
  if (idx >= 0) {
248
425
  const [removed] = entries.splice(idx, 1);
426
+ destroyShadowRecord(removed);
249
427
  billboardLayer.remove(removed.node);
250
428
  }
251
429
  },
430
+ setSunShadow(cfg) {
431
+ shadowState.enabled = cfg.enabled ?? true;
432
+ if (cfg.sunAzimuthRad !== void 0) shadowState.sunAzimuthRad = cfg.sunAzimuthRad;
433
+ if (cfg.sunElevationRad !== void 0) shadowState.sunElevationRad = cfg.sunElevationRad;
434
+ if (cfg.color !== void 0) shadowState.color = cfg.color;
435
+ if (cfg.alpha !== void 0) shadowState.alpha = cfg.alpha;
436
+ if (cfg.darkness !== void 0) shadowState.darkness = cfg.darkness;
437
+ sync();
438
+ },
439
+ syncDayNight(state) {
440
+ shadowState.enabled = true;
441
+ shadowState.sunAzimuthRad = state.sunAzimuthRad;
442
+ shadowState.sunElevationRad = state.sunElevationRad;
443
+ shadowState.darkness = state.darkness;
444
+ sync();
445
+ },
252
446
  update(deltaMs = 0) {
253
447
  if (deltaMs > 0) camera.step(deltaMs);
254
448
  sync();
255
449
  },
256
450
  destroy() {
451
+ for (let i = 0; i < entries.length; i++) destroyShadowRecord(entries[i]);
257
452
  entries.length = 0;
453
+ shadowMap.clear();
258
454
  root.destroy();
259
455
  }
260
456
  };