Needle Engine

Changes between version 3.16.0-beta and 3.16.1-beta
Files changed (2) hide show
  1. src/engine-components/CameraUtils.ts +38 -26
  2. src/engine/engine_context.ts +1 -1
src/engine-components/CameraUtils.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { OrbitControls } from "./OrbitControls.js";
2
2
  import { addNewComponent, getOrAddComponent } from "../engine/engine_components.js";
3
3
  import { Object3D } from "three";
4
- import { ICamera } from "../engine/engine_types.js";
4
+ import { ICamera, IContext } from "../engine/engine_types.js";
5
5
  import { RGBAColor } from "./js-extensions/RGBAColor.js";
6
6
  import { ContextEvent, ContextRegistry } from "../engine/engine_context_registry.js";
7
7
  import { getCameraController } from "../engine/engine_camera.js";
@@ -14,28 +14,35 @@
14
14
  ContextRegistry.registerCallback(ContextEvent.MissingCamera, (evt) => {
15
15
  if (debug) console.warn("Creating missing camera")
16
16
  const scene = evt.context.scene;
17
- const srcId = "unknown";
18
17
 
19
18
  const cameraObject = new Object3D();
19
+ cameraObject.name = "Default Fallback Camera"
20
20
  scene.add(cameraObject);
21
21
 
22
22
  const camInstance = new Camera();
23
+ camInstance.sourceId = evt.files?.[0]?.src ?? "unknown"
24
+ // Set the clearFlags to a skybox
25
+ camInstance.clearFlags = 1;
26
+ camInstance.backgroundColor = new RGBAColor(0.5, 0.5, 0.5, 1);
27
+ camInstance.fieldOfView = 35;
28
+ // TODO: can we store the backgroundBlurriness in the gltf file somewhere except inside the camera?
29
+ // e.g. when we export a scene from blender without a camera in the scene
30
+ camInstance.backgroundBlurriness = .5;
23
31
  const cam = addNewComponent(cameraObject, camInstance, true) as ICamera;
24
- cam.sourceId = srcId;
25
- cam.clearFlags = 2;
26
- cam.backgroundColor = new RGBAColor(0.5, 0.5, 0.5, 1);
27
- cam.fieldOfView = 35;
28
32
 
29
33
  cameraObject.position.x = 0;
30
34
  cameraObject.position.y = 1;
31
35
  cameraObject.position.z = 2;
32
36
 
37
+
38
+ createDefaultCameraControls(evt.context, cam);
39
+
33
40
  return cam;
34
41
  });
35
42
 
36
43
  ContextRegistry.registerCallback(ContextEvent.ContextCreated, (evt) => {
37
44
  if (!evt.context.mainCamera) {
38
- if(debug) console.log("Will not auto-fit because a default camera exists");
45
+ if (debug) console.log("Will not auto-fit because a default camera exists");
39
46
  return;
40
47
  }
41
48
 
@@ -47,27 +54,32 @@
47
54
  // Check if something else already acts as a camera controller
48
55
  const existing = getCameraController(evt.context.mainCamera);
49
56
  if (existing?.isCameraController === true) {
50
- if(debug) console.log("Will not auto-fit because a camera controller exists");
57
+ if (debug) console.log("Will not auto-fit because a camera controller exists");
51
58
  return;
52
59
  }
60
+ createDefaultCameraControls(evt.context);
61
+ }
62
+ })
53
63
 
54
- const cam = evt.context.mainCameraComponent;
55
- const cameraObject = cam?.gameObject;
56
- if (cameraObject) {
57
- const orbit = getOrAddComponent(cameraObject, OrbitControls) as OrbitControls;
58
- orbit.sourceId = "unknown";
59
- const autoRotate = evt.context.domElement.getAttribute("auto-rotate");
60
- orbit.autoRotate = autoRotate !== undefined && (autoRotate === "" || autoRotate === "true")
61
- orbit.autoRotateSpeed = 0.5;
62
- const ctx = evt.context;
63
- const fitCamera = () => {
64
- ctx.pre_render_callbacks.splice(ctx.pre_render_callbacks.indexOf(fitCamera), 1);
65
- orbit.fitCamera();
66
- }
67
- ctx.pre_render_callbacks.push(fitCamera);
64
+ function createDefaultCameraControls(context: IContext, cam?: ICamera) {
65
+
66
+ cam = cam ?? context.mainCameraComponent;
67
+ const cameraObject = cam?.gameObject;
68
+ if (debug) console.log("Creating default camera controls", cam?.name)
69
+ if (cameraObject) {
70
+ const orbit = getOrAddComponent(cameraObject, OrbitControls) as OrbitControls;
71
+ orbit.sourceId = cam?.sourceId ?? "unknown";
72
+ const autoRotate = context.domElement.getAttribute("auto-rotate");
73
+ orbit.autoRotate = autoRotate !== undefined && (autoRotate === "" || autoRotate === "true")
74
+ orbit.autoRotateSpeed = 0.5;
75
+ const ctx = context;
76
+ const fitCamera = () => {
77
+ ctx.pre_render_callbacks.splice(ctx.pre_render_callbacks.indexOf(fitCamera), 1);
78
+ orbit.fitCamera();
68
79
  }
69
- else {
70
- console.warn("Missing camera object, can not add orbit controls")
71
- }
80
+ ctx.pre_render_callbacks.push(fitCamera);
72
81
  }
73
- })
82
+ else {
83
+ console.warn("Missing camera object, can not add orbit controls")
84
+ }
85
+ }
src/engine/engine_context.ts CHANGED
@@ -750,7 +750,7 @@
750
750
  this.setCurrentCamera(camera);
751
751
  }
752
752
  else {
753
- const res = ContextRegistry.dispatchCallback(ContextEvent.MissingCamera, this);
753
+ const res = ContextRegistry.dispatchCallback(ContextEvent.MissingCamera, this, { files: loadedFiles });
754
754
  if (!res && !this.mainCamera && !this.isManagedExternally)
755
755
  console.warn("Missing camera in main scene", this);
756
756
  }