three-base 2.0.130 → 2.0.137
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/ApngTexture.d.ts +13 -13
- package/ApngTexture.js +61 -61
- package/ApngTextureManage.d.ts +18 -18
- package/ApngTextureManage.js +87 -87
- package/DirectionalLight.d.ts +6 -0
- package/DirectionalLight.js +68 -0
- package/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/ExtrudeMesh.d.ts +112 -112
- package/ExtrudeMesh.js +366 -366
- package/FPScontrol.d.ts +78 -50
- package/FPScontrol.js +617 -232
- package/Fire/Fire.d.ts +28 -28
- package/Fire/Fire.js +79 -79
- package/Fire/FireGromtry.d.ts +9 -9
- package/Fire/FireGromtry.js +40 -40
- package/Fire/FireMaterial.d.ts +28 -28
- package/Fire/FireMaterial.js +32 -32
- package/Fire/index.d.ts +1 -1
- package/Fire/index.js +1 -1
- package/Grass/Grass.d.ts +8 -8
- package/Grass/Grass.js +38 -38
- package/Grass/index.d.ts +1 -1
- package/Grass/index.js +1 -1
- package/Ground1/Ground1.d.ts +11 -11
- package/Ground1/Ground1.js +54 -54
- package/Ground1/index.d.ts +1 -1
- package/Ground1/index.js +1 -1
- package/InstancedMeshBase.d.ts +39 -39
- package/InstancedMeshBase.js +293 -293
- package/KeyFrameAnimation.d.ts +25 -25
- package/KeyFrameAnimation.js +78 -78
- package/MeshBase.d.ts +35 -35
- package/MeshBase.js +223 -223
- package/Mirror.d.ts +25 -25
- package/Mirror.js +139 -139
- package/PipeMesh/PathGeometry.d.ts +44 -44
- package/PipeMesh/PathGeometry.js +300 -300
- package/PipeMesh/PathPoint.d.ts +17 -17
- package/PipeMesh/PathPoint.js +31 -31
- package/PipeMesh/PathPointList.d.ts +31 -31
- package/PipeMesh/PathPointList.js +208 -208
- package/PipeMesh/PathTubeGeometry.d.ts +25 -25
- package/PipeMesh/PathTubeGeometry.js +130 -130
- package/PipeMesh/PipeMesh.d.ts +25 -25
- package/PipeMesh/PipeMesh.js +63 -63
- package/PipeMesh/index.d.ts +1 -1
- package/PipeMesh/index.js +1 -1
- package/PlaneModal1/PlaneModal1.d.ts +31 -31
- package/PlaneModal1/PlaneModal1.js +105 -105
- package/PlaneModal1/index.d.ts +1 -1
- package/PlaneModal1/index.js +1 -1
- package/PlaneText.d.ts +39 -39
- package/PlaneText.js +30 -30
- package/PlaneWithHtml.d.ts +11 -11
- package/PlaneWithHtml.js +63 -68
- package/PointLight/PointLight.d.ts +14 -13
- package/PointLight/PointLight.js +70 -64
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/Sky/Sky.d.ts +9 -9
- package/Sky/Sky.js +22 -22
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- package/SpotLight.d.ts +6 -0
- package/SpotLight.js +72 -0
- package/SpriteModal1/SpriteModal1.d.ts +30 -30
- package/SpriteModal1/SpriteModal1.js +110 -110
- package/SpriteModal1/index.d.ts +1 -1
- package/SpriteModal1/index.js +1 -1
- package/SpriteText.d.ts +45 -45
- package/SpriteText.js +163 -163
- package/SpriteText2.d.ts +47 -47
- package/SpriteText2.js +198 -198
- package/SpriteWithHtml.d.ts +7 -7
- package/SpriteWithHtml.js +44 -49
- package/Text3D/Text3D.d.ts +20 -20
- package/Text3D/Text3D.js +62 -62
- package/Text3D/fontController.d.ts +59 -59
- package/Text3D/fontController.js +199 -199
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/ThreeBase.d.ts +111 -81
- package/ThreeBase.js +656 -560
- package/ThreeMouse.d.ts +40 -40
- package/ThreeMouse.js +407 -407
- package/Transition.d.ts +40 -40
- package/Transition.js +210 -210
- package/Water/Water.d.ts +5 -0
- package/Water/Water.js +26 -0
- package/Water/waternormals.jpg +0 -0
- package/declare.d.ts +167 -0
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/keyContorls.d.ts +25 -25
- package/keyContorls.js +98 -98
- package/package.json +3 -2
- package/pass/bloom_pass.d.ts +2 -2
- package/pass/bloom_pass.js +99 -99
- package/pass/cover_pass.d.ts +2 -2
- package/pass/cover_pass.js +95 -95
- package/pass/emissive_ao_pass.d.ts +13 -0
- package/pass/emissive_ao_pass.js +370 -0
- package/pass/frosted_glass_pass.d.ts +2 -0
- package/pass/frosted_glass_pass.js +275 -0
- package/pass/fsQuad.d.ts +9 -9
- package/pass/fsQuad.js +28 -28
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +310 -254
- package/pass/ssao_pass.d.ts +13 -0
- package/pass/ssao_pass.js +372 -0
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +199 -199
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +21 -21
- package/shaders/baseTextureToRenderShader.d.ts +2 -2
- package/shaders/baseTextureToRenderShader.js +14 -14
- package/shaders/copyShader.d.ts +3 -3
- package/shaders/copyShader.js +14 -14
- package/shaders.d.ts +5 -5
- package/shaders.js +42 -42
- package/threeLoader.d.ts +34 -34
- package/threeLoader.js +527 -527
- package/threeUtils.d.ts +52 -52
- package/threeUtils.js +330 -330
package/InstancedMeshBase.js
CHANGED
|
@@ -1,293 +1,293 @@
|
|
|
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
|
-
import { first, isString, throttle } from "lodash";
|
|
8
|
-
import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Quaternion, Vector3, } from "three";
|
|
9
|
-
import MeshBase from "./MeshBase";
|
|
10
|
-
import threeUtils from "./threeUtils";
|
|
11
|
-
import threeLoader from "./threeLoader";
|
|
12
|
-
// 实例模型组对象
|
|
13
|
-
export default class InstancedMeshBase extends MeshBase {
|
|
14
|
-
constructor(url) {
|
|
15
|
-
super(url);
|
|
16
|
-
}
|
|
17
|
-
isInstancedGroupMesh = true;
|
|
18
|
-
count = 0;
|
|
19
|
-
size = new Vector3(1, 1, 1);
|
|
20
|
-
meshCollect = {};
|
|
21
|
-
instanceCollect = {};
|
|
22
|
-
colorMap = new Map();
|
|
23
|
-
static _position = new Vector3();
|
|
24
|
-
static _scale = new Vector3(1, 1, 1);
|
|
25
|
-
static _rotation = new Euler();
|
|
26
|
-
static _Quaternion = new Quaternion();
|
|
27
|
-
static _matrix = new Matrix4();
|
|
28
|
-
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
29
|
-
instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
30
|
-
// 重置子元素
|
|
31
|
-
reset(...object) {
|
|
32
|
-
super.clear();
|
|
33
|
-
super.add(...object);
|
|
34
|
-
return this;
|
|
35
|
-
}
|
|
36
|
-
// 重构加载模型的方法
|
|
37
|
-
async loadModel(url, filename = "") {
|
|
38
|
-
//获取模型文件的地址
|
|
39
|
-
const modelurl = await (async () => {
|
|
40
|
-
if (isString(url)) {
|
|
41
|
-
return url;
|
|
42
|
-
}
|
|
43
|
-
else if (url instanceof File) {
|
|
44
|
-
return url.name;
|
|
45
|
-
}
|
|
46
|
-
else if (url instanceof Array) {
|
|
47
|
-
let modelurl = "";
|
|
48
|
-
const suffixs = [
|
|
49
|
-
"zip",
|
|
50
|
-
"obj",
|
|
51
|
-
"glb",
|
|
52
|
-
"gltf",
|
|
53
|
-
"fbx",
|
|
54
|
-
"dae",
|
|
55
|
-
"stl",
|
|
56
|
-
"usdz",
|
|
57
|
-
];
|
|
58
|
-
for (let file of url) {
|
|
59
|
-
if (suffixs.includes(threeUtils.getSuffix(file.name))) {
|
|
60
|
-
modelurl = file.name;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return modelurl;
|
|
64
|
-
}
|
|
65
|
-
})();
|
|
66
|
-
this.url = modelurl || "";
|
|
67
|
-
if (!url) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
this.loaded = false;
|
|
71
|
-
// 没有模型并且没有加载任务时就去加载模型
|
|
72
|
-
if (!MeshBase.models.get(this.url) &&
|
|
73
|
-
!MeshBase.loadModeling.get(this.url)) {
|
|
74
|
-
MeshBase.loadModeling.set(this.url, true);
|
|
75
|
-
// 万能加载器
|
|
76
|
-
let modal = await threeLoader.loadModel(url, filename);
|
|
77
|
-
MeshBase.models.set(this.url, modal);
|
|
78
|
-
MeshBase.loadModeling.set(this.url, false);
|
|
79
|
-
}
|
|
80
|
-
// 如果模型还在加载中, 就等待模型加载完成
|
|
81
|
-
do {
|
|
82
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
83
|
-
} while (MeshBase.loadModeling.get(this.url));
|
|
84
|
-
if (MeshBase.models.get(this.url)) {
|
|
85
|
-
const mesh = MeshBase.models.get(this.url);
|
|
86
|
-
// // 判断是否有动画
|
|
87
|
-
// this.hasAnimation = mesh.animations.length > 0;
|
|
88
|
-
// if (this.hasAnimation) {
|
|
89
|
-
// const model = SkeletonUtils.clone(mesh);
|
|
90
|
-
// this.mixer = new AnimationMixer(model);
|
|
91
|
-
// this.animationActions = {};
|
|
92
|
-
// mesh.animations.map(animation => {
|
|
93
|
-
// const animationAction = this.mixer!.clipAction(animation).play();
|
|
94
|
-
// animationAction.weight = 0;
|
|
95
|
-
// this.animationActions[animation.name] = animationAction;
|
|
96
|
-
// });
|
|
97
|
-
// this.add(model);
|
|
98
|
-
// } else {
|
|
99
|
-
// this.add(mesh.clone(true));
|
|
100
|
-
// }
|
|
101
|
-
this.size.copy(threeUtils.getSize(mesh));
|
|
102
|
-
const { meshCollect, instanceCollect } = this;
|
|
103
|
-
mesh.updateMatrixWorld(true);
|
|
104
|
-
// collect all mesh counts, in case there are more than one copy of the same mesh in this group
|
|
105
|
-
mesh.traverse((obj) => {
|
|
106
|
-
const mesh = obj;
|
|
107
|
-
if (mesh.isMesh) {
|
|
108
|
-
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
109
|
-
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
110
|
-
mesh.updateMatrix();
|
|
111
|
-
// same geometry and same material consider as the same mesh
|
|
112
|
-
const uuid = mesh.geometry.uuid + "/" + mesh.material.uuid;
|
|
113
|
-
if (meshCollect[uuid]) {
|
|
114
|
-
meshCollect[uuid].push(mesh);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
meshCollect[uuid] = [mesh];
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
obj.position.set(0, 0, 0);
|
|
122
|
-
obj.scale.set(1, 1, 1);
|
|
123
|
-
obj.rotation.set(0, 0, 0);
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
this.clear();
|
|
127
|
-
// create all instances
|
|
128
|
-
Object.keys(meshCollect).forEach((uuid) => {
|
|
129
|
-
const meshes = meshCollect[uuid];
|
|
130
|
-
const mesh = meshes[0];
|
|
131
|
-
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
|
|
132
|
-
instancedMesh.name = mesh.name;
|
|
133
|
-
instancedMesh.frustumCulled = false;
|
|
134
|
-
instanceCollect[uuid] = instancedMesh;
|
|
135
|
-
this.add(instancedMesh);
|
|
136
|
-
});
|
|
137
|
-
// 加载完成事件
|
|
138
|
-
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
139
|
-
// 加载完成事件
|
|
140
|
-
this.loaded = true;
|
|
141
|
-
this.dispatchEvent({ type: "loaded" });
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
// 更新包围盒
|
|
145
|
-
computeBoundingSphere = throttle(() => {
|
|
146
|
-
this.children.map((instancedMesh) => {
|
|
147
|
-
instancedMesh.computeBoundingSphere && instancedMesh.computeBoundingSphere();
|
|
148
|
-
});
|
|
149
|
-
}, 1000);
|
|
150
|
-
//初始实例矩阵的数量
|
|
151
|
-
async setCount(count = 0) {
|
|
152
|
-
if (this.count === count) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
await this.awaitLoaded();
|
|
156
|
-
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
157
|
-
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
158
|
-
for (let i = 0; i < count; i++) {
|
|
159
|
-
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
160
|
-
instanceColor.set([1, 1, 1], i * 3);
|
|
161
|
-
}
|
|
162
|
-
for (let i = 0; i < this.count; i++) {
|
|
163
|
-
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
164
|
-
instanceColor.copyAt(i, this.instanceColor, i);
|
|
165
|
-
}
|
|
166
|
-
this.instanceMatrix = instanceMatrix;
|
|
167
|
-
this.instanceColor = instanceColor;
|
|
168
|
-
this.count = count;
|
|
169
|
-
this.children.map((instanceMesh) => {
|
|
170
|
-
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
171
|
-
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
172
|
-
for (let i = 0; i < count; i++) {
|
|
173
|
-
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
174
|
-
instanceColor.set([1, 1, 1], i * 3);
|
|
175
|
-
}
|
|
176
|
-
for (let i = 0; i < instanceMesh.count; i++) {
|
|
177
|
-
instanceMatrix.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
178
|
-
instanceColor.copyAt(i, instanceMesh.instanceColor, i);
|
|
179
|
-
}
|
|
180
|
-
instanceMesh.instanceMatrix = instanceMatrix;
|
|
181
|
-
instanceMesh.instanceColor = instanceColor;
|
|
182
|
-
instanceMesh.count = count;
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
// 设置矩阵
|
|
186
|
-
async setMatrixAt(index, matrix = new Matrix4().identity()) {
|
|
187
|
-
const new_matrix = matrix.clone(); // 把矩阵克隆一下避免对象引用问题
|
|
188
|
-
await this.awaitLoaded();
|
|
189
|
-
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
190
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
191
|
-
const collect = this.meshCollect[uuid];
|
|
192
|
-
collect.forEach((mesh, i) => {
|
|
193
|
-
InstancedMeshBase._matrix.copy(mesh.matrix);
|
|
194
|
-
InstancedMeshBase._matrix.premultiply(new_matrix);
|
|
195
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
196
|
-
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
this.computeBoundingSphere();
|
|
200
|
-
}
|
|
201
|
-
//设置移动旋转缩放
|
|
202
|
-
async setTRS(index, { position, rotation, scale, }) {
|
|
203
|
-
const new_position = position ? position.clone() : undefined;
|
|
204
|
-
const new_rotation = rotation ? rotation.clone() : undefined;
|
|
205
|
-
const new_scale = scale ? scale.clone() : undefined;
|
|
206
|
-
await this.awaitLoaded();
|
|
207
|
-
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
208
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
209
|
-
const collect = this.meshCollect[uuid];
|
|
210
|
-
collect.forEach((mesh, i) => {
|
|
211
|
-
//获取对应的模型的矩阵
|
|
212
|
-
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
213
|
-
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
214
|
-
InstancedMeshBase._matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
215
|
-
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
216
|
-
new_position && InstancedMeshBase._position.copy(new_position);
|
|
217
|
-
new_rotation && InstancedMeshBase._rotation.copy(new_rotation);
|
|
218
|
-
new_scale && InstancedMeshBase._scale.copy(new_scale);
|
|
219
|
-
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
220
|
-
InstancedMeshBase._matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
221
|
-
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
222
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
223
|
-
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
this.computeBoundingSphere();
|
|
227
|
-
}
|
|
228
|
-
//获取指定位置的移动旋转缩放
|
|
229
|
-
async getTRS(index = 0) {
|
|
230
|
-
await this.awaitLoaded();
|
|
231
|
-
const position = new Vector3();
|
|
232
|
-
const rotation = new Euler();
|
|
233
|
-
const scale = new Vector3();
|
|
234
|
-
const uuid = first(Object.keys(this.instanceCollect));
|
|
235
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
236
|
-
if (instancedMesh) {
|
|
237
|
-
instancedMesh.getMatrixAt(index, InstancedMeshBase._matrix);
|
|
238
|
-
InstancedMeshBase._matrix.decompose(position, InstancedMeshBase._Quaternion, scale);
|
|
239
|
-
rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
240
|
-
}
|
|
241
|
-
return { position, rotation, scale };
|
|
242
|
-
}
|
|
243
|
-
// 设置颜色
|
|
244
|
-
async setColorAt(index, color) {
|
|
245
|
-
await this.awaitLoaded();
|
|
246
|
-
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
247
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
248
|
-
const collect = this.meshCollect[uuid];
|
|
249
|
-
collect.forEach((mesh, i) => {
|
|
250
|
-
const c = color(instancedMesh);
|
|
251
|
-
if (c) {
|
|
252
|
-
instancedMesh.setColorAt(collect.length * index + i, c);
|
|
253
|
-
instancedMesh.instanceColor.needsUpdate = true;
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
// 获取颜色
|
|
259
|
-
async getColorAt(index = 0) {
|
|
260
|
-
await this.awaitLoaded();
|
|
261
|
-
const color = new Color();
|
|
262
|
-
const uuid = first(Object.keys(this.instanceCollect));
|
|
263
|
-
const instancedMesh = this.instanceCollect[uuid];
|
|
264
|
-
if (instancedMesh) {
|
|
265
|
-
instancedMesh.getColorAt(index, color);
|
|
266
|
-
}
|
|
267
|
-
return color;
|
|
268
|
-
}
|
|
269
|
-
// 同步属性
|
|
270
|
-
updateMatrixWorld(force) {
|
|
271
|
-
super.updateMatrixWorld(force);
|
|
272
|
-
const { cursor, enableEvent } = this.userData;
|
|
273
|
-
this.traverse((obj) => {
|
|
274
|
-
Object.assign(obj.userData, { cursor, enableEvent });
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
// 事件监听
|
|
278
|
-
async addEventListener(name, callback) {
|
|
279
|
-
super.addEventListener(name, callback);
|
|
280
|
-
await this.awaitLoaded();
|
|
281
|
-
super.addEventListener(name, callback);
|
|
282
|
-
this.children.map((mesh) => {
|
|
283
|
-
mesh.addEventListener(name, callback);
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
// 取消事件的监听
|
|
287
|
-
removeEventListener(name, callback) {
|
|
288
|
-
super.removeEventListener(name, callback);
|
|
289
|
-
this.children.map((mesh) => {
|
|
290
|
-
mesh.removeEventListener(name, callback);
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
}
|
|
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
|
+
import { first, isString, throttle } from "lodash";
|
|
8
|
+
import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Quaternion, Vector3, } from "three";
|
|
9
|
+
import MeshBase from "./MeshBase";
|
|
10
|
+
import threeUtils from "./threeUtils";
|
|
11
|
+
import threeLoader from "./threeLoader";
|
|
12
|
+
// 实例模型组对象
|
|
13
|
+
export default class InstancedMeshBase extends MeshBase {
|
|
14
|
+
constructor(url) {
|
|
15
|
+
super(url);
|
|
16
|
+
}
|
|
17
|
+
isInstancedGroupMesh = true;
|
|
18
|
+
count = 0;
|
|
19
|
+
size = new Vector3(1, 1, 1);
|
|
20
|
+
meshCollect = {};
|
|
21
|
+
instanceCollect = {};
|
|
22
|
+
colorMap = new Map();
|
|
23
|
+
static _position = new Vector3();
|
|
24
|
+
static _scale = new Vector3(1, 1, 1);
|
|
25
|
+
static _rotation = new Euler();
|
|
26
|
+
static _Quaternion = new Quaternion();
|
|
27
|
+
static _matrix = new Matrix4();
|
|
28
|
+
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
29
|
+
instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
30
|
+
// 重置子元素
|
|
31
|
+
reset(...object) {
|
|
32
|
+
super.clear();
|
|
33
|
+
super.add(...object);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
// 重构加载模型的方法
|
|
37
|
+
async loadModel(url, filename = "") {
|
|
38
|
+
//获取模型文件的地址
|
|
39
|
+
const modelurl = await (async () => {
|
|
40
|
+
if (isString(url)) {
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
43
|
+
else if (url instanceof File) {
|
|
44
|
+
return url.name;
|
|
45
|
+
}
|
|
46
|
+
else if (url instanceof Array) {
|
|
47
|
+
let modelurl = "";
|
|
48
|
+
const suffixs = [
|
|
49
|
+
"zip",
|
|
50
|
+
"obj",
|
|
51
|
+
"glb",
|
|
52
|
+
"gltf",
|
|
53
|
+
"fbx",
|
|
54
|
+
"dae",
|
|
55
|
+
"stl",
|
|
56
|
+
"usdz",
|
|
57
|
+
];
|
|
58
|
+
for (let file of url) {
|
|
59
|
+
if (suffixs.includes(threeUtils.getSuffix(file.name))) {
|
|
60
|
+
modelurl = file.name;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return modelurl;
|
|
64
|
+
}
|
|
65
|
+
})();
|
|
66
|
+
this.url = modelurl || "";
|
|
67
|
+
if (!url) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.loaded = false;
|
|
71
|
+
// 没有模型并且没有加载任务时就去加载模型
|
|
72
|
+
if (!MeshBase.models.get(this.url) &&
|
|
73
|
+
!MeshBase.loadModeling.get(this.url)) {
|
|
74
|
+
MeshBase.loadModeling.set(this.url, true);
|
|
75
|
+
// 万能加载器
|
|
76
|
+
let modal = await threeLoader.loadModel(url, filename);
|
|
77
|
+
MeshBase.models.set(this.url, modal);
|
|
78
|
+
MeshBase.loadModeling.set(this.url, false);
|
|
79
|
+
}
|
|
80
|
+
// 如果模型还在加载中, 就等待模型加载完成
|
|
81
|
+
do {
|
|
82
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
83
|
+
} while (MeshBase.loadModeling.get(this.url));
|
|
84
|
+
if (MeshBase.models.get(this.url)) {
|
|
85
|
+
const mesh = MeshBase.models.get(this.url);
|
|
86
|
+
// // 判断是否有动画
|
|
87
|
+
// this.hasAnimation = mesh.animations.length > 0;
|
|
88
|
+
// if (this.hasAnimation) {
|
|
89
|
+
// const model = SkeletonUtils.clone(mesh);
|
|
90
|
+
// this.mixer = new AnimationMixer(model);
|
|
91
|
+
// this.animationActions = {};
|
|
92
|
+
// mesh.animations.map(animation => {
|
|
93
|
+
// const animationAction = this.mixer!.clipAction(animation).play();
|
|
94
|
+
// animationAction.weight = 0;
|
|
95
|
+
// this.animationActions[animation.name] = animationAction;
|
|
96
|
+
// });
|
|
97
|
+
// this.add(model);
|
|
98
|
+
// } else {
|
|
99
|
+
// this.add(mesh.clone(true));
|
|
100
|
+
// }
|
|
101
|
+
this.size.copy(threeUtils.getSize(mesh));
|
|
102
|
+
const { meshCollect, instanceCollect } = this;
|
|
103
|
+
mesh.updateMatrixWorld(true);
|
|
104
|
+
// collect all mesh counts, in case there are more than one copy of the same mesh in this group
|
|
105
|
+
mesh.traverse((obj) => {
|
|
106
|
+
const mesh = obj;
|
|
107
|
+
if (mesh.isMesh) {
|
|
108
|
+
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
109
|
+
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
110
|
+
mesh.updateMatrix();
|
|
111
|
+
// same geometry and same material consider as the same mesh
|
|
112
|
+
const uuid = mesh.geometry.uuid + "/" + mesh.material.uuid;
|
|
113
|
+
if (meshCollect[uuid]) {
|
|
114
|
+
meshCollect[uuid].push(mesh);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
meshCollect[uuid] = [mesh];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
obj.position.set(0, 0, 0);
|
|
122
|
+
obj.scale.set(1, 1, 1);
|
|
123
|
+
obj.rotation.set(0, 0, 0);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
this.clear();
|
|
127
|
+
// create all instances
|
|
128
|
+
Object.keys(meshCollect).forEach((uuid) => {
|
|
129
|
+
const meshes = meshCollect[uuid];
|
|
130
|
+
const mesh = meshes[0];
|
|
131
|
+
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
|
|
132
|
+
instancedMesh.name = mesh.name;
|
|
133
|
+
instancedMesh.frustumCulled = false;
|
|
134
|
+
instanceCollect[uuid] = instancedMesh;
|
|
135
|
+
this.add(instancedMesh);
|
|
136
|
+
});
|
|
137
|
+
// 加载完成事件
|
|
138
|
+
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
139
|
+
// 加载完成事件
|
|
140
|
+
this.loaded = true;
|
|
141
|
+
this.dispatchEvent({ type: "loaded" });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// 更新包围盒
|
|
145
|
+
computeBoundingSphere = throttle(() => {
|
|
146
|
+
this.children.map((instancedMesh) => {
|
|
147
|
+
instancedMesh.computeBoundingSphere && instancedMesh.computeBoundingSphere();
|
|
148
|
+
});
|
|
149
|
+
}, 1000);
|
|
150
|
+
//初始实例矩阵的数量
|
|
151
|
+
async setCount(count = 0) {
|
|
152
|
+
if (this.count === count) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
await this.awaitLoaded();
|
|
156
|
+
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
157
|
+
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
158
|
+
for (let i = 0; i < count; i++) {
|
|
159
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
160
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
161
|
+
}
|
|
162
|
+
for (let i = 0; i < this.count; i++) {
|
|
163
|
+
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
164
|
+
instanceColor.copyAt(i, this.instanceColor, i);
|
|
165
|
+
}
|
|
166
|
+
this.instanceMatrix = instanceMatrix;
|
|
167
|
+
this.instanceColor = instanceColor;
|
|
168
|
+
this.count = count;
|
|
169
|
+
this.children.map((instanceMesh) => {
|
|
170
|
+
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
171
|
+
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
172
|
+
for (let i = 0; i < count; i++) {
|
|
173
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
174
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
175
|
+
}
|
|
176
|
+
for (let i = 0; i < instanceMesh.count; i++) {
|
|
177
|
+
instanceMatrix.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
178
|
+
instanceColor.copyAt(i, instanceMesh.instanceColor, i);
|
|
179
|
+
}
|
|
180
|
+
instanceMesh.instanceMatrix = instanceMatrix;
|
|
181
|
+
instanceMesh.instanceColor = instanceColor;
|
|
182
|
+
instanceMesh.count = count;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// 设置矩阵
|
|
186
|
+
async setMatrixAt(index, matrix = new Matrix4().identity()) {
|
|
187
|
+
const new_matrix = matrix.clone(); // 把矩阵克隆一下避免对象引用问题
|
|
188
|
+
await this.awaitLoaded();
|
|
189
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
190
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
191
|
+
const collect = this.meshCollect[uuid];
|
|
192
|
+
collect.forEach((mesh, i) => {
|
|
193
|
+
InstancedMeshBase._matrix.copy(mesh.matrix);
|
|
194
|
+
InstancedMeshBase._matrix.premultiply(new_matrix);
|
|
195
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
196
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
this.computeBoundingSphere();
|
|
200
|
+
}
|
|
201
|
+
//设置移动旋转缩放
|
|
202
|
+
async setTRS(index, { position, rotation, scale, }) {
|
|
203
|
+
const new_position = position ? position.clone() : undefined;
|
|
204
|
+
const new_rotation = rotation ? rotation.clone() : undefined;
|
|
205
|
+
const new_scale = scale ? scale.clone() : undefined;
|
|
206
|
+
await this.awaitLoaded();
|
|
207
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
208
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
209
|
+
const collect = this.meshCollect[uuid];
|
|
210
|
+
collect.forEach((mesh, i) => {
|
|
211
|
+
//获取对应的模型的矩阵
|
|
212
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
213
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
214
|
+
InstancedMeshBase._matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
215
|
+
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
216
|
+
new_position && InstancedMeshBase._position.copy(new_position);
|
|
217
|
+
new_rotation && InstancedMeshBase._rotation.copy(new_rotation);
|
|
218
|
+
new_scale && InstancedMeshBase._scale.copy(new_scale);
|
|
219
|
+
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
220
|
+
InstancedMeshBase._matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
221
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
222
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
223
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
this.computeBoundingSphere();
|
|
227
|
+
}
|
|
228
|
+
//获取指定位置的移动旋转缩放
|
|
229
|
+
async getTRS(index = 0) {
|
|
230
|
+
await this.awaitLoaded();
|
|
231
|
+
const position = new Vector3();
|
|
232
|
+
const rotation = new Euler();
|
|
233
|
+
const scale = new Vector3();
|
|
234
|
+
const uuid = first(Object.keys(this.instanceCollect));
|
|
235
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
236
|
+
if (instancedMesh) {
|
|
237
|
+
instancedMesh.getMatrixAt(index, InstancedMeshBase._matrix);
|
|
238
|
+
InstancedMeshBase._matrix.decompose(position, InstancedMeshBase._Quaternion, scale);
|
|
239
|
+
rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
240
|
+
}
|
|
241
|
+
return { position, rotation, scale };
|
|
242
|
+
}
|
|
243
|
+
// 设置颜色
|
|
244
|
+
async setColorAt(index, color) {
|
|
245
|
+
await this.awaitLoaded();
|
|
246
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
247
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
248
|
+
const collect = this.meshCollect[uuid];
|
|
249
|
+
collect.forEach((mesh, i) => {
|
|
250
|
+
const c = color(instancedMesh);
|
|
251
|
+
if (c) {
|
|
252
|
+
instancedMesh.setColorAt(collect.length * index + i, c);
|
|
253
|
+
instancedMesh.instanceColor.needsUpdate = true;
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
// 获取颜色
|
|
259
|
+
async getColorAt(index = 0) {
|
|
260
|
+
await this.awaitLoaded();
|
|
261
|
+
const color = new Color();
|
|
262
|
+
const uuid = first(Object.keys(this.instanceCollect));
|
|
263
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
264
|
+
if (instancedMesh) {
|
|
265
|
+
instancedMesh.getColorAt(index, color);
|
|
266
|
+
}
|
|
267
|
+
return color;
|
|
268
|
+
}
|
|
269
|
+
// 同步属性
|
|
270
|
+
updateMatrixWorld(force) {
|
|
271
|
+
super.updateMatrixWorld(force);
|
|
272
|
+
const { cursor, enableEvent } = this.userData;
|
|
273
|
+
this.traverse((obj) => {
|
|
274
|
+
Object.assign(obj.userData, { cursor, enableEvent });
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// 事件监听
|
|
278
|
+
async addEventListener(name, callback) {
|
|
279
|
+
super.addEventListener(name, callback);
|
|
280
|
+
await this.awaitLoaded();
|
|
281
|
+
super.addEventListener(name, callback);
|
|
282
|
+
this.children.map((mesh) => {
|
|
283
|
+
mesh.addEventListener(name, callback);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
// 取消事件的监听
|
|
287
|
+
removeEventListener(name, callback) {
|
|
288
|
+
super.removeEventListener(name, callback);
|
|
289
|
+
this.children.map((mesh) => {
|
|
290
|
+
mesh.removeEventListener(name, callback);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
package/KeyFrameAnimation.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Euler, Line, LineBasicMaterial, Object3D, Vector3 } from "three";
|
|
2
|
-
export type Keyframe = {
|
|
3
|
-
position: Vector3;
|
|
4
|
-
scale: Vector3;
|
|
5
|
-
rotation: Euler;
|
|
6
|
-
timeTemp: number;
|
|
7
|
-
};
|
|
8
|
-
export type KeyframeAnimationProps = {
|
|
9
|
-
speedScale?: number;
|
|
10
|
-
object3d: Object3D;
|
|
11
|
-
keyframes: Array<Keyframe>;
|
|
12
|
-
};
|
|
13
|
-
export default class KeyframeAnimation extends Object3D {
|
|
14
|
-
constructor(props: KeyframeAnimationProps);
|
|
15
|
-
speedScale: number;
|
|
16
|
-
object3d: Object3D;
|
|
17
|
-
halperLine: Line<any, LineBasicMaterial, import("three").Object3DEventMap>;
|
|
18
|
-
keyframes: Array<Keyframe>;
|
|
19
|
-
setObject3D(object3d: Object3D): void;
|
|
20
|
-
getKeyframe(i: number): Keyframe;
|
|
21
|
-
setKeyframe(i: number, keyframe: Keyframe): void;
|
|
22
|
-
setKeyframes(keyframes: Array<Keyframe>): void;
|
|
23
|
-
update(): void;
|
|
24
|
-
updateMatrix(): void;
|
|
25
|
-
}
|
|
1
|
+
import { Euler, Line, LineBasicMaterial, Object3D, Vector3 } from "three";
|
|
2
|
+
export type Keyframe = {
|
|
3
|
+
position: Vector3;
|
|
4
|
+
scale: Vector3;
|
|
5
|
+
rotation: Euler;
|
|
6
|
+
timeTemp: number;
|
|
7
|
+
};
|
|
8
|
+
export type KeyframeAnimationProps = {
|
|
9
|
+
speedScale?: number;
|
|
10
|
+
object3d: Object3D;
|
|
11
|
+
keyframes: Array<Keyframe>;
|
|
12
|
+
};
|
|
13
|
+
export default class KeyframeAnimation extends Object3D {
|
|
14
|
+
constructor(props: KeyframeAnimationProps);
|
|
15
|
+
speedScale: number;
|
|
16
|
+
object3d: Object3D;
|
|
17
|
+
halperLine: Line<any, LineBasicMaterial, import("three").Object3DEventMap>;
|
|
18
|
+
keyframes: Array<Keyframe>;
|
|
19
|
+
setObject3D(object3d: Object3D): void;
|
|
20
|
+
getKeyframe(i: number): Keyframe;
|
|
21
|
+
setKeyframe(i: number, keyframe: Keyframe): void;
|
|
22
|
+
setKeyframes(keyframes: Array<Keyframe>): void;
|
|
23
|
+
update(): void;
|
|
24
|
+
updateMatrix(): void;
|
|
25
|
+
}
|