@xviewer.js/core 1.0.0-alpha.56 → 1.0.0-alpha.57

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
@@ -1104,10 +1104,14 @@ aLoader._texKeys = [
1104
1104
  ];
1105
1105
 
1106
1106
  class aEXRLoader extends aLoader {
1107
- load({ url, onLoad, onProgress, onError, texSettings }) {
1108
- const loader = new EXRLoader(this.manager.loadingManager);
1109
- loader.setPath(this.manager.path);
1110
- loader.setResourcePath(this.manager.resourcePath);
1107
+ load({ url, path, resourcePath, manager, texSettings, onLoad, onProgress, onError }) {
1108
+ let loader = this._loader;
1109
+ if (loader === undefined) {
1110
+ loader = this._loader = new EXRLoader();
1111
+ }
1112
+ loader.manager = manager;
1113
+ loader.setPath(path);
1114
+ loader.setResourcePath(resourcePath);
1111
1115
  loader.load(url, (tex)=>onLoad(Object.assign(tex, texSettings)), onProgress, onError);
1112
1116
  }
1113
1117
  constructor(...args){
@@ -1119,15 +1123,19 @@ class aEXRLoader extends aLoader {
1119
1123
  }
1120
1124
 
1121
1125
  class aFBXLoader extends aLoader {
1122
- load({ url, buffer, onLoad, onProgress, onError }) {
1126
+ load({ url, buffer, path, resourcePath, manager, onLoad, onProgress, onError }) {
1127
+ let loader = this._loader;
1128
+ if (loader === undefined) {
1129
+ loader = this._loader = new FBXLoader();
1130
+ }
1131
+ loader.manager = manager;
1132
+ loader.setPath(path);
1133
+ loader.setResourcePath(resourcePath);
1123
1134
  const loadCallback = (node)=>{
1124
1135
  onLoad(aLoader._setUserData(node));
1125
1136
  };
1126
- const loader = new FBXLoader(this.manager.loadingManager);
1127
- loader.setPath(this.manager.path);
1128
- loader.setResourcePath(this.manager.resourcePath);
1129
1137
  if (buffer) {
1130
- loadCallback(loader.parse(buffer, this.manager.path));
1138
+ loadCallback(loader.parse(buffer, path));
1131
1139
  } else {
1132
1140
  loader.load(url, loadCallback, onProgress, onError);
1133
1141
  }
@@ -1141,23 +1149,31 @@ class aFBXLoader extends aLoader {
1141
1149
  }
1142
1150
 
1143
1151
  class aGLTFLoader extends aLoader {
1144
- load({ url, buffer, onLoad, onProgress, onError }) {
1152
+ load({ url, buffer, path, resourcePath, dracoPath, manager, onLoad, onProgress, onError }) {
1153
+ let dracoLoader = this._dracoLoader;
1154
+ if (dracoLoader === undefined) {
1155
+ dracoLoader = this._dracoLoader = new DRACOLoader();
1156
+ }
1157
+ dracoLoader.manager = manager;
1158
+ dracoLoader.setDecoderPath(dracoPath);
1159
+ let loader = this._loader;
1160
+ if (loader === undefined) {
1161
+ loader = this._loader = new GLTFLoader();
1162
+ loader.setDRACOLoader(dracoLoader);
1163
+ loader.setMeshoptDecoder(MeshoptDecoder);
1164
+ }
1165
+ loader.manager = manager;
1166
+ loader.setPath(path);
1167
+ loader.setResourcePath(resourcePath);
1145
1168
  const loadCallback = (gltf)=>{
1146
1169
  const node = gltf.scene;
1147
- if (gltf.animations) node.animations = gltf.animations;
1170
+ node.animations.push(...gltf.animations);
1148
1171
  onLoad(aLoader._setUserData(node));
1149
1172
  };
1150
- let gltfLoader = new GLTFLoader(this.manager.loadingManager);
1151
- gltfLoader.setPath(this.manager.path);
1152
- gltfLoader.setResourcePath(this.manager.resourcePath);
1153
- let dracoLoader = new DRACOLoader(this.manager.loadingManager);
1154
- dracoLoader.setDecoderPath(this.manager.dracoPath);
1155
- gltfLoader.setDRACOLoader(dracoLoader);
1156
- gltfLoader.setMeshoptDecoder(MeshoptDecoder);
1157
1173
  if (buffer) {
1158
- gltfLoader.parse(buffer, this.manager.path, loadCallback, onError);
1174
+ loader.parse(buffer, resourcePath, loadCallback, onError);
1159
1175
  } else {
1160
- gltfLoader.load(url, loadCallback, onProgress, onError);
1176
+ loader.load(url, loadCallback, onProgress, onError);
1161
1177
  }
1162
1178
  }
1163
1179
  constructor(...args){
@@ -1170,13 +1186,17 @@ class aGLTFLoader extends aLoader {
1170
1186
  }
1171
1187
 
1172
1188
  class aHDRLoader extends aLoader {
1173
- load({ url, onLoad, onProgress, onError, texSettings }) {
1189
+ load({ url, path, resourcePath, manager, texSettings, onLoad, onProgress, onError }) {
1190
+ let loader = this._loader;
1191
+ if (loader === undefined) {
1192
+ loader = this._loader = new RGBELoader();
1193
+ }
1194
+ loader.manager = manager;
1195
+ loader.setPath(path);
1196
+ loader.setResourcePath(resourcePath);
1174
1197
  const settings = Object.assign({
1175
1198
  mapping: EquirectangularReflectionMapping
1176
1199
  }, texSettings);
1177
- const loader = new RGBELoader(this.manager.loadingManager);
1178
- loader.setPath(this.manager.path);
1179
- loader.setResourcePath(this.manager.resourcePath);
1180
1200
  loader.load(url, (tex)=>onLoad(Object.assign(tex, settings)), onProgress, onError);
1181
1201
  }
1182
1202
  constructor(...args){
@@ -1188,10 +1208,14 @@ class aHDRLoader extends aLoader {
1188
1208
  }
1189
1209
 
1190
1210
  class aJSONLoader extends aLoader {
1191
- load({ url, onLoad, onProgress, onError }) {
1192
- const loader = new FileLoader(this.manager.loadingManager);
1193
- loader.setPath(this.manager.path);
1194
- loader.setResourcePath(this.manager.resourcePath);
1211
+ load({ url, path, resourcePath, manager, onLoad, onProgress, onError }) {
1212
+ let loader = this._loader;
1213
+ if (loader === undefined) {
1214
+ loader = this._loader = new FileLoader();
1215
+ }
1216
+ loader.manager = manager;
1217
+ loader.setPath(path);
1218
+ loader.setResourcePath(resourcePath);
1195
1219
  loader.load(url, (file)=>onLoad(JSON.parse(file)), onProgress, onError);
1196
1220
  }
1197
1221
  constructor(...args){
@@ -1203,14 +1227,18 @@ class aJSONLoader extends aLoader {
1203
1227
  }
1204
1228
 
1205
1229
  class aTextureLoader extends aLoader {
1206
- load({ url, onLoad, onProgress, onError, texSettings }) {
1230
+ load({ url, path, resourcePath, manager, texSettings, onLoad, onProgress, onError }) {
1231
+ let loader = this._loader;
1232
+ if (loader === undefined) {
1233
+ loader = this._loader = new TextureLoader();
1234
+ }
1235
+ loader.manager = manager;
1236
+ loader.setPath(path);
1237
+ loader.setResourcePath(resourcePath);
1207
1238
  const loadCallback = (tex)=>{
1208
1239
  tex.colorSpace = SRGBColorSpace;
1209
1240
  onLoad(Object.assign(tex, texSettings));
1210
1241
  };
1211
- const loader = new TextureLoader(this.manager.loadingManager);
1212
- loader.setPath(this.manager.path);
1213
- loader.setResourcePath(this.manager.resourcePath);
1214
1242
  loader.load(url, loadCallback, onProgress, onError);
1215
1243
  }
1216
1244
  constructor(...args){
@@ -2872,13 +2900,13 @@ class FreelookVirtualCamera extends VirtualCamera {
2872
2900
  this._touchID = -1;
2873
2901
  this._preLoc0 = new Vector2();
2874
2902
  this._preLoc1 = new Vector2();
2875
- this._spherical = new Spherical(2, Math.PI / 2);
2903
+ this._spherical = new Spherical(4, Math.PI / 2);
2876
2904
  this._lookAt = new Vector3();
2877
2905
  this._tempSmoothing = 6;
2878
2906
  this._tempRotateSmoothing = 8;
2879
2907
  this._targetTheta = 0;
2880
2908
  this._targetPhi = 0;
2881
- this._targetSpringLength = 1;
2909
+ this._targetSpringLength = 4;
2882
2910
  this._targetFov = this.fov;
2883
2911
  this._targetLookAt = new Vector3();
2884
2912
  this.forbidX = false;
@@ -4261,15 +4289,6 @@ class ResourceManager {
4261
4289
  let str = path.split("/").pop();
4262
4290
  return ext ? str.replace("." + ext, "") : str;
4263
4291
  }
4264
- static _getTextureKey(url, settings) {
4265
- let keys = [
4266
- url
4267
- ];
4268
- for(let k in settings){
4269
- if (settings[k] !== undefined) keys.push(k + "=" + settings[k]);
4270
- }
4271
- return keys.join(",");
4272
- }
4273
4292
  static _splitTextureSettings(props) {
4274
4293
  let settings = {};
4275
4294
  for (let v of ResourceManager._texSettingKeys){
@@ -4284,7 +4303,6 @@ class ResourceManager {
4284
4303
  return settings;
4285
4304
  }
4286
4305
  destroy() {
4287
- this._caches.clear();
4288
4306
  this._loaders.clear();
4289
4307
  }
4290
4308
  getLoader(ext) {
@@ -4296,47 +4314,32 @@ class ResourceManager {
4296
4314
  this._loaders.set(ext, loader);
4297
4315
  }
4298
4316
  }
4299
- loadAsset({ url, buffer, ext, onProgress, ...props }) {
4317
+ loadAsset({ url, buffer, ext, path, resourcePath, dracoPath, manager, onProgress, ...props }) {
4300
4318
  return new Promise((resolve, reject)=>{
4301
- const ext_ = ResourceManager.extension(url);
4319
+ const sel = ext || url && ResourceManager.extension(url) || "";
4302
4320
  const texSettings = ResourceManager._splitTextureSettings(props);
4303
- const key = ResourceManager._getTextureKey(url, texSettings);
4304
- let result = this._caches.get(key);
4305
- if (result) {
4306
- resolve(result);
4321
+ if (this._loaders.has(sel)) {
4322
+ this._loaders.get(sel).load({
4323
+ url,
4324
+ buffer,
4325
+ texSettings,
4326
+ path,
4327
+ resourcePath,
4328
+ dracoPath,
4329
+ manager,
4330
+ onProgress,
4331
+ onLoad: resolve,
4332
+ onError: (e)=>{
4333
+ console.error(`${url} not exist`, e);
4334
+ }
4335
+ });
4307
4336
  } else {
4308
- const onLoad = (file)=>{
4309
- this._caches.set(key, file);
4310
- resolve(file);
4311
- };
4312
- const sel = ext || ext_;
4313
- if (this._loaders.has(sel)) {
4314
- this._loaders.get(sel).load({
4315
- url,
4316
- buffer,
4317
- texSettings,
4318
- onProgress,
4319
- onLoad,
4320
- onError: (e)=>{
4321
- console.error(`${url} not exist`, e);
4322
- }
4323
- });
4324
- } else {
4325
- reject("ResourceManager.loadAsset: missing loader for " + url);
4326
- }
4337
+ reject("ResourceManager.loadAsset: missing loader for " + url);
4327
4338
  }
4328
4339
  });
4329
4340
  }
4330
- constructor(props){
4331
- this.path = "";
4332
- this.resourcePath = "";
4333
- this.dracoPath = "";
4334
- this._caches = new Map();
4341
+ constructor(){
4335
4342
  this._loaders = new Map();
4336
- this.path = props.path;
4337
- this.resourcePath = props.resourcePath;
4338
- this.dracoPath = props.dracoPath;
4339
- this.loadingManager = props.loadingManager;
4340
4343
  }
4341
4344
  }
4342
4345
  ResourceManager._texSettingKeys = [
@@ -4545,9 +4548,6 @@ class Viewer extends EventEmitter {
4545
4548
  static getMesh(node, name, group = false) {
4546
4549
  return Viewer._getObjectValue(node.userData.meshes, name, group);
4547
4550
  }
4548
- get instanceId() {
4549
- return this._instanceId;
4550
- }
4551
4551
  get root() {
4552
4552
  return this._root;
4553
4553
  }
@@ -4599,6 +4599,9 @@ class Viewer extends EventEmitter {
4599
4599
  set targetFrameRate(v) {
4600
4600
  this._targetFrameRate = Math.max(0.001, v);
4601
4601
  }
4602
+ get instanceId() {
4603
+ return this._instanceId;
4604
+ }
4602
4605
  get loadingManager() {
4603
4606
  return this._loadingManager;
4604
4607
  }
@@ -4732,7 +4735,7 @@ class Viewer extends EventEmitter {
4732
4735
  }
4733
4736
  }
4734
4737
  loadAsset(props) {
4735
- return this._resourceManager.loadAsset(props);
4738
+ return this._resourceManager.loadAsset(Object.assign(Object.assign({}, this._defaultProps), props));
4736
4739
  }
4737
4740
  getLoader(ext) {
4738
4741
  return this._resourceManager.getLoader(ext);
@@ -4928,6 +4931,7 @@ class Viewer extends EventEmitter {
4928
4931
  factor: 1
4929
4932
  };
4930
4933
  this._orientation = Orientation.AUTO;
4934
+ this._defaultProps = {};
4931
4935
  this._running = false;
4932
4936
  this._rootRotated = false;
4933
4937
  this._time = 0;
@@ -4961,17 +4965,18 @@ class Viewer extends EventEmitter {
4961
4965
  this._renderer.shadowMap.enabled = !!shadows;
4962
4966
  this._renderer.shadowMap.type = typeof shadows === "boolean" ? PCFSoftShadowMap : shadows;
4963
4967
  this._renderer.info.autoReset = false;
4964
- const loadingManager = this._loadingManager = new LoadingManager(loader.onLoad, loader.onProgress, loader.onError);
4965
- this._resourceManager = new ResourceManager({
4966
- path,
4967
- resourcePath,
4968
- dracoPath,
4969
- loadingManager
4970
- });
4968
+ this._resourceManager = new ResourceManager();
4969
+ this._loadingManager = new LoadingManager(loader.onLoad, loader.onProgress, loader.onError);
4971
4970
  this._taskManager = new TaskManager(tasker.onComplete, tasker.onProgress, tasker.onError);
4972
4971
  this._componentManager = new ComponentManager(this);
4973
4972
  this._pluginManager = new PluginManager(this);
4974
4973
  this._tweenManager = new TweenManager();
4974
+ this._defaultProps = {
4975
+ path,
4976
+ resourcePath,
4977
+ dracoPath,
4978
+ manager: this._loadingManager
4979
+ };
4975
4980
  this._autoResize = autoResize;
4976
4981
  this._targetFrameRate = targetFrameRate;
4977
4982
  this._input = new DeviceInput(this);
@@ -5565,5 +5570,148 @@ class PerformanceMonitorPlugin extends Plugin {
5565
5570
  }
5566
5571
  }
5567
5572
 
5568
- export { AnimationCurve, Box, BoxProjectionPlugin, CinestationBlendDefinition, CinestationBrain, Component, DebugPlugin, DeviceInput, Easing, EnvironmentPlugin, EventEmitter, FInterpConstantTo, FInterpTo, FreelookVirtualCamera, Logger, ObjectInstance, Orientation, PerformanceMonitorPlugin, Perlin, Plane, Plugin, PropertyManager, QInterpConstantTo, QInterpTo, Quat_AngularDistance, Quat_Equals, Quat_exponentialDamp, Quat_quarticDamp, Quat_smoothDamp, Reflector, ReflectorMaterial, ResourceManager, Sphere, SystemInfo, Tween, TweenChain, 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, exponentialDamp, frag_BoxfilterBlur, frag_cubeMapToPanorama, frag_panoramaToCubeMap, getClassInstance, getShaderMaterial, mixin, property, quarticDamp, smoothDamp, vert_fullscreen };
5573
+ class DropFilePlugin extends Plugin {
5574
+ install() {
5575
+ document.addEventListener("dragover", this._onDrogOver);
5576
+ document.addEventListener("drop", this._onDrop);
5577
+ }
5578
+ uninstall() {
5579
+ document.removeEventListener("dragover", this._onDrogOver);
5580
+ document.removeEventListener("drop", this._onDrop);
5581
+ }
5582
+ _loadItemList(items) {
5583
+ getFilesFromItemList(items, (files, filesMap, dirs)=>{
5584
+ this._loadFiles(files, filesMap, dirs);
5585
+ });
5586
+ }
5587
+ _loadFiles(files, filesMap, dirs) {
5588
+ if (files.length > 0) {
5589
+ filesMap = filesMap || createFilesMap(files);
5590
+ dirs = dirs || [];
5591
+ this.viewer.loadingManager.setURLModifier((url)=>{
5592
+ url = url.replace(/^(\.?\/)/, ''); // remove './'
5593
+ const file = filesMap[url];
5594
+ if (file) {
5595
+ console.log('Loading', url);
5596
+ return URL.createObjectURL(file);
5597
+ }
5598
+ return url;
5599
+ });
5600
+ for(let i = 0; i < files.length; i++){
5601
+ this._loadFile(files[i], dirs[i]);
5602
+ }
5603
+ }
5604
+ }
5605
+ _loadFile(file, dir = "") {
5606
+ const filename = file.name;
5607
+ const ext = ResourceManager.extension(filename);
5608
+ if (this._extensions.some((v)=>v === ext)) {
5609
+ const reader = new FileReader();
5610
+ reader.addEventListener("progress", (event)=>{
5611
+ const size = '(' + Math.floor(event.total / 1000) + ' KB)';
5612
+ const progress = Math.floor(event.loaded / event.total * 100) + '%';
5613
+ console.log('Loading', filename, size, progress);
5614
+ });
5615
+ const loadCallback = (asset)=>{
5616
+ asset.userData.ext = ext;
5617
+ this._onLoad && this._onLoad(asset);
5618
+ };
5619
+ if ([
5620
+ "fbx",
5621
+ "gltf",
5622
+ "glb"
5623
+ ].some((v)=>v === ext)) {
5624
+ reader.addEventListener("load", (event)=>{
5625
+ this.viewer.loadAsset({
5626
+ ext,
5627
+ buffer: event.target.result,
5628
+ resourcePath: dir
5629
+ }).then(loadCallback);
5630
+ }, false);
5631
+ reader.readAsArrayBuffer(file);
5632
+ } else {
5633
+ reader.addEventListener("load", (event)=>{
5634
+ this.viewer.loadAsset({
5635
+ ext,
5636
+ url: event.target.result
5637
+ }).then(loadCallback);
5638
+ }, false);
5639
+ reader.readAsDataURL(file);
5640
+ }
5641
+ }
5642
+ }
5643
+ constructor({ onLoad, extension = [
5644
+ "glb",
5645
+ "gltf",
5646
+ "fbx",
5647
+ "hdr"
5648
+ ] } = {}){
5649
+ super();
5650
+ this._onDrogOver = (e)=>{
5651
+ e.preventDefault();
5652
+ e.dataTransfer.dropEffect = "copy";
5653
+ };
5654
+ this._onDrop = (e)=>{
5655
+ e.preventDefault();
5656
+ if (e.dataTransfer.types[0] === "text/plain") return; // Outliner drop
5657
+ if (e.dataTransfer.items) {
5658
+ this._loadItemList(e.dataTransfer.items);
5659
+ } else {
5660
+ this._loadFiles(e.dataTransfer.files);
5661
+ }
5662
+ };
5663
+ this._onLoad = onLoad;
5664
+ this._extensions = Array.isArray(extension) ? extension : [
5665
+ extension
5666
+ ];
5667
+ }
5668
+ }
5669
+ function createFilesMap(files) {
5670
+ const map = {};
5671
+ for(let i = 0; i < files.length; i++){
5672
+ const file = files[i];
5673
+ map[file.name] = file;
5674
+ }
5675
+ return map;
5676
+ }
5677
+ function getFilesFromItemList(items, onDone) {
5678
+ let itemsCount = 0;
5679
+ let itemsTotal = 0;
5680
+ const dirs = [];
5681
+ const files = [];
5682
+ const filesMap = {};
5683
+ function onEntryHandled() {
5684
+ itemsCount++;
5685
+ if (itemsCount === itemsTotal) {
5686
+ onDone(files, filesMap, dirs);
5687
+ }
5688
+ }
5689
+ function handleEntry(entry, dir = "") {
5690
+ if (entry.isDirectory) {
5691
+ const reader = entry.createReader();
5692
+ reader.readEntries((entries)=>{
5693
+ for(let i = 0; i < entries.length; i++){
5694
+ handleEntry(entries[i], dir + "/" + entry.name + "/");
5695
+ }
5696
+ onEntryHandled();
5697
+ });
5698
+ } else if (entry.isFile) {
5699
+ entry.file((file)=>{
5700
+ files.push(file);
5701
+ filesMap[entry.fullPath.slice(1)] = file;
5702
+ dirs.push(dir);
5703
+ onEntryHandled();
5704
+ });
5705
+ }
5706
+ itemsTotal++;
5707
+ }
5708
+ for(let i = 0; i < items.length; i++){
5709
+ const item = items[i];
5710
+ if (item.kind === 'file') {
5711
+ handleEntry(item.webkitGetAsEntry());
5712
+ }
5713
+ }
5714
+ }
5715
+
5716
+ export { AnimationCurve, Box, BoxProjectionPlugin, CinestationBlendDefinition, CinestationBrain, Component, DebugPlugin, DeviceInput, DropFilePlugin, Easing, EnvironmentPlugin, EventEmitter, FInterpConstantTo, FInterpTo, FreelookVirtualCamera, Logger, ObjectInstance, Orientation, PerformanceMonitorPlugin, Perlin, Plane, Plugin, PropertyManager, QInterpConstantTo, QInterpTo, Quat_AngularDistance, Quat_Equals, Quat_exponentialDamp, Quat_quarticDamp, Quat_smoothDamp, Reflector, ReflectorMaterial, ResourceManager, Sphere, SystemInfo, Tween, TweenChain, 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, exponentialDamp, frag_BoxfilterBlur, frag_cubeMapToPanorama, frag_panoramaToCubeMap, getClassInstance, getShaderMaterial, mixin, property, quarticDamp, smoothDamp, vert_fullscreen };
5569
5717
  //# sourceMappingURL=module.js.map