@needle-tools/engine 4.9.0-alpha.4 → 4.9.0-next.43185

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.
Files changed (48) hide show
  1. package/components.needle.json +1 -1
  2. package/dist/{needle-engine.bundle-Bi2IbHHS.umd.cjs → needle-engine.bundle-Bf4EhG38.umd.cjs} +129 -129
  3. package/dist/{needle-engine.bundle-HZA484FG.js → needle-engine.bundle-Bs33m9iI.js} +5014 -4835
  4. package/dist/{needle-engine.bundle-Bs7Jz8Xi.min.js → needle-engine.bundle-CZSmogSi.min.js} +128 -128
  5. package/dist/needle-engine.js +556 -552
  6. package/dist/needle-engine.min.js +1 -1
  7. package/dist/needle-engine.umd.cjs +1 -1
  8. package/lib/engine/codegen/register_types.js +8 -0
  9. package/lib/engine/codegen/register_types.js.map +1 -1
  10. package/lib/engine/engine_physics_rapier.js +8 -8
  11. package/lib/engine/engine_physics_rapier.js.map +1 -1
  12. package/lib/engine-components/Animation.d.ts +1 -0
  13. package/lib/engine-components/Animation.js +9 -0
  14. package/lib/engine-components/Animation.js.map +1 -1
  15. package/lib/engine-components/EventTrigger.js.map +1 -1
  16. package/lib/engine-components/api.d.ts +1 -0
  17. package/lib/engine-components/api.js +1 -0
  18. package/lib/engine-components/api.js.map +1 -1
  19. package/lib/engine-components/codegen/components.d.ts +4 -0
  20. package/lib/engine-components/codegen/components.js +4 -0
  21. package/lib/engine-components/codegen/components.js.map +1 -1
  22. package/lib/engine-components/physics/Attractor.d.ts +8 -0
  23. package/lib/engine-components/physics/Attractor.js +42 -0
  24. package/lib/engine-components/physics/Attractor.js.map +1 -0
  25. package/lib/engine-components/web/Clickthrough.d.ts +19 -0
  26. package/lib/engine-components/web/Clickthrough.js +57 -0
  27. package/lib/engine-components/web/Clickthrough.js.map +1 -0
  28. package/lib/engine-components/web/CursorFollow.d.ts +12 -0
  29. package/lib/engine-components/web/CursorFollow.js +46 -0
  30. package/lib/engine-components/web/CursorFollow.js.map +1 -0
  31. package/lib/engine-components/web/ScrollFollow.d.ts +45 -0
  32. package/lib/engine-components/web/ScrollFollow.js +132 -0
  33. package/lib/engine-components/web/ScrollFollow.js.map +1 -0
  34. package/lib/engine-components/web/index.d.ts +3 -0
  35. package/lib/engine-components/web/index.js +4 -0
  36. package/lib/engine-components/web/index.js.map +1 -0
  37. package/package.json +3 -3
  38. package/src/engine/codegen/register_types.ts +8 -0
  39. package/src/engine/engine_physics_rapier.ts +11 -11
  40. package/src/engine-components/Animation.ts +9 -0
  41. package/src/engine-components/EventTrigger.ts +0 -1
  42. package/src/engine-components/api.ts +1 -0
  43. package/src/engine-components/codegen/components.ts +4 -0
  44. package/src/engine-components/physics/Attractor.ts +35 -0
  45. package/src/engine-components/web/Clickthrough.ts +60 -0
  46. package/src/engine-components/web/CursorFollow.ts +49 -0
  47. package/src/engine-components/web/ScrollFollow.ts +150 -0
  48. package/src/engine-components/web/index.ts +3 -0
@@ -0,0 +1,150 @@
1
+ import { Object3D } from "three";
2
+ import { serializable } from "../../engine/engine_serialization.js";
3
+ import { Mathf } from "../../engine/engine_math.js";
4
+ import { Behaviour } from "../Component.js";
5
+ import { PlayableDirector } from "../timeline/PlayableDirector.js";
6
+ import { EventList } from "../EventList.js";
7
+ import { Animation } from "../Animation.js";
8
+ import { AudioSource } from "../AudioSource.js";
9
+
10
+ type ScrollFollowEvent = {
11
+ /** Event type */
12
+ type: "change",
13
+ /** Current scroll value */
14
+ value: number,
15
+ /** ScrollFollow component that raised the event */
16
+ component: ScrollFollow,
17
+ /** Call to prevent invocation of default (e.g. updating targets) */
18
+ preventDefault: () => void,
19
+ defaultPrevented: boolean,
20
+ }
21
+
22
+ export class ScrollFollow extends Behaviour {
23
+
24
+ /**
25
+ * Target object(s) to follow the scroll position of the page. If null, the main camera is used.
26
+ */
27
+ @serializable([Behaviour, Object3D])
28
+ target: object[] | object | null = null;
29
+
30
+ /**
31
+ * Damping for the movement, set to 0 for instant movement
32
+ * @default 0
33
+ */
34
+ @serializable()
35
+ damping: number = 0;
36
+
37
+ @serializable()
38
+ mode: "window" = "window";
39
+
40
+ /**
41
+ * Event fired when the scroll position changes
42
+ */
43
+ @serializable(EventList)
44
+ changed: EventList<ScrollFollowEvent> = new EventList<ScrollFollowEvent>();
45
+
46
+ /**
47
+ * Current scroll value in "pages" (0 = top of page, 1 = bottom of page)
48
+ */
49
+ get currentValue() {
50
+ return this.current_value;
51
+ }
52
+
53
+ private current_value: number = 0;
54
+ private target_value: number = 0;
55
+ private applied_value: number = -1;
56
+
57
+ /** @internal */
58
+ onEnable() {
59
+ window.addEventListener("wheel", this.updateCurrentScroll, { passive: true });
60
+ this.applied_value = -1;
61
+ this.updateCurrentScroll();
62
+ }
63
+
64
+ /** @internal */
65
+ onDisable() {
66
+ window.removeEventListener("wheel", this.updateCurrentScroll);
67
+ }
68
+
69
+ /** @internal */
70
+ lateUpdate() {
71
+
72
+ // apply damping if any
73
+ if (this.damping > 0) {
74
+ this.current_value = Mathf.lerp(this.current_value, this.target_value, this.context.time.deltaTime / this.damping);
75
+ }
76
+
77
+ if (this.current_value !== this.applied_value) {
78
+ this.applied_value = this.current_value;
79
+
80
+ let defaultPrevented = false;
81
+ if (this.changed.listenerCount > 0) {
82
+ // fire change event
83
+ const event: ScrollFollowEvent = {
84
+ type: "change",
85
+ value: this.current_value,
86
+ component: this,
87
+ preventDefault: () => { event.defaultPrevented = true; },
88
+ defaultPrevented: false,
89
+ };
90
+ this.changed.invoke(event);
91
+ defaultPrevented = event.defaultPrevented;
92
+ }
93
+
94
+ // if not prevented apply scroll
95
+ if (!defaultPrevented) {
96
+
97
+ // apply scroll to target(s)
98
+ if (Array.isArray(this.target)) {
99
+ this.target.forEach(t => t && this.applyScroll(t));
100
+ }
101
+ else if (this.target) {
102
+ this.applyScroll(this.target);
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+
109
+ private updateCurrentScroll = () => {
110
+
111
+ switch (this.mode) {
112
+ case "window":
113
+ this.target_value = window.scrollY / (document.body.scrollHeight - window.innerHeight);
114
+ if (isNaN(this.target_value) || !isFinite(this.target_value)) this.target_value = 0;
115
+ break;
116
+ }
117
+
118
+ if (this.damping <= 0) {
119
+ this.current_value = this.target_value;
120
+ }
121
+
122
+ }
123
+
124
+
125
+ private applyScroll(target: object) {
126
+
127
+ if (!target) return;
128
+
129
+ if (target instanceof PlayableDirector) {
130
+ target.time = this.current_value * target.duration;
131
+ if (!target.isPlaying) target.evaluate();
132
+ }
133
+ else if (target instanceof Animation) {
134
+ target.time = this.current_value * target.duration;
135
+ }
136
+ else if (target instanceof AudioSource) {
137
+ if (!target.duration) return;
138
+ target.time = this.current_value * target.duration;
139
+ }
140
+ else if ("scroll" in target) {
141
+ if (typeof target.scroll === "number") {
142
+ target.scroll = this.current_value;
143
+ }
144
+ else if (typeof target.scroll === "function") {
145
+ target.scroll(this.current_value);
146
+ }
147
+ }
148
+ }
149
+
150
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./Clickthrough.js";
2
+ export * from "./CursorFollow.js";
3
+ export * from "./ScrollFollow.js";