@xviewer.js/core 1.0.0 → 1.0.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/dist/module.js CHANGED
@@ -3,8 +3,9 @@ import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js';
3
3
  import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
4
4
  import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
5
5
  import { MeshoptDecoder } from 'three/examples/jsm/libs/meshopt_decoder.module.js';
6
- import { EquirectangularReflectionMapping, FileLoader, TextureLoader, SRGBColorSpace, MathUtils, Vector3, Quaternion, Raycaster, Vector2, LinearInterpolant, Spherical, Mesh, BoxGeometry, SphereGeometry, PlaneGeometry, Plane as Plane$1, Matrix4, Vector4, PerspectiveCamera, WebGLRenderTarget, HalfFloatType, LinearMipMapLinearFilter, LinearFilter, AnimationMixer, ShaderMaterial, UniformsUtils, Color, OrthographicCamera, NoBlending, AdditiveBlending, FloatType, UnsignedByteType, BufferGeometry, Float32BufferAttribute, Object3D, ClampToEdgeWrapping, NearestFilter, LinearSRGBColorSpace, WebGLCubeRenderTarget, DataTexture, RGBAFormat, UVMapping, Scene, WebGLRenderer, PCFSoftShadowMap, LoadingManager, LinearToneMapping, PMREMGenerator, CubeCamera, ShaderLib, ShaderChunk } from 'three';
6
+ import { EquirectangularReflectionMapping, FileLoader, TextureLoader, SRGBColorSpace, MathUtils, Vector3, Quaternion, Raycaster, Vector2, LinearInterpolant, Spherical, Mesh, BoxGeometry, SphereGeometry, PlaneGeometry, Plane as Plane$1, Matrix4, Vector4, PerspectiveCamera, WebGLRenderTarget, HalfFloatType, LinearMipMapLinearFilter, LinearFilter, AnimationMixer, Color, OrthographicCamera, MeshDepthMaterial, ShaderMaterial, Euler, Group, UniformsUtils, NoBlending, AdditiveBlending, FloatType, UnsignedByteType, BufferGeometry, Float32BufferAttribute, Object3D, ClampToEdgeWrapping, NearestFilter, LinearSRGBColorSpace, WebGLCubeRenderTarget, DataTexture, RGBAFormat, UVMapping, Scene, WebGLRenderer, PCFSoftShadowMap, LoadingManager, LinearToneMapping, PMREMGenerator, CubeCamera, ShaderLib, ShaderChunk } from 'three';
7
7
  import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
8
+ import { HorizontalBlurShader, VerticalBlurShader } from 'three/examples/jsm/Addons.js';
8
9
 
9
10
  class EventEmitter {
10
11
  _removeEvent(list, index, name) {
@@ -916,10 +917,11 @@ class Tween {
916
917
  } else if (typeof end === 'object' && end) {
917
918
  // eslint-disable-next-line
918
919
  // @ts-ignore FIXME?
919
- this._updateProperties(_object[property], start, end, value);
920
- let des = Object.getOwnPropertyDescriptor(_object, property);
921
- if (des && des.set) {
922
- _object[property] = _object[property];
920
+ let des = getPropertyDescriptor(_object, property);
921
+ if ((des == null ? void 0 : des.set) || (des == null ? void 0 : des.writable)) {
922
+ _object[property] = this._updateProperties(_object[property], start, end, value);
923
+ } else {
924
+ this._updateProperties(_object[property], start, end, value);
923
925
  }
924
926
  } else {
925
927
  // Parses relative end values with start as base (e.g.: +10, -3)
@@ -932,6 +934,7 @@ class Tween {
932
934
  }
933
935
  }
934
936
  }
937
+ return _object;
935
938
  }
936
939
  _handleRelativeValue(start, end) {
937
940
  if (typeof end !== 'string') {
@@ -983,6 +986,14 @@ class Tween {
983
986
  this._goToEnd = false;
984
987
  }
985
988
  }
989
+ function getPropertyDescriptor(obj, prop) {
990
+ while(obj){
991
+ let desc = Object.getOwnPropertyDescriptor(obj, prop);
992
+ if (desc) return desc;
993
+ obj = Object.getPrototypeOf(obj);
994
+ }
995
+ return undefined;
996
+ }
986
997
 
987
998
  class TweenChain {
988
999
  start() {
@@ -1684,34 +1695,34 @@ class DeviceInput {
1684
1695
  return this._keys;
1685
1696
  }
1686
1697
  addEventListeners() {
1687
- const domElement = this.viewer.canvas;
1688
- domElement.style.touchAction = 'none';
1689
- this._addEventListener(domElement, 'contextmenu', (e)=>e.preventDefault());
1690
- this._addEventListener(domElement, "pointerdown", (e)=>this._onPointerDown(e), {
1698
+ const { canvas } = this.viewer;
1699
+ canvas.style.touchAction = 'none';
1700
+ this._addEventListener(canvas, 'contextmenu', (e)=>e.preventDefault());
1701
+ this._addEventListener(canvas, "pointerdown", (e)=>this._onPointerDown(e), {
1691
1702
  passive: false
1692
1703
  });
1693
- this._addEventListener(domElement, "pointerup", (e)=>this._onPointerUp(e), {
1704
+ this._addEventListener(canvas, "pointerup", (e)=>this._onPointerUp(e), {
1694
1705
  passive: false
1695
1706
  });
1696
- this._addEventListener(domElement, "pointercancel", (e)=>this._onPointerUp(e), {
1707
+ this._addEventListener(canvas, "pointercancel", (e)=>this._onPointerUp(e), {
1697
1708
  passive: false
1698
1709
  });
1699
- this._addEventListener(domElement, "pointerout", (e)=>this._onPointerUp(e), {
1710
+ this._addEventListener(canvas, "pointerout", (e)=>this._onPointerUp(e), {
1700
1711
  passive: false
1701
1712
  });
1702
- this._addEventListener(domElement, "pointermove", (e)=>this._onPointerMove(e), {
1713
+ this._addEventListener(canvas, "pointermove", (e)=>this._onPointerMove(e), {
1703
1714
  passive: true
1704
1715
  });
1705
- this._addEventListener(domElement, "wheel", (e)=>this._onMouseWheel(e), {
1716
+ this._addEventListener(canvas, "wheel", (e)=>this._onMouseWheel(e), {
1706
1717
  passive: false
1707
1718
  });
1708
- this._addEventListener(domElement, "touchstart", (e)=>this._onTouchStart(e), {
1719
+ this._addEventListener(canvas, "touchstart", (e)=>this._onTouchStart(e), {
1709
1720
  passive: true
1710
1721
  });
1711
- this._addEventListener(domElement, "touchend", (e)=>this._onTouchEnd(e), {
1722
+ this._addEventListener(canvas, "touchend", (e)=>this._onTouchEnd(e), {
1712
1723
  passive: true
1713
1724
  });
1714
- this._addEventListener(domElement, "touchmove", (e)=>this._onTouchMove(e), {
1725
+ this._addEventListener(canvas, "touchmove", (e)=>this._onTouchMove(e), {
1715
1726
  passive: true
1716
1727
  });
1717
1728
  //监听了pointer事件之后,key事件失效,改成window解决
@@ -1724,6 +1735,9 @@ class DeviceInput {
1724
1735
  this._addEventListener(window, "keyup", (e)=>this._onKeyUp(e), {
1725
1736
  passive: false
1726
1737
  });
1738
+ this._addEventListener(window, "resize", (e)=>this._onResize(e), {
1739
+ passive: false
1740
+ });
1727
1741
  }
1728
1742
  removeAllListeners() {
1729
1743
  for (let { target, type, callback } of this._listeners){
@@ -1755,6 +1769,10 @@ class DeviceInput {
1755
1769
  callback
1756
1770
  });
1757
1771
  }
1772
+ _onResize(e) {
1773
+ this.viewer.rotate();
1774
+ this.viewer.resize();
1775
+ }
1758
1776
  _remapPointer(e) {
1759
1777
  const width = this.viewer.width;
1760
1778
  return this.viewer.rootRotated ? {
@@ -2156,19 +2174,21 @@ const Vector3_NEG_ONE = Object.freeze(new Vector3(-1, -1, -1));
2156
2174
  const { clamp: clamp$2 } = MathUtils;
2157
2175
  const { max, abs: abs$1, acos } = Math;
2158
2176
  const EPSILON = 1.e-6;
2159
- const __delta = new Vector3();
2160
- function VInterpTo(current, target, deltaTime, speed, out = current) {
2177
+ const __deltaMap = new Map();
2178
+ __deltaMap.set(Vector3, new Vector3());
2179
+ __deltaMap.set(Vector2, new Vector2());
2180
+ function VInterpTo(current, target, deltaTime, speed, out = current, epsilon = EPSILON) {
2161
2181
  if (speed <= 0) {
2162
2182
  return out.copy(target);
2163
2183
  }
2164
- let dist = __delta.copy(target).sub(current);
2165
- if (dist.lengthSq() < EPSILON) {
2184
+ let dist = __deltaMap.get(current.constructor).copy(target).sub(current);
2185
+ if (dist.lengthSq() < epsilon) {
2166
2186
  return out.copy(target);
2167
2187
  }
2168
2188
  return out.copy(current).add(dist.multiplyScalar(clamp$2(deltaTime * speed, 0, 1)));
2169
2189
  }
2170
2190
  function VInterpConstantTo(current, target, deltaTime, speed, out = current) {
2171
- let delta = __delta.copy(target).sub(current);
2191
+ let delta = __deltaMap.get(current.constructor).copy(target).sub(current);
2172
2192
  let deltaM = delta.length();
2173
2193
  let maxStep = speed * deltaTime;
2174
2194
  if (deltaM > maxStep) {
@@ -2188,11 +2208,11 @@ function Quat_AngularDistance(a, b) {
2188
2208
  let innerProd = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
2189
2209
  return acos(2 * innerProd * innerProd - 1);
2190
2210
  }
2191
- function QInterpTo(current, target, deltaTime, speed) {
2211
+ function QInterpTo(current, target, deltaTime, speed, epsilon = EPSILON) {
2192
2212
  if (speed <= 0) {
2193
2213
  return target;
2194
2214
  }
2195
- if (Quat_Equals(current, target)) {
2215
+ if (Quat_Equals(current, target, epsilon)) {
2196
2216
  return target;
2197
2217
  }
2198
2218
  return current.slerp(target, clamp$2(speed * deltaTime, 0, 1));
@@ -2209,20 +2229,20 @@ function QInterpConstantTo(current, target, deltaTime, speed, out = current) {
2209
2229
  return out.copy(current).slerp(target, clamp$2(deltaSpeed / angularDist, 0, 1));
2210
2230
  }
2211
2231
  //非线性
2212
- function FInterpTo(current, target, deltaTime, speed) {
2232
+ function FInterpTo(current, target, deltaTime, speed, epsilon = EPSILON) {
2213
2233
  if (speed <= 0) {
2214
2234
  return target;
2215
2235
  }
2216
2236
  let dist = target - current;
2217
- if (abs$1(dist) < EPSILON) {
2237
+ if (abs$1(dist) < epsilon) {
2218
2238
  return target;
2219
2239
  }
2220
2240
  return current + dist * clamp$2(speed * deltaTime, 0, 1);
2221
2241
  }
2222
2242
  //线性插值
2223
- function FInterpConstantTo(current, target, deltaTime, speed) {
2243
+ function FInterpConstantTo(current, target, deltaTime, speed, epsilon = EPSILON) {
2224
2244
  let dist = target - current;
2225
- if (abs$1(dist) < EPSILON) {
2245
+ if (abs$1(dist) < epsilon) {
2226
2246
  return target;
2227
2247
  }
2228
2248
  let step = speed * deltaTime;
@@ -2670,6 +2690,7 @@ Perlin._Permutation = [
2670
2690
 
2671
2691
  const { clamp, degToRad } = MathUtils;
2672
2692
  const { abs, tan, PI } = Math;
2693
+ const PI2 = PI * 2;
2673
2694
  const ESP = 0.001;
2674
2695
  class FreelookVirtualCamera extends VirtualCamera {
2675
2696
  get lookAt() {
@@ -2679,6 +2700,13 @@ class FreelookVirtualCamera extends VirtualCamera {
2679
2700
  this._lookAt.copy(v);
2680
2701
  this._targetLookAt.copy(v);
2681
2702
  }
2703
+ get offset() {
2704
+ return this._offset;
2705
+ }
2706
+ set offset(v) {
2707
+ this._offset.copy(v);
2708
+ this._targetOffset.copy(v);
2709
+ }
2682
2710
  get springLength() {
2683
2711
  return this._spherical.radius;
2684
2712
  }
@@ -2800,8 +2828,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2800
2828
  }
2801
2829
  _calculateRotatelDelta(out, loc0, loc1) {
2802
2830
  this._tempRotateSmoothing = this.rotateSmoothing;
2803
- const domElement = this.viewer.canvas;
2804
- out.copy(loc1).sub(loc0).multiplyScalar(this.rotateSpeed * 2 * Math.PI / domElement.clientHeight);
2831
+ out.copy(loc1).sub(loc0).multiplyScalar(this.rotateSpeed * 2 * Math.PI / this.viewer.height);
2805
2832
  out.y = -out.y;
2806
2833
  if (this.forbidX) {
2807
2834
  out.x = 0;
@@ -2813,8 +2840,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2813
2840
  }
2814
2841
  _calculatePanDelta(out, loc0, loc1) {
2815
2842
  this._tempRotateSmoothing = this.rotateSmoothing;
2816
- const domElement = this.viewer.canvas;
2817
- out.copy(loc1).sub(loc0).multiplyScalar(this.panSpeed / domElement.clientHeight);
2843
+ out.copy(loc1).sub(loc0).multiplyScalar(this.panSpeed / this.viewer.height);
2818
2844
  if (this.forbidPanX) {
2819
2845
  out.x = 0;
2820
2846
  }
@@ -2823,7 +2849,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2823
2849
  }
2824
2850
  return out;
2825
2851
  }
2826
- _setTargetLookAt(panDelta) {
2852
+ _calculateLookAtOffset(panDelta, out = new Vector3()) {
2827
2853
  const { __xAxis, __yAxis, __posDelta } = FreelookVirtualCamera;
2828
2854
  __xAxis.setFromMatrixColumn(this.node.matrix, 0);
2829
2855
  __yAxis.setFromMatrixColumn(this.node.matrix, 1);
@@ -2833,7 +2859,11 @@ class FreelookVirtualCamera extends VirtualCamera {
2833
2859
  }
2834
2860
  __posDelta.copy(this.node.position).sub(this.lookAt);
2835
2861
  const length = __posDelta.length() * 2 * tan(degToRad(this.fov * 0.5));
2836
- this._targetLookAt.sub(__xAxis.multiplyScalar(panDelta.x * length)).add(__yAxis.multiplyScalar(panDelta.y * length));
2862
+ return out.set(0, 0, 0).sub(__xAxis.multiplyScalar(panDelta.x * length)).add(__yAxis.multiplyScalar(panDelta.y * length));
2863
+ }
2864
+ _setTargetLookAt(panDelta) {
2865
+ const { __lookAtOffset } = FreelookVirtualCamera;
2866
+ this._targetLookAt.add(this._calculateLookAtOffset(panDelta, __lookAtOffset));
2837
2867
  }
2838
2868
  _setTargetRotation(rotateDelta) {
2839
2869
  this._targetTheta -= rotateDelta.x;
@@ -2844,11 +2874,15 @@ class FreelookVirtualCamera extends VirtualCamera {
2844
2874
  _setTargetSpringLength(radius) {
2845
2875
  this._targetSpringLength = clamp(radius, this.distanceMin, this.distanceMax);
2846
2876
  }
2847
- goto({ springLength = this._targetSpringLength, theta = this._targetTheta, phi = this._targetPhi, lookAt = this._lookAt, fov = this.lens.fov, smoothing = this.smoothing, rotateSmoothing = this.smoothing }) {
2848
- this._targetSpringLength = clamp(springLength, this.distanceMin, this.distanceMax);
2849
- this._targetTheta = clamp(theta, this.thetaMin, this.thetaMax);
2877
+ goto({ springLength = this._targetSpringLength, theta = this._targetTheta, phi = this._targetPhi, lookAt = this._lookAt, offset = this._targetOffset, fov = this.lens.fov, smoothing = this.smoothing, rotateSmoothing = this.smoothing }) {
2878
+ const t0 = this._spherical.theta = this._spherical.theta % PI2;
2879
+ const t1 = theta % PI2;
2880
+ const delta = MathUtils.euclideanModulo(t1 - t0 + Math.PI, PI2) - Math.PI;
2881
+ this._targetTheta = clamp(t0 + delta, this.thetaMin, this.thetaMax);
2850
2882
  this._targetPhi = clamp(phi, this.phiMin, this.phiMax);
2883
+ this._targetSpringLength = clamp(springLength, this.distanceMin, this.distanceMax);
2851
2884
  this._targetLookAt.copy(lookAt);
2885
+ this._targetOffset.copy(offset);
2852
2886
  this._targetFov = fov;
2853
2887
  this._tempSmoothing = smoothing;
2854
2888
  this._tempRotateSmoothing = rotateSmoothing;
@@ -2861,8 +2895,10 @@ class FreelookVirtualCamera extends VirtualCamera {
2861
2895
  this._spherical.radius = FInterpTo(this._spherical.radius, this._targetSpringLength, dt, smoothing);
2862
2896
  this.lens.fov = FInterpTo(this.lens.fov, this._targetFov, dt, smoothing);
2863
2897
  VInterpTo(this._lookAt, this._targetLookAt, dt, smoothing);
2864
- this.node.position.setFromSpherical(this._spherical).add(this._lookAt);
2865
- this.node.lookAt(this._lookAt);
2898
+ VInterpTo(this._offset, this._targetOffset, dt, smoothing);
2899
+ this._calculateLookAtOffset(this._offset, this._finalLookAt).add(this._lookAt);
2900
+ this.node.position.setFromSpherical(this._spherical).add(this._finalLookAt);
2901
+ this.node.lookAt(this._finalLookAt);
2866
2902
  }
2867
2903
  constructor(...args){
2868
2904
  super(...args);
@@ -2872,6 +2908,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2872
2908
  this._preLoc1 = new Vector2();
2873
2909
  this._spherical = new Spherical(4, Math.PI / 2);
2874
2910
  this._lookAt = new Vector3();
2911
+ this._offset = new Vector2();
2875
2912
  this._tempSmoothing = 6;
2876
2913
  this._tempRotateSmoothing = 8;
2877
2914
  this._targetTheta = 0;
@@ -2879,6 +2916,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2879
2916
  this._targetSpringLength = 4;
2880
2917
  this._targetFov = this.fov;
2881
2918
  this._targetLookAt = new Vector3();
2919
+ this._targetOffset = new Vector2();
2882
2920
  this.forbidX = false;
2883
2921
  this.forbidY = false;
2884
2922
  this.forbidZ = false;
@@ -2895,6 +2933,7 @@ class FreelookVirtualCamera extends VirtualCamera {
2895
2933
  this.thetaMax = Infinity;
2896
2934
  this.distanceMin = ESP;
2897
2935
  this.distanceMax = Infinity;
2936
+ this._finalLookAt = new Vector3();
2898
2937
  }
2899
2938
  }
2900
2939
  FreelookVirtualCamera.__loc0 = new Vector2();
@@ -2905,6 +2944,7 @@ FreelookVirtualCamera.__panDelta = new Vector2();
2905
2944
  FreelookVirtualCamera.__panTarget = new Vector2();
2906
2945
  FreelookVirtualCamera.__rotateDelta = new Vector2();
2907
2946
  FreelookVirtualCamera.__posDelta = new Vector3();
2947
+ FreelookVirtualCamera.__lookAtOffset = new Vector3();
2908
2948
  FreelookVirtualCamera.__xAxis = new Vector3();
2909
2949
  FreelookVirtualCamera.__yAxis = new Vector3();
2910
2950
  FreelookVirtualCamera.__quat = new Quaternion();
@@ -2999,10 +3039,11 @@ __decorate([
2999
3039
  })
3000
3040
  ], FreelookVirtualCamera.prototype, "distanceMax", void 0);
3001
3041
  __decorate([
3002
- property({
3003
- step: 0.01
3004
- })
3042
+ property
3005
3043
  ], FreelookVirtualCamera.prototype, "lookAt", null);
3044
+ __decorate([
3045
+ property
3046
+ ], FreelookVirtualCamera.prototype, "offset", null);
3006
3047
  __decorate([
3007
3048
  property({
3008
3049
  step: 0.01
@@ -3214,6 +3255,186 @@ class Animation extends Component {
3214
3255
  }
3215
3256
  }
3216
3257
 
3258
+ function applyProps(target, props) {
3259
+ for(let k in props){
3260
+ let prop = props[k];
3261
+ if (prop === undefined) {
3262
+ continue;
3263
+ }
3264
+ if (target[k] !== undefined) {
3265
+ let des = Object.getOwnPropertyDescriptor(target, k);
3266
+ if (!des || des.writable || des.set) {
3267
+ target[k] = prop;
3268
+ } else {
3269
+ let value = target[k];
3270
+ if (value && value.copy) {
3271
+ value.copy(prop);
3272
+ }
3273
+ }
3274
+ }
3275
+ }
3276
+ return target;
3277
+ }
3278
+ function find(node, path) {
3279
+ let child = null;
3280
+ let parts = path.split("/");
3281
+ let children = node.children;
3282
+ for (let part of parts){
3283
+ child = children.find((v)=>v.name === part);
3284
+ if (child) {
3285
+ children = child.children;
3286
+ } else {
3287
+ return null;
3288
+ }
3289
+ }
3290
+ return child;
3291
+ }
3292
+ function getChildren(node, filter, group = false) {
3293
+ const queue = [
3294
+ node
3295
+ ];
3296
+ const objects = [];
3297
+ while(queue.length !== 0){
3298
+ let object = queue.shift();
3299
+ let target = filter(object);
3300
+ if (target) {
3301
+ objects.push(object);
3302
+ if (!group) {
3303
+ break;
3304
+ }
3305
+ }
3306
+ object.children.forEach((v)=>queue.push(v));
3307
+ }
3308
+ if (group) {
3309
+ return objects;
3310
+ }
3311
+ return objects[0];
3312
+ }
3313
+ function getChildByName(node, name) {
3314
+ return getChildren(node, (v)=>v && v.name === name);
3315
+ }
3316
+ const __emtpyObject = {};
3317
+ function queryValues(object, name, group = false) {
3318
+ if (name) {
3319
+ if (group) {
3320
+ return Object.values(object || __emtpyObject).filter((v)=>v.name === name);
3321
+ }
3322
+ return Object.values(object || __emtpyObject).find((v)=>v.name === name);
3323
+ }
3324
+ return Object.values(object);
3325
+ }
3326
+
3327
+ class ContactShadows extends Component {
3328
+ get visible() {
3329
+ return this.node.visible;
3330
+ }
3331
+ set visible(v) {
3332
+ this.node.visible = v;
3333
+ }
3334
+ constructor({ scale = 10, frames = Infinity, width = 1, height = 1, intensity = 1, blur = 1, near = 0, far = 10, resolution = 512, smooth = true, color = new Color(0x0), renderOrder = 0, layerMask = 1 << 0, ...props } = {}){
3335
+ super();
3336
+ this.layerMask = 1 << 0;
3337
+ this.color = new Color();
3338
+ this.blur = 1;
3339
+ this.blur = blur;
3340
+ this.layerMask = layerMask;
3341
+ this.color.copy(color);
3342
+ width = width * scale;
3343
+ height = height * scale;
3344
+ const planeGeometry = new PlaneGeometry(width, height);
3345
+ const shadowCamera = new OrthographicCamera(-width / 2, width / 2, height / 2, -height / 2, near, far);
3346
+ const renderTarget = new WebGLRenderTarget(resolution, resolution);
3347
+ const renderTargetBlur = new WebGLRenderTarget(resolution, resolution);
3348
+ renderTargetBlur.texture.generateMipmaps = renderTarget.texture.generateMipmaps = false;
3349
+ const depthMaterial = new MeshDepthMaterial();
3350
+ depthMaterial.depthTest = depthMaterial.depthWrite = false;
3351
+ depthMaterial.onBeforeCompile = (shader)=>{
3352
+ shader.uniforms = {
3353
+ ...shader.uniforms,
3354
+ color: {
3355
+ value: this.color
3356
+ }
3357
+ };
3358
+ shader.fragmentShader = shader.fragmentShader.replace(`void main() {`, `uniform vec3 color;
3359
+ uniform float intensity;
3360
+ void main() {
3361
+ `);
3362
+ shader.fragmentShader = shader.fragmentShader.replace('vec4( vec3( 1.0 - fragCoordZ ), opacity );', // Colorize the shadow, multiply by the falloff so that the center can remain darker
3363
+ 'vec4( color * fragCoordZ * 2.0, ( 1.0 - fragCoordZ ) * 1.0 );');
3364
+ };
3365
+ const horizontalBlurMaterial = new ShaderMaterial(HorizontalBlurShader);
3366
+ const verticalBlurMaterial = new ShaderMaterial(VerticalBlurShader);
3367
+ verticalBlurMaterial.depthTest = horizontalBlurMaterial.depthTest = false;
3368
+ const blurPlane = new Mesh(planeGeometry);
3369
+ this.node = applyProps(new Group(), {
3370
+ rotation: new Euler(Math.PI / 2, 0, 0),
3371
+ ...props
3372
+ });
3373
+ this.node.add(shadowCamera);
3374
+ this.node.add(blurPlane);
3375
+ const textureMatrix = new Matrix4();
3376
+ const blurShadows = (gl, blur)=>{
3377
+ blurPlane.visible = true;
3378
+ blurPlane.material = horizontalBlurMaterial;
3379
+ horizontalBlurMaterial.uniforms.tDiffuse.value = renderTarget.texture;
3380
+ horizontalBlurMaterial.uniforms.h.value = blur * 1 / 256;
3381
+ gl.setRenderTarget(renderTargetBlur);
3382
+ gl.render(blurPlane, shadowCamera);
3383
+ blurPlane.material = verticalBlurMaterial;
3384
+ verticalBlurMaterial.uniforms.tDiffuse.value = renderTargetBlur.texture;
3385
+ verticalBlurMaterial.uniforms.v.value = blur * 1 / 256;
3386
+ gl.setRenderTarget(renderTarget);
3387
+ gl.render(blurPlane, shadowCamera);
3388
+ blurPlane.visible = false;
3389
+ };
3390
+ let count = 0;
3391
+ let initialBackground;
3392
+ let initialOverrideMaterial;
3393
+ let initialClearAlpha;
3394
+ this.update = ()=>{
3395
+ shadowCamera.layers.mask = blurPlane.layers.mask = this.layerMask;
3396
+ if (frames === Infinity || count < frames) {
3397
+ count++;
3398
+ const { scene, renderer: gl } = this.viewer;
3399
+ initialBackground = scene.background;
3400
+ initialOverrideMaterial = scene.overrideMaterial;
3401
+ this.node.visible = false;
3402
+ scene.background = null;
3403
+ scene.overrideMaterial = depthMaterial;
3404
+ textureMatrix.set(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);
3405
+ textureMatrix.multiply(shadowCamera.projectionMatrix);
3406
+ textureMatrix.multiply(shadowCamera.matrixWorldInverse);
3407
+ initialClearAlpha = gl.getClearAlpha();
3408
+ gl.setClearAlpha(0);
3409
+ gl.setRenderTarget(renderTarget);
3410
+ gl.render(scene, shadowCamera);
3411
+ blurShadows(gl, this.blur);
3412
+ if (smooth) blurShadows(gl, this.blur * 0.4);
3413
+ gl.setRenderTarget(null);
3414
+ gl.setClearAlpha(initialClearAlpha);
3415
+ this.node.visible = true;
3416
+ scene.overrideMaterial = initialOverrideMaterial;
3417
+ scene.background = initialBackground;
3418
+ }
3419
+ };
3420
+ this.onDestroy = ()=>renderTarget.dispose();
3421
+ this.getRenderTarget = ()=>renderTarget;
3422
+ this.getTextureMatrix = ()=>textureMatrix;
3423
+ }
3424
+ }
3425
+ __decorate([
3426
+ property
3427
+ ], ContactShadows.prototype, "visible", null);
3428
+ __decorate([
3429
+ property
3430
+ ], ContactShadows.prototype, "color", void 0);
3431
+ __decorate([
3432
+ property({
3433
+ min: 0,
3434
+ max: 15
3435
+ })
3436
+ ], ContactShadows.prototype, "blur", void 0);
3437
+
3217
3438
  const vert_fullscreen = /*glsl*/ `
3218
3439
  varying vec2 vUv;
3219
3440
  void main() {
@@ -3570,7 +3791,6 @@ class ReflectorMaterial extends ShaderMaterial {
3570
3791
  set lightMapIntensity(v) {
3571
3792
  this.uniforms.lightMapIntensity.value = v;
3572
3793
  }
3573
- // @property
3574
3794
  get reflectMap() {
3575
3795
  return this.uniforms.reflectMap.value;
3576
3796
  }
@@ -3686,6 +3906,9 @@ __decorate([
3686
3906
  parent: "lightMap"
3687
3907
  })
3688
3908
  ], ReflectorMaterial.prototype, "lightMapIntensity", null);
3909
+ __decorate([
3910
+ property()
3911
+ ], ReflectorMaterial.prototype, "reflectMap", null);
3689
3912
  __decorate([
3690
3913
  property({
3691
3914
  min: 0,
@@ -3695,6 +3918,72 @@ __decorate([
3695
3918
  })
3696
3919
  ], ReflectorMaterial.prototype, "reflectIntensity", null);
3697
3920
 
3921
+ const vert_Shadow = `
3922
+ varying vec4 v_shadows;
3923
+ uniform mat4 shadowsMatrix;
3924
+
3925
+ void main() {
3926
+ vec4 worldPos = modelMatrix * vec4(position, 1.);
3927
+ vec4 mvPosition = viewMatrix * worldPos;
3928
+
3929
+ gl_Position = projectionMatrix * mvPosition;
3930
+
3931
+ v_shadows = shadowsMatrix * worldPos;
3932
+ }
3933
+ `;
3934
+ const frag_Shadow = `
3935
+ uniform vec3 color;
3936
+ uniform float opacity;
3937
+
3938
+ varying vec4 v_shadows;
3939
+ uniform sampler2D shadowsMap;
3940
+ uniform float shadowsIntensity;
3941
+
3942
+ void main() {
3943
+ vec4 shadows = texture2DProj(shadowsMap, v_shadows);
3944
+ gl_FragColor = vec4(color * shadows.rgb, shadows.a * opacity);
3945
+
3946
+ #include <tonemapping_fragment>
3947
+ #include <colorspace_fragment>
3948
+ }
3949
+ `;
3950
+ class ShadowMaterial extends ShaderMaterial {
3951
+ get color() {
3952
+ return this.uniforms.color.value;
3953
+ }
3954
+ set color(v) {
3955
+ this.uniforms.color.value.copy(v);
3956
+ }
3957
+ constructor({ shadows, ...props }){
3958
+ super();
3959
+ this.vertexShader = vert_Shadow;
3960
+ this.fragmentShader = frag_Shadow;
3961
+ this.uniforms = {
3962
+ color: {
3963
+ value: new Color()
3964
+ },
3965
+ opacity: {
3966
+ value: 1
3967
+ },
3968
+ shadowsMap: {
3969
+ value: null
3970
+ },
3971
+ shadowsMatrix: {
3972
+ value: new Matrix4()
3973
+ }
3974
+ };
3975
+ Object.defineProperty(this, "opacity", {
3976
+ get: ()=>this.uniforms.opacity.value,
3977
+ set: (v)=>this.uniforms.opacity.value = v
3978
+ });
3979
+ this.uniforms.shadowsMap.value = shadows.getRenderTarget().texture;
3980
+ this.uniforms.shadowsMatrix.value = shadows.getTextureMatrix();
3981
+ this.transparent = true;
3982
+ this.depthWrite = false;
3983
+ this.setValues(props);
3984
+ }
3985
+ }
3986
+
3698
3987
  const LOD_MIN = 4;
3699
3988
  const _flatCamera = /*@__PURE__*/ new OrthographicCamera();
3700
3989
  // Golden Ratio
@@ -4477,75 +4766,6 @@ ResourceManager._texSettingKeys = [
4477
4766
  "channel"
4478
4767
  ];
4479
4768
 
4480
- function applyProps(target, props) {
4481
- for(let k in props){
4482
- let prop = props[k];
4483
- if (prop === undefined) {
4484
- continue;
4485
- }
4486
- if (target[k] !== undefined) {
4487
- let des = Object.getOwnPropertyDescriptor(target, k);
4488
- if (!des || des.writable || des.set) {
4489
- target[k] = prop;
4490
- } else {
4491
- let value = target[k];
4492
- if (value && value.copy) {
4493
- value.copy(prop);
4494
- }
4495
- }
4496
- }
4497
- }
4498
- return target;
4499
- }
4500
- function find(node, path) {
4501
- let child = null;
4502
- let parts = path.split("/");
4503
- let children = node.children;
4504
- for (let part of parts){
4505
- child = children.find((v)=>v.name === part);
4506
- if (child) {
4507
- children = child.children;
4508
- } else {
4509
- return null;
4510
- }
4511
- }
4512
- return child;
4513
- }
4514
- function getChidren(node, filter, group = false) {
4515
- const queue = [
4516
- node
4517
- ];
4518
- const objects = [];
4519
- while(queue.length !== 0){
4520
- let object = queue.shift();
4521
- let target = filter(object);
4522
- if (target) {
4523
- objects.push(object);
4524
- if (!group) {
4525
- break;
4526
- }
4527
- }
4528
- object.children.forEach((v)=>queue.push(v));
4529
- }
4530
- if (group) {
4531
- return objects;
4532
- }
4533
- return objects[0];
4534
- }
4535
- function getChildByName(node, name) {
4536
- return getChidren(node, (v)=>v && v.name === name);
4537
- }
4538
- const __emtpyObject = {};
4539
- function queryValues(object, name, group = false) {
4540
- if (name) {
4541
- if (group) {
4542
- return Object.values(object || __emtpyObject).filter((v)=>v.name === name);
4543
- }
4544
- return Object.values(object || __emtpyObject).find((v)=>v.name === name);
4545
- }
4546
- return Object.values(object);
4547
- }
4548
-
4549
4769
  class Viewer extends EventEmitter {
4550
4770
  static _setDirectLightShadow(shadow, props) {
4551
4771
  const shadowCamera = shadow.camera;
@@ -4609,6 +4829,9 @@ class Viewer extends EventEmitter {
4609
4829
  get root() {
4610
4830
  return this._root;
4611
4831
  }
4832
+ get container() {
4833
+ return this._container;
4834
+ }
4612
4835
  get time() {
4613
4836
  return this._time;
4614
4837
  }
@@ -4701,7 +4924,6 @@ class Viewer extends EventEmitter {
4701
4924
  }
4702
4925
  }
4703
4926
  loop(dt) {
4704
- if (this._autoResize) this.resize();
4705
4927
  dt = Math.min(dt, 0.067);
4706
4928
  this.update(dt);
4707
4929
  this.render(dt);
@@ -4739,59 +4961,75 @@ class Viewer extends EventEmitter {
4739
4961
  this._time = this._lastTime = 0;
4740
4962
  return this;
4741
4963
  }
4742
- resize(width = window.innerWidth, height = window.innerHeight) {
4743
- this._rootRotated = this._orientation === Orientation.LANDSCAPE ? width < height : this._orientation === Orientation.PORTRAIT ? width > height : false;
4964
+ rotate() {
4965
+ let width = window.innerWidth;
4966
+ let height = window.innerHeight;
4967
+ if (this._orientation === Orientation.LANDSCAPE) {
4968
+ this._rootRotated = width < height;
4969
+ } else if (this._orientation === Orientation.PORTRAIT) {
4970
+ this._rootRotated = width > height;
4971
+ } else {
4972
+ return;
4973
+ }
4744
4974
  if (this._rootRotated) {
4745
4975
  let tmp = width;
4746
4976
  width = height;
4747
4977
  height = tmp;
4748
4978
  }
4749
- if (this._width !== width || this._height !== height) {
4750
- this._width = width;
4751
- this._height = height;
4752
- let root = this._root;
4753
- if (this._rootRotated) {
4754
- root.style['-webkit-transform'] = 'rotate(90deg)';
4755
- root.style.transform = 'rotate(90deg)';
4756
- root.style['-webkit-transform-origin'] = '0px 0px 0px';
4757
- root.style.transformOrigin = '0px 0px 0px';
4758
- root.style.margin = `0 0 0 ${height}px`;
4759
- root.style.width = `${width}px`;
4760
- root.style.height = `${height}px`;
4761
- } else {
4762
- root.style['-webkit-transform'] = 'rotate(0deg)';
4763
- root.style.transform = 'rotate(0deg)';
4764
- root.style.margin = '0px auto';
4765
- root.style.width = `${width}px`;
4766
- root.style.height = `${height}px`;
4767
- }
4768
- let camera = this._camera;
4769
- if (camera.isOrthographicCamera) {
4770
- this._viewport = {
4771
- width: width / camera.zoom,
4772
- height: height / camera.zoom,
4773
- factor: 1
4774
- };
4775
- } else if (camera.isPerspectiveCamera) {
4776
- if (!camera.manual) {
4777
- camera.aspect = width / height;
4778
- camera.updateProjectionMatrix();
4779
- }
4780
- let distance = camera.getWorldPosition(Viewer.__worldPos).distanceTo(Viewer.__target);
4781
- let h = 2 * Math.tan(MathUtils.degToRad(camera.fov * 0.5)) * distance;
4782
- let w = h * width / height;
4783
- this._viewport = {
4784
- width: w,
4785
- height: h,
4786
- factor: width / w
4787
- };
4979
+ const root = this._root;
4980
+ if (this._rootRotated) {
4981
+ root.style['-webkit-transform'] = 'rotate(90deg)';
4982
+ root.style.transform = 'rotate(90deg)';
4983
+ root.style['-webkit-transform-origin'] = '0px 0px 0px';
4984
+ root.style.transformOrigin = '0px 0px 0px';
4985
+ root.style.margin = `0 0 0 ${height}px`;
4986
+ root.style.width = `${width}px`;
4987
+ root.style.height = `${height}px`;
4988
+ } else {
4989
+ root.style['-webkit-transform'] = 'rotate(0deg)';
4990
+ root.style.transform = 'rotate(0deg)';
4991
+ root.style.margin = '0px auto';
4992
+ root.style.width = `${width}px`;
4993
+ root.style.height = `${height}px`;
4994
+ }
4995
+ }
4996
+ resize(width = this._container.offsetWidth, height = this._container.offsetHeight) {
4997
+ if (!this._autoResize) {
4998
+ return;
4999
+ }
5000
+ if (this._rootRotated) {
5001
+ let tmp = width;
5002
+ width = height;
5003
+ height = tmp;
5004
+ }
5005
+ this._width = width;
5006
+ this._height = height;
5007
+ let camera = this._camera;
5008
+ if (camera.isOrthographicCamera) {
5009
+ this._viewport = {
5010
+ width: width / camera.zoom,
5011
+ height: height / camera.zoom,
5012
+ factor: 1
5013
+ };
5014
+ } else if (camera.isPerspectiveCamera) {
5015
+ if (!camera.manual) {
5016
+ camera.aspect = width / height;
5017
+ camera.updateProjectionMatrix();
4788
5018
  }
4789
- this._resizing(width, height);
4790
- this._componentManager.resize(width, height);
5019
+ let distance = camera.getWorldPosition(Viewer.__worldPos).distanceTo(Viewer.__target);
5020
+ let h = 2 * Math.tan(MathUtils.degToRad(camera.fov * 0.5)) * distance;
5021
+ let w = h * width / height;
5022
+ this._viewport = {
5023
+ width: w,
5024
+ height: h,
5025
+ factor: width / w
5026
+ };
4791
5027
  }
5028
+ this._resizing(width, height);
5029
+ this._componentManager.resize(width, height);
4792
5030
  }
4793
5031
  loadAsset(props) {
4794
- return this._resourceManager.loadAsset(Object.assign(Object.assign({}, this._defaultProps), props));
5032
+ return this._resourceManager.loadAsset(Object.assign({}, this._defaultProps, props));
4795
5033
  }
4796
5034
  getLoader(ext) {
4797
5035
  return this._resourceManager.getLoader(ext);
@@ -4954,7 +5192,7 @@ class Viewer extends EventEmitter {
4954
5192
  Viewer.CompileObject3D(this._renderer, this._scene, this._camera, this._scene);
4955
5193
  }
4956
5194
  }
4957
- constructor({ root, canvas, autoStart = true, autoResize = true, shadows = false, camera = {
5195
+ constructor({ root, canvas, container, autoStart = true, autoResize = true, shadows = false, camera = {
4958
5196
  fov: 45,
4959
5197
  near: 0.1,
4960
5198
  far: 1000,
@@ -4988,6 +5226,7 @@ class Viewer extends EventEmitter {
4988
5226
  }
4989
5227
  this._root = root || el;
4990
5228
  this._canvas = el;
5229
+ this._container = container || el.parentElement;
4991
5230
  this._gl = webgl.gl;
4992
5231
  this._RENDER_TARGET_FLOAT_TYPE = webgl.RENDER_TARGET_FLOAT_TYPE;
4993
5232
  this._DATA_FLOAT_TYPE = webgl.DATA_FLOAT_TYPE;
@@ -5022,7 +5261,10 @@ class Viewer extends EventEmitter {
5022
5261
  this._input = new DeviceInput(this);
5023
5262
  this._input.addEventListeners();
5024
5263
  this._addDefaultLoaders();
5025
- if (autoStart) this.start();
5264
+ if (autoStart) {
5265
+ this.start();
5266
+ }
5267
+ this.rotate();
5026
5268
  this.resize();
5027
5269
  }
5028
5270
  }
@@ -5699,5 +5941,5 @@ function getFilesFromItemList(items, onDone) {
5699
5941
  }
5700
5942
  }
5701
5943
 
5702
- export { Animation, AnimationCurve, Box, BoxProjection, CinestationBlendDefinition, CinestationBrain, Component, DependentMode, DeviceInput, DropFile, Easing, Environment, EventEmitter, FInterpConstantTo, FInterpTo, FreelookVirtualCamera, Logger, Mount, ObjectInstance, Orientation, PerformanceMonitor, Perlin, Plane, PropertyManager, QInterpConstantTo, QInterpTo, Quat_AngularDistance, Quat_Equals, Quat_exponentialDamp, Quat_quarticDamp, Quat_smoothDamp, Reflector, ReflectorMaterial, ResourceManager, Sphere, SystemInfo, Tween, TweenChain, TweenGroup, TweenManager, VInterpConstantTo, VInterpTo, Vec3_smoothDamp, Vector3_NEG_ONE, Vector3_ONE, Vector3_RIGHT, Vector3_UNIT_X, Vector3_UNIT_Y, Vector3_UNIT_Z, Vector3_UP, Vector3_ZERO, Viewer, VirtualCamera, aEXRLoader, aFBXLoader, aGLTFLoader, aHDRLoader, aJSONLoader, aLoader, aTextureLoader, applyProps, dependencies, exponentialDamp, find, frag_BoxfilterBlur, frag_cubeMapToPanorama, frag_panoramaToCubeMap, getChidren, getChildByName, getClassInstance, getShaderMaterial, mixin, property, quarticDamp, queryValues, smoothDamp, vert_fullscreen };
5944
+ export { Animation, AnimationCurve, Box, BoxProjection, CinestationBlendDefinition, CinestationBrain, Component, ContactShadows, DependentMode, DeviceInput, DropFile, Easing, Environment, EventEmitter, FInterpConstantTo, FInterpTo, FreelookVirtualCamera, Logger, Mount, ObjectInstance, Orientation, PerformanceMonitor, Perlin, Plane, PropertyManager, QInterpConstantTo, QInterpTo, Quat_AngularDistance, Quat_Equals, Quat_exponentialDamp, Quat_quarticDamp, Quat_smoothDamp, Reflector, ReflectorMaterial, ResourceManager, ShadowMaterial, Sphere, SystemInfo, Tween, TweenChain, TweenGroup, TweenManager, VInterpConstantTo, VInterpTo, Vec3_smoothDamp, Vector3_NEG_ONE, Vector3_ONE, Vector3_RIGHT, Vector3_UNIT_X, Vector3_UNIT_Y, Vector3_UNIT_Z, Vector3_UP, Vector3_ZERO, Viewer, VirtualCamera, aEXRLoader, aFBXLoader, aGLTFLoader, aHDRLoader, aJSONLoader, aLoader, aTextureLoader, applyProps, dependencies, exponentialDamp, find, frag_BoxfilterBlur, frag_cubeMapToPanorama, frag_panoramaToCubeMap, getChildByName, getChildren, getClassInstance, getShaderMaterial, mixin, property, quarticDamp, queryValues, smoothDamp, vert_fullscreen };
5703
5945
  //# sourceMappingURL=module.js.map