rayzee 7.9.2 → 7.10.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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Rayzee Engine
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/rayzee?label=npm)](https://www.npmjs.com/package/rayzee)
4
- [![minzipped size](https://img.shields.io/badge/minzipped-163.8%20KB-blue)](https://www.npmjs.com/package/rayzee)
4
+ [![minzipped size](https://img.shields.io/badge/minzipped-165.1%20KB-blue)](https://www.npmjs.com/package/rayzee)
5
5
  [![downloads](https://img.shields.io/npm/dw/rayzee?label=downloads)](https://www.npmjs.com/package/rayzee)
6
6
  [![jsDelivr](https://img.shields.io/jsdelivr/npm/hm/rayzee?label=jsDelivr)](https://www.jsdelivr.com/package/npm/rayzee)
7
7
 
package/dist/rayzee.es.js CHANGED
@@ -9541,7 +9541,7 @@ var Lu = class extends $s {
9541
9541
  super.setSize(e, t), this._rebuildKernelsIfResized(n, r);
9542
9542
  }
9543
9543
  _maybeReadbackCounters() {
9544
- if (this.cameraChanged) {
9544
+ if (this.cameraChanged || this.cameraOptimizer?.isInInteractionMode()) {
9545
9545
  this._readbackFrameCounter = this._readbackEveryNFrames;
9546
9546
  return;
9547
9547
  }
@@ -12812,12 +12812,47 @@ var Wd = {
12812
12812
  type: "archive",
12813
12813
  name: "ZIP Archive"
12814
12814
  }
12815
- }, Gd = class extends q {
12815
+ }, Gd = class e extends q {
12816
12816
  constructor(e, t, n) {
12817
- super(), this.scene = e, this.camera = t, this.controls = n, this.targetModel = null, this._externalModel = null, this.floorPlane = null, this.sceneScale = 1, this.loaderCache = {}, this.uploadedFileInfo = null, this.animations = [], this.renderer = null;
12817
+ super(), this.scene = e, this.camera = t, this.controls = n, this.targetModel = null, this._externalModel = null, this.floorPlane = null, this.sceneScale = 1, this.loaderCache = {}, this.uploadedFileInfo = null, this.animations = [], this.renderer = null, this._loadingManager = new ht(), this._loadCancelled = !1;
12818
+ }
12819
+ cancelActiveLoad() {
12820
+ this._loadCancelled = !0, this._loadingManager.abort();
12821
+ }
12822
+ _isCancellation(e) {
12823
+ return this._loadCancelled || e?.name === "AbortError" || e?.code === "LOAD_CANCELLED";
12824
+ }
12825
+ _cancellationError() {
12826
+ let e = /* @__PURE__ */ Error("Load cancelled");
12827
+ return e.code = "LOAD_CANCELLED", e;
12828
+ }
12829
+ _downloadProgress(e, t) {
12830
+ return (n) => {
12831
+ let r = n?.loaded || 0, i = n?.lengthComputable && n.total || 0;
12832
+ ar({
12833
+ isLoading: !0,
12834
+ status: e,
12835
+ loadedBytes: r,
12836
+ totalBytes: i,
12837
+ canCancel: !!t,
12838
+ progress: i ? Math.min(60, 2 + Math.round(r / i * 58)) : 2
12839
+ });
12840
+ };
12841
+ }
12842
+ _downloadComplete(e = "Processing Data...", t = 62) {
12843
+ ar({
12844
+ status: e,
12845
+ progress: t,
12846
+ canCancel: !1,
12847
+ loadedBytes: null,
12848
+ totalBytes: null
12849
+ });
12850
+ }
12851
+ static _isNetworkUrl(e) {
12852
+ return typeof e == "string" && /^https?:/i.test(e);
12818
12853
  }
12819
12854
  releaseTargetModel() {
12820
- this.targetModel && (this.targetModel === this._externalModel ? this.targetModel.parent?.remove(this.targetModel) : dr(this.targetModel), this.targetModel = null, this._externalModel = null);
12855
+ this.targetModel && (this.targetModel === this._externalModel ? this.targetModel.parent?.remove(this.targetModel) : dr(this.targetModel), this.targetModel = null, this._externalModel = null, this.animations = []);
12821
12856
  }
12822
12857
  setRenderer(e) {
12823
12858
  this.renderer = e;
@@ -12903,6 +12938,7 @@ var Wd = {
12903
12938
  }
12904
12939
  }
12905
12940
  async loadEnvironment(e) {
12941
+ this._loadCancelled = !1;
12906
12942
  try {
12907
12943
  this.dispatchEvent({
12908
12944
  type: "beforeEnvironmentLoad",
@@ -12919,11 +12955,11 @@ var Wd = {
12919
12955
  texture: t
12920
12956
  }), t;
12921
12957
  } catch (t) {
12922
- throw console.error("Error loading environment:", t), this.dispatchEvent({
12958
+ throw this._isCancellation(t) ? this._cancellationError() : (console.error("Error loading environment:", t), this.dispatchEvent({
12923
12959
  type: "error",
12924
12960
  message: t.message,
12925
12961
  filename: e
12926
- }), t;
12962
+ }), t);
12927
12963
  }
12928
12964
  }
12929
12965
  async loadEnvironmentFromBlob(e) {
@@ -12947,9 +12983,9 @@ var Wd = {
12947
12983
  }
12948
12984
  return !n && this.uploadedFileInfo && (n = this.uploadedFileInfo.name.split(".").pop().toLowerCase()), n;
12949
12985
  }
12950
- async loadEnvironmentByExtension(e, t) {
12951
- let n;
12952
- return t === "hdr" || t === "exr" ? n = await (t === "hdr" ? this.loaderCache.hdr || (this.loaderCache.hdr = new cn().setDataType(ct)) : this.loaderCache.exr || (this.loaderCache.exr = new dn().setDataType(ct))).loadAsync(e) : (this.loaderCache.texture || (this.loaderCache.texture = new Zt()), n = await this.loaderCache.texture.loadAsync(e), n.colorSpace = Wt), n.mapping = ot, n.minFilter = J, n.magFilter = J, n;
12986
+ async loadEnvironmentByExtension(t, n) {
12987
+ let r = e._isNetworkUrl(t), i = this._downloadProgress("Downloading Environment...", r), a;
12988
+ return n === "hdr" || n === "exr" ? a = await (n === "hdr" ? this.loaderCache.hdr || (this.loaderCache.hdr = new cn(this._loadingManager).setDataType(ct)) : this.loaderCache.exr || (this.loaderCache.exr = new dn(this._loadingManager).setDataType(ct))).loadAsync(t, i) : (this.loaderCache.texture || (this.loaderCache.texture = new Zt(this._loadingManager)), a = await this.loaderCache.texture.loadAsync(t, i), a.colorSpace = Wt), this._downloadComplete("Processing Environment...", 62), a.mapping = ot, a.minFilter = J, a.magFilter = J, a;
12953
12989
  }
12954
12990
  applyEnvironmentToScene(e) {
12955
12991
  this.scene.background = e, this.scene.environment = e;
@@ -13239,7 +13275,7 @@ var Wd = {
13239
13275
  bptcSupported: !1,
13240
13276
  pvrtcSupported: !1
13241
13277
  });
13242
- let i = new sn();
13278
+ let i = new sn(this._loadingManager);
13243
13279
  return i.setDRACOLoader(n), i.setKTX2Loader(r), i.setMeshoptDecoder(pn), i;
13244
13280
  }
13245
13281
  _disposeGLTFLoader(e) {
@@ -13250,32 +13286,66 @@ var Wd = {
13250
13286
  let n = `${t[e].url}`;
13251
13287
  return await this.loadModel(n);
13252
13288
  }
13253
- async loadModel(e) {
13254
- let t = await this.createGLTFLoader();
13289
+ async loadModel(t) {
13290
+ this._loadCancelled = !1;
13291
+ let n = await this.createGLTFLoader(), r = e._isNetworkUrl(t);
13255
13292
  try {
13256
13293
  ar({
13257
- status: "Loading Model...",
13258
- progress: 2
13294
+ isLoading: !0,
13295
+ status: "Downloading Model...",
13296
+ progress: 2,
13297
+ canCancel: r,
13298
+ loadedBytes: 0,
13299
+ totalBytes: 0
13259
13300
  });
13260
- let n = await t.loadAsync(e);
13261
- return ar({
13262
- status: "Processing Data...",
13263
- progress: 10
13264
- }), this.releaseTargetModel(), this.targetModel = n.scene, this.animations = n.animations || [], await this.onModelLoad(this.targetModel), this.dispatchEvent({
13301
+ let e = await n.loadAsync(t, this._downloadProgress("Downloading Model...", r));
13302
+ return this._downloadComplete(), this.releaseTargetModel(), this.targetModel = e.scene, this.animations = e.animations || [], await this.onModelLoad(this.targetModel), this.dispatchEvent({
13265
13303
  type: "load",
13266
- model: n.scene,
13267
- filename: e.split("/").pop()
13268
- }), n;
13269
- } catch (t) {
13270
- throw console.error("Error loading model:", t), this.dispatchEvent({
13304
+ model: e.scene,
13305
+ filename: t.split("/").pop()
13306
+ }), e;
13307
+ } catch (e) {
13308
+ throw this._isCancellation(e) ? this._cancellationError() : (console.error("Error loading model:", e), this.dispatchEvent({
13271
13309
  type: "error",
13272
- message: t.message,
13273
- filename: e
13274
- }), t;
13310
+ message: e.message,
13311
+ filename: t
13312
+ }), e);
13275
13313
  } finally {
13276
- this._disposeGLTFLoader(t);
13314
+ this._disposeGLTFLoader(n);
13277
13315
  }
13278
13316
  }
13317
+ _processAndParent(e) {
13318
+ this.processModelObjects(e), this.scene.add(e), e.updateMatrixWorld(!0);
13319
+ }
13320
+ async appendModel(t) {
13321
+ this._loadCancelled = !1;
13322
+ let n = await this.createGLTFLoader(), r = e._isNetworkUrl(t);
13323
+ try {
13324
+ ar({
13325
+ isLoading: !0,
13326
+ status: "Downloading Model...",
13327
+ progress: 2,
13328
+ canCancel: r,
13329
+ loadedBytes: 0,
13330
+ totalBytes: 0
13331
+ });
13332
+ let e = await n.loadAsync(t, this._downloadProgress("Downloading Model...", r));
13333
+ return this._downloadComplete(), this._processAndParent(e.scene), {
13334
+ root: e.scene,
13335
+ animations: e.animations || []
13336
+ };
13337
+ } catch (e) {
13338
+ throw this._isCancellation(e) ? this._cancellationError() : e;
13339
+ } finally {
13340
+ this._disposeGLTFLoader(n);
13341
+ }
13342
+ }
13343
+ appendObject3D(e, t = "object3d") {
13344
+ return e.name = e.name || t, this._processAndParent(e), { root: e };
13345
+ }
13346
+ removeModelRoot(e, { external: t = !1 } = {}) {
13347
+ e && (t ? e.parent?.remove(e) : dr(e));
13348
+ }
13279
13349
  async loadGLBFromArrayBuffer(e, t = "model.glb") {
13280
13350
  let n = await this.createGLTFLoader();
13281
13351
  try {
@@ -15614,7 +15684,7 @@ var If = {
15614
15684
  indices: i
15615
15685
  }), this._skinnedCache.push(new Float32Array(o * 3)), this._normalCache.push(new Float32Array(o * 3)), t += a;
15616
15686
  }
15617
- this._posBuffer = new Float32Array(t * 9), this._normalBuffer = new Float32Array(t * 9);
15687
+ this._posBuffer = new Float32Array(t * 9), this._normalBuffer = new Float32Array(t * 9), this._baselineComputed = !1;
15618
15688
  }
15619
15689
  attach(e) {
15620
15690
  this._attached !== e && (this._controls.attach(e), this._attached = e);
@@ -15860,6 +15930,9 @@ var If = {
15860
15930
  index: e
15861
15931
  });
15862
15932
  }
15933
+ cancelLoad() {
15934
+ this._loadingInProgress && this.assetLoader?.cancelActiveLoad();
15935
+ }
15863
15936
  async setMaxTextureSize(e, { reprocess: t = !0 } = {}) {
15864
15937
  let n = this._maxTextureSize;
15865
15938
  if (this._maxTextureSize = this._sdf?.setMaxTextureSize(e) ?? e, typeof this.stages?.pathTracer?.sdfs?.setMaxTextureSize == "function" && this.stages.pathTracer.sdfs.setMaxTextureSize(this._maxTextureSize), t && this._maxTextureSize !== n && this._sdf?.triangleData && !this._loadingInProgress) {
@@ -15878,7 +15951,7 @@ var If = {
15878
15951
  if (this._loadingInProgress) throw Error("PathTracerApp: another load is already in progress");
15879
15952
  this._loadingInProgress = !0;
15880
15953
  try {
15881
- await e(), this._syncControlsAfterLoad(), await this.loadSceneData(), this.pipeline?.eventBus.emit("autoexposure:resetHistory"), this.reset(), this.cameraManager.currentCameraIndex = 0, this.dispatchEvent(t), this.dispatchEvent({
15954
+ await e(), this._clearAppendedModels(), this._syncControlsAfterLoad(), await this.loadSceneData(), this.pipeline?.eventBus.emit("autoexposure:resetHistory"), this.reset(), this.cameraManager.currentCameraIndex = 0, this.dispatchEvent(t), this.dispatchEvent({
15882
15955
  type: "CamerasUpdated",
15883
15956
  cameras: this.cameraManager.cameras,
15884
15957
  cameraNames: this.cameraManager.getCameraNames()
@@ -15888,7 +15961,7 @@ var If = {
15888
15961
  }
15889
15962
  }
15890
15963
  async loadSceneData() {
15891
- this.interactionManager?.deselect(), this.animationManager.dispose(), this._animRefitInFlight = !1;
15964
+ this._preserveSelectionOnRebuild || this.interactionManager?.deselect(), this.animationManager.dispose(), this._animRefitInFlight = !1, this._tagPrimarySceneObject();
15892
15965
  let e = new zs("loadSceneData"), t = this.meshScene.environment, n = null;
15893
15966
  if (t?.image?.data && (e.start("Environment CDF build (worker)"), this.stages.pathTracer.scene.environment = t, n = this.stages.pathTracer.environment.buildEnvironmentCDF().then(() => e.end("Environment CDF build (worker)"))), e.start("BVH build (SceneProcessor)"), this._sdf.setMaxTextureSize(this._maxTextureSize), await this._sdf.buildBVH(this.meshScene), e.end("BVH build (SceneProcessor)"), ar({
15894
15967
  status: "Transferring data to GPU...",
@@ -15910,6 +15983,67 @@ var If = {
15910
15983
  let r = this.getSceneMinY();
15911
15984
  return this.settings.set("groundProjectionLevel", r, { reset: !1 }), this.settings.set("groundCatcherHeight", r, { reset: !1 }), e.start("Apply settings"), this.settings.applyAll(), this.stages.compositor.setTransparentBackground(this.settings.get("transparentBackground")), e.end("Apply settings"), e.print(), ir(), this._initAnimationAndTransforms(), this.dispatchEvent({ type: "SceneRebuild" }), !0;
15912
15985
  }
15986
+ _tagPrimarySceneObject() {
15987
+ let e = this.assetLoader?.targetModel;
15988
+ e && (e.userData.__rayzeeSceneObject = !0, e.userData.__rayzeeExternal = e === this.assetLoader._externalModel);
15989
+ }
15990
+ _clearAppendedModels() {
15991
+ let e = this.meshScene;
15992
+ if (!e) return;
15993
+ let t = this.assetLoader?.floorPlane, n = this.assetLoader?.targetModel;
15994
+ for (let r of [...e.children]) r === t || r === n || r.userData?.__rayzeeSceneObject && this.assetLoader.removeModelRoot(r, { external: !!r.userData.__rayzeeExternal });
15995
+ }
15996
+ async _finishRebuildNoReframe(e) {
15997
+ await this.loadSceneData(), this._recalibrateControlLimits(), this.pipeline?.eventBus.emit("autoexposure:resetHistory"), this.reset(), e && this.dispatchEvent(e);
15998
+ }
15999
+ async addModel(e, { name: t } = {}) {
16000
+ if (this._loadingInProgress) throw Error("PathTracerApp.addModel: another load is already in progress");
16001
+ this._loadingInProgress = !0, this._preserveSelectionOnRebuild = !0;
16002
+ try {
16003
+ let { root: n } = await this.assetLoader.appendModel(e);
16004
+ return n.userData.__rayzeeSceneObject = !0, n.userData.__rayzeeExternal = !1, t && (n.userData.__rayzeeName = t), await this._finishRebuildNoReframe({
16005
+ type: "ModelAdded",
16006
+ url: e,
16007
+ id: n.uuid
16008
+ }), n.uuid;
16009
+ } finally {
16010
+ this._preserveSelectionOnRebuild = !1, this._loadingInProgress = !1;
16011
+ }
16012
+ }
16013
+ async addModelFromObject3D(e, { name: t } = {}) {
16014
+ if (this._loadingInProgress) throw Error("PathTracerApp.addModelFromObject3D: another load is already in progress");
16015
+ this._loadingInProgress = !0, this._preserveSelectionOnRebuild = !0;
16016
+ try {
16017
+ let { root: n } = this.assetLoader.appendObject3D(e, t || "object3d");
16018
+ return n.userData.__rayzeeSceneObject = !0, n.userData.__rayzeeExternal = !0, t && (n.userData.__rayzeeName = t), await this._finishRebuildNoReframe({
16019
+ type: "ModelAdded",
16020
+ id: n.uuid
16021
+ }), n.uuid;
16022
+ } finally {
16023
+ this._preserveSelectionOnRebuild = !1, this._loadingInProgress = !1;
16024
+ }
16025
+ }
16026
+ async removeSceneObject(e) {
16027
+ let t = this.meshScene;
16028
+ if (!t) return !1;
16029
+ let n = this.assetLoader?.floorPlane;
16030
+ if (n && n.uuid === e) return !1;
16031
+ let r = t.children.find((t) => t.uuid === e && t.userData?.__rayzeeSceneObject);
16032
+ if (!r) return !1;
16033
+ if (this._loadingInProgress) throw Error("PathTracerApp.removeSceneObject: another load is already in progress");
16034
+ this._loadingInProgress = !0;
16035
+ try {
16036
+ return this.interactionManager?.deselect(), this.transformManager?.detach?.(), r === this.assetLoader.targetModel ? this.assetLoader.releaseTargetModel() : this.assetLoader.removeModelRoot(r, { external: !!r.userData.__rayzeeExternal }), await this._finishRebuildNoReframe({
16037
+ type: "SceneObjectRemoved",
16038
+ id: e
16039
+ }), !0;
16040
+ } finally {
16041
+ this._loadingInProgress = !1;
16042
+ }
16043
+ }
16044
+ setSceneObjectVisibility(e, t) {
16045
+ return this.setMeshVisibilityByUuid(e, t);
16046
+ }
15913
16047
  async refitBVH(e, t) {
15914
16048
  let n = await this._sdf.refitBVH(e, t);
15915
16049
  return this.stages.pathTracer.updateTriangleData(this._sdf.triangleData), this.stages.pathTracer.updateBVHData(this._sdf.bvhData), this.reset(), n;
@@ -16143,7 +16277,7 @@ var If = {
16143
16277
  reconcileCompletion: () => this._reconcileCompletion()
16144
16278
  }), this.renderer.toneMappingExposure = this.settings.get("exposure") ?? 1, this.onResize(), this.resizeHandler = () => this.onResize(), this._autoResize && this._addTrackedListener(window, "resize", this.resizeHandler), this._onAssetLoaded = async (e) => {
16145
16279
  if (!this._loadingInProgress) {
16146
- if (e.model) await this.loadSceneData();
16280
+ if (e.model) this._clearAppendedModels(), await this.loadSceneData();
16147
16281
  else if (e.texture) {
16148
16282
  let e = this.meshScene.environment;
16149
16283
  e && this.stages.pathTracer && await this.stages.pathTracer.environment.setEnvironmentMap(e), ir();
@@ -16226,6 +16360,18 @@ var If = {
16226
16360
  _syncControlsAfterLoad() {
16227
16361
  this.cameraManager.controls.saveState(), this.cameraManager.controls.update();
16228
16362
  }
16363
+ _recalibrateControlLimits() {
16364
+ if (!this.meshScene || !this.cameraManager) return;
16365
+ let e = new qe(), t = new qe();
16366
+ for (let n of this.meshScene.children) n.userData?.__rayzeeSceneObject && (t.setFromObject(n), t.isEmpty() || e.union(t));
16367
+ if (e.isEmpty()) return;
16368
+ let n = Math.max(e.max.x - e.min.x, e.max.y - e.min.y, e.max.z - e.min.z);
16369
+ if (!Number.isFinite(n) || n <= 0) return;
16370
+ let { camera: r, controls: i } = this.cameraManager, a = r.fov * (Math.PI / 180), o = Math.abs(n / Math.sin(a / 2) / 2);
16371
+ this.cameraManager.currentCameraIndex === 0 && (r.near = n / 100, r.far = n * 100, r.updateProjectionMatrix());
16372
+ let s = r.position.distanceTo(i.target);
16373
+ i.minDistance = Math.min(n / 1e3, s), i.maxDistance = Math.max(o * 10, s * 1.1), i.update();
16374
+ }
16229
16375
  _forwardEvents(e, t) {
16230
16376
  if (e) for (let n of t) this._addTrackedListener(e, n, (e) => this.dispatchEvent(e));
16231
16377
  }