three-base 2.0.99 → 2.0.101

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.
@@ -1,4 +1,4 @@
1
- import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Quaternion, Vector3 } from "three";
1
+ import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from "three";
2
2
  import MeshBase from "./MeshBase";
3
3
  export default class InstancedMeshBase extends MeshBase {
4
4
  constructor(url?: string);
@@ -16,6 +16,7 @@ export default class InstancedMeshBase extends MeshBase {
16
16
  static _matrix: Matrix4;
17
17
  instanceMatrix: InstancedBufferAttribute;
18
18
  instanceColor: InstancedBufferAttribute;
19
+ reset(...object: Object3D[]): this;
19
20
  loadModel(url: string | File | Array<File>, filename?: string): Promise<void>;
20
21
  computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
21
22
  setCount(count?: number): Promise<void>;
@@ -27,6 +27,12 @@ export default class InstancedMeshBase extends MeshBase {
27
27
  static _matrix = new Matrix4();
28
28
  instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
29
29
  instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
30
+ // 重置子元素
31
+ reset(...object) {
32
+ super.clear();
33
+ super.add(...object);
34
+ return this;
35
+ }
30
36
  // 重构加载模型的方法
31
37
  async loadModel(url, filename = "") {
32
38
  //获取模型文件的地址
@@ -59,7 +65,6 @@ export default class InstancedMeshBase extends MeshBase {
59
65
  })();
60
66
  this.url = modelurl || "";
61
67
  if (!url) {
62
- this.clear();
63
68
  return;
64
69
  }
65
70
  this.loaded = false;
@@ -77,7 +82,6 @@ export default class InstancedMeshBase extends MeshBase {
77
82
  await new Promise((resolve) => setTimeout(resolve, 50));
78
83
  } while (MeshBase.loadModeling.get(this.url));
79
84
  if (MeshBase.models.get(this.url)) {
80
- this.clear();
81
85
  const mesh = MeshBase.models.get(this.url);
82
86
  // // 判断是否有动画
83
87
  // this.hasAnimation = mesh.animations.length > 0;
@@ -119,6 +123,7 @@ export default class InstancedMeshBase extends MeshBase {
119
123
  obj.rotation.set(0, 0, 0);
120
124
  }
121
125
  });
126
+ this.clear();
122
127
  // create all instances
123
128
  Object.keys(meshCollect).forEach((uuid) => {
124
129
  const meshes = meshCollect[uuid];
package/MeshBase.d.ts CHANGED
@@ -11,6 +11,7 @@ export default class MeshBase extends Group {
11
11
  [key: string]: AnimationAction;
12
12
  };
13
13
  getSuffix(filename: string): string;
14
+ reset(...object: Object3D[]): this;
14
15
  loadModel(url: string | File | Array<File>, filename?: string): Promise<void>;
15
16
  frustumCulled: boolean;
16
17
  castShadow: boolean;
package/MeshBase.js CHANGED
@@ -27,6 +27,12 @@ export default class MeshBase extends Group {
27
27
  const match = filename.match(/\.([^\.]+)$/);
28
28
  return match ? match[1].toLowerCase() : "";
29
29
  }
30
+ // 重置子元素
31
+ reset(...object) {
32
+ super.clear();
33
+ super.add(...object);
34
+ return this;
35
+ }
30
36
  // 如果url 是blob地址的话,就用suffix字段来制定模型的类型
31
37
  async loadModel(url, filename = "") {
32
38
  //获取模型文件的地址
@@ -59,7 +65,6 @@ export default class MeshBase extends Group {
59
65
  })();
60
66
  this.url = modelurl || "";
61
67
  if (!modelurl) {
62
- this.clear();
63
68
  return;
64
69
  }
65
70
  this.loaded = false;
@@ -77,7 +82,6 @@ export default class MeshBase extends Group {
77
82
  await new Promise((resolve) => setTimeout(resolve, 50));
78
83
  } while (MeshBase.loadModeling.get(modelurl));
79
84
  if (MeshBase.models.get(modelurl)) {
80
- this.clear();
81
85
  const mesh = MeshBase.models.get(modelurl);
82
86
  // 判断是否有动画
83
87
  this.hasAnimation = mesh.animations.length > 0;
@@ -90,10 +94,10 @@ export default class MeshBase extends Group {
90
94
  animationAction.weight = 0;
91
95
  this.animationActions[animation.name] = animationAction;
92
96
  });
93
- this.add(model);
97
+ this.reset(model);
94
98
  }
95
99
  else {
96
- this.add(mesh.clone(true));
100
+ this.reset(mesh.clone(true));
97
101
  }
98
102
  }
99
103
  await new Promise((resolve) => requestAnimationFrame(resolve));
package/ThreeBase.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * @Last Modified time: 2024-09-23 21:20:32
6
6
  */
7
7
  import { round } from "lodash";
8
- import { AxesHelper, Box3, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, MOUSE, Mesh, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer, } from "three";
8
+ import { AxesHelper, Box3, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, MOUSE, Mesh, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, SRGBColorSpace, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer, } from "three";
9
9
  import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
10
10
  import { degToRad, generateUUID } from "three/src/math/MathUtils";
11
11
  import ThreeMouse from "./ThreeMouse";
@@ -99,6 +99,7 @@ export default class ThreeBase extends EventDispatcher {
99
99
  renderer.setPixelRatio(window.devicePixelRatio);
100
100
  renderer.autoClear = false;
101
101
  renderer.clear();
102
+ renderer.outputColorSpace = SRGBColorSpace;
102
103
  threeLoader.setAnisotropy(renderer.capabilities.getMaxAnisotropy());
103
104
  return renderer;
104
105
  })();
@@ -271,7 +272,7 @@ export default class ThreeBase extends EventDispatcher {
271
272
  return axesHelper;
272
273
  })();
273
274
  //TODO 设置帧频率 默认50帧
274
- frame = 50;
275
+ frame = 30;
275
276
  init() { }
276
277
  // 重置控制器的位置
277
278
  resetLookAt({ position = this.perspectiveCamera.position, center = this.controls.target, }) {
@@ -360,7 +361,7 @@ export default class ThreeBase extends EventDispatcher {
360
361
  lookatObjects({ objects, duration = 1000, direction = new Vector3(0.3, 0.3, 1), offset = new Vector3(0, 0, 0), distance = 1, }) {
361
362
  const box3 = new Box3();
362
363
  objects.map((o) => {
363
- o.traverse((oo) => {
364
+ o.traverseVisible((oo) => {
364
365
  box3.expandByObject(oo);
365
366
  });
366
367
  });
package/Transition.d.ts CHANGED
@@ -30,7 +30,7 @@ export default class Transition<T extends TransitionParame, K extends Extract<ke
30
30
  onChange(onChangeCall: (josn: T) => void): this;
31
31
  onDone(onDoneCall: (josn: T) => void): this;
32
32
  get(key: Extract<keyof T, string>): T[K];
33
- set(param: T): Promise<this>;
33
+ set(param: Partial<T>): Promise<this>;
34
34
  reset(param: Partial<T>): this;
35
35
  getKeys(): Array<Extract<keyof T, string>>;
36
36
  hasKey(key: Extract<keyof T, string>): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.99",
3
+ "version": "2.0.101",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
package/threeLoader.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2024-09-11 11:43:31
6
6
  */
7
- import { CanvasTexture, DefaultLoadingManager, EventDispatcher, Mesh, MeshStandardMaterial, RepeatWrapping, TextureLoader, VideoTexture, } from "three";
7
+ import { CanvasTexture, DefaultLoadingManager, EventDispatcher, Mesh, MeshStandardMaterial, RepeatWrapping, SRGBColorSpace, TextureLoader, VideoTexture, } from "three";
8
8
  import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
9
9
  import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
10
10
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
@@ -26,6 +26,8 @@ import { isArray, isEmpty, isNull, isString, isUndefined } from "lodash";
26
26
  import { generateUUID } from "three/src/math/MathUtils";
27
27
  import dracoDecoderWASM from "./lib/draco/draco_decoder.wasm?url";
28
28
  import threeUtils from "./threeUtils";
29
+ import { TGALoader } from "three/examples/jsm/Addons.js";
30
+ DefaultLoadingManager.addHandler(/tga$/, new TGALoader());
29
31
  // 加载器
30
32
  class ThreeLoader extends EventDispatcher {
31
33
  constructor() {
@@ -76,7 +78,7 @@ class ThreeLoader extends EventDispatcher {
76
78
  const canvasTexture = new CanvasTexture(src);
77
79
  canvasTexture.wrapS = canvasTexture.wrapT = RepeatWrapping;
78
80
  canvasTexture.anisotropy = this.anisotropy;
79
- // canvasTexture.colorSpace = SRGBColorSpace;
81
+ canvasTexture.colorSpace = SRGBColorSpace;
80
82
  return canvasTexture;
81
83
  }
82
84
  let texture = this.textureMap.get(src);
@@ -110,7 +112,7 @@ class ThreeLoader extends EventDispatcher {
110
112
  }
111
113
  texture.wrapS = texture.wrapT = RepeatWrapping;
112
114
  texture.anisotropy = this.anisotropy;
113
- // texture.colorSpace = SRGBColorSpace;
115
+ texture.colorSpace = SRGBColorSpace;
114
116
  return texture;
115
117
  }
116
118
  // 资源列表
@@ -344,6 +346,11 @@ class ThreeLoader extends EventDispatcher {
344
346
  if (stl) {
345
347
  return await this.loadModel(stl);
346
348
  }
349
+ //如果有zip文件就加载zip文件
350
+ let zipFile = Object.keys(zip.files).find((filename) => /\.stl$/.test(filename));
351
+ if (zipFile) {
352
+ return await this.loadModel(zipFile);
353
+ }
347
354
  if ([obj, gltf, dae, fbx, usdz, stl].every((v) => !v)) {
348
355
  throw new Error(`${url}压缩包里没有obj.glb.gltf.dae.fbx.usde.stl等模型文件`);
349
356
  }