three-base 2.0.68 → 2.0.69
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 +10 -1
- package/InstancedMeshBase.js +29 -2
- package/InstancedMeshBase.ts +57 -2
- package/package.json +1 -1
package/InstancedMeshBase.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Color, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Vector3 } from "three";
|
|
1
|
+
import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Quaternion, Vector3 } from "three";
|
|
2
2
|
import MeshBase from "./MeshBase";
|
|
3
3
|
export default class InstancedMeshBase extends MeshBase {
|
|
4
4
|
constructor(url?: string);
|
|
@@ -10,6 +10,10 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
10
10
|
instanceCollect: Record<string, InstancedMesh>;
|
|
11
11
|
colorMap: Map<String, Color>;
|
|
12
12
|
children: InstancedMesh[];
|
|
13
|
+
static _position: Vector3;
|
|
14
|
+
static _scale: Vector3;
|
|
15
|
+
static _rotation: Euler;
|
|
16
|
+
static _Quaternion: Quaternion;
|
|
13
17
|
static matrix: Matrix4;
|
|
14
18
|
instanceMatrix: InstancedBufferAttribute;
|
|
15
19
|
instanceColor: InstancedBufferAttribute;
|
|
@@ -17,6 +21,11 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
17
21
|
computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
|
|
18
22
|
setCount(count?: number): void;
|
|
19
23
|
setMatrixAt(index: number, matrix: Matrix4): void;
|
|
24
|
+
setTRS(index: number, { position, rotation, scale }: {
|
|
25
|
+
position?: Vector3;
|
|
26
|
+
rotation?: Euler;
|
|
27
|
+
scale?: Vector3;
|
|
28
|
+
}): void;
|
|
20
29
|
setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): void;
|
|
21
30
|
updateMatrixWorld(force?: boolean): void;
|
|
22
31
|
addEventListener(name: string, callback: (e: any) => void): void;
|
package/InstancedMeshBase.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @Last Modified time: 2024-06-07 22:14:53
|
|
6
6
|
*/
|
|
7
7
|
import { throttle } from "lodash";
|
|
8
|
-
import { InstancedBufferAttribute, InstancedMesh, Matrix4, Vector3 } from "three";
|
|
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";
|
|
@@ -21,6 +21,10 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
21
21
|
meshCollect = {};
|
|
22
22
|
instanceCollect = {};
|
|
23
23
|
colorMap = new Map();
|
|
24
|
+
static _position = new Vector3();
|
|
25
|
+
static _scale = new Vector3(1, 1, 1);
|
|
26
|
+
static _rotation = new Euler();
|
|
27
|
+
static _Quaternion = new Quaternion();
|
|
24
28
|
static matrix = new Matrix4();
|
|
25
29
|
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
26
30
|
instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
@@ -30,6 +34,7 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
30
34
|
this.clear();
|
|
31
35
|
return;
|
|
32
36
|
}
|
|
37
|
+
this.url = url;
|
|
33
38
|
this.loaded = false;
|
|
34
39
|
// 没有模型并且没有加载任务时就去加载模型
|
|
35
40
|
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
@@ -37,7 +42,6 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
37
42
|
// 万能加载器
|
|
38
43
|
let modal = await threeLoader.loadModel(url, filename);
|
|
39
44
|
threeUtils.center(modal);
|
|
40
|
-
threeUtils.alignBottom(modal);
|
|
41
45
|
modal.updateMatrixWorld(true);
|
|
42
46
|
MeshBase.models.set(url, modal);
|
|
43
47
|
MeshBase.loadModeling.set(url, false);
|
|
@@ -151,6 +155,29 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
151
155
|
});
|
|
152
156
|
this.computeBoundingSphere();
|
|
153
157
|
}
|
|
158
|
+
//设置移动旋转缩放
|
|
159
|
+
setTRS(index, { position = InstancedMeshBase._position, rotation = InstancedMeshBase._rotation, scale = InstancedMeshBase._scale }) {
|
|
160
|
+
Object.keys(this.meshCollect).forEach(uuid => {
|
|
161
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
162
|
+
const collect = this.meshCollect[uuid];
|
|
163
|
+
collect.forEach((mesh, i) => {
|
|
164
|
+
//获取对应的模型的矩阵
|
|
165
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
|
|
166
|
+
InstancedMeshBase.matrix.premultiply(mesh.matrix.clone().invert());
|
|
167
|
+
InstancedMeshBase.matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
168
|
+
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
169
|
+
InstancedMeshBase._position.copy(position);
|
|
170
|
+
InstancedMeshBase._rotation.copy(rotation);
|
|
171
|
+
InstancedMeshBase._scale.copy(scale);
|
|
172
|
+
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
173
|
+
InstancedMeshBase.matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
174
|
+
InstancedMeshBase.matrix.premultiply(mesh.matrix);
|
|
175
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
|
|
176
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
this.computeBoundingSphere();
|
|
180
|
+
}
|
|
154
181
|
// 设置颜色
|
|
155
182
|
setColorAt(index, color) {
|
|
156
183
|
Object.keys(this.meshCollect).forEach(uuid => {
|
package/InstancedMeshBase.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import { throttle } from "lodash";
|
|
9
|
-
import { Color, InstancedBufferAttribute, InstancedMesh, Material, Matrix4, Mesh, Object3D, Vector3 } from "three";
|
|
9
|
+
import { Color, Euler, InstancedBufferAttribute, InstancedMesh, Material, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from "three";
|
|
10
10
|
import MeshBase from "./MeshBase";
|
|
11
11
|
import threeUtils from "./threeUtils";
|
|
12
12
|
import threeLoader from "./threeLoader";
|
|
@@ -33,6 +33,10 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
33
33
|
|
|
34
34
|
declare children: InstancedMesh[];
|
|
35
35
|
|
|
36
|
+
static _position: Vector3 = new Vector3();
|
|
37
|
+
static _scale: Vector3 = new Vector3(1, 1, 1);
|
|
38
|
+
static _rotation: Euler = new Euler();
|
|
39
|
+
static _Quaternion: Quaternion = new Quaternion();
|
|
36
40
|
static matrix = new Matrix4()
|
|
37
41
|
|
|
38
42
|
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
@@ -46,6 +50,8 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
46
50
|
return;
|
|
47
51
|
}
|
|
48
52
|
|
|
53
|
+
this.url = url;
|
|
54
|
+
|
|
49
55
|
this.loaded = false;
|
|
50
56
|
|
|
51
57
|
// 没有模型并且没有加载任务时就去加载模型
|
|
@@ -56,7 +62,6 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
56
62
|
let modal = await threeLoader.loadModel(url, filename);
|
|
57
63
|
|
|
58
64
|
threeUtils.center(modal);
|
|
59
|
-
threeUtils.alignBottom(modal);
|
|
60
65
|
modal.updateMatrixWorld(true);
|
|
61
66
|
|
|
62
67
|
MeshBase.models.set(url, modal);
|
|
@@ -201,6 +206,56 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
201
206
|
this.computeBoundingSphere();
|
|
202
207
|
}
|
|
203
208
|
|
|
209
|
+
//设置移动旋转缩放
|
|
210
|
+
setTRS(
|
|
211
|
+
index: number,
|
|
212
|
+
{
|
|
213
|
+
position = InstancedMeshBase._position,
|
|
214
|
+
rotation = InstancedMeshBase._rotation,
|
|
215
|
+
scale = InstancedMeshBase._scale
|
|
216
|
+
}: {
|
|
217
|
+
position?: Vector3;
|
|
218
|
+
rotation?: Euler;
|
|
219
|
+
scale?: Vector3
|
|
220
|
+
}
|
|
221
|
+
) {
|
|
222
|
+
Object.keys(this.meshCollect).forEach(uuid => {
|
|
223
|
+
const instancedMesh = this.instanceCollect[uuid]
|
|
224
|
+
const collect = this.meshCollect[uuid]
|
|
225
|
+
collect.forEach((mesh: Mesh, i: number) => {
|
|
226
|
+
|
|
227
|
+
//获取对应的模型的矩阵
|
|
228
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
|
|
229
|
+
|
|
230
|
+
InstancedMeshBase.matrix.premultiply(mesh.matrix.clone().invert());
|
|
231
|
+
|
|
232
|
+
InstancedMeshBase.matrix.decompose(
|
|
233
|
+
InstancedMeshBase._position,
|
|
234
|
+
InstancedMeshBase._Quaternion,
|
|
235
|
+
InstancedMeshBase._scale
|
|
236
|
+
);
|
|
237
|
+
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
238
|
+
InstancedMeshBase._position.copy(position);
|
|
239
|
+
InstancedMeshBase._rotation.copy(rotation);
|
|
240
|
+
InstancedMeshBase._scale.copy(scale);
|
|
241
|
+
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
242
|
+
|
|
243
|
+
InstancedMeshBase.matrix.compose(
|
|
244
|
+
InstancedMeshBase._position,
|
|
245
|
+
InstancedMeshBase._Quaternion,
|
|
246
|
+
InstancedMeshBase._scale
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
InstancedMeshBase.matrix.premultiply(mesh.matrix);
|
|
250
|
+
|
|
251
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
|
|
252
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
253
|
+
})
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
this.computeBoundingSphere();
|
|
257
|
+
}
|
|
258
|
+
|
|
204
259
|
// 设置颜色
|
|
205
260
|
setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
|
|
206
261
|
Object.keys(this.meshCollect).forEach(uuid => {
|