@xviewer.js/core 1.0.0-alpha.44 → 1.0.0-alpha.46

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/dist/main.js CHANGED
@@ -1052,7 +1052,7 @@ class TweenManager {
1052
1052
  }
1053
1053
 
1054
1054
  class aLoader {
1055
- static _setMeshData(node) {
1055
+ static _setUserData(node) {
1056
1056
  const keys = aLoader._texKeys;
1057
1057
  const meshes = [];
1058
1058
  const textures = {};
@@ -1123,7 +1123,7 @@ class aEXRLoader extends aLoader {
1123
1123
  class aFBXLoader extends aLoader {
1124
1124
  load({ url, onLoad, onProgress, onError }) {
1125
1125
  const loadCallback = (node)=>{
1126
- onLoad(aLoader._setMeshData(node));
1126
+ onLoad(aLoader._setUserData(node));
1127
1127
  };
1128
1128
  const loader = new FBXLoader_js.FBXLoader(this.manager.loadingManager);
1129
1129
  loader.setPath(this.manager.path);
@@ -1143,7 +1143,7 @@ class aGLTFLoader extends aLoader {
1143
1143
  const loadCallback = (gltf)=>{
1144
1144
  const node = gltf.scene;
1145
1145
  if (gltf.animations) node.animations = gltf.animations;
1146
- onLoad(aLoader._setMeshData(node));
1146
+ onLoad(aLoader._setUserData(node));
1147
1147
  };
1148
1148
  let gltfLoader = new GLTFLoader_js.GLTFLoader(this.manager.loadingManager);
1149
1149
  gltfLoader.setPath(this.manager.path);
@@ -2692,7 +2692,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2692
2692
  this._button = e.button;
2693
2693
  this._preLoc0.set(e.pageX, e.pageY);
2694
2694
  }
2695
- _onPointerUp(e) {
2695
+ _onPointerUp(_) {
2696
2696
  if (SystemInfo.isMobile) return;
2697
2697
  this._button = -1;
2698
2698
  }
@@ -2812,7 +2812,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2812
2812
  this._targetPhi = clamp(this._targetPhi, this.phiMin, this.phiMax);
2813
2813
  this._targetSpringLength = clamp(this._targetSpringLength, this.distanceMin, this.distanceMax);
2814
2814
  }
2815
- gotoPOI({ duration = -1, easing = Easing.Cubic.InOut, springLength = this._targetSpringLength, theta = this._targetTheta, phi = this._targetPhi, lookAt = this._lookAt, fov = this.lens.fov, smoothing = this.smoothing, rotateSmoothing = this.rotateSmoothing }) {
2815
+ gotoPOI({ duration = -1, easing = Easing.Cubic.InOut, springLength = this._targetSpringLength, theta = this._targetTheta, phi = this._targetPhi, lookAt = this._lookAt, fov = this.lens.fov, smoothing = this.smoothing, rotateSmoothing = smoothing }) {
2816
2816
  this._targetFov = fov;
2817
2817
  this._tempSmoothing = smoothing;
2818
2818
  this._tempRotateSmoothing = rotateSmoothing;
@@ -3146,6 +3146,79 @@ class Reflector extends Component {
3146
3146
  }
3147
3147
  }
3148
3148
 
3149
+ class PerformanceMonitor extends Component {
3150
+ subscribe(subscriber) {
3151
+ this.subscriptions.set(Symbol(), subscriber);
3152
+ }
3153
+ constructor({ iterations = 10, ms = 250, threshold = 0.75, step = 0.1, factor: _factor = 0.5, flipflops = Infinity, bounds = (refreshrate)=>refreshrate > 100 ? [
3154
+ 60,
3155
+ 100
3156
+ ] : [
3157
+ 40,
3158
+ 60
3159
+ ], onIncline, onDecline, onChange, onFallback }){
3160
+ super();
3161
+ this.fps = 0;
3162
+ this.index = 0;
3163
+ this.factor = 0.5;
3164
+ this.flipped = 0;
3165
+ this.refreshrate = 0;
3166
+ this.fallback = false;
3167
+ this.frames = [];
3168
+ this.averages = [];
3169
+ this.subscriptions = new Map();
3170
+ let lastFactor = 0;
3171
+ const decimalPlacesRatio = Math.pow(10, 0);
3172
+ this.update = (dt)=>{
3173
+ // If the fallback has been reached do not continue running samples
3174
+ if (this.fallback) return;
3175
+ const { frames, averages } = this;
3176
+ if (averages.length < iterations) {
3177
+ frames.push(performance.now());
3178
+ const msPassed = frames[frames.length - 1] - frames[0];
3179
+ if (msPassed >= ms) {
3180
+ this.fps = Math.round(frames.length / msPassed * 1000 * decimalPlacesRatio) / decimalPlacesRatio;
3181
+ this.refreshrate = Math.max(this.refreshrate, this.fps);
3182
+ averages[this.index++ % iterations] = this.fps;
3183
+ if (averages.length === iterations) {
3184
+ const [lower, upper] = bounds(this.refreshrate);
3185
+ const upperBounds = averages.filter((value)=>value >= upper);
3186
+ const lowerBounds = averages.filter((value)=>value < lower);
3187
+ // Trigger incline when more than -threshold- avgs exceed the upper bound
3188
+ if (upperBounds.length > iterations * threshold) {
3189
+ this.factor = Math.min(1, this.factor + step);
3190
+ this.flipped++;
3191
+ if (onIncline) onIncline(this);
3192
+ this.subscriptions.forEach((value)=>value.onIncline && value.onIncline(this));
3193
+ }
3194
+ // Trigger decline when more than -threshold- avgs are below the lower bound
3195
+ if (lowerBounds.length > iterations * threshold) {
3196
+ this.factor = Math.max(0, this.factor - step);
3197
+ this.flipped++;
3198
+ if (onDecline) onDecline(this);
3199
+ this.subscriptions.forEach((value)=>value.onDecline && value.onDecline(this));
3200
+ }
3201
+ if (lastFactor !== this.factor) {
3202
+ lastFactor = this.factor;
3203
+ if (onChange) onChange(this);
3204
+ this.subscriptions.forEach((value)=>value.onChange && value.onChange(this));
3205
+ }
3206
+ if (this.flipped > flipflops && !this.fallback) {
3207
+ this.fallback = true;
3208
+ if (onFallback) onFallback(this);
3209
+ this.subscriptions.forEach((value)=>value.onFallback && value.onFallback(this));
3210
+ }
3211
+ this.averages = [];
3212
+ // Resetting the refreshrate creates more problems than it solves atm
3213
+ // this.refreshrate = 0
3214
+ }
3215
+ this.frames = [];
3216
+ }
3217
+ }
3218
+ };
3219
+ }
3220
+ }
3221
+
3149
3222
  const vert_fullscreen = /*glsl*/ `
3150
3223
  varying vec2 vUv;
3151
3224
  void main() {
@@ -4520,33 +4593,44 @@ class Viewer extends EventEmitter {
4520
4593
  static getChildByName(node, name) {
4521
4594
  return Viewer.getObject(node, (v)=>v.name === name && v);
4522
4595
  }
4523
- static getObject(node, filter) {
4596
+ static getObject(node, filter, group = false) {
4524
4597
  const queue = [
4525
4598
  node
4526
4599
  ];
4600
+ const objects = [];
4527
4601
  while(queue.length !== 0){
4528
4602
  let object = queue.shift();
4529
4603
  let target = filter(object);
4530
4604
  if (target) {
4531
- return target;
4605
+ objects.push(object);
4606
+ if (!group) {
4607
+ break;
4608
+ }
4532
4609
  }
4533
4610
  object.children.forEach((v)=>queue.push(v));
4534
4611
  }
4612
+ if (group) {
4613
+ return objects;
4614
+ }
4615
+ return objects[0];
4535
4616
  }
4536
- static getObjects(node, filter) {
4537
- const queue = [
4538
- node
4539
- ];
4540
- const objects = [];
4541
- while(queue.length !== 0){
4542
- let object = queue.shift();
4543
- let target = filter(object);
4544
- if (target) {
4545
- objects.push(object);
4617
+ static _getObjectValue(object, name, group = false) {
4618
+ if (name) {
4619
+ if (group) {
4620
+ return Object.values(object || Viewer.__emtpyObject).filter((v)=>v.name === name);
4546
4621
  }
4547
- object.children.forEach((v)=>queue.push(v));
4622
+ return Object.values(object || Viewer.__emtpyObject).find((v)=>v.name === name);
4548
4623
  }
4549
- return objects;
4624
+ return Object.values(object);
4625
+ }
4626
+ static getMaterial(node, name, group = false) {
4627
+ return Viewer._getObjectValue(node.userData.materials, name, group);
4628
+ }
4629
+ static getTexture(node, name, group = false) {
4630
+ return Viewer._getObjectValue(node.userData.textures, name, group);
4631
+ }
4632
+ static getMesh(node, name, group = false) {
4633
+ return Viewer._getObjectValue(node.userData.meshes, name, group);
4550
4634
  }
4551
4635
  get root() {
4552
4636
  return this._root;
@@ -4742,12 +4826,12 @@ class Viewer extends EventEmitter {
4742
4826
  this._tweenManager.killTweensOf(target);
4743
4827
  }
4744
4828
  traverseMaterials(callback) {
4745
- Viewer.getObjects(this._scene, (item)=>{
4746
- if (item.isMesh) {
4829
+ Viewer.getObject(this._scene, (item)=>{
4830
+ if (item.material) {
4747
4831
  if (Array.isArray(item.material)) item.material.forEach(callback);
4748
4832
  else callback(item.material);
4749
4833
  }
4750
- });
4834
+ }, true);
4751
4835
  }
4752
4836
  traversePlugins(callback) {
4753
4837
  return this._pluginManager.traversePlugins(callback);
@@ -4827,7 +4911,7 @@ class Viewer extends EventEmitter {
4827
4911
  node.visible = active;
4828
4912
  return node;
4829
4913
  }
4830
- createRenderTarget(width, height, nearest = false, floatType = false, msaa = 0, mipmap = false) {
4914
+ createRenderTarget(width, height, nearest = false, floatType = false, msaa = 0, mipmap = false, depth = false) {
4831
4915
  return new three.WebGLRenderTarget(width, height, {
4832
4916
  wrapS: three.ClampToEdgeWrapping,
4833
4917
  wrapT: three.ClampToEdgeWrapping,
@@ -4836,13 +4920,13 @@ class Viewer extends EventEmitter {
4836
4920
  type: typeof floatType === "boolean" ? floatType ? this.DATA_FLOAT_TYPE : three.UnsignedByteType : floatType,
4837
4921
  anisotropy: 0,
4838
4922
  colorSpace: three.LinearSRGBColorSpace,
4839
- depthBuffer: false,
4923
+ depthBuffer: depth,
4840
4924
  stencilBuffer: false,
4841
4925
  samples: SystemInfo.isSupportMSAA ? msaa : 0,
4842
4926
  generateMipmaps: mipmap
4843
4927
  });
4844
4928
  }
4845
- createCubeRenderTarget(size, nearest = false, floatType = false, msaa = 0, mipmap = false) {
4929
+ createCubeRenderTarget(size, nearest = false, floatType = false, msaa = 0, mipmap = false, depth = false) {
4846
4930
  return new three.WebGLCubeRenderTarget(size, {
4847
4931
  wrapS: three.ClampToEdgeWrapping,
4848
4932
  wrapT: three.ClampToEdgeWrapping,
@@ -4851,7 +4935,7 @@ class Viewer extends EventEmitter {
4851
4935
  type: typeof floatType === "boolean" ? floatType ? this.DATA_FLOAT_TYPE : three.UnsignedByteType : floatType,
4852
4936
  anisotropy: 0,
4853
4937
  colorSpace: three.LinearSRGBColorSpace,
4854
- depthBuffer: false,
4938
+ depthBuffer: depth,
4855
4939
  stencilBuffer: false,
4856
4940
  samples: SystemInfo.isSupportMSAA ? msaa : 0,
4857
4941
  generateMipmaps: mipmap
@@ -4979,6 +5063,7 @@ Viewer._shadowCameraKeys = [
4979
5063
  "near",
4980
5064
  "far"
4981
5065
  ];
5066
+ Viewer.__emtpyObject = {};
4982
5067
  Viewer.fullscreenMesh = new three.Mesh(Viewer.createFullscreenTriangle());
4983
5068
  Viewer.fullscreenCamera = new three.OrthographicCamera(-1, 1, 1, -1, 0, 1);
4984
5069
 
@@ -5233,7 +5318,14 @@ class EnvironmentPlugin extends Plugin {
5233
5318
  }
5234
5319
  }
5235
5320
  }
5236
- onRender() {
5321
+ onRender(dt) {
5322
+ if (this._elapsed >= this.interval || this._elapsed === 0) {
5323
+ this._elapsed = 0;
5324
+ this._renderFrame();
5325
+ }
5326
+ this._elapsed += dt;
5327
+ }
5328
+ _renderFrame() {
5237
5329
  const { renderer } = this.viewer;
5238
5330
  const autoClear = renderer.autoClear;
5239
5331
  const oldTarget = renderer.getRenderTarget();
@@ -5270,14 +5362,16 @@ class EnvironmentPlugin extends Plugin {
5270
5362
  };
5271
5363
  this._debug = false;
5272
5364
  this._debugNode = null;
5365
+ this._elapsed = 0;
5273
5366
  this.performance = _Performance.HIGH;
5274
5367
  this.envMapIntensity = 1;
5275
5368
  this.reflectExposure = 1;
5276
5369
  this.reflectBlurIntensity = 5;
5370
+ this.interval = 0;
5277
5371
  this.install = ()=>{
5278
5372
  const viewer = this.viewer;
5279
5373
  this._scene = scene || viewer.scene;
5280
- const cubeRenderTarget = viewer.createCubeRenderTarget(resolution, false, floatType, 0, true);
5374
+ const cubeRenderTarget = viewer.createCubeRenderTarget(resolution, false, floatType, 0, true, true);
5281
5375
  this._cubeCamera = new three.CubeCamera(near || viewer.camera.near, far || viewer.camera.far, cubeRenderTarget);
5282
5376
  this._cubeCamera.position.copy(position);
5283
5377
  this._cubeCamera.layers.mask = layer;
@@ -5326,6 +5420,9 @@ __decorate([
5326
5420
  step: 0.1
5327
5421
  })
5328
5422
  ], EnvironmentPlugin.prototype, "reflectBlurIntensity", void 0);
5423
+ __decorate([
5424
+ property
5425
+ ], EnvironmentPlugin.prototype, "interval", void 0);
5329
5426
 
5330
5427
  class BoxProjectionPlugin extends Plugin {
5331
5428
  get debug() {
@@ -5470,6 +5567,7 @@ exports.FInterpTo = FInterpTo;
5470
5567
  exports.FreelookVirtualCamera = FreelookVirtualCamera;
5471
5568
  exports.Logger = Logger;
5472
5569
  exports.ObjectInstance = ObjectInstance;
5570
+ exports.PerformanceMonitor = PerformanceMonitor;
5473
5571
  exports.Perlin = Perlin;
5474
5572
  exports.Plane = Plane;
5475
5573
  exports.Plugin = Plugin;