three-base 2.0.88 → 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.
- package/InstancedMeshBase.d.ts +2 -2
- package/InstancedMeshBase.js +45 -22
- package/MeshBase.d.ts +1 -1
- package/MeshBase.js +43 -19
- package/package.json +1 -1
- package/threeLoader.d.ts +2 -2
- package/threeLoader.js +97 -16
- package/threeUtils.d.ts +1 -0
- package/threeUtils.js +17 -1
package/InstancedMeshBase.d.ts
CHANGED
|
@@ -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
|
|
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;
|
package/InstancedMeshBase.js
CHANGED
|
@@ -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
|
-
|
|
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) &&
|
|
40
|
-
MeshBase.loadModeling.
|
|
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 +
|
|
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
|
|
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
|
|
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
|
-
|
|
32
|
-
|
|
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(
|
|
39
|
-
MeshBase.loadModeling.
|
|
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(
|
|
43
|
-
MeshBase.loadModeling.set(
|
|
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(
|
|
49
|
-
if (MeshBase.models.get(
|
|
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(
|
|
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
package/threeLoader.d.ts
CHANGED
|
@@ -23,8 +23,8 @@ declare class ThreeLoader extends EventDispatcher {
|
|
|
23
23
|
gltfLoader: GLTFLoader;
|
|
24
24
|
usdzLoader: USDZLoader;
|
|
25
25
|
setFlipYIsFalse<T extends Material | Material[]>(material: T): void;
|
|
26
|
-
/** 加载模型支持glb,fbx,stl,obj */
|
|
27
|
-
loadModel(url: string
|
|
26
|
+
/** 加载模型支持glb,gltf,fbx,stl,obj,usdz,dae,zip */
|
|
27
|
+
loadModel(url: string | File | Array<File>, filename?: string): any;
|
|
28
28
|
onload: boolean;
|
|
29
29
|
}
|
|
30
30
|
declare const threeLoader: ThreeLoader;
|
package/threeLoader.js
CHANGED
|
@@ -25,6 +25,7 @@ import axios from "axios";
|
|
|
25
25
|
import dracoDecoderWASM from "./lib/draco/draco_decoder.wasm?url";
|
|
26
26
|
import threeUtils from "./threeUtils";
|
|
27
27
|
import { generateUUID } from "three/src/math/MathUtils";
|
|
28
|
+
import { isString } from "lodash";
|
|
28
29
|
// 加载器
|
|
29
30
|
class ThreeLoader extends EventDispatcher {
|
|
30
31
|
constructor() {
|
|
@@ -178,21 +179,65 @@ class ThreeLoader extends EventDispatcher {
|
|
|
178
179
|
});
|
|
179
180
|
}
|
|
180
181
|
}
|
|
181
|
-
/** 加载模型支持glb,fbx,stl,obj */
|
|
182
|
+
/** 加载模型支持glb,gltf,fbx,stl,obj,usdz,dae,zip */
|
|
182
183
|
async loadModel(url, filename = "") {
|
|
183
|
-
//
|
|
184
|
-
const
|
|
184
|
+
// 本次需要销毁的bloburl,加载完成后就去销毁本次的文件
|
|
185
|
+
const revokeObjectfile = [];
|
|
186
|
+
// 获取模型文件的后缀
|
|
187
|
+
const suffix = await (async () => {
|
|
188
|
+
if (isString(url)) {
|
|
189
|
+
return threeUtils.getSuffix(filename || url);
|
|
190
|
+
}
|
|
191
|
+
else if (url instanceof File) {
|
|
192
|
+
return threeUtils.getSuffix(filename || url.name);
|
|
193
|
+
}
|
|
194
|
+
else if (url instanceof Array) {
|
|
195
|
+
let suffix = "";
|
|
196
|
+
const suffixs = ["obj", "glb", "gltf", "fbx", "dae", "stl", "usdz"];
|
|
197
|
+
for (let file of url) {
|
|
198
|
+
suffix = threeUtils.getSuffix(file.name);
|
|
199
|
+
if (suffixs.includes(suffix)) {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return threeUtils.getSuffix(filename || suffix);
|
|
204
|
+
}
|
|
205
|
+
})();
|
|
206
|
+
//获取模型文件的地址
|
|
207
|
+
const modelurl = await (async () => {
|
|
208
|
+
if (isString(url)) {
|
|
209
|
+
return url;
|
|
210
|
+
}
|
|
211
|
+
else if (url instanceof File) {
|
|
212
|
+
const bloburl = await threeUtils.fileToBloburl(url);
|
|
213
|
+
this._blobs[url.name] = bloburl;
|
|
214
|
+
revokeObjectfile.push(url.name);
|
|
215
|
+
return bloburl;
|
|
216
|
+
}
|
|
217
|
+
else if (url instanceof Array) {
|
|
218
|
+
let modelurl = "";
|
|
219
|
+
const suffixs = ["obj", "glb", "gltf", "fbx", "dae", "stl", "usdz"];
|
|
220
|
+
for (let file of url) {
|
|
221
|
+
const bloburl = await threeUtils.fileToBloburl(file);
|
|
222
|
+
this._blobs[file.name] = bloburl;
|
|
223
|
+
revokeObjectfile.push(file.name);
|
|
224
|
+
if (suffixs.includes(threeUtils.getSuffix(file.name))) {
|
|
225
|
+
modelurl = file.name;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return modelurl;
|
|
229
|
+
}
|
|
230
|
+
})();
|
|
185
231
|
switch (suffix) {
|
|
186
232
|
case "zip":
|
|
187
233
|
const JSZip = (await import("jszip")).default;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
});
|
|
234
|
+
let arrayBuffer = isString(url)
|
|
235
|
+
? await axios
|
|
236
|
+
.get(url, {
|
|
237
|
+
responseType: "arraybuffer",
|
|
238
|
+
})
|
|
239
|
+
.then((res) => res.data)
|
|
240
|
+
: url;
|
|
196
241
|
return await JSZip.loadAsync(arrayBuffer).then(async (zip) => {
|
|
197
242
|
for (let filename in zip.files) {
|
|
198
243
|
if (!zip.files[filename].dir) {
|
|
@@ -203,6 +248,7 @@ class ThreeLoader extends EventDispatcher {
|
|
|
203
248
|
.then((blob) => {
|
|
204
249
|
var blobURL = URL.createObjectURL(blob);
|
|
205
250
|
this._blobs[filename] = blobURL;
|
|
251
|
+
revokeObjectfile.push(filename);
|
|
206
252
|
});
|
|
207
253
|
}
|
|
208
254
|
}
|
|
@@ -221,6 +267,10 @@ class ThreeLoader extends EventDispatcher {
|
|
|
221
267
|
let obj = Object.keys(zip.files).find((filename) => /\.obj$/.test(filename));
|
|
222
268
|
if (obj) {
|
|
223
269
|
const objObject3d = await objLoader.loadAsync(obj);
|
|
270
|
+
revokeObjectfile.map((filename) => {
|
|
271
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
272
|
+
delete this._blobs[filename];
|
|
273
|
+
});
|
|
224
274
|
objObject3d.traverse((obj) => {
|
|
225
275
|
const mesh = obj;
|
|
226
276
|
if (mesh.isMesh) {
|
|
@@ -274,8 +324,12 @@ class ThreeLoader extends EventDispatcher {
|
|
|
274
324
|
});
|
|
275
325
|
case "fbx":
|
|
276
326
|
return await threeLoader.fbxLoader
|
|
277
|
-
.loadAsync(
|
|
327
|
+
.loadAsync(modelurl)
|
|
278
328
|
.then((res) => {
|
|
329
|
+
revokeObjectfile.map((filename) => {
|
|
330
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
331
|
+
delete this._blobs[filename];
|
|
332
|
+
});
|
|
279
333
|
return res;
|
|
280
334
|
})
|
|
281
335
|
.catch(() => {
|
|
@@ -284,8 +338,12 @@ class ThreeLoader extends EventDispatcher {
|
|
|
284
338
|
case "gltf":
|
|
285
339
|
case "glb":
|
|
286
340
|
return await threeLoader.gltfLoader
|
|
287
|
-
.loadAsync(
|
|
341
|
+
.loadAsync(modelurl)
|
|
288
342
|
.then((res) => {
|
|
343
|
+
revokeObjectfile.map((filename) => {
|
|
344
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
345
|
+
delete this._blobs[filename];
|
|
346
|
+
});
|
|
289
347
|
res.scene.animations = res.animations;
|
|
290
348
|
return res.scene;
|
|
291
349
|
})
|
|
@@ -294,8 +352,12 @@ class ThreeLoader extends EventDispatcher {
|
|
|
294
352
|
});
|
|
295
353
|
case "stl":
|
|
296
354
|
return await threeLoader.stlLoader
|
|
297
|
-
.loadAsync(
|
|
355
|
+
.loadAsync(modelurl)
|
|
298
356
|
.then((geometry) => {
|
|
357
|
+
revokeObjectfile.map((filename) => {
|
|
358
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
359
|
+
delete this._blobs[filename];
|
|
360
|
+
});
|
|
299
361
|
return new Mesh(geometry, new MeshStandardMaterial({
|
|
300
362
|
metalness: 0.5,
|
|
301
363
|
roughness: 0.5,
|
|
@@ -306,8 +368,12 @@ class ThreeLoader extends EventDispatcher {
|
|
|
306
368
|
});
|
|
307
369
|
case "usdz":
|
|
308
370
|
return await threeLoader.usdzLoader
|
|
309
|
-
.loadAsync(
|
|
371
|
+
.loadAsync(modelurl)
|
|
310
372
|
.then((res) => {
|
|
373
|
+
revokeObjectfile.map((filename) => {
|
|
374
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
375
|
+
delete this._blobs[filename];
|
|
376
|
+
});
|
|
311
377
|
res.traverse((obj) => {
|
|
312
378
|
const mesh = obj;
|
|
313
379
|
if (mesh.isMesh) {
|
|
@@ -320,7 +386,18 @@ class ThreeLoader extends EventDispatcher {
|
|
|
320
386
|
console.log(`${url},加载失败`, err);
|
|
321
387
|
});
|
|
322
388
|
case "obj":
|
|
323
|
-
|
|
389
|
+
let objLoader = new OBJLoader();
|
|
390
|
+
// 如果有mtl文件就加载mtl文件
|
|
391
|
+
let mtl = revokeObjectfile.find((filename) => /\.mtl$/.test(filename));
|
|
392
|
+
if (mtl) {
|
|
393
|
+
const materials = await this.mtlLoader.loadAsync(mtl);
|
|
394
|
+
objLoader.setMaterials(materials);
|
|
395
|
+
}
|
|
396
|
+
const objObject3d = await objLoader.loadAsync(modelurl);
|
|
397
|
+
revokeObjectfile.map((filename) => {
|
|
398
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
399
|
+
delete this._blobs[filename];
|
|
400
|
+
});
|
|
324
401
|
objObject3d.traverse((obj) => {
|
|
325
402
|
const mesh = obj;
|
|
326
403
|
if (mesh.isMesh) {
|
|
@@ -343,6 +420,10 @@ class ThreeLoader extends EventDispatcher {
|
|
|
343
420
|
});
|
|
344
421
|
return objObject3d;
|
|
345
422
|
default:
|
|
423
|
+
revokeObjectfile.map((filename) => {
|
|
424
|
+
URL.revokeObjectURL(this._blobs[filename]);
|
|
425
|
+
delete this._blobs[filename];
|
|
426
|
+
});
|
|
346
427
|
if (suffix) {
|
|
347
428
|
console.log(`${suffix},不支持的格式`);
|
|
348
429
|
}
|
package/threeUtils.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ declare class ThreeUtils extends EventDispatcher {
|
|
|
44
44
|
dipose(object3d: Object3D): void;
|
|
45
45
|
getSuffix(filename: string): string;
|
|
46
46
|
getFilename(url: string): string;
|
|
47
|
+
fileToBloburl(file: File): Promise<string>;
|
|
47
48
|
}
|
|
48
49
|
declare const threeUtils: ThreeUtils;
|
|
49
50
|
export default threeUtils;
|
package/threeUtils.js
CHANGED
|
@@ -262,7 +262,7 @@ class ThreeUtils extends EventDispatcher {
|
|
|
262
262
|
}
|
|
263
263
|
// 获取文件名的后缀
|
|
264
264
|
getSuffix(filename) {
|
|
265
|
-
const match = filename.match(
|
|
265
|
+
const match = filename.match(/([^\.]+)$/);
|
|
266
266
|
return match ? match[1].toLowerCase() : "";
|
|
267
267
|
}
|
|
268
268
|
// 从url 中提取文件名
|
|
@@ -273,6 +273,22 @@ class ThreeUtils extends EventDispatcher {
|
|
|
273
273
|
const filename = path.substring(path.lastIndexOf("/") + 1);
|
|
274
274
|
return filename;
|
|
275
275
|
}
|
|
276
|
+
//文件转blob
|
|
277
|
+
fileToBloburl(file) {
|
|
278
|
+
return new Promise((resolve, reject) => {
|
|
279
|
+
const reader = new FileReader();
|
|
280
|
+
reader.onload = function (event) {
|
|
281
|
+
const arrayBuffer = event.target.result;
|
|
282
|
+
const blob = new Blob([arrayBuffer]);
|
|
283
|
+
const bloburl = URL.createObjectURL(blob);
|
|
284
|
+
resolve(bloburl);
|
|
285
|
+
};
|
|
286
|
+
reader.onerror = function (error) {
|
|
287
|
+
throw new Error(`文件转blbo 失败,${file.name}`);
|
|
288
|
+
};
|
|
289
|
+
reader.readAsArrayBuffer(file);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
276
292
|
}
|
|
277
293
|
const threeUtils = new ThreeUtils();
|
|
278
294
|
export default threeUtils;
|