three-base 2.0.87 → 2.0.89
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/package.json +2 -2
- package/threeLoader.d.ts +2 -2
- package/threeLoader.js +97 -16
- package/threeUtils.d.ts +1 -0
- package/threeUtils.js +17 -1
- package/ApngTexture.ts +0 -91
- package/ApngTextureManage.ts +0 -130
- package/EventMesh.ts +0 -79
- package/ExtrudeMesh.ts +0 -576
- package/FPScontrol.ts +0 -209
- package/Fire/Fire.ts +0 -99
- package/Fire/FireGromtry.ts +0 -54
- package/Fire/FireMaterial.ts +0 -92
- package/InstancedMeshBase.ts +0 -316
- package/KeyFrameAnimation.ts +0 -122
- package/MeshBase.ts +0 -236
- package/Mirror.ts +0 -254
- package/PlaneModal1/PlaneModal1.ts +0 -151
- package/PlaneModal1/index.ts +0 -1
- package/PlaneText.ts +0 -58
- package/PlaneWithHtml.ts +0 -85
- package/PointLight/PointLight.ts +0 -75
- package/PointLight/index.ts +0 -1
- package/Sky/Sky.ts +0 -36
- package/Sky/index.ts +0 -1
- package/SpriteModal1/SpriteModal1.ts +0 -160
- package/SpriteModal1/index.ts +0 -1
- package/SpriteText.ts +0 -190
- package/SpriteText2.ts +0 -237
- package/SpriteWithHtml.ts +0 -62
- package/Text3D/Text3D.ts +0 -93
- package/Text3D/fontController.ts +0 -284
- package/Text3D/index.ts +0 -1
- package/ThreeBase.ts +0 -758
- package/ThreeMouse.ts +0 -483
- package/Transition.ts +0 -288
- package/index.ts +0 -2
- package/keyContorls.ts +0 -127
- package/pass/FullScreenQuad.ts +0 -33
- package/pass/glow_pass.ts +0 -607
- package/pass/stroke_pass.ts +0 -561
- package/shaders/FXAAShaderMaterial.ts +0 -248
- package/shaders.ts +0 -264
- package/threeLoader.ts +0 -447
- package/threeUtils.ts +0 -351
package/InstancedMeshBase.ts
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiaosihan
|
|
3
|
-
* @Date: 2024-02-05 09:31:57
|
|
4
|
-
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2024-06-07 22:14:53
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { throttle } from "lodash";
|
|
9
|
-
import { BufferAttribute, Color, Euler, InstancedBufferAttribute, InstancedMesh, Material, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from "three";
|
|
10
|
-
import MeshBase from "./MeshBase";
|
|
11
|
-
import threeUtils from "./threeUtils";
|
|
12
|
-
import threeLoader from "./threeLoader";
|
|
13
|
-
|
|
14
|
-
// 实例模型组对象
|
|
15
|
-
export default class InstancedMeshBase extends MeshBase {
|
|
16
|
-
constructor(url?: string) {
|
|
17
|
-
super(url);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
isInstancedGroupMesh = true
|
|
21
|
-
|
|
22
|
-
count: number = 0;
|
|
23
|
-
|
|
24
|
-
size = new Vector3(1, 1, 1);
|
|
25
|
-
|
|
26
|
-
meshCollect: Record<string, Mesh[]> = {}
|
|
27
|
-
|
|
28
|
-
instanceCollect: Record<string, InstancedMesh> = {}
|
|
29
|
-
|
|
30
|
-
colorMap = new Map<String, Color>();
|
|
31
|
-
|
|
32
|
-
declare children: InstancedMesh[];
|
|
33
|
-
|
|
34
|
-
static _position: Vector3 = new Vector3();
|
|
35
|
-
static _scale: Vector3 = new Vector3(1, 1, 1);
|
|
36
|
-
static _rotation: Euler = new Euler();
|
|
37
|
-
static _Quaternion: Quaternion = new Quaternion();
|
|
38
|
-
static _matrix = new Matrix4()
|
|
39
|
-
|
|
40
|
-
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
41
|
-
|
|
42
|
-
instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
43
|
-
|
|
44
|
-
// 重构加载模型的方法
|
|
45
|
-
async loadModel(url: string, filename: string = "") {
|
|
46
|
-
this.url = url;
|
|
47
|
-
|
|
48
|
-
if (!url) {
|
|
49
|
-
this.clear();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.loaded = false;
|
|
54
|
-
|
|
55
|
-
// 没有模型并且没有加载任务时就去加载模型
|
|
56
|
-
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
57
|
-
MeshBase.loadModeling.set(url, true);
|
|
58
|
-
|
|
59
|
-
// 万能加载器
|
|
60
|
-
let modal = await threeLoader.loadModel(url, filename);
|
|
61
|
-
|
|
62
|
-
MeshBase.models.set(url, modal);
|
|
63
|
-
MeshBase.loadModeling.set(url, false);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// 如果模型还在加载中, 就等待模型加载完成
|
|
67
|
-
do {
|
|
68
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
69
|
-
} while (MeshBase.loadModeling.get(url));
|
|
70
|
-
|
|
71
|
-
if (MeshBase.models.get(url)) {
|
|
72
|
-
|
|
73
|
-
this.clear();
|
|
74
|
-
const mesh = MeshBase.models.get(url)!;
|
|
75
|
-
// // 判断是否有动画
|
|
76
|
-
// this.hasAnimation = mesh.animations.length > 0;
|
|
77
|
-
|
|
78
|
-
// if (this.hasAnimation) {
|
|
79
|
-
// const model = SkeletonUtils.clone(mesh);
|
|
80
|
-
// this.mixer = new AnimationMixer(model);
|
|
81
|
-
|
|
82
|
-
// this.animationActions = {};
|
|
83
|
-
// mesh.animations.map(animation => {
|
|
84
|
-
// const animationAction = this.mixer!.clipAction(animation).play();
|
|
85
|
-
// animationAction.weight = 0;
|
|
86
|
-
// this.animationActions[animation.name] = animationAction;
|
|
87
|
-
// });
|
|
88
|
-
|
|
89
|
-
// this.add(model);
|
|
90
|
-
// } else {
|
|
91
|
-
// this.add(mesh.clone(true));
|
|
92
|
-
// }
|
|
93
|
-
|
|
94
|
-
this.size.copy(threeUtils.getSize(mesh));
|
|
95
|
-
|
|
96
|
-
const { meshCollect, instanceCollect } = this
|
|
97
|
-
|
|
98
|
-
mesh.updateMatrixWorld(true);
|
|
99
|
-
|
|
100
|
-
// collect all mesh counts, in case there are more than one copy of the same mesh in this group
|
|
101
|
-
mesh.traverse((obj) => {
|
|
102
|
-
const mesh = obj as Mesh
|
|
103
|
-
if (mesh.isMesh) {
|
|
104
|
-
|
|
105
|
-
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
106
|
-
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
107
|
-
mesh.updateMatrix();
|
|
108
|
-
|
|
109
|
-
// same geometry and same material consider as the same mesh
|
|
110
|
-
const uuid = mesh.geometry.uuid + '/' + (mesh.material as Material).uuid
|
|
111
|
-
|
|
112
|
-
if (meshCollect[uuid]) {
|
|
113
|
-
meshCollect[uuid].push(mesh)
|
|
114
|
-
} else {
|
|
115
|
-
meshCollect[uuid] = [mesh]
|
|
116
|
-
}
|
|
117
|
-
} else {
|
|
118
|
-
obj.position.set(0, 0, 0);
|
|
119
|
-
obj.scale.set(1, 1, 1);
|
|
120
|
-
obj.rotation.set(0, 0, 0);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
// create all instances
|
|
126
|
-
Object.keys(meshCollect).forEach(uuid => {
|
|
127
|
-
const meshes = meshCollect[uuid]
|
|
128
|
-
const mesh = meshes[0]
|
|
129
|
-
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count)
|
|
130
|
-
instancedMesh.name = mesh.name;
|
|
131
|
-
instancedMesh.frustumCulled = false;
|
|
132
|
-
instanceCollect[uuid] = instancedMesh
|
|
133
|
-
this.add(instancedMesh)
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
// 加载完成事件
|
|
137
|
-
await new Promise(resolve => setTimeout(resolve, 30));
|
|
138
|
-
// 加载完成事件
|
|
139
|
-
this.loaded = true;
|
|
140
|
-
this.dispatchEvent({ type: "loaded" } as never);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 更新包围盒
|
|
146
|
-
computeBoundingSphere = throttle(() => {
|
|
147
|
-
this.children.map(instancedMesh => {
|
|
148
|
-
instancedMesh.computeBoundingSphere();
|
|
149
|
-
});
|
|
150
|
-
}, 1000)
|
|
151
|
-
|
|
152
|
-
//初始实例矩阵的数量
|
|
153
|
-
async setCount(count: number = 0) {
|
|
154
|
-
if (this.count === count) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
await this.awaitLoaded();
|
|
158
|
-
|
|
159
|
-
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
160
|
-
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
161
|
-
|
|
162
|
-
for (let i = 0; i < count; i++) {
|
|
163
|
-
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
164
|
-
instanceColor.set([1, 1, 1], i * 3);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
for (let i = 0; i < this.count; i++) {
|
|
168
|
-
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
169
|
-
instanceColor.copyAt(i, this.instanceColor, i);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
this.instanceMatrix = instanceMatrix;
|
|
173
|
-
this.instanceColor = instanceColor;
|
|
174
|
-
|
|
175
|
-
this.count = count;
|
|
176
|
-
|
|
177
|
-
this.children.map((instanceMesh: InstancedMesh) => {
|
|
178
|
-
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
179
|
-
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
180
|
-
|
|
181
|
-
for (let i = 0; i < count; i++) {
|
|
182
|
-
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
183
|
-
instanceColor.set([1, 1, 1], i * 3);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
for (let i = 0; i < instanceMesh.count; i++) {
|
|
187
|
-
instanceMatrix.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
188
|
-
instanceColor.copyAt(i, instanceMesh.instanceColor as BufferAttribute, i);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
instanceMesh.instanceMatrix = instanceMatrix;
|
|
192
|
-
instanceMesh.instanceColor = instanceColor;
|
|
193
|
-
instanceMesh.count = count;
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// 设置矩阵
|
|
199
|
-
async setMatrixAt(index: number, matrix: Matrix4 = new Matrix4().identity()) {
|
|
200
|
-
const new_matrix = matrix.clone(); // 把矩阵克隆一下避免对象引用问题
|
|
201
|
-
await this.awaitLoaded();
|
|
202
|
-
Object.keys(this.meshCollect).forEach(uuid => {
|
|
203
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
204
|
-
const collect = this.meshCollect[uuid]
|
|
205
|
-
collect.forEach((mesh: Mesh, i: number) => {
|
|
206
|
-
InstancedMeshBase._matrix.copy(mesh.matrix);
|
|
207
|
-
InstancedMeshBase._matrix.premultiply(new_matrix);
|
|
208
|
-
|
|
209
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
210
|
-
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
this.computeBoundingSphere();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
//设置移动旋转缩放
|
|
218
|
-
async setTRS(
|
|
219
|
-
index: number,
|
|
220
|
-
{
|
|
221
|
-
position,
|
|
222
|
-
rotation,
|
|
223
|
-
scale
|
|
224
|
-
}: {
|
|
225
|
-
position?: Vector3;
|
|
226
|
-
rotation?: Euler;
|
|
227
|
-
scale?: Vector3
|
|
228
|
-
}
|
|
229
|
-
) {
|
|
230
|
-
|
|
231
|
-
const new_position = position ? position.clone() : undefined;
|
|
232
|
-
const new_rotation = rotation ? rotation.clone() : undefined;
|
|
233
|
-
const new_scale = scale ? scale.clone() : undefined;
|
|
234
|
-
|
|
235
|
-
await this.awaitLoaded();
|
|
236
|
-
|
|
237
|
-
Object.keys(this.meshCollect).forEach(uuid => {
|
|
238
|
-
const instancedMesh = this.instanceCollect[uuid]
|
|
239
|
-
const collect = this.meshCollect[uuid]
|
|
240
|
-
collect.forEach((mesh: Mesh, i: number) => {
|
|
241
|
-
|
|
242
|
-
//获取对应的模型的矩阵
|
|
243
|
-
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
244
|
-
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
245
|
-
|
|
246
|
-
InstancedMeshBase._matrix.decompose(
|
|
247
|
-
InstancedMeshBase._position,
|
|
248
|
-
InstancedMeshBase._Quaternion,
|
|
249
|
-
InstancedMeshBase._scale
|
|
250
|
-
);
|
|
251
|
-
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
252
|
-
new_position && InstancedMeshBase._position.copy(new_position);
|
|
253
|
-
new_rotation && InstancedMeshBase._rotation.copy(new_rotation);
|
|
254
|
-
new_scale && InstancedMeshBase._scale.copy(new_scale);
|
|
255
|
-
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
256
|
-
|
|
257
|
-
InstancedMeshBase._matrix.compose(
|
|
258
|
-
InstancedMeshBase._position,
|
|
259
|
-
InstancedMeshBase._Quaternion,
|
|
260
|
-
InstancedMeshBase._scale
|
|
261
|
-
);
|
|
262
|
-
|
|
263
|
-
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
264
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
265
|
-
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
266
|
-
})
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
this.computeBoundingSphere();
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// 设置颜色
|
|
273
|
-
async setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
|
|
274
|
-
await this.awaitLoaded();
|
|
275
|
-
|
|
276
|
-
Object.keys(this.meshCollect).forEach(uuid => {
|
|
277
|
-
const instancedMesh = this.instanceCollect[uuid]
|
|
278
|
-
const collect = this.meshCollect[uuid]
|
|
279
|
-
collect.forEach((mesh: Mesh, i: number) => {
|
|
280
|
-
const c = color(instancedMesh);
|
|
281
|
-
if (c) {
|
|
282
|
-
instancedMesh.setColorAt(collect.length * index + i, c);
|
|
283
|
-
instancedMesh.instanceColor!.needsUpdate = true;
|
|
284
|
-
}
|
|
285
|
-
})
|
|
286
|
-
})
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// 同步属性
|
|
290
|
-
updateMatrixWorld(force?: boolean): void {
|
|
291
|
-
super.updateMatrixWorld(force);
|
|
292
|
-
const { cursor, enableEvent } = this.userData;
|
|
293
|
-
this.traverse(obj => {
|
|
294
|
-
Object.assign(obj.userData, { cursor, enableEvent });
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// 事件监听
|
|
299
|
-
async addEventListener(name: string, callback: (e: any) => void) {
|
|
300
|
-
super.addEventListener(name, callback);
|
|
301
|
-
await this.awaitLoaded();
|
|
302
|
-
super.addEventListener(name, callback);
|
|
303
|
-
this.children.map(mesh => {
|
|
304
|
-
mesh.addEventListener(name, callback);
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// 取消事件的监听
|
|
309
|
-
removeEventListener(name: string, callback: (e: any) => void) {
|
|
310
|
-
super.removeEventListener(name, callback);
|
|
311
|
-
this.children.map(mesh => {
|
|
312
|
-
mesh.removeEventListener(name, callback);
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
}
|
package/KeyFrameAnimation.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Author: xiaosihan
|
|
3
|
-
* @Date: 2023-02-16 00:22:31
|
|
4
|
-
* @Last Modified by: xiaosihan
|
|
5
|
-
* @Last Modified time: 2023-02-16 19:08:55
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { last } from "lodash";
|
|
9
|
-
import { BufferAttribute, Euler, Line, LineBasicMaterial, Object3D, Vector3 } from "three";
|
|
10
|
-
import threeUtils from "./threeUtils";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// 关键帧类型
|
|
14
|
-
export type Keyframe = {
|
|
15
|
-
position: Vector3;
|
|
16
|
-
scale: Vector3;
|
|
17
|
-
rotation: Euler;
|
|
18
|
-
timeTemp: number; //时间节点 单位毫秒.
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export type KeyframeAnimationProps = {
|
|
23
|
-
speedScale?: number; // 速度调整
|
|
24
|
-
object3d: Object3D; // 需要移动的对象
|
|
25
|
-
keyframes: Array<Keyframe>,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// 关键帧动画对象
|
|
29
|
-
export default class KeyframeAnimation extends Object3D {
|
|
30
|
-
|
|
31
|
-
constructor(props: KeyframeAnimationProps) {
|
|
32
|
-
super();
|
|
33
|
-
|
|
34
|
-
// 速度调整
|
|
35
|
-
this.speedScale = props.speedScale || 1;
|
|
36
|
-
|
|
37
|
-
// 设置关键帧对象数组
|
|
38
|
-
this.setKeyframes(props.keyframes);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// 设置对象
|
|
42
|
-
this.setObject3D(props.object3d);
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// 速度调整
|
|
47
|
-
speedScale = 1;
|
|
48
|
-
|
|
49
|
-
// 需要需要移动的对象
|
|
50
|
-
object3d!: Object3D;
|
|
51
|
-
|
|
52
|
-
//路径辅助线
|
|
53
|
-
halperLine = new Line(undefined, new LineBasicMaterial({ color: "#f00" }));
|
|
54
|
-
|
|
55
|
-
// 关键帧数组
|
|
56
|
-
keyframes: Array<Keyframe> = [];
|
|
57
|
-
|
|
58
|
-
// 设置需要移动的对象
|
|
59
|
-
setObject3D(object3d: Object3D) {
|
|
60
|
-
this.object3d = object3d;
|
|
61
|
-
if (threeUtils.isDev) {
|
|
62
|
-
this.children = [object3d, this.halperLine];
|
|
63
|
-
} else {
|
|
64
|
-
this.children = [object3d];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 获取单个关键帧
|
|
69
|
-
getKeyframe(i: number) {
|
|
70
|
-
return this.keyframes[i];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// 设置单个关键帧
|
|
74
|
-
setKeyframe(i: number, keyframe: Keyframe) {
|
|
75
|
-
this.keyframes[i] = keyframe;
|
|
76
|
-
this.setKeyframes(this.keyframes);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
//设置全部关键帧
|
|
80
|
-
setKeyframes(keyframes: Array<Keyframe>) {
|
|
81
|
-
this.keyframes = keyframes;
|
|
82
|
-
const position: Array<number> = [];
|
|
83
|
-
keyframes.map(k => {
|
|
84
|
-
position.push(k.position.x);
|
|
85
|
-
position.push(k.position.y);
|
|
86
|
-
position.push(k.position.z);
|
|
87
|
-
});
|
|
88
|
-
this.halperLine.geometry.setAttribute("position", new BufferAttribute(new Float32Array(position), 3, false));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// 更具关键帧的信息更新 对象的位置
|
|
92
|
-
update() {
|
|
93
|
-
const { _timeTemp } = threeUtils;
|
|
94
|
-
const maxtimetemp = last(this.keyframes)!.timeTemp;
|
|
95
|
-
const currentTime = _timeTemp % maxtimetemp;
|
|
96
|
-
|
|
97
|
-
const currentKeyframeIndex = this.keyframes.findIndex(k => currentTime < k.timeTemp);
|
|
98
|
-
|
|
99
|
-
const startKeyframe = this.keyframes[currentKeyframeIndex - 1];
|
|
100
|
-
const endKeyframe = this.keyframes[currentKeyframeIndex];
|
|
101
|
-
const progress = (currentTime - startKeyframe.timeTemp) / (endKeyframe.timeTemp - startKeyframe.timeTemp);
|
|
102
|
-
|
|
103
|
-
this.object3d.position.copy(endKeyframe.position).sub(startKeyframe.position).multiplyScalar(progress).add(startKeyframe.position);
|
|
104
|
-
this.object3d.scale.copy(endKeyframe.scale).sub(startKeyframe.scale).multiplyScalar(progress).add(startKeyframe.scale);
|
|
105
|
-
|
|
106
|
-
this.object3d.rotation.set(
|
|
107
|
-
(endKeyframe.rotation.x - startKeyframe.rotation.x) * progress + startKeyframe.rotation.x,
|
|
108
|
-
(endKeyframe.rotation.y - startKeyframe.rotation.y) * progress + startKeyframe.rotation.y,
|
|
109
|
-
(endKeyframe.rotation.z - startKeyframe.rotation.z) * progress + startKeyframe.rotation.z,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
this.dispatchEvent({ type: "update" } as never);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
//更新关键帧的实例矩阵
|
|
116
|
-
updateMatrix(): void {
|
|
117
|
-
this.update();
|
|
118
|
-
super.updateMatrix();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
package/MeshBase.ts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { AnimationAction, AnimationMixer, Clock, DoubleSide, Group, Material, Mesh, MeshStandardMaterial, Object3D, Vector3 } from "three";
|
|
3
|
-
import threeUtils from "./threeUtils";
|
|
4
|
-
import threeLoader from "./threeLoader";
|
|
5
|
-
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
|
|
6
|
-
import { get, includes } from "lodash";
|
|
7
|
-
|
|
8
|
-
// 模型基类
|
|
9
|
-
export default class MeshBase extends Group {
|
|
10
|
-
constructor(url?: string) {
|
|
11
|
-
super();
|
|
12
|
-
url && this.loadModel(url);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
url = "";
|
|
16
|
-
|
|
17
|
-
// 模型对象
|
|
18
|
-
static models: Map<string, Object3D> = new Map();
|
|
19
|
-
|
|
20
|
-
// 模型是否在加载中
|
|
21
|
-
static loadModeling: Map<string, boolean> = new Map();
|
|
22
|
-
|
|
23
|
-
// 加载完成
|
|
24
|
-
loaded = false;
|
|
25
|
-
|
|
26
|
-
// 是否有动画
|
|
27
|
-
hasAnimation = false;
|
|
28
|
-
|
|
29
|
-
//动画混合器
|
|
30
|
-
mixer?: AnimationMixer;
|
|
31
|
-
|
|
32
|
-
//动画剪辑对象数组
|
|
33
|
-
animationActions: { [key: string]: AnimationAction } = {};
|
|
34
|
-
|
|
35
|
-
// 获取文件名的后缀
|
|
36
|
-
getSuffix(filename: string) {
|
|
37
|
-
const match = filename.match(/\.([^\.]+)$/);
|
|
38
|
-
return match ? match[1].toLowerCase() : '';
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// 如果url 是blob地址的话,就用suffix字段来制定模型的类型
|
|
42
|
-
async loadModel(url: string, filename: string = "") {
|
|
43
|
-
|
|
44
|
-
this.url = url;
|
|
45
|
-
|
|
46
|
-
if (!url) {
|
|
47
|
-
this.clear();
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.loaded = false;
|
|
52
|
-
|
|
53
|
-
// 没有模型并且没有加载任务时就去加载模型
|
|
54
|
-
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
55
|
-
MeshBase.loadModeling.set(url, true);
|
|
56
|
-
|
|
57
|
-
// 万能加载器
|
|
58
|
-
let modal = await threeLoader.loadModel(url, filename);
|
|
59
|
-
|
|
60
|
-
MeshBase.models.set(url, modal);
|
|
61
|
-
MeshBase.loadModeling.set(url, false);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// 如果模型还在加载中, 就等待模型加载完成
|
|
65
|
-
do {
|
|
66
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
67
|
-
} while (MeshBase.loadModeling.get(url));
|
|
68
|
-
|
|
69
|
-
if (MeshBase.models.get(url)) {
|
|
70
|
-
|
|
71
|
-
this.clear();
|
|
72
|
-
const mesh = MeshBase.models.get(url)!;
|
|
73
|
-
// 判断是否有动画
|
|
74
|
-
this.hasAnimation = mesh.animations.length > 0;
|
|
75
|
-
|
|
76
|
-
if (this.hasAnimation) {
|
|
77
|
-
const model = SkeletonUtils.clone(mesh);
|
|
78
|
-
this.mixer = new AnimationMixer(model);
|
|
79
|
-
|
|
80
|
-
this.animationActions = {};
|
|
81
|
-
mesh.animations.map(animation => {
|
|
82
|
-
const animationAction = this.mixer!.clipAction(animation).play();
|
|
83
|
-
animationAction.weight = 0;
|
|
84
|
-
this.animationActions[animation.name] = animationAction;
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
this.add(model);
|
|
88
|
-
} else {
|
|
89
|
-
this.add(mesh.clone(true));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
95
|
-
|
|
96
|
-
this.loaded = true;
|
|
97
|
-
|
|
98
|
-
// 加载完成事件
|
|
99
|
-
this.dispatchEvent({ type: "loaded" } as never);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 超出绘制区域时不绘制
|
|
103
|
-
frustumCulled = false;
|
|
104
|
-
|
|
105
|
-
// 创建阴影
|
|
106
|
-
castShadow: boolean = false;
|
|
107
|
-
|
|
108
|
-
// 接受阴影
|
|
109
|
-
receiveShadow: boolean = false;
|
|
110
|
-
|
|
111
|
-
// promise.resole 队列
|
|
112
|
-
resoles: Array<(value: void | PromiseLike<void>) => void> = [];
|
|
113
|
-
// 等待加载加载完成
|
|
114
|
-
async awaitLoaded() {
|
|
115
|
-
if (!this.loaded) {
|
|
116
|
-
await new Promise<void>((resolve) => {
|
|
117
|
-
if (this.resoles.length === 0) {
|
|
118
|
-
(async () => {
|
|
119
|
-
while (!this.loaded) {
|
|
120
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
121
|
-
}
|
|
122
|
-
while (this.resoles.length > 0) {
|
|
123
|
-
const resole = this.resoles.pop();
|
|
124
|
-
resole && resole();
|
|
125
|
-
}
|
|
126
|
-
})();
|
|
127
|
-
}
|
|
128
|
-
this.resoles.unshift(resolve);
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
//遍历模型
|
|
134
|
-
async traverseMesh(callback: (mesh: Mesh) => void) {
|
|
135
|
-
// 等待加载完成
|
|
136
|
-
await this.awaitLoaded();
|
|
137
|
-
|
|
138
|
-
this.traverse(obj => {
|
|
139
|
-
const mesh = obj as Mesh;
|
|
140
|
-
if (mesh.isMesh) {
|
|
141
|
-
callback(mesh);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
clock = new Clock();
|
|
147
|
-
|
|
148
|
-
//设置动画的权重
|
|
149
|
-
async setClipActionWeight(weights: { [key: string]: number } = {}) {
|
|
150
|
-
// 等待加载完成
|
|
151
|
-
await this.awaitLoaded();
|
|
152
|
-
for (let key in this.animationActions) {
|
|
153
|
-
this.animationActions[key].weight = weights[key] || 0;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
//获取动画列表
|
|
158
|
-
getAnimas() {
|
|
159
|
-
return Object.keys(this.animationActions);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//播放指定动画
|
|
163
|
-
setAnimas(anima: string = "") {
|
|
164
|
-
this.setClipActionWeight({ [anima]: 1 });
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
//停止所有动画
|
|
168
|
-
stop() {
|
|
169
|
-
this.setClipActionWeight();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
updateMatrix(): void {
|
|
173
|
-
//播放动画
|
|
174
|
-
if (this.mixer) {
|
|
175
|
-
this.mixer.update(this.clock.getDelta());
|
|
176
|
-
}
|
|
177
|
-
// 深度应用属性
|
|
178
|
-
this.traverse(obj => {
|
|
179
|
-
const mesh = obj as Mesh;
|
|
180
|
-
if (mesh.isMesh) {
|
|
181
|
-
mesh.frustumCulled = this.frustumCulled;
|
|
182
|
-
mesh.castShadow = this.castShadow;
|
|
183
|
-
mesh.receiveShadow = this.receiveShadow;
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
super.updateMatrix();
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// 获取模型的尺寸
|
|
191
|
-
async getSize() {
|
|
192
|
-
await this.awaitLoaded();
|
|
193
|
-
return threeUtils.getSize(this);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 居中
|
|
197
|
-
async center() {
|
|
198
|
-
await this.awaitLoaded();
|
|
199
|
-
threeUtils.center(this);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
//底对齐
|
|
203
|
-
async alignBottom(y: number = 0) {
|
|
204
|
-
await this.awaitLoaded();
|
|
205
|
-
threeUtils.alignBottom(this, y);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// 放大到指定尺寸
|
|
209
|
-
async scaleTo(size: number = 1, key: Array<"x" | "y" | "z"> = ["x", "y", "z"]) {
|
|
210
|
-
await this.awaitLoaded();
|
|
211
|
-
threeUtils.scaleTo(this, size, key);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
//按照法线拓展
|
|
215
|
-
expandBynormal(length: number) {
|
|
216
|
-
this.traverseMesh(mesh => {
|
|
217
|
-
// 没有法线就计算法线
|
|
218
|
-
mesh.geometry = mesh.geometry.clone();
|
|
219
|
-
if (!mesh.geometry.getAttribute("normal")) {
|
|
220
|
-
mesh.geometry.computeVertexNormals();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const normal = mesh.geometry.getAttribute("normal");
|
|
224
|
-
const position = mesh.geometry.getAttribute("position");
|
|
225
|
-
const v3 = new Vector3(0, 0, 0);
|
|
226
|
-
for (let i = 0; i < position.count; i++) {
|
|
227
|
-
v3.set(normal.getX(i), normal.getY(i), normal.getZ(i));
|
|
228
|
-
v3.setLength(length);
|
|
229
|
-
position.setXYZ(i, position.getX(i) + v3.x, position.getY(i) + v3.y, position.getZ(i) + v3.z);
|
|
230
|
-
}
|
|
231
|
-
position.needsUpdate = true;
|
|
232
|
-
|
|
233
|
-
})
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
}
|