File size: 2,737 Bytes
970fc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as THREE from "three";
import { isCamera, isLight, objectQuaternion, rigFrame } from "../../shared/cinema.mjs";
function createCinemaLayer(world) {
  const root = new THREE.Group();
  world.add(root);
  let key = "", definition = { objects: [], curves: [] };
  const cameras = /* @__PURE__ */ new Map(), lights = /* @__PURE__ */ new Map();
  function clear() {
    for (const entry of lights.values()) {
      entry.light.dispose?.();
    }
    root.clear();
    cameras.clear();
    lights.clear();
  }
  return {
    cameras,
    lights,
    root,
    sync(scene) {
      const next = JSON.stringify(scene);
      if (next === key) return;
      key = next;
      definition = scene;
      clear();
      for (const o of scene.objects) {
        if (isCamera(o)) {
          const camera = new THREE.PerspectiveCamera(o.camera.fov, o.camera.aspect, 0.02, 200);
          root.add(camera);
          cameras.set(o.id, camera);
        }
        if (isLight(o)) {
          const p = o.light, light = p.type === "spot" ? new THREE.SpotLight(p.color, p.intensity, p.range, p.angle * Math.PI / 180, 0.35) : new THREE.PointLight(p.color, p.intensity, p.range);
          root.add(light);
          if (light.target) root.add(light.target);
          lights.set(o.id, { light });
        }
      }
    },
    frame(time = 0, targets = [], overrides = [], activeIds = null, triggerClocks = {}) {
      const changes = new Map(overrides.map((p) => [p.id, p])), frames = [], active = activeIds === null ? null : new Set(activeIds);
      for (const o of definition.objects) {
        const localTime = o.track?.trigger ? triggerClocks[o.id] ?? null : time;
        const animate = time !== null && localTime !== null && (!active || active.has(o.id));
        const f = { ...rigFrame(animate ? o : { ...o, track: null }, definition.curves || [], animate ? localTime : 0, animate ? targets : [...definition.objects, ...definition.actors || []]), ...changes.get(o.id) };
        frames.push(f);
        const camera = cameras.get(o.id);
        if (camera) {
          camera.position.fromArray(f.position);
          camera.quaternion.fromArray(f.quaternion || objectQuaternion(o));
          camera.updateMatrixWorld(true);
        }
        const light = lights.get(o.id)?.light;
        if (light) {
          light.position.fromArray(f.position);
          light.intensity = f.intensity ?? o.light.intensity;
          if (light.target) light.target.position.set(0, 0, -1).applyQuaternion(new THREE.Quaternion(...f.quaternion)).add(light.position);
        }
      }
      root.updateMatrixWorld(true);
      return frames;
    },
    dispose() {
      clear();
      world.remove(root);
    }
  };
}
export {
  createCinemaLayer
};