@umicat/three-sdk 0.8.1 → 0.8.2

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.
@@ -211,6 +211,42 @@ export async function loadScene3D(scene3d, manifest, opts = {}) {
211
211
  bodies.set(e.id, body);
212
212
  }
213
213
  }
214
+ // Fit every shadow-casting directional light to the scene it is lighting.
215
+ //
216
+ // three.js defaults a directional light's shadow camera to an orthographic
217
+ // box of +-5 units, and nothing warns when a scene is larger: shadows simply
218
+ // stop at an invisible square edge, which reads as a strange dark patch in
219
+ // the middle of the ground rather than as a setting. A board 13 units across
220
+ // hits it immediately.
221
+ {
222
+ const bounds = new THREE.Box3();
223
+ for (const o of entities.values())
224
+ bounds.expandByObject(o);
225
+ if (!bounds.isEmpty()) {
226
+ const size = bounds.getSize(new THREE.Vector3());
227
+ const centre = bounds.getCenter(new THREE.Vector3());
228
+ const radius = Math.max(size.x, size.z) * 0.62 + 1;
229
+ for (const l of scene.children) {
230
+ const d = l;
231
+ if (!d.isDirectionalLight || !d.castShadow)
232
+ continue;
233
+ const c = d.shadow.camera;
234
+ c.left = -radius;
235
+ c.right = radius;
236
+ c.top = radius;
237
+ c.bottom = -radius;
238
+ c.near = 0.1;
239
+ c.far = d.position.length() + radius * 2 + size.y;
240
+ c.updateProjectionMatrix();
241
+ // The light points at the scene, not at the origin — an offset scene
242
+ // would otherwise be lit from the side of a box centred somewhere else.
243
+ d.target.position.copy(centre);
244
+ scene.add(d.target);
245
+ d.shadow.mapSize.set(2048, 2048);
246
+ d.shadow.bias = -0.0012;
247
+ }
248
+ }
249
+ }
214
250
  const camera = new THREE.PerspectiveCamera(scene3d.camera?.fov ?? 55, 1, 0.1, 500);
215
251
  const camOffset = toVec(scene3d.camera?.offset ?? { x: 0, y: 4, z: 8 });
216
252
  camera.position.copy(camOffset);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umicat/three-sdk",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "Three.js runtime for Umicat games: the scene3d design format, its loader with physics, a kinematic character controller, and the Umicat platform via @umicat/platform-sdk.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",