three-base 2.0.89 → 2.0.91

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.
@@ -16,11 +16,11 @@ export default class InstancedMeshBase extends MeshBase {
16
16
  static _matrix: Matrix4;
17
17
  instanceMatrix: InstancedBufferAttribute;
18
18
  instanceColor: InstancedBufferAttribute;
19
- loadModel(url: string, filename?: string): Promise<void>;
19
+ loadModel(url: string | File | Array<File>, filename?: string): Promise<void>;
20
20
  computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
21
21
  setCount(count?: number): Promise<void>;
22
22
  setMatrixAt(index: number, matrix?: Matrix4): Promise<void>;
23
- setTRS(index: number, { position, rotation, scale }: {
23
+ setTRS(index: number, { position, rotation, scale, }: {
24
24
  position?: Vector3;
25
25
  rotation?: Euler;
26
26
  scale?: Vector3;
@@ -4,8 +4,8 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2024-06-07 22:14:53
6
6
  */
7
- import { throttle } from "lodash";
8
- import { Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Quaternion, Vector3 } from "three";
7
+ import { isString, throttle } from "lodash";
8
+ import { Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Quaternion, Vector3, } from "three";
9
9
  import MeshBase from "./MeshBase";
10
10
  import threeUtils from "./threeUtils";
11
11
  import threeLoader from "./threeLoader";
@@ -29,27 +29,50 @@ export default class InstancedMeshBase extends MeshBase {
29
29
  instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
30
30
  // 重构加载模型的方法
31
31
  async loadModel(url, filename = "") {
32
- this.url = url;
32
+ //获取模型文件的地址
33
+ const modelurl = await (async () => {
34
+ if (isString(url)) {
35
+ return url;
36
+ }
37
+ else if (url instanceof File) {
38
+ return url.name;
39
+ }
40
+ else if (url instanceof Array) {
41
+ let modelurl = "";
42
+ const suffixs = ["obj", "glb", "gltf", "fbx", "dae", "stl", "usdz"];
43
+ for (let file of url) {
44
+ if (suffixs.includes(threeUtils.getSuffix(file.name))) {
45
+ modelurl = file.name;
46
+ }
47
+ }
48
+ return modelurl;
49
+ }
50
+ })();
51
+ if (this.url === modelurl) {
52
+ return;
53
+ }
54
+ this.url = modelurl;
33
55
  if (!url) {
34
56
  this.clear();
35
57
  return;
36
58
  }
37
59
  this.loaded = false;
38
60
  // 没有模型并且没有加载任务时就去加载模型
39
- if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
40
- MeshBase.loadModeling.set(url, true);
61
+ if (!MeshBase.models.get(this.url) &&
62
+ !MeshBase.loadModeling.get(this.url)) {
63
+ MeshBase.loadModeling.set(this.url, true);
41
64
  // 万能加载器
42
65
  let modal = await threeLoader.loadModel(url, filename);
43
- MeshBase.models.set(url, modal);
44
- MeshBase.loadModeling.set(url, false);
66
+ MeshBase.models.set(this.url, modal);
67
+ MeshBase.loadModeling.set(this.url, false);
45
68
  }
46
69
  // 如果模型还在加载中, 就等待模型加载完成
47
70
  do {
48
- await new Promise(resolve => setTimeout(resolve, 50));
49
- } while (MeshBase.loadModeling.get(url));
50
- if (MeshBase.models.get(url)) {
71
+ await new Promise((resolve) => setTimeout(resolve, 50));
72
+ } while (MeshBase.loadModeling.get(this.url));
73
+ if (MeshBase.models.get(this.url)) {
51
74
  this.clear();
52
- const mesh = MeshBase.models.get(url);
75
+ const mesh = MeshBase.models.get(this.url);
53
76
  // // 判断是否有动画
54
77
  // this.hasAnimation = mesh.animations.length > 0;
55
78
  // if (this.hasAnimation) {
@@ -76,7 +99,7 @@ export default class InstancedMeshBase extends MeshBase {
76
99
  mesh.rotation.setFromQuaternion(mesh.quaternion);
77
100
  mesh.updateMatrix();
78
101
  // same geometry and same material consider as the same mesh
79
- const uuid = mesh.geometry.uuid + '/' + mesh.material.uuid;
102
+ const uuid = mesh.geometry.uuid + "/" + mesh.material.uuid;
80
103
  if (meshCollect[uuid]) {
81
104
  meshCollect[uuid].push(mesh);
82
105
  }
@@ -91,7 +114,7 @@ export default class InstancedMeshBase extends MeshBase {
91
114
  }
92
115
  });
93
116
  // create all instances
94
- Object.keys(meshCollect).forEach(uuid => {
117
+ Object.keys(meshCollect).forEach((uuid) => {
95
118
  const meshes = meshCollect[uuid];
96
119
  const mesh = meshes[0];
97
120
  const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
@@ -101,7 +124,7 @@ export default class InstancedMeshBase extends MeshBase {
101
124
  this.add(instancedMesh);
102
125
  });
103
126
  // 加载完成事件
104
- await new Promise(resolve => setTimeout(resolve, 30));
127
+ await new Promise((resolve) => setTimeout(resolve, 30));
105
128
  // 加载完成事件
106
129
  this.loaded = true;
107
130
  this.dispatchEvent({ type: "loaded" });
@@ -109,7 +132,7 @@ export default class InstancedMeshBase extends MeshBase {
109
132
  }
110
133
  // 更新包围盒
111
134
  computeBoundingSphere = throttle(() => {
112
- this.children.map(instancedMesh => {
135
+ this.children.map((instancedMesh) => {
113
136
  instancedMesh.computeBoundingSphere();
114
137
  });
115
138
  }, 1000);
@@ -152,7 +175,7 @@ export default class InstancedMeshBase extends MeshBase {
152
175
  async setMatrixAt(index, matrix = new Matrix4().identity()) {
153
176
  const new_matrix = matrix.clone(); // 把矩阵克隆一下避免对象引用问题
154
177
  await this.awaitLoaded();
155
- Object.keys(this.meshCollect).forEach(uuid => {
178
+ Object.keys(this.meshCollect).forEach((uuid) => {
156
179
  const instancedMesh = this.instanceCollect[uuid];
157
180
  const collect = this.meshCollect[uuid];
158
181
  collect.forEach((mesh, i) => {
@@ -165,12 +188,12 @@ export default class InstancedMeshBase extends MeshBase {
165
188
  this.computeBoundingSphere();
166
189
  }
167
190
  //设置移动旋转缩放
168
- async setTRS(index, { position, rotation, scale }) {
191
+ async setTRS(index, { position, rotation, scale, }) {
169
192
  const new_position = position ? position.clone() : undefined;
170
193
  const new_rotation = rotation ? rotation.clone() : undefined;
171
194
  const new_scale = scale ? scale.clone() : undefined;
172
195
  await this.awaitLoaded();
173
- Object.keys(this.meshCollect).forEach(uuid => {
196
+ Object.keys(this.meshCollect).forEach((uuid) => {
174
197
  const instancedMesh = this.instanceCollect[uuid];
175
198
  const collect = this.meshCollect[uuid];
176
199
  collect.forEach((mesh, i) => {
@@ -194,7 +217,7 @@ export default class InstancedMeshBase extends MeshBase {
194
217
  // 设置颜色
195
218
  async setColorAt(index, color) {
196
219
  await this.awaitLoaded();
197
- Object.keys(this.meshCollect).forEach(uuid => {
220
+ Object.keys(this.meshCollect).forEach((uuid) => {
198
221
  const instancedMesh = this.instanceCollect[uuid];
199
222
  const collect = this.meshCollect[uuid];
200
223
  collect.forEach((mesh, i) => {
@@ -210,7 +233,7 @@ export default class InstancedMeshBase extends MeshBase {
210
233
  updateMatrixWorld(force) {
211
234
  super.updateMatrixWorld(force);
212
235
  const { cursor, enableEvent } = this.userData;
213
- this.traverse(obj => {
236
+ this.traverse((obj) => {
214
237
  Object.assign(obj.userData, { cursor, enableEvent });
215
238
  });
216
239
  }
@@ -219,14 +242,14 @@ export default class InstancedMeshBase extends MeshBase {
219
242
  super.addEventListener(name, callback);
220
243
  await this.awaitLoaded();
221
244
  super.addEventListener(name, callback);
222
- this.children.map(mesh => {
245
+ this.children.map((mesh) => {
223
246
  mesh.addEventListener(name, callback);
224
247
  });
225
248
  }
226
249
  // 取消事件的监听
227
250
  removeEventListener(name, callback) {
228
251
  super.removeEventListener(name, callback);
229
- this.children.map(mesh => {
252
+ this.children.map((mesh) => {
230
253
  mesh.removeEventListener(name, callback);
231
254
  });
232
255
  }
package/MeshBase.d.ts CHANGED
@@ -11,7 +11,7 @@ export default class MeshBase extends Group {
11
11
  [key: string]: AnimationAction;
12
12
  };
13
13
  getSuffix(filename: string): string;
14
- loadModel(url: string, filename?: string): Promise<void>;
14
+ loadModel(url: string | File | Array<File>, filename?: string): Promise<void>;
15
15
  frustumCulled: boolean;
16
16
  castShadow: boolean;
17
17
  receiveShadow: boolean;
package/MeshBase.js CHANGED
@@ -1,7 +1,8 @@
1
- import { AnimationMixer, Clock, Group, Vector3 } from "three";
1
+ import { AnimationMixer, Clock, Group, Vector3, } from "three";
2
2
  import threeUtils from "./threeUtils";
3
3
  import threeLoader from "./threeLoader";
4
- import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
4
+ import * as SkeletonUtils from "three/examples/jsm/utils/SkeletonUtils";
5
+ import { isString } from "lodash";
5
6
  // 模型基类
6
7
  export default class MeshBase extends Group {
7
8
  constructor(url) {
@@ -24,38 +25,61 @@ export default class MeshBase extends Group {
24
25
  // 获取文件名的后缀
25
26
  getSuffix(filename) {
26
27
  const match = filename.match(/\.([^\.]+)$/);
27
- return match ? match[1].toLowerCase() : '';
28
+ return match ? match[1].toLowerCase() : "";
28
29
  }
29
30
  // 如果url 是blob地址的话,就用suffix字段来制定模型的类型
30
31
  async loadModel(url, filename = "") {
31
- this.url = url;
32
- if (!url) {
32
+ //获取模型文件的地址
33
+ const modelurl = await (async () => {
34
+ if (isString(url)) {
35
+ return url;
36
+ }
37
+ else if (url instanceof File) {
38
+ return url.name;
39
+ }
40
+ else if (url instanceof Array) {
41
+ let modelurl = "";
42
+ const suffixs = ["obj", "glb", "gltf", "fbx", "dae", "stl", "usdz"];
43
+ for (let file of url) {
44
+ if (suffixs.includes(threeUtils.getSuffix(file.name))) {
45
+ modelurl = file.name;
46
+ }
47
+ }
48
+ return modelurl;
49
+ }
50
+ })();
51
+ if (this.url === modelurl) {
52
+ return;
53
+ }
54
+ this.url = modelurl;
55
+ if (!modelurl) {
33
56
  this.clear();
34
57
  return;
35
58
  }
36
59
  this.loaded = false;
37
60
  // 没有模型并且没有加载任务时就去加载模型
38
- if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
39
- MeshBase.loadModeling.set(url, true);
61
+ if (!MeshBase.models.get(modelurl) &&
62
+ !MeshBase.loadModeling.get(modelurl)) {
63
+ MeshBase.loadModeling.set(modelurl, true);
40
64
  // 万能加载器
41
65
  let modal = await threeLoader.loadModel(url, filename);
42
- MeshBase.models.set(url, modal);
43
- MeshBase.loadModeling.set(url, false);
66
+ MeshBase.models.set(modelurl, modal);
67
+ MeshBase.loadModeling.set(modelurl, false);
44
68
  }
45
69
  // 如果模型还在加载中, 就等待模型加载完成
46
70
  do {
47
- await new Promise(resolve => setTimeout(resolve, 50));
48
- } while (MeshBase.loadModeling.get(url));
49
- if (MeshBase.models.get(url)) {
71
+ await new Promise((resolve) => setTimeout(resolve, 50));
72
+ } while (MeshBase.loadModeling.get(modelurl));
73
+ if (MeshBase.models.get(modelurl)) {
50
74
  this.clear();
51
- const mesh = MeshBase.models.get(url);
75
+ const mesh = MeshBase.models.get(modelurl);
52
76
  // 判断是否有动画
53
77
  this.hasAnimation = mesh.animations.length > 0;
54
78
  if (this.hasAnimation) {
55
79
  const model = SkeletonUtils.clone(mesh);
56
80
  this.mixer = new AnimationMixer(model);
57
81
  this.animationActions = {};
58
- mesh.animations.map(animation => {
82
+ mesh.animations.map((animation) => {
59
83
  const animationAction = this.mixer.clipAction(animation).play();
60
84
  animationAction.weight = 0;
61
85
  this.animationActions[animation.name] = animationAction;
@@ -66,7 +90,7 @@ export default class MeshBase extends Group {
66
90
  this.add(mesh.clone(true));
67
91
  }
68
92
  }
69
- await new Promise(resolve => requestAnimationFrame(resolve));
93
+ await new Promise((resolve) => requestAnimationFrame(resolve));
70
94
  this.loaded = true;
71
95
  // 加载完成事件
72
96
  this.dispatchEvent({ type: "loaded" });
@@ -86,7 +110,7 @@ export default class MeshBase extends Group {
86
110
  if (this.resoles.length === 0) {
87
111
  (async () => {
88
112
  while (!this.loaded) {
89
- await new Promise(resolve => requestAnimationFrame(resolve));
113
+ await new Promise((resolve) => requestAnimationFrame(resolve));
90
114
  }
91
115
  while (this.resoles.length > 0) {
92
116
  const resole = this.resoles.pop();
@@ -102,7 +126,7 @@ export default class MeshBase extends Group {
102
126
  async traverseMesh(callback) {
103
127
  // 等待加载完成
104
128
  await this.awaitLoaded();
105
- this.traverse(obj => {
129
+ this.traverse((obj) => {
106
130
  const mesh = obj;
107
131
  if (mesh.isMesh) {
108
132
  callback(mesh);
@@ -136,7 +160,7 @@ export default class MeshBase extends Group {
136
160
  this.mixer.update(this.clock.getDelta());
137
161
  }
138
162
  // 深度应用属性
139
- this.traverse(obj => {
163
+ this.traverse((obj) => {
140
164
  const mesh = obj;
141
165
  if (mesh.isMesh) {
142
166
  mesh.frustumCulled = this.frustumCulled;
@@ -168,7 +192,7 @@ export default class MeshBase extends Group {
168
192
  }
169
193
  //按照法线拓展
170
194
  expandBynormal(length) {
171
- this.traverseMesh(mesh => {
195
+ this.traverseMesh((mesh) => {
172
196
  // 没有法线就计算法线
173
197
  mesh.geometry = mesh.geometry.clone();
174
198
  if (!mesh.geometry.getAttribute("normal")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.89",
3
+ "version": "2.0.91",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"