@needle-tools/engine 4.8.7-next.c2b0897 → 4.8.7-next.c5a1d5a
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/components.needle.json +1 -1
- package/dist/{needle-engine.bundle-VZrYILJY.min.js → needle-engine.bundle-B8uN1t53.min.js} +69 -69
- package/dist/{needle-engine.bundle-BQgw9DL-.js → needle-engine.bundle-BSEPqN_G.js} +983 -979
- package/dist/{needle-engine.bundle-SLXWZgxj.umd.cjs → needle-engine.bundle-izSBbBnF.umd.cjs} +73 -73
- package/dist/needle-engine.js +2 -2
- package/dist/needle-engine.min.js +1 -1
- package/dist/needle-engine.umd.cjs +1 -1
- package/lib/engine/engine_addressables.d.ts +12 -12
- package/lib/engine/engine_addressables.js +30 -23
- package/lib/engine/engine_addressables.js.map +1 -1
- package/lib/engine/engine_animation.d.ts +1 -3
- package/lib/engine/engine_animation.js +15 -9
- package/lib/engine/engine_animation.js.map +1 -1
- package/lib/engine/engine_loaders.js +15 -11
- package/lib/engine/engine_loaders.js.map +1 -1
- package/lib/engine-components/Animation.js +2 -1
- package/lib/engine-components/Animation.js.map +1 -1
- package/lib/engine-components/AnimationUtilsAutoplay.js +1 -6
- package/lib/engine-components/AnimationUtilsAutoplay.js.map +1 -1
- package/lib/engine-components/DropListener.d.ts +17 -12
- package/lib/engine-components/DropListener.js +34 -31
- package/lib/engine-components/DropListener.js.map +1 -1
- package/package.json +1 -1
- package/plugins/vite/alias.js +26 -17
- package/src/engine/engine_addressables.ts +44 -33
- package/src/engine/engine_animation.ts +17 -9
- package/src/engine/engine_loaders.ts +18 -13
- package/src/engine-components/Animation.ts +1 -1
- package/src/engine-components/AnimationUtilsAutoplay.ts +1 -6
- package/src/engine-components/DropListener.ts +40 -31
package/plugins/vite/alias.js
CHANGED
|
@@ -62,7 +62,6 @@ const packages_to_resolve = {
|
|
|
62
62
|
|
|
63
63
|
'@needle-tools/gltf-progressive': "auto-resolve",
|
|
64
64
|
'@needle-tools/materialx': "auto-resolve",
|
|
65
|
-
'@needle-tools/materialx/needle': "auto-resolve"
|
|
66
65
|
}
|
|
67
66
|
|
|
68
67
|
/**
|
|
@@ -84,7 +83,7 @@ export const needleViteAlias = (command, config, userSettings) => {
|
|
|
84
83
|
outputDebugFile?.write(msg.join(" ") + "\n");
|
|
85
84
|
}
|
|
86
85
|
if (debug) {
|
|
87
|
-
const outputFilePath = path.resolve(projectDir, 'node_modules/.
|
|
86
|
+
const outputFilePath = path.resolve(projectDir, 'node_modules/.needle/needle.alias.log');
|
|
88
87
|
const outputDirectory = path.dirname(outputFilePath);
|
|
89
88
|
if (!existsSync(outputDirectory)) mkdirSync(outputDirectory, { recursive: true });
|
|
90
89
|
outputDebugFile = createWriteStream(outputFilePath, { flags: "a" });
|
|
@@ -110,6 +109,18 @@ export const needleViteAlias = (command, config, userSettings) => {
|
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
|
|
112
|
+
|
|
113
|
+
// is needle engine a local package?
|
|
114
|
+
// This will cause local changes to not be reflected anymore...
|
|
115
|
+
const localEnginePath = path.resolve(projectDir, 'node_modules/', '@needle-tools/engine', 'node_modules');
|
|
116
|
+
if (existsSync(localEnginePath)) {
|
|
117
|
+
config.optimizeDeps ??= {};
|
|
118
|
+
config.optimizeDeps.exclude ??= [];
|
|
119
|
+
config.optimizeDeps.exclude.push('@needle-tools/engine');
|
|
120
|
+
log("[needle-alias] Detected local @needle-tools/engine package: will exclude it from optimization");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
113
124
|
if (debug) {
|
|
114
125
|
const testResults = [];
|
|
115
126
|
for (const name in aliasDict) {
|
|
@@ -179,22 +190,20 @@ export const needleViteAlias = (command, config, userSettings) => {
|
|
|
179
190
|
}
|
|
180
191
|
|
|
181
192
|
const workingDirectory = `${process.cwd()}/`;
|
|
193
|
+
const pathExists = existsSync(fullpath);
|
|
194
|
+
|
|
195
|
+
aliasDict[name] = (packageName, index, path) => {
|
|
196
|
+
if (callback !== null) {
|
|
197
|
+
const overrideResult = callback(fullpath, packageName, index, path);
|
|
198
|
+
if (typeof overrideResult === "string")
|
|
199
|
+
if (existsSync(overrideResult)) {
|
|
200
|
+
if (debug && overrideResult !== packageName) log(`[needle-alias] Resolved \"${path}\" → \"${overrideResult.substring(workingDirectory.length).replaceAll("\\", "/")}\"`);
|
|
201
|
+
return overrideResult;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
182
204
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (callback !== null) {
|
|
186
|
-
const overrideResult = callback(fullpath, packageName, index, path);
|
|
187
|
-
if (typeof overrideResult === "string")
|
|
188
|
-
if (existsSync(overrideResult)) {
|
|
189
|
-
if (debug && overrideResult !== packageName) console.warn(`[needle-alias] Resolved \"${path}\" → \"${overrideResult.substring(workingDirectory.length).replaceAll("\\", "/")}\"`);
|
|
190
|
-
return overrideResult;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (debug && path !== packageName) {
|
|
195
|
-
console.warn(`[needle-alias] Resolved \"${path}\" → \"${fullpath.substring(workingDirectory.length).replaceAll("\\", "/")}\"`);
|
|
196
|
-
}
|
|
197
|
-
|
|
205
|
+
if (pathExists) {
|
|
206
|
+
if (debug && path !== packageName) log(`[needle-alias] Resolved \"${path}\" → \"${fullpath.substring(workingDirectory.length).replaceAll("\\", "/")}\"`);
|
|
198
207
|
return fullpath;
|
|
199
208
|
}
|
|
200
209
|
}
|
|
@@ -8,7 +8,7 @@ import { BlobStorage } from "./engine_networking_blob.js";
|
|
|
8
8
|
import { registerPrefabProvider, syncInstantiate, SyncInstantiateOptions } from "./engine_networking_instantiate.js";
|
|
9
9
|
import { SerializationContext, TypeSerializer } from "./engine_serialization_core.js";
|
|
10
10
|
import { Context } from "./engine_setup.js";
|
|
11
|
-
import type { GLTF, IComponent, IGameObject, SourceIdentifier } from "./engine_types.js";
|
|
11
|
+
import type { GLTF, IComponent, IGameObject, Model, SourceIdentifier } from "./engine_types.js";
|
|
12
12
|
|
|
13
13
|
const debug = getParam("debugaddressables");
|
|
14
14
|
|
|
@@ -138,21 +138,35 @@ export class AssetReference {
|
|
|
138
138
|
return ref;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
private static currentlyInstantiating: Map<string, number> = new Map<string, number>();
|
|
142
141
|
|
|
143
|
-
/** @returns true if this is an AssetReference instance */
|
|
144
|
-
get isAssetReference() { return true; }
|
|
145
142
|
|
|
146
|
-
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
readonly isAssetReference = true;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* This is the loaded asset root object. If the asset is a glb/gltf file this will be the {@link three#Scene} object.
|
|
150
|
+
*/
|
|
151
|
+
get rawAsset(): any { return this._rawAsset; }
|
|
152
|
+
|
|
153
|
+
/** The loaded asset root
|
|
154
|
+
*/
|
|
147
155
|
get asset(): Object3D | null {
|
|
148
|
-
return this._glbRoot ?? this.
|
|
156
|
+
return this._glbRoot ?? (this._rawAsset?.scene || null);
|
|
149
157
|
}
|
|
150
158
|
|
|
151
|
-
protected set asset(val:
|
|
152
|
-
|
|
159
|
+
protected set asset(val: Object3D | null) {
|
|
160
|
+
if (val) {
|
|
161
|
+
this._rawAsset = {
|
|
162
|
+
animations: val.animations,
|
|
163
|
+
scene: val,
|
|
164
|
+
scenes: [val],
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else this._rawAsset = null;
|
|
153
168
|
}
|
|
154
169
|
|
|
155
|
-
private _loading?: PromiseLike<any>;
|
|
156
170
|
|
|
157
171
|
/** The url of the loaded asset (or the asset to be loaded)
|
|
158
172
|
* @deprecated use url */
|
|
@@ -177,12 +191,7 @@ export class AssetReference {
|
|
|
177
191
|
(this._url.startsWith("http") || this._url.startsWith("blob:") || this._url.startsWith("www.") || this._url.includes("/"));
|
|
178
192
|
}
|
|
179
193
|
|
|
180
|
-
|
|
181
|
-
* This is the loaded asset root object. If the asset is a glb/gltf file this will be the {@link three#Scene} object.
|
|
182
|
-
*/
|
|
183
|
-
get rawAsset(): any { return this._asset; }
|
|
184
|
-
|
|
185
|
-
private _asset: any;
|
|
194
|
+
private _rawAsset: Model | null = null;
|
|
186
195
|
private _glbRoot?: Object3D | null;
|
|
187
196
|
private _url: string;
|
|
188
197
|
private _urlName: string;
|
|
@@ -207,7 +216,7 @@ export class AssetReference {
|
|
|
207
216
|
else {
|
|
208
217
|
this._urlName = uri;
|
|
209
218
|
}
|
|
210
|
-
|
|
219
|
+
|
|
211
220
|
if (asset !== null) this.asset = asset;
|
|
212
221
|
registerPrefabProvider(this._url, this.onResolvePrefab.bind(this));
|
|
213
222
|
}
|
|
@@ -226,6 +235,8 @@ export class AssetReference {
|
|
|
226
235
|
return !this.asset || (this.asset as any).__destroyed === true || isDestroyed(this.asset) === true;
|
|
227
236
|
}
|
|
228
237
|
|
|
238
|
+
private _loadingPromise: Promise<Model | null | undefined> | null = null;
|
|
239
|
+
|
|
229
240
|
/**
|
|
230
241
|
* @returns `true` if the asset has been loaded (via preload) or if it exists already (assigned to `asset`) */
|
|
231
242
|
isLoaded() { return this._rawBinary || this.asset !== undefined }
|
|
@@ -242,7 +253,7 @@ export class AssetReference {
|
|
|
242
253
|
this.asset = null;
|
|
243
254
|
this._rawBinary = undefined;
|
|
244
255
|
this._glbRoot = null;
|
|
245
|
-
this.
|
|
256
|
+
this._loadingPromise = null;
|
|
246
257
|
if (Context.Current) {
|
|
247
258
|
Context.Current.addressables.unregisterAssetReference(this);
|
|
248
259
|
}
|
|
@@ -267,16 +278,14 @@ export class AssetReference {
|
|
|
267
278
|
/** Loads the asset and creates one instance (assigned to `asset`)
|
|
268
279
|
* @returns the loaded asset
|
|
269
280
|
*/
|
|
270
|
-
async loadAssetAsync(prog?: ProgressCallback | null) {
|
|
271
|
-
if (debug)
|
|
272
|
-
console.log("loadAssetAsync", this.url);
|
|
281
|
+
async loadAssetAsync(prog?: ProgressCallback | null): Promise<Object3D | null> {
|
|
282
|
+
if (debug) console.log("[AssetReference] loadAssetAsync", this.url);
|
|
273
283
|
if (!this.mustLoad) return this.asset;
|
|
274
284
|
|
|
275
285
|
if (prog) this._progressListeners.push(prog);
|
|
276
|
-
|
|
277
|
-
if (this.
|
|
278
|
-
|
|
279
|
-
return this._loading.then(_ => this.asset);
|
|
286
|
+
|
|
287
|
+
if (this._loadingPromise !== null) {
|
|
288
|
+
return this._loadingPromise.then(_ => this.asset);
|
|
280
289
|
}
|
|
281
290
|
const context = Context.Current;
|
|
282
291
|
// TODO: technically we shouldnt call awake only when the object is added to a scene
|
|
@@ -285,31 +294,30 @@ export class AssetReference {
|
|
|
285
294
|
// console.log("START LOADING");
|
|
286
295
|
if (this._rawBinary) {
|
|
287
296
|
if (!(this._rawBinary instanceof ArrayBuffer)) {
|
|
288
|
-
console.error("Failed loading
|
|
297
|
+
console.error("[AssetReference] Failed loading – Invalid data. Must be of type ArrayBuffer. " + (typeof this._rawBinary));
|
|
289
298
|
return null;
|
|
290
299
|
}
|
|
291
|
-
this.
|
|
300
|
+
this._loadingPromise = getLoader().parseSync(context, this._rawBinary, this.url, null);
|
|
292
301
|
this.raiseProgressEvent(new ProgressEvent("progress", { loaded: this._rawBinary.byteLength, total: this._rawBinary.byteLength }));
|
|
293
302
|
}
|
|
294
303
|
else {
|
|
295
304
|
if (debug) console.log("Load async", this.url);
|
|
296
|
-
this.
|
|
305
|
+
this._loadingPromise = getLoader().loadSync(context, this.url, this.url, null, prog => {
|
|
297
306
|
this.raiseProgressEvent(prog);
|
|
298
307
|
});
|
|
299
308
|
}
|
|
300
|
-
|
|
309
|
+
this._loadingPromise.finally(() => this._loadingPromise = null);
|
|
310
|
+
const res = await this._loadingPromise;
|
|
301
311
|
// clear all progress listeners after download has finished
|
|
302
312
|
this._progressListeners.length = 0;
|
|
303
313
|
this._glbRoot = this.tryGetActualGameObjectRoot(res);
|
|
304
|
-
this._loading = undefined;
|
|
305
314
|
if (res) {
|
|
306
315
|
// Make sure the loaded roots all have a reference to this AssetReference
|
|
307
316
|
// that was originally loading it.
|
|
308
317
|
// We need this when the loaded asset is being disposed
|
|
309
318
|
// TODO: we have to prevent disposing resources that are still in use
|
|
310
319
|
res[$assetReference] = this;
|
|
311
|
-
if (this._glbRoot)
|
|
312
|
-
this._glbRoot[$assetReference] = this;
|
|
320
|
+
if (this._glbRoot) this._glbRoot[$assetReference] = this;
|
|
313
321
|
if (this.asset) this.asset[$assetReference] = this;
|
|
314
322
|
|
|
315
323
|
// we need to handle the pre_setup callsbacks before instantiating
|
|
@@ -317,7 +325,7 @@ export class AssetReference {
|
|
|
317
325
|
processNewScripts(context);
|
|
318
326
|
|
|
319
327
|
if (res.scene !== undefined) {
|
|
320
|
-
this.
|
|
328
|
+
this._rawAsset = res;
|
|
321
329
|
}
|
|
322
330
|
return this.asset;
|
|
323
331
|
}
|
|
@@ -352,6 +360,9 @@ export class AssetReference {
|
|
|
352
360
|
}
|
|
353
361
|
}
|
|
354
362
|
|
|
363
|
+
|
|
364
|
+
private static readonly currentlyInstantiating: Map<string, number> = new Map<string, number>();
|
|
365
|
+
|
|
355
366
|
private async onInstantiate(opts?: Object3D | IInstantiateOptions | SyncInstantiateOptions | null, networked: boolean = false, saveOnServer?: boolean): Promise<Object3D | null> {
|
|
356
367
|
const context = Context.Current;
|
|
357
368
|
|
|
@@ -544,7 +555,7 @@ export class ImageReference {
|
|
|
544
555
|
console.error("Can not load texture without url");
|
|
545
556
|
return failedTexturePromise;
|
|
546
557
|
}
|
|
547
|
-
|
|
558
|
+
|
|
548
559
|
if (!this.loader) this.loader = new TextureLoader();
|
|
549
560
|
this.loader.setCrossOrigin("anonymous");
|
|
550
561
|
return this.loader.loadAsync(this.url).then(res => {
|
|
@@ -2,6 +2,7 @@ import { AnimationAction, AnimationClip, AnimationMixer, Object3D, PropertyBindi
|
|
|
2
2
|
|
|
3
3
|
import type { Context } from "./engine_context.js";
|
|
4
4
|
import { GLTF, IAnimationComponent, Model } from "./engine_types.js";
|
|
5
|
+
import { TypeStore } from "./engine_typestore.js";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Registry for animation related data. Use {@link registerAnimationMixer} to register an animation mixer instance.
|
|
@@ -88,12 +89,15 @@ export class AnimationUtils {
|
|
|
88
89
|
* This method will look for objects in the scene that have animations and assign them to the correct objects.
|
|
89
90
|
* @param file The GLTF file to assign the animations from
|
|
90
91
|
*/
|
|
91
|
-
static
|
|
92
|
+
static autoplayAnimations(file: Object3D | Pick<Model, "animations" | "scene">): Array<IAnimationComponent> | null {
|
|
92
93
|
if (!file || !file.animations) {
|
|
93
94
|
console.debug("No animations found in file");
|
|
94
|
-
return;
|
|
95
|
+
return null;
|
|
95
96
|
}
|
|
96
97
|
|
|
98
|
+
const scene = "scene" in file ? file.scene : file as Object3D;
|
|
99
|
+
const animationComponents = new Array<IAnimationComponent>();
|
|
100
|
+
|
|
97
101
|
for (let i = 0; i < file.animations.length; i++) {
|
|
98
102
|
const animation = file.animations[i];
|
|
99
103
|
if (!animation.tracks || animation.tracks.length <= 0) {
|
|
@@ -103,12 +107,12 @@ export class AnimationUtils {
|
|
|
103
107
|
for (const t in animation.tracks) {
|
|
104
108
|
const track = animation.tracks[t];
|
|
105
109
|
const parsedPath = PropertyBinding.parseTrackName(track.name);
|
|
106
|
-
let obj = PropertyBinding.findNode(
|
|
110
|
+
let obj = PropertyBinding.findNode(scene, parsedPath.nodeName);
|
|
107
111
|
if (!obj) {
|
|
108
112
|
const objectName = track["__objectName"] ?? track.name.substring(0, track.name.indexOf("."));
|
|
109
113
|
// let obj = gltf.scene.getObjectByName(objectName);
|
|
110
114
|
// this finds unnamed objects that still have tracks targeting them
|
|
111
|
-
obj =
|
|
115
|
+
obj = scene.getObjectByProperty('uuid', objectName);
|
|
112
116
|
|
|
113
117
|
if (!obj) {
|
|
114
118
|
// console.warn("could not find " + objectName, animation, gltf.scene);
|
|
@@ -116,19 +120,23 @@ export class AnimationUtils {
|
|
|
116
120
|
}
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
let animationComponent = findAnimationGameObjectInParent(obj) || findAnimationGameObjectInParent(
|
|
123
|
+
let animationComponent = findAnimationGameObjectInParent(obj) || findAnimationGameObjectInParent(scene);
|
|
120
124
|
if (!animationComponent) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
125
|
+
const anim = TypeStore.get("Animation");
|
|
126
|
+
animationComponent = scene.addComponent(anim);
|
|
127
|
+
if (!animationComponent) {
|
|
128
|
+
console.error("Failed creating Animation component: No 'Animation' component found in TypeStore");
|
|
129
|
+
break;
|
|
124
130
|
}
|
|
125
|
-
animationComponent = opts.createAnimationComponent(file.scene, animation);
|
|
126
131
|
}
|
|
132
|
+
animationComponents.push(animationComponent);
|
|
127
133
|
if (animationComponent.addClip) {
|
|
128
134
|
animationComponent.addClip(animation);
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
137
|
}
|
|
138
|
+
return animationComponents;
|
|
139
|
+
|
|
132
140
|
function findAnimationGameObjectInParent(obj): IAnimationComponent | null {
|
|
133
141
|
if (!obj) return null;
|
|
134
142
|
const components = obj.userData?.components;
|
|
@@ -61,7 +61,7 @@ export async function onCreateLoader(url: string, context: Context): Promise<Cus
|
|
|
61
61
|
switch (type) {
|
|
62
62
|
case "unsupported":
|
|
63
63
|
return null;
|
|
64
|
-
|
|
64
|
+
|
|
65
65
|
default:
|
|
66
66
|
case "unknown":
|
|
67
67
|
{
|
|
@@ -287,15 +287,6 @@ async function onAfterLoaded(loader: Loader | CustomLoader, context: Context, gl
|
|
|
287
287
|
gltfId = gltfId.split("?")[0];
|
|
288
288
|
}
|
|
289
289
|
|
|
290
|
-
// assign animations of loaded glTF to all scenes
|
|
291
|
-
if ("scenes" in model) {
|
|
292
|
-
for (const scene of model.scenes) {
|
|
293
|
-
if (scene && !scene.animations?.length) {
|
|
294
|
-
scene.animations = [...model.animations];
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
290
|
// E.g. fbx material cleanup
|
|
300
291
|
postprocessLoadedFile(loader, model);
|
|
301
292
|
|
|
@@ -357,13 +348,27 @@ function checkIfUserAttemptedToLoadALocalFile(url: string) {
|
|
|
357
348
|
/**
|
|
358
349
|
* Postprocess the loaded file. This is used to apply any custom postprocessing to the loaded file.
|
|
359
350
|
*/
|
|
360
|
-
function postprocessLoadedFile(loader: object,
|
|
351
|
+
function postprocessLoadedFile(loader: object, model: Model) {
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
// assign animations of loaded glTF to all scenes
|
|
355
|
+
if ("scenes" in model) {
|
|
356
|
+
for (const scene of model.scenes) {
|
|
357
|
+
if (scene && !scene.animations?.length) {
|
|
358
|
+
for (const anim of model.animations) {
|
|
359
|
+
if (!scene.animations.includes(anim)) {
|
|
360
|
+
scene.animations.push(anim);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
361
366
|
|
|
362
367
|
if (loader instanceof FBXLoader || loader instanceof OBJLoader) {
|
|
363
368
|
|
|
364
|
-
let obj: Object3D | Model =
|
|
369
|
+
let obj: Object3D | Model = model;
|
|
365
370
|
if (!(obj instanceof Object3D)) {
|
|
366
|
-
obj = (
|
|
371
|
+
obj = (model as GLTF).scene || model.scenes.find(s => s);
|
|
367
372
|
}
|
|
368
373
|
|
|
369
374
|
obj.traverse((child) => {
|
|
@@ -67,7 +67,7 @@ export class Animation extends Behaviour implements IAnimationComponent {
|
|
|
67
67
|
get isAnimationComponent(): boolean { return true; }
|
|
68
68
|
addClip(clip: AnimationClip) {
|
|
69
69
|
if (!this.animations) this.animations = [];
|
|
70
|
-
this.animations.push(clip);
|
|
70
|
+
if (!this.animations.includes(clip)) this.animations.push(clip);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
/**
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { GLTF } from "three/examples/jsm/loaders/GLTFLoader.js";
|
|
2
2
|
|
|
3
3
|
import { AnimationUtils } from "../engine/engine_animation.js";
|
|
4
|
-
import { addComponent } from "../engine/engine_components.js";
|
|
5
4
|
import { ContextEvent, ContextRegistry } from "../engine/engine_context_registry.js";
|
|
6
5
|
import { Animation } from "./Animation.js";
|
|
7
6
|
import { Animator } from "./Animator.js";
|
|
@@ -29,11 +28,7 @@ ContextRegistry.registerCallback(ContextEvent.ContextCreated, args => {
|
|
|
29
28
|
return undefined;
|
|
30
29
|
}, true);
|
|
31
30
|
if (hasAnimation !== true) {
|
|
32
|
-
AnimationUtils.
|
|
33
|
-
createAnimationComponent: (obj, _clip) => {
|
|
34
|
-
return addComponent(obj, Animation);
|
|
35
|
-
},
|
|
36
|
-
});
|
|
31
|
+
AnimationUtils.autoplayAnimations(file.file as GLTF);
|
|
37
32
|
}
|
|
38
33
|
}
|
|
39
34
|
}
|
|
@@ -143,13 +143,6 @@ const blobKeyName = "blob";
|
|
|
143
143
|
*/
|
|
144
144
|
export class DropListener extends Behaviour {
|
|
145
145
|
|
|
146
|
-
/**
|
|
147
|
-
* When enabled, the DropListener will automatically synchronize dropped files to other connected clients.
|
|
148
|
-
* When a file is dropped locally, it will be uploaded to blob storage and the URL will be shared with other clients.
|
|
149
|
-
*/
|
|
150
|
-
@serializable()
|
|
151
|
-
useNetworking: boolean = true;
|
|
152
|
-
|
|
153
146
|
/**
|
|
154
147
|
* When assigned, the DropListener will only accept files that are dropped on this specific object.
|
|
155
148
|
* This allows creating designated drop zones in your scene.
|
|
@@ -159,7 +152,10 @@ export class DropListener extends Behaviour {
|
|
|
159
152
|
|
|
160
153
|
/**
|
|
161
154
|
* When enabled, dropped objects will be automatically scaled to fit within the volume defined by fitVolumeSize.
|
|
162
|
-
* Useful for ensuring dropped models appear at an appropriate scale.
|
|
155
|
+
* Useful for ensuring dropped models appear at an appropriate scale.
|
|
156
|
+
*
|
|
157
|
+
* **Tip**: Use the handy `fitObjectIntoVolume` function (`import { fitObjectIntoVolume } from "@needle-tools/engine"`) for custom fitting needs.
|
|
158
|
+
*
|
|
163
159
|
* @default false
|
|
164
160
|
*/
|
|
165
161
|
@serializable()
|
|
@@ -180,6 +176,14 @@ export class DropListener extends Behaviour {
|
|
|
180
176
|
@serializable()
|
|
181
177
|
placeAtHitPosition: boolean = true;
|
|
182
178
|
|
|
179
|
+
/**
|
|
180
|
+
* When enabled, the DropListener will automatically synchronize dropped files to other connected clients.
|
|
181
|
+
* When a file is dropped locally, it will be uploaded to blob storage and the URL will be shared with other clients.
|
|
182
|
+
* @default false
|
|
183
|
+
*/
|
|
184
|
+
@serializable()
|
|
185
|
+
useNetworking: boolean = false;
|
|
186
|
+
|
|
183
187
|
/**
|
|
184
188
|
* Event list that gets invoked after a file has been successfully added to the scene.
|
|
185
189
|
* Receives {@link DropListenerOnDropArguments} containing the added object and related information.
|
|
@@ -193,6 +197,27 @@ export class DropListener extends Behaviour {
|
|
|
193
197
|
@serializable(EventList)
|
|
194
198
|
onDropped: EventList<DropListenerOnDropArguments> = new EventList();
|
|
195
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Loads a file from the given URL and adds it to the scene.
|
|
202
|
+
* @returns A promise that resolves to the loaded object or null if loading failed.
|
|
203
|
+
*/
|
|
204
|
+
loadFromURL(url: string, data?: { point?: Vec3, size?: Vec3 }): Promise<Object3D | null> {
|
|
205
|
+
return this.addFromUrl(url, { screenposition: new Vector2(), point: data?.point, size: data?.size, }, false);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Forgets all previously added objects.
|
|
210
|
+
* The droplistener will then not be able to remove previously added objects.
|
|
211
|
+
*/
|
|
212
|
+
forgetObjects() {
|
|
213
|
+
this.removePreviouslyAddedObjects(false);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
// #region internals
|
|
220
|
+
|
|
196
221
|
/** @internal */
|
|
197
222
|
onEnable(): void {
|
|
198
223
|
this.context.renderer.domElement.addEventListener("dragover", this.onDrag);
|
|
@@ -208,21 +233,6 @@ export class DropListener extends Behaviour {
|
|
|
208
233
|
this.context.connection.stopListen("droplistener", this.onNetworkEvent);
|
|
209
234
|
}
|
|
210
235
|
|
|
211
|
-
/**
|
|
212
|
-
* Loads a file from the given URL and adds it to the scene.
|
|
213
|
-
*/
|
|
214
|
-
loadFromURL(url: string, data?: { point?: Vec3, size?: Vec3 }) {
|
|
215
|
-
this.addFromUrl(url, { screenposition: new Vector2(), point: data?.point, size: data?.size, }, true);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Forgets all previously added objects.
|
|
220
|
-
* The droplistener will then not be able to remove previously added objects.
|
|
221
|
-
*/
|
|
222
|
-
forgetObjects() {
|
|
223
|
-
this.removePreviouslyAddedObjects(false);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
236
|
/**
|
|
227
237
|
* Handles network events received from other clients containing information about dropped objects
|
|
228
238
|
* @param evt Network event data containing object information, position, and content URL
|
|
@@ -326,7 +336,7 @@ export class DropListener extends Behaviour {
|
|
|
326
336
|
}
|
|
327
337
|
}
|
|
328
338
|
if (files.length > 0) {
|
|
329
|
-
await this.
|
|
339
|
+
await this.addFromFiles(files, ctx);
|
|
330
340
|
}
|
|
331
341
|
}
|
|
332
342
|
|
|
@@ -359,6 +369,7 @@ export class DropListener extends Behaviour {
|
|
|
359
369
|
// Ignore dropped images
|
|
360
370
|
const lowercaseUrl = url.toLowerCase();
|
|
361
371
|
if (lowercaseUrl.endsWith(".hdr") || lowercaseUrl.endsWith(".hdri") || lowercaseUrl.endsWith(".exr") || lowercaseUrl.endsWith(".png") || lowercaseUrl.endsWith(".jpg") || lowercaseUrl.endsWith(".jpeg")) {
|
|
372
|
+
console.warn(`Fileformat is not supported: ${lowercaseUrl}`);
|
|
362
373
|
return null;
|
|
363
374
|
}
|
|
364
375
|
|
|
@@ -374,7 +385,7 @@ export class DropListener extends Behaviour {
|
|
|
374
385
|
});
|
|
375
386
|
if (res && this._addedObjects.length <= 0) {
|
|
376
387
|
ctx.url = url;
|
|
377
|
-
const obj = this.
|
|
388
|
+
const obj = this.onObjectLoaded(res, ctx, isRemote);
|
|
378
389
|
return obj;
|
|
379
390
|
}
|
|
380
391
|
}
|
|
@@ -393,7 +404,7 @@ export class DropListener extends Behaviour {
|
|
|
393
404
|
* @param fileList Array of dropped files
|
|
394
405
|
* @param ctx Context information about where the drop occurred
|
|
395
406
|
*/
|
|
396
|
-
private async
|
|
407
|
+
private async addFromFiles(fileList: Array<File>, ctx: DropContext) {
|
|
397
408
|
if (debug) console.log("Add files", fileList)
|
|
398
409
|
if (!Array.isArray(fileList)) return;
|
|
399
410
|
if (!fileList.length) return;
|
|
@@ -427,7 +438,7 @@ export class DropListener extends Behaviour {
|
|
|
427
438
|
if (res) {
|
|
428
439
|
this.dispatchEvent(new CustomEvent(DropListenerEvents.FileDropped, { detail: file }));
|
|
429
440
|
ctx.file = file;
|
|
430
|
-
const obj = this.
|
|
441
|
+
const obj = this.onObjectLoaded(res, ctx, false);
|
|
431
442
|
|
|
432
443
|
// handle uploading the dropped object and networking the event
|
|
433
444
|
if (obj && this.context.connection.isConnected && this.useNetworking) {
|
|
@@ -478,7 +489,7 @@ export class DropListener extends Behaviour {
|
|
|
478
489
|
* @param isRemote Whether this object was shared from a remote client
|
|
479
490
|
* @returns The added object or null if adding failed
|
|
480
491
|
*/
|
|
481
|
-
private
|
|
492
|
+
private onObjectLoaded(data: { model: Model, contentMD5: string }, ctx: DropContext, isRemote: boolean): Object3D | null {
|
|
482
493
|
|
|
483
494
|
const { model, contentMD5 } = data;
|
|
484
495
|
|
|
@@ -522,9 +533,7 @@ export class DropListener extends Behaviour {
|
|
|
522
533
|
}
|
|
523
534
|
}
|
|
524
535
|
|
|
525
|
-
AnimationUtils.
|
|
526
|
-
createAnimationComponent: obj => addComponent(obj, Animation)
|
|
527
|
-
});
|
|
536
|
+
AnimationUtils.autoplayAnimations(model);
|
|
528
537
|
|
|
529
538
|
const evt = new DropListenerAddedEvent({
|
|
530
539
|
sender: this,
|