@needle-tools/engine 4.10.0-beta.4 → 4.10.0-beta.7

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 { Camera, PerspectiveCamera, Quaternion, Vector2, Vector3 } from "three";
1
+ import { Camera, PerspectiveCamera, Quaternion, Scene, Vector2, Vector3 } from "three";
2
2
 
3
3
  import { isDevEnvironment } from "../../engine/debug/debug.js";
4
4
  import { Gizmos } from "../../engine/engine_gizmos.js";
@@ -13,45 +13,47 @@ import { Behaviour } from "../Component.js";
13
13
  const debugParam = getParam("debugviewbox");
14
14
  const disabledGizmoColor = new RGBAColor(.5, .5, .5, .5);
15
15
 
16
+ /**
17
+ * This component can be used to automatically fit a certain box area into the camera view - no matter your screen size or aspect ratio.
18
+ *
19
+ * Add the ViewBox to an object into your scene
20
+ */
16
21
  @registerType
17
- export class ResponsiveBox extends Behaviour {
22
+ export class ViewBox extends Behaviour {
18
23
 
19
- static instances: ResponsiveBox[] = [];
24
+ static readonly instances: ViewBox[] = [];
20
25
 
26
+ /**
27
+ * The reference field of view is used to calculate the box size. This should usually be the same as your camera's fov.
28
+ * @default undefined (meaning it will use the camera fov on the first frame)
29
+ */
21
30
  @serializable()
22
- referenceFieldOfView: number = 60;
31
+ referenceFieldOfView: number | undefined = undefined;
23
32
 
33
+ /**
34
+ * Enable debug logs and rendering for this component instance
35
+ */
24
36
  @serializable()
25
37
  debug: boolean = false;
26
38
 
27
- // awake() {
28
- // // this.referenceFieldOfView = (this.context.mainCamera as PerspectiveCamera)?.fov || 60;
29
- // setInterval(()=>{
30
- // if(Math.random() > .5)
31
- // this.enabled = !this.enabled
32
- // }, 1000)
33
- // }
34
-
35
39
  onEnable(): void {
36
40
  if (debugParam || this.debug || isDevEnvironment()) console.debug("[ViewBox] Using camera fov:", this.referenceFieldOfView);
37
- ResponsiveBox.instances.push(this);
38
- // this.context.pre_render_callbacks.push(this.internalUpdate);
41
+ // register instance
42
+ ViewBox.instances.push(this);
39
43
  }
40
44
 
41
45
  onDisable(): void {
42
46
  if (debugParam || this.debug) console.debug("[ViewBox] Disabled");
43
- const idx = ResponsiveBox.instances.indexOf(this);
44
- if (idx !== -1) ResponsiveBox.instances.splice(idx, 1);
47
+ // unregister instance
48
+ const idx = ViewBox.instances.indexOf(this);
49
+ if (idx !== -1) ViewBox.instances.splice(idx, 1);
45
50
  this._projectedBoxElement?.remove();
46
-
47
- // const cb_idx = this.context.pre_render_callbacks.indexOf(this.internalUpdate);
48
- // if (cb_idx !== -1) this.context.pre_render_callbacks.splice(cb_idx, 1);
49
51
  }
50
52
 
51
53
  onBeforeRender(): void {
52
54
  if (this.context.isInXR) return;
53
- if(this.destroyed) return;
54
- const isActive = ResponsiveBox.instances[ResponsiveBox.instances.length - 1] === this;
55
+ if (this.destroyed) return;
56
+ const isActive = ViewBox.instances[ViewBox.instances.length - 1] === this;
55
57
  if (!isActive) {
56
58
  if (debugParam || this.debug) {
57
59
  Gizmos.DrawWireBox(this.gameObject.worldPosition, this.gameObject.worldScale, disabledGizmoColor);
@@ -68,6 +70,10 @@ export class ResponsiveBox extends Behaviour {
68
70
  return;
69
71
  }
70
72
 
73
+ if (this.referenceFieldOfView === undefined) {
74
+ this.referenceFieldOfView = camera.fov;
75
+ }
76
+
71
77
  if (this.referenceFieldOfView === undefined || this.referenceFieldOfView <= 0) {
72
78
  if (debugParam || this.debug) console.warn("[ViewBox] No valid referenceFieldOfView set, cannot adjust box size:", this.referenceFieldOfView);
73
79
  return;