@needle-tools/engine 4.2.5 → 4.3.0-alpha.1
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.
- package/CHANGELOG.md +9 -0
- package/components.needle.json +1 -1
- package/dist/needle-engine.bundle.js +4245 -2977
- package/dist/needle-engine.bundle.light.js +4236 -2968
- package/dist/needle-engine.bundle.light.min.js +73 -73
- package/dist/needle-engine.bundle.light.umd.cjs +61 -61
- package/dist/needle-engine.bundle.min.js +62 -62
- package/dist/needle-engine.bundle.umd.cjs +61 -61
- package/dist/needle-engine.light.d.ts +9 -9
- package/dist/three-examples.js +825 -794
- package/dist/three-examples.light.js +825 -794
- package/dist/three-examples.light.min.js +12 -12
- package/dist/three-examples.light.umd.cjs +10 -10
- package/dist/three-examples.min.js +12 -12
- package/dist/three-examples.umd.cjs +10 -10
- package/lib/engine/engine_addressables.d.ts +3 -0
- package/lib/engine/engine_addressables.js +18 -0
- package/lib/engine/engine_addressables.js.map +1 -1
- package/lib/engine/engine_input.d.ts +20 -1
- package/lib/engine/engine_input.js.map +1 -1
- package/lib/engine/engine_types.d.ts +162 -17
- package/lib/engine-components/Animator.d.ts +129 -21
- package/lib/engine-components/Animator.js +115 -21
- package/lib/engine-components/Animator.js.map +1 -1
- package/lib/engine-components/AnimatorController.d.ts +161 -32
- package/lib/engine-components/AnimatorController.js +176 -29
- package/lib/engine-components/AnimatorController.js.map +1 -1
- package/lib/engine-components/AudioListener.d.ts +16 -5
- package/lib/engine-components/AudioListener.js +16 -5
- package/lib/engine-components/AudioListener.js.map +1 -1
- package/lib/engine-components/AudioSource.d.ts +120 -28
- package/lib/engine-components/AudioSource.js +120 -37
- package/lib/engine-components/AudioSource.js.map +1 -1
- package/lib/engine-components/AvatarLoader.d.ts +61 -0
- package/lib/engine-components/AvatarLoader.js +61 -1
- package/lib/engine-components/AvatarLoader.js.map +1 -1
- package/lib/engine-components/AxesHelper.d.ts +19 -1
- package/lib/engine-components/AxesHelper.js +19 -1
- package/lib/engine-components/AxesHelper.js.map +1 -1
- package/lib/engine-components/BoxHelperComponent.d.ts +26 -0
- package/lib/engine-components/BoxHelperComponent.js +26 -0
- package/lib/engine-components/BoxHelperComponent.js.map +1 -1
- package/lib/engine-components/Camera.d.ts +126 -37
- package/lib/engine-components/Camera.js +139 -37
- package/lib/engine-components/Camera.js.map +1 -1
- package/lib/engine-components/CameraUtils.js +20 -0
- package/lib/engine-components/CameraUtils.js.map +1 -1
- package/lib/engine-components/Collider.d.ts +95 -21
- package/lib/engine-components/Collider.js +100 -23
- package/lib/engine-components/Collider.js.map +1 -1
- package/lib/engine-components/Component.d.ts +554 -106
- package/lib/engine-components/Component.js +352 -81
- package/lib/engine-components/Component.js.map +1 -1
- package/lib/engine-components/DragControls.d.ts +95 -21
- package/lib/engine-components/DragControls.js +126 -32
- package/lib/engine-components/DragControls.js.map +1 -1
- package/lib/engine-components/DropListener.d.ts +99 -16
- package/lib/engine-components/DropListener.js +119 -14
- package/lib/engine-components/DropListener.js.map +1 -1
- package/lib/engine-components/Light.d.ts +102 -5
- package/lib/engine-components/Light.js +102 -44
- package/lib/engine-components/Light.js.map +1 -1
- package/lib/engine-components/NeedleMenu.d.ts +28 -11
- package/lib/engine-components/NeedleMenu.js +28 -11
- package/lib/engine-components/NeedleMenu.js.map +1 -1
- package/lib/engine-components/Networking.d.ts +37 -5
- package/lib/engine-components/Networking.js +37 -5
- package/lib/engine-components/Networking.js.map +1 -1
- package/lib/engine-components/SceneSwitcher.d.ts +8 -0
- package/lib/engine-components/SceneSwitcher.js +72 -8
- package/lib/engine-components/SceneSwitcher.js.map +1 -1
- package/lib/engine-components/SpatialTrigger.d.ts +66 -1
- package/lib/engine-components/SpatialTrigger.js +74 -2
- package/lib/engine-components/SpatialTrigger.js.map +1 -1
- package/lib/engine-components/SpectatorCamera.d.ts +66 -4
- package/lib/engine-components/SpectatorCamera.js +132 -6
- package/lib/engine-components/SpectatorCamera.js.map +1 -1
- package/lib/engine-components/SyncedTransform.d.ts +45 -6
- package/lib/engine-components/SyncedTransform.js +45 -6
- package/lib/engine-components/SyncedTransform.js.map +1 -1
- package/lib/engine-components/TransformGizmo.d.ts +49 -3
- package/lib/engine-components/TransformGizmo.js +49 -3
- package/lib/engine-components/TransformGizmo.js.map +1 -1
- package/lib/engine-components/ui/EventSystem.d.ts +1 -0
- package/lib/engine-components/ui/EventSystem.js +8 -5
- package/lib/engine-components/ui/EventSystem.js.map +1 -1
- package/lib/engine-components/webxr/WebXR.d.ts +131 -22
- package/lib/engine-components/webxr/WebXR.js +132 -23
- package/lib/engine-components/webxr/WebXR.js.map +1 -1
- package/lib/engine-components-experimental/networking/PlayerSync.d.ts +82 -9
- package/lib/engine-components-experimental/networking/PlayerSync.js +76 -11
- package/lib/engine-components-experimental/networking/PlayerSync.js.map +1 -1
- package/package.json +1 -1
- package/plugins/vite/alias.js +6 -3
- package/src/engine/engine_addressables.ts +21 -0
- package/src/engine/engine_input.ts +20 -1
- package/src/engine/engine_types.ts +179 -18
- package/src/engine-components/Animator.ts +142 -22
- package/src/engine-components/AnimatorController.ts +184 -34
- package/src/engine-components/AudioListener.ts +16 -5
- package/src/engine-components/AudioSource.ts +126 -37
- package/src/engine-components/AvatarLoader.ts +61 -2
- package/src/engine-components/AxesHelper.ts +21 -1
- package/src/engine-components/BoxHelperComponent.ts +26 -0
- package/src/engine-components/Camera.ts +147 -41
- package/src/engine-components/CameraUtils.ts +20 -0
- package/src/engine-components/Collider.ts +102 -27
- package/src/engine-components/Component.ts +605 -129
- package/src/engine-components/DragControls.ts +134 -38
- package/src/engine-components/DropListener.ts +143 -23
- package/src/engine-components/Light.ts +105 -44
- package/src/engine-components/NeedleMenu.ts +29 -11
- package/src/engine-components/Networking.ts +37 -6
- package/src/engine-components/SceneSwitcher.ts +78 -9
- package/src/engine-components/SpatialTrigger.ts +80 -3
- package/src/engine-components/SpectatorCamera.ts +136 -18
- package/src/engine-components/SyncedTransform.ts +50 -7
- package/src/engine-components/TransformGizmo.ts +49 -4
- package/src/engine-components/ui/EventSystem.ts +9 -7
- package/src/engine-components/webxr/WebXR.ts +144 -27
- package/src/engine-components-experimental/networking/PlayerSync.ts +85 -13
|
@@ -15,17 +15,40 @@ import * as threeutils from "../engine/engine_three_utils.js";
|
|
|
15
15
|
// onDeserialize?(key: string, value: any): any | void;
|
|
16
16
|
// }
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
18
|
+
* Base class for objects in Needle Engine. Extends {@link Object3D} from three.js.
|
|
19
|
+
* GameObjects can have components attached to them, which can be used to add functionality to the object.
|
|
20
|
+
* They manage their components and provide methods to add, remove and get components.
|
|
21
|
+
*
|
|
22
|
+
* All {@link Object3D} types loaded in Needle Engine have methods like {@link addComponent}.
|
|
23
|
+
* These methods are available directly on the GameObject instance:
|
|
20
24
|
* ```typescript
|
|
21
|
-
*
|
|
25
|
+
* target.addComponent(MyComponent);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* And can be called statically on the GameObject class as well:
|
|
29
|
+
* ```typescript
|
|
30
|
+
* GameObject.setActive(target, true);
|
|
22
31
|
* ```
|
|
23
32
|
*/
|
|
24
33
|
export class GameObject extends Object3D {
|
|
34
|
+
/**
|
|
35
|
+
* Unique identifier for this GameObject
|
|
36
|
+
*/
|
|
25
37
|
guid;
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a GameObject has been destroyed
|
|
40
|
+
* @param go The GameObject to check
|
|
41
|
+
* @returns True if the GameObject has been destroyed
|
|
42
|
+
*/
|
|
26
43
|
static isDestroyed(go) {
|
|
27
44
|
return isDestroyed(go);
|
|
28
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Sets the active state of a GameObject
|
|
48
|
+
* @param go The GameObject to modify
|
|
49
|
+
* @param active Whether the GameObject should be active
|
|
50
|
+
* @param processStart Whether to process the start callbacks if being activated
|
|
51
|
+
*/
|
|
29
52
|
static setActive(go, active, processStart = true) {
|
|
30
53
|
if (!go)
|
|
31
54
|
return;
|
|
@@ -35,32 +58,51 @@ export class GameObject extends Object3D {
|
|
|
35
58
|
if (active && processStart)
|
|
36
59
|
main.processStart(Context.Current, go);
|
|
37
60
|
}
|
|
38
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* Checks if the GameObject itself is active (same as go.visible)
|
|
63
|
+
* @param go The GameObject to check
|
|
64
|
+
* @returns True if the GameObject is active
|
|
65
|
+
*/
|
|
39
66
|
static isActiveSelf(go) {
|
|
40
67
|
return isActiveSelf(go);
|
|
41
68
|
}
|
|
42
|
-
/**
|
|
43
|
-
*
|
|
44
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Checks if the GameObject is active in the hierarchy (e.g. if any parent is invisible or not in the scene it will be false)
|
|
71
|
+
* @param go The GameObject to check
|
|
72
|
+
* @returns True if the GameObject is active in the hierarchy
|
|
73
|
+
*/
|
|
45
74
|
static isActiveInHierarchy(go) {
|
|
46
75
|
return isActiveInHierarchy(go);
|
|
47
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Marks a GameObject to be rendered using instancing
|
|
79
|
+
* @param go The GameObject to mark
|
|
80
|
+
* @param instanced Whether the GameObject should use instanced rendering
|
|
81
|
+
*/
|
|
48
82
|
static markAsInstancedRendered(go, instanced) {
|
|
49
83
|
markAsInstancedRendered(go, instanced);
|
|
50
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Checks if a GameObject is using instanced rendering
|
|
87
|
+
* @param instance The GameObject to check
|
|
88
|
+
* @returns True if the GameObject is using instanced rendering
|
|
89
|
+
*/
|
|
51
90
|
static isUsingInstancing(instance) { return isUsingInstancing(instance); }
|
|
52
|
-
/**
|
|
53
|
-
*
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
56
|
-
* @
|
|
91
|
+
/**
|
|
92
|
+
* Executes a callback for all components of the provided type on the provided object and its children
|
|
93
|
+
* @param instance Object to run the method on
|
|
94
|
+
* @param cb Callback to run on each component, "return undefined;" to continue and "return <anything>;" to break the loop
|
|
95
|
+
* @param recursive If true, the method will be run on all children as well
|
|
96
|
+
* @returns The last return value of the callback
|
|
57
97
|
*/
|
|
58
98
|
static foreachComponent(instance, cb, recursive = true) {
|
|
59
99
|
return foreachComponent(instance, cb, recursive);
|
|
60
100
|
}
|
|
61
|
-
/**
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
101
|
+
/**
|
|
102
|
+
* Creates a new instance of the provided object that will be replicated to all connected clients
|
|
103
|
+
* @param instance Object to instantiate
|
|
104
|
+
* @param opts Options for the instantiation
|
|
105
|
+
* @returns The newly created instance or null if creation failed
|
|
64
106
|
*/
|
|
65
107
|
static instantiateSynced(instance, opts) {
|
|
66
108
|
if (!instance)
|
|
@@ -73,9 +115,12 @@ export class GameObject extends Object3D {
|
|
|
73
115
|
}
|
|
74
116
|
return instantiate(instance, opts);
|
|
75
117
|
}
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Destroys an object on all connected clients (if in a networked session)
|
|
120
|
+
* @param instance Object to destroy
|
|
121
|
+
* @param context Optional context to use
|
|
122
|
+
* @param recursive If true, all children will be destroyed as well
|
|
123
|
+
*/
|
|
79
124
|
static destroySynced(instance, context, recursive = true) {
|
|
80
125
|
if (!instance)
|
|
81
126
|
return;
|
|
@@ -83,15 +128,19 @@ export class GameObject extends Object3D {
|
|
|
83
128
|
context = context ?? Context.Current;
|
|
84
129
|
syncDestroy(go, context.connection, recursive);
|
|
85
130
|
}
|
|
86
|
-
/**
|
|
87
|
-
*
|
|
88
|
-
* @param
|
|
131
|
+
/**
|
|
132
|
+
* Destroys an object
|
|
133
|
+
* @param instance Object to destroy
|
|
134
|
+
* @param recursive If true, all children will be destroyed as well. Default: true
|
|
89
135
|
*/
|
|
90
136
|
static destroy(instance, recursive = true) {
|
|
91
137
|
return destroy(instance, recursive);
|
|
92
138
|
}
|
|
93
139
|
/**
|
|
94
|
-
*
|
|
140
|
+
* Adds an object to parent and ensures all components are properly registered
|
|
141
|
+
* @param instance Object to add
|
|
142
|
+
* @param parent Parent to add the object to
|
|
143
|
+
* @param context Optional context to use
|
|
95
144
|
*/
|
|
96
145
|
static add(instance, parent, context) {
|
|
97
146
|
if (!instance || !parent)
|
|
@@ -122,6 +171,7 @@ export class GameObject extends Object3D {
|
|
|
122
171
|
}
|
|
123
172
|
/**
|
|
124
173
|
* Removes the object from its parent and deactivates all of its components
|
|
174
|
+
* @param instance Object to remove
|
|
125
175
|
*/
|
|
126
176
|
static remove(instance) {
|
|
127
177
|
if (!instance)
|
|
@@ -133,13 +183,21 @@ export class GameObject extends Object3D {
|
|
|
133
183
|
main.processRemoveFromScene(comp);
|
|
134
184
|
}, true);
|
|
135
185
|
}
|
|
136
|
-
/**
|
|
186
|
+
/**
|
|
187
|
+
* Invokes a method on all components including children (if a method with that name exists)
|
|
188
|
+
* @param go GameObject to invoke the method on
|
|
189
|
+
* @param functionName Name of the method to invoke
|
|
190
|
+
* @param args Arguments to pass to the method
|
|
191
|
+
*/
|
|
137
192
|
static invokeOnChildren(go, functionName, ...args) {
|
|
138
193
|
this.invoke(go, functionName, true, args);
|
|
139
194
|
}
|
|
140
|
-
/**
|
|
141
|
-
*
|
|
142
|
-
* @param
|
|
195
|
+
/**
|
|
196
|
+
* Invokes a method on all components that have a method matching the provided name
|
|
197
|
+
* @param go GameObject to invoke the method on
|
|
198
|
+
* @param functionName Name of the method to invoke
|
|
199
|
+
* @param children Whether to invoke on children as well
|
|
200
|
+
* @param args Arguments to pass to the method
|
|
143
201
|
*/
|
|
144
202
|
static invoke(go, functionName, children = false, ...args) {
|
|
145
203
|
if (!go)
|
|
@@ -157,34 +215,49 @@ export class GameObject extends Object3D {
|
|
|
157
215
|
return addComponent(go, type, init, { callAwake });
|
|
158
216
|
}
|
|
159
217
|
/**
|
|
160
|
-
*
|
|
161
|
-
* @param go
|
|
162
|
-
* @param instanceOrType
|
|
163
|
-
* @param init
|
|
164
|
-
* @param
|
|
218
|
+
* Adds a new component (or moves an existing component) to the provided object
|
|
219
|
+
* @param go Object to add the component to
|
|
220
|
+
* @param instanceOrType If an instance is provided it will be moved to the new object, if a type is provided a new instance will be created
|
|
221
|
+
* @param init Optional init object to initialize the component with
|
|
222
|
+
* @param opts Optional options for adding the component
|
|
223
|
+
* @returns The added or moved component
|
|
165
224
|
*/
|
|
166
225
|
static addComponent(go, instanceOrType, init, opts) {
|
|
167
226
|
return addComponent(go, instanceOrType, init, opts);
|
|
168
227
|
}
|
|
169
228
|
/**
|
|
170
229
|
* Moves a component to a new object
|
|
171
|
-
* @param go
|
|
172
|
-
* @param instance
|
|
230
|
+
* @param go GameObject to move the component to
|
|
231
|
+
* @param instance Component to move
|
|
232
|
+
* @returns The moved component
|
|
173
233
|
*/
|
|
174
234
|
static moveComponent(go, instance) {
|
|
175
235
|
return addComponent(go, instance);
|
|
176
236
|
}
|
|
177
|
-
/**
|
|
178
|
-
*
|
|
237
|
+
/**
|
|
238
|
+
* Removes a component from its object
|
|
239
|
+
* @param instance Component to remove
|
|
240
|
+
* @returns The removed component
|
|
179
241
|
*/
|
|
180
242
|
static removeComponent(instance) {
|
|
181
243
|
removeComponent(instance.gameObject, instance);
|
|
182
244
|
return instance;
|
|
183
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Gets or adds a component of the specified type
|
|
248
|
+
* @param go GameObject to get or add the component to
|
|
249
|
+
* @param typeName Constructor of the component type
|
|
250
|
+
* @returns The existing or newly added component
|
|
251
|
+
*/
|
|
184
252
|
static getOrAddComponent(go, typeName) {
|
|
185
253
|
return getOrAddComponent(go, typeName);
|
|
186
254
|
}
|
|
187
|
-
/**
|
|
255
|
+
/**
|
|
256
|
+
* Gets a component on the provided object
|
|
257
|
+
* @param go GameObject to get the component from
|
|
258
|
+
* @param typeName Constructor of the component type
|
|
259
|
+
* @returns The component if found, otherwise null
|
|
260
|
+
*/
|
|
188
261
|
static getComponent(go, typeName) {
|
|
189
262
|
if (go === null)
|
|
190
263
|
return null;
|
|
@@ -194,35 +267,92 @@ export class GameObject extends Object3D {
|
|
|
194
267
|
// if(res) typeName = res;
|
|
195
268
|
return getComponent(go, typeName);
|
|
196
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Gets all components of the specified type on the provided object
|
|
272
|
+
* @param go GameObject to get the components from
|
|
273
|
+
* @param typeName Constructor of the component type
|
|
274
|
+
* @param arr Optional array to populate with the components
|
|
275
|
+
* @returns Array of components
|
|
276
|
+
*/
|
|
197
277
|
static getComponents(go, typeName, arr = null) {
|
|
198
278
|
if (go === null)
|
|
199
279
|
return arr ?? [];
|
|
200
280
|
return getComponents(go, typeName, arr);
|
|
201
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Finds an object or component by its unique identifier
|
|
284
|
+
* @param guid Unique identifier to search for
|
|
285
|
+
* @param hierarchy Root object to search in
|
|
286
|
+
* @returns The found GameObject or Component, or null/undefined if not found
|
|
287
|
+
*/
|
|
202
288
|
static findByGuid(guid, hierarchy) {
|
|
203
289
|
const res = findByGuid(guid, hierarchy);
|
|
204
290
|
return res;
|
|
205
291
|
}
|
|
292
|
+
/**
|
|
293
|
+
* Finds the first object of the specified component type in the scene
|
|
294
|
+
* @param typeName Constructor of the component type
|
|
295
|
+
* @param context Context or root object to search in
|
|
296
|
+
* @param includeInactive Whether to include inactive objects in the search
|
|
297
|
+
* @returns The first matching component if found, otherwise null
|
|
298
|
+
*/
|
|
206
299
|
static findObjectOfType(typeName, context, includeInactive = true) {
|
|
207
300
|
return findObjectOfType(typeName, context ?? Context.Current, includeInactive);
|
|
208
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Finds all objects of the specified component type in the scene
|
|
304
|
+
* @param typeName Constructor of the component type
|
|
305
|
+
* @param context Context or root object to search in
|
|
306
|
+
* @returns Array of matching components
|
|
307
|
+
*/
|
|
209
308
|
static findObjectsOfType(typeName, context) {
|
|
210
309
|
const arr = [];
|
|
211
310
|
findObjectsOfType(typeName, arr, context);
|
|
212
311
|
return arr;
|
|
213
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* Gets a component of the specified type in the gameObject's children hierarchy
|
|
315
|
+
* @param go GameObject to search in
|
|
316
|
+
* @param typeName Constructor of the component type
|
|
317
|
+
* @returns The first matching component if found, otherwise null
|
|
318
|
+
*/
|
|
214
319
|
static getComponentInChildren(go, typeName) {
|
|
215
320
|
return getComponentInChildren(go, typeName);
|
|
216
321
|
}
|
|
322
|
+
/**
|
|
323
|
+
* Gets all components of the specified type in the gameObject's children hierarchy
|
|
324
|
+
* @param go GameObject to search in
|
|
325
|
+
* @param typeName Constructor of the component type
|
|
326
|
+
* @param arr Optional array to populate with the components
|
|
327
|
+
* @returns Array of components
|
|
328
|
+
*/
|
|
217
329
|
static getComponentsInChildren(go, typeName, arr = null) {
|
|
218
330
|
return getComponentsInChildren(go, typeName, arr ?? undefined);
|
|
219
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* Gets a component of the specified type in the gameObject's parent hierarchy
|
|
334
|
+
* @param go GameObject to search in
|
|
335
|
+
* @param typeName Constructor of the component type
|
|
336
|
+
* @returns The first matching component if found, otherwise null
|
|
337
|
+
*/
|
|
220
338
|
static getComponentInParent(go, typeName) {
|
|
221
339
|
return getComponentInParent(go, typeName);
|
|
222
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Gets all components of the specified type in the gameObject's parent hierarchy
|
|
343
|
+
* @param go GameObject to search in
|
|
344
|
+
* @param typeName Constructor of the component type
|
|
345
|
+
* @param arr Optional array to populate with the components
|
|
346
|
+
* @returns Array of components
|
|
347
|
+
*/
|
|
223
348
|
static getComponentsInParent(go, typeName, arr = null) {
|
|
224
349
|
return getComponentsInParent(go, typeName, arr);
|
|
225
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Gets all components on the gameObject
|
|
353
|
+
* @param go GameObject to get components from
|
|
354
|
+
* @returns Array of all components
|
|
355
|
+
*/
|
|
226
356
|
static getAllComponents(go) {
|
|
227
357
|
const componentsList = go.userData?.components;
|
|
228
358
|
if (!componentsList)
|
|
@@ -230,6 +360,11 @@ export class GameObject extends Object3D {
|
|
|
230
360
|
const newList = [...componentsList];
|
|
231
361
|
return newList;
|
|
232
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Iterates through all components on the gameObject
|
|
365
|
+
* @param go GameObject to iterate components on
|
|
366
|
+
* @returns Generator yielding each component
|
|
367
|
+
*/
|
|
233
368
|
static *iterateComponents(go) {
|
|
234
369
|
const list = go?.userData?.components;
|
|
235
370
|
if (list && Array.isArray(list)) {
|
|
@@ -266,24 +401,38 @@ export class GameObject extends Object3D {
|
|
|
266
401
|
* @group Components
|
|
267
402
|
*/
|
|
268
403
|
export class Component {
|
|
269
|
-
/**
|
|
404
|
+
/**
|
|
405
|
+
* Indicates whether this object is a component
|
|
406
|
+
* @internal
|
|
407
|
+
*/
|
|
270
408
|
get isComponent() { return true; }
|
|
271
409
|
__context;
|
|
272
|
-
/**
|
|
410
|
+
/**
|
|
411
|
+
* The context this component belongs to, providing access to the runtime environment
|
|
412
|
+
* including physics, timing utilities, camera, and scene
|
|
413
|
+
*/
|
|
273
414
|
get context() {
|
|
274
415
|
return this.__context ?? Context.Current;
|
|
275
416
|
}
|
|
276
417
|
set context(context) {
|
|
277
418
|
this.__context = context;
|
|
278
419
|
}
|
|
279
|
-
/**
|
|
280
|
-
*
|
|
420
|
+
/**
|
|
421
|
+
* Shorthand accessor for the current scene from the context
|
|
422
|
+
* @returns The scene this component belongs to
|
|
423
|
+
*/
|
|
281
424
|
get scene() { return this.context.scene; }
|
|
282
|
-
/**
|
|
425
|
+
/**
|
|
426
|
+
* The layer value of the GameObject this component is attached to
|
|
427
|
+
* Used for visibility and physics filtering
|
|
428
|
+
*/
|
|
283
429
|
get layer() {
|
|
284
430
|
return this.gameObject?.userData?.layer;
|
|
285
431
|
}
|
|
286
|
-
/**
|
|
432
|
+
/**
|
|
433
|
+
* The name of the GameObject this component is attached to
|
|
434
|
+
* Used for debugging and finding objects
|
|
435
|
+
*/
|
|
287
436
|
get name() {
|
|
288
437
|
if (this.gameObject?.name) {
|
|
289
438
|
return this.gameObject.name;
|
|
@@ -302,7 +451,10 @@ export class Component {
|
|
|
302
451
|
this.__name = str;
|
|
303
452
|
}
|
|
304
453
|
}
|
|
305
|
-
/**
|
|
454
|
+
/**
|
|
455
|
+
* The tag of the GameObject this component is attached to
|
|
456
|
+
* Used for categorizing objects and efficient lookup
|
|
457
|
+
*/
|
|
306
458
|
get tag() {
|
|
307
459
|
return this.gameObject?.userData.tag;
|
|
308
460
|
}
|
|
@@ -313,7 +465,10 @@ export class Component {
|
|
|
313
465
|
this.gameObject.userData.tag = str;
|
|
314
466
|
}
|
|
315
467
|
}
|
|
316
|
-
/**
|
|
468
|
+
/**
|
|
469
|
+
* Indicates whether the GameObject is marked as static
|
|
470
|
+
* Static objects typically don't move and can be optimized by the engine
|
|
471
|
+
*/
|
|
317
472
|
get static() {
|
|
318
473
|
return this.gameObject?.userData.static;
|
|
319
474
|
}
|
|
@@ -327,7 +482,11 @@ export class Component {
|
|
|
327
482
|
// get hideFlags(): HideFlags {
|
|
328
483
|
// return this.gameObject?.hideFlags;
|
|
329
484
|
// }
|
|
330
|
-
/**
|
|
485
|
+
/**
|
|
486
|
+
* Checks if this component is currently active (enabled and part of an active GameObject hierarchy)
|
|
487
|
+
* Components that are inactive won't receive lifecycle method calls
|
|
488
|
+
* @returns True if the component is enabled and all parent GameObjects are active
|
|
489
|
+
*/
|
|
331
490
|
get activeAndEnabled() {
|
|
332
491
|
if (this.destroyed)
|
|
333
492
|
return false;
|
|
@@ -360,42 +519,72 @@ export class Component {
|
|
|
360
519
|
return;
|
|
361
520
|
this.gameObject[activeInHierarchyFieldName] = val;
|
|
362
521
|
}
|
|
363
|
-
/**
|
|
522
|
+
/**
|
|
523
|
+
* Reference to the GameObject this component is attached to
|
|
524
|
+
* This is a three.js Object3D with additional GameObject functionality
|
|
525
|
+
*/
|
|
364
526
|
gameObject;
|
|
365
|
-
/**
|
|
527
|
+
/**
|
|
528
|
+
* Unique identifier for this component instance,
|
|
529
|
+
* used for finding and tracking components
|
|
530
|
+
*/
|
|
366
531
|
guid = "invalid";
|
|
367
|
-
/**
|
|
532
|
+
/**
|
|
533
|
+
* Identifier for the source asset that created this component.
|
|
534
|
+
* For example, URL to the glTF file this component was loaded from
|
|
535
|
+
*/
|
|
368
536
|
sourceId;
|
|
369
|
-
/**
|
|
370
|
-
*
|
|
537
|
+
/**
|
|
538
|
+
* Called once when the component becomes active for the first time.
|
|
539
|
+
* This is the first lifecycle callback to be invoked
|
|
540
|
+
*/
|
|
371
541
|
awake() { }
|
|
372
|
-
/**
|
|
373
|
-
*
|
|
374
|
-
|
|
542
|
+
/**
|
|
543
|
+
* Called every time the component becomes enabled or active in the hierarchy.
|
|
544
|
+
* Invoked after {@link awake} and before {@link start}.
|
|
545
|
+
*/
|
|
375
546
|
onEnable() { }
|
|
376
|
-
/**
|
|
547
|
+
/**
|
|
548
|
+
* Called every time the component becomes disabled or inactive in the hierarchy.
|
|
549
|
+
* Invoked when the component or any parent GameObject becomes invisible
|
|
550
|
+
*/
|
|
377
551
|
onDisable() { }
|
|
378
|
-
/**
|
|
552
|
+
/**
|
|
553
|
+
* Called when the component is destroyed.
|
|
554
|
+
* Use for cleanup operations like removing event listeners
|
|
555
|
+
*/
|
|
379
556
|
onDestroy() {
|
|
380
557
|
this.__destroyed = true;
|
|
381
558
|
}
|
|
382
|
-
/**
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* @param routine
|
|
388
|
-
* @param evt
|
|
389
|
-
* @returns
|
|
559
|
+
/**
|
|
560
|
+
* Starts a coroutine that can yield to wait for events.
|
|
561
|
+
* Coroutines allow for time-based sequencing of operations without blocking.
|
|
562
|
+
* Coroutines are based on generator functions, a JavaScript language feature.
|
|
563
|
+
*
|
|
564
|
+
* @param routine Generator function to start
|
|
565
|
+
* @param evt Event to register the coroutine for (default: FrameEvent.Update)
|
|
566
|
+
* @returns The generator function that can be used to stop the coroutine
|
|
567
|
+
* @example
|
|
568
|
+
* Time-based sequencing of operations
|
|
569
|
+
* ```ts
|
|
570
|
+
* *myCoroutine() {
|
|
571
|
+
* yield WaitForSeconds(1); // wait for 1 second
|
|
572
|
+
* yield WaitForFrames(10); // wait for 10 frames
|
|
573
|
+
* yield new Promise(resolve => setTimeout(resolve, 1000)); // wait for a promise to resolve
|
|
574
|
+
* }
|
|
575
|
+
* ```
|
|
390
576
|
* @example
|
|
577
|
+
* Coroutine that logs a message every 5 frames
|
|
391
578
|
* ```ts
|
|
392
|
-
* onEnable() {
|
|
579
|
+
* onEnable() {
|
|
580
|
+
* this.startCoroutine(this.myCoroutine());
|
|
581
|
+
* }
|
|
393
582
|
* private *myCoroutine() {
|
|
394
|
-
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
583
|
+
* while(this.activeAndEnabled) {
|
|
584
|
+
* console.log("Hello World", this.context.time.frame);
|
|
585
|
+
* // wait for 5 frames
|
|
586
|
+
* for(let i = 0; i < 5; i++) yield;
|
|
587
|
+
* }
|
|
399
588
|
* }
|
|
400
589
|
* ```
|
|
401
590
|
*/
|
|
@@ -403,19 +592,23 @@ export class Component {
|
|
|
403
592
|
return this.context.registerCoroutineUpdate(this, routine, evt);
|
|
404
593
|
}
|
|
405
594
|
/**
|
|
406
|
-
*
|
|
407
|
-
* @param routine
|
|
408
|
-
* @param evt
|
|
595
|
+
* Stops a coroutine that was previously started with startCoroutine
|
|
596
|
+
* @param routine The routine to be stopped
|
|
597
|
+
* @param evt The frame event the routine was registered with
|
|
409
598
|
*/
|
|
410
599
|
stopCoroutine(routine, evt = FrameEvent.Update) {
|
|
411
600
|
this.context.unregisterCoroutineUpdate(routine, evt);
|
|
412
601
|
}
|
|
413
|
-
/**
|
|
602
|
+
/**
|
|
603
|
+
* Checks if this component has been destroyed
|
|
604
|
+
* @returns True if the component or its GameObject has been destroyed
|
|
605
|
+
*/
|
|
414
606
|
get destroyed() {
|
|
415
607
|
return this.__destroyed;
|
|
416
608
|
}
|
|
417
609
|
/**
|
|
418
|
-
* Destroys this component
|
|
610
|
+
* Destroys this component and removes it from its GameObject
|
|
611
|
+
* After destruction, the component will no longer receive lifecycle callbacks
|
|
419
612
|
*/
|
|
420
613
|
destroy() {
|
|
421
614
|
if (this.__destroyed)
|
|
@@ -453,6 +646,11 @@ export class Component {
|
|
|
453
646
|
this._internalInit(init);
|
|
454
647
|
return this;
|
|
455
648
|
}
|
|
649
|
+
/**
|
|
650
|
+
* Initializes component properties from an initialization object
|
|
651
|
+
* @param init Object with properties to copy to this component
|
|
652
|
+
* @internal
|
|
653
|
+
*/
|
|
456
654
|
_internalInit(init) {
|
|
457
655
|
if (typeof init === "object") {
|
|
458
656
|
for (const key of Object.keys(init)) {
|
|
@@ -531,6 +729,10 @@ export class Component {
|
|
|
531
729
|
}
|
|
532
730
|
destroyComponentInstance(this);
|
|
533
731
|
}
|
|
732
|
+
/**
|
|
733
|
+
* Controls whether this component is enabled
|
|
734
|
+
* Disabled components don't receive lifecycle callbacks
|
|
735
|
+
*/
|
|
534
736
|
get enabled() {
|
|
535
737
|
return typeof this.__isEnabled === "boolean" ? this.__isEnabled : true; // if it has no enabled field it is always enabled
|
|
536
738
|
}
|
|
@@ -561,64 +763,128 @@ export class Component {
|
|
|
561
763
|
this.__internalDisable();
|
|
562
764
|
}
|
|
563
765
|
}
|
|
766
|
+
/**
|
|
767
|
+
* Gets the position of this component's GameObject in world space
|
|
768
|
+
*/
|
|
564
769
|
get worldPosition() {
|
|
565
770
|
return threeutils.getWorldPosition(this.gameObject);
|
|
566
771
|
}
|
|
772
|
+
/**
|
|
773
|
+
* Sets the position of this component's GameObject in world space
|
|
774
|
+
* @param val The world position vector to set
|
|
775
|
+
*/
|
|
567
776
|
set worldPosition(val) {
|
|
568
777
|
threeutils.setWorldPosition(this.gameObject, val);
|
|
569
778
|
}
|
|
779
|
+
/**
|
|
780
|
+
* Sets the position of this component's GameObject in world space using individual coordinates
|
|
781
|
+
* @param x X-coordinate in world space
|
|
782
|
+
* @param y Y-coordinate in world space
|
|
783
|
+
* @param z Z-coordinate in world space
|
|
784
|
+
*/
|
|
570
785
|
setWorldPosition(x, y, z) {
|
|
571
786
|
threeutils.setWorldPositionXYZ(this.gameObject, x, y, z);
|
|
572
787
|
}
|
|
788
|
+
/**
|
|
789
|
+
* Gets the rotation of this component's GameObject in world space as a quaternion
|
|
790
|
+
*/
|
|
573
791
|
get worldQuaternion() {
|
|
574
792
|
return threeutils.getWorldQuaternion(this.gameObject);
|
|
575
793
|
}
|
|
794
|
+
/**
|
|
795
|
+
* Sets the rotation of this component's GameObject in world space using a quaternion
|
|
796
|
+
* @param val The world rotation quaternion to set
|
|
797
|
+
*/
|
|
576
798
|
set worldQuaternion(val) {
|
|
577
799
|
threeutils.setWorldQuaternion(this.gameObject, val);
|
|
578
800
|
}
|
|
801
|
+
/**
|
|
802
|
+
* Sets the rotation of this component's GameObject in world space using quaternion components
|
|
803
|
+
* @param x X component of the quaternion
|
|
804
|
+
* @param y Y component of the quaternion
|
|
805
|
+
* @param z Z component of the quaternion
|
|
806
|
+
* @param w W component of the quaternion
|
|
807
|
+
*/
|
|
579
808
|
setWorldQuaternion(x, y, z, w) {
|
|
580
809
|
threeutils.setWorldQuaternionXYZW(this.gameObject, x, y, z, w);
|
|
581
810
|
}
|
|
582
|
-
|
|
811
|
+
/**
|
|
812
|
+
* Gets the rotation of this component's GameObject in world space as Euler angles (in radians)
|
|
813
|
+
*/
|
|
583
814
|
get worldEuler() {
|
|
584
815
|
return threeutils.getWorldEuler(this.gameObject);
|
|
585
816
|
}
|
|
586
|
-
|
|
817
|
+
/**
|
|
818
|
+
* Sets the rotation of this component's GameObject in world space using Euler angles (in radians)
|
|
819
|
+
* @param val The world rotation Euler angles to set
|
|
820
|
+
*/
|
|
587
821
|
set worldEuler(val) {
|
|
588
822
|
threeutils.setWorldEuler(this.gameObject, val);
|
|
589
823
|
}
|
|
590
|
-
|
|
824
|
+
/**
|
|
825
|
+
* Gets the rotation of this component's GameObject in world space as Euler angles (in degrees)
|
|
826
|
+
*/
|
|
591
827
|
get worldRotation() {
|
|
592
828
|
return this.gameObject.worldRotation;
|
|
593
|
-
;
|
|
594
829
|
}
|
|
830
|
+
/**
|
|
831
|
+
* Sets the rotation of this component's GameObject in world space using Euler angles (in degrees)
|
|
832
|
+
* @param val The world rotation vector to set (in degrees)
|
|
833
|
+
*/
|
|
595
834
|
set worldRotation(val) {
|
|
596
835
|
this.setWorldRotation(val.x, val.y, val.z, true);
|
|
597
836
|
}
|
|
837
|
+
/**
|
|
838
|
+
* Sets the rotation of this component's GameObject in world space using individual Euler angles
|
|
839
|
+
* @param x X-axis rotation
|
|
840
|
+
* @param y Y-axis rotation
|
|
841
|
+
* @param z Z-axis rotation
|
|
842
|
+
* @param degrees Whether the values are in degrees (true) or radians (false)
|
|
843
|
+
*/
|
|
598
844
|
setWorldRotation(x, y, z, degrees = true) {
|
|
599
845
|
threeutils.setWorldRotationXYZ(this.gameObject, x, y, z, degrees);
|
|
600
846
|
}
|
|
601
847
|
static _forward = new Vector3();
|
|
602
|
-
/**
|
|
848
|
+
/**
|
|
849
|
+
* Gets the forward direction vector (0,0,-1) of this component's GameObject in world space
|
|
850
|
+
*/
|
|
603
851
|
get forward() {
|
|
604
852
|
return Component._forward.set(0, 0, -1).applyQuaternion(this.worldQuaternion);
|
|
605
853
|
}
|
|
606
854
|
static _right = new Vector3();
|
|
607
|
-
/**
|
|
855
|
+
/**
|
|
856
|
+
* Gets the right direction vector (1,0,0) of this component's GameObject in world space
|
|
857
|
+
*/
|
|
608
858
|
get right() {
|
|
609
859
|
return Component._right.set(1, 0, 0).applyQuaternion(this.worldQuaternion);
|
|
610
860
|
}
|
|
611
861
|
static _up = new Vector3();
|
|
612
|
-
/**
|
|
862
|
+
/**
|
|
863
|
+
* Gets the up direction vector (0,1,0) of this component's GameObject in world space
|
|
864
|
+
*/
|
|
613
865
|
get up() {
|
|
614
866
|
return Component._up.set(0, 1, 0).applyQuaternion(this.worldQuaternion);
|
|
615
867
|
}
|
|
616
868
|
// EventTarget implementation:
|
|
869
|
+
/**
|
|
870
|
+
* Storage for event listeners registered to this component
|
|
871
|
+
* @private
|
|
872
|
+
*/
|
|
617
873
|
_eventListeners = new Map();
|
|
874
|
+
/**
|
|
875
|
+
* Registers an event listener for the specified event type
|
|
876
|
+
* @param type The event type to listen for
|
|
877
|
+
* @param listener The callback function to execute when the event occurs
|
|
878
|
+
*/
|
|
618
879
|
addEventListener(type, listener) {
|
|
619
880
|
this._eventListeners[type] = this._eventListeners[type] || [];
|
|
620
881
|
this._eventListeners[type].push(listener);
|
|
621
882
|
}
|
|
883
|
+
/**
|
|
884
|
+
* Removes a previously registered event listener
|
|
885
|
+
* @param type The event type the listener was registered for
|
|
886
|
+
* @param listener The callback function to remove
|
|
887
|
+
*/
|
|
622
888
|
removeEventListener(type, listener) {
|
|
623
889
|
if (!this._eventListeners[type])
|
|
624
890
|
return;
|
|
@@ -626,6 +892,11 @@ export class Component {
|
|
|
626
892
|
if (index >= 0)
|
|
627
893
|
this._eventListeners[type].splice(index, 1);
|
|
628
894
|
}
|
|
895
|
+
/**
|
|
896
|
+
* Dispatches an event to all registered listeners
|
|
897
|
+
* @param evt The event object to dispatch
|
|
898
|
+
* @returns Always returns false (standard implementation of EventTarget)
|
|
899
|
+
*/
|
|
629
900
|
dispatchEvent(evt) {
|
|
630
901
|
if (!evt || !this._eventListeners[evt.type])
|
|
631
902
|
return false;
|