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