three-base 1.0.14 → 1.0.15

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/index.ts CHANGED
@@ -14,6 +14,9 @@ export { default as KeyFrameAnimation } from "./lib/KeyFrameAnimation";
14
14
  export type { KeyframeAnimationProps } from "./lib/KeyFrameAnimation";
15
15
  export type { Keyframe } from "./lib/KeyFrameAnimation";
16
16
 
17
+ //实例模型组对象
18
+ export { default as InstancedGroupMesh } from "./lib/InstancedGroupMesh";
19
+
17
20
  //点光源
18
21
  export { default as PointLight } from "./lib/PointLight";
19
22
 
@@ -0,0 +1,71 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-02-21 09:55:52
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-02-21 10:12:33
6
+ */
7
+
8
+ import * as THREE from 'three'
9
+
10
+ // 模型组转成 实例对象
11
+ export default class InstancedGroupMesh extends THREE.Mesh {
12
+ isInstancedGroupMesh = true
13
+
14
+ count: number = 0
15
+
16
+ meshCollect: Record<string, THREE.Mesh[]> = {}
17
+
18
+ instanceCollect: Record<string, THREE.InstancedMesh> = {}
19
+
20
+ static matrix = new THREE.Matrix4()
21
+
22
+ constructor(group: THREE.Group, count: number) {
23
+ super()
24
+
25
+ this.count = count
26
+
27
+ const { meshCollect, instanceCollect } = this
28
+
29
+ // collect all mesh counts, in case there are more than one copy of the same mesh in this group
30
+ group.traverse((obj) => {
31
+ if (obj.type !== 'Mesh') return
32
+ const mesh = obj as THREE.Mesh
33
+
34
+ // this is important
35
+ mesh.updateMatrix()
36
+
37
+ // same geometry and same material consider as the same mesh
38
+ const uuid = mesh.geometry.uuid + '/' + (mesh.material as THREE.Material).uuid
39
+
40
+ if (meshCollect[uuid]) {
41
+ meshCollect[uuid].push(mesh)
42
+ } else {
43
+ meshCollect[uuid] = [mesh]
44
+ }
45
+ })
46
+
47
+ // create all instances
48
+ Object.keys(meshCollect).forEach(uuid => {
49
+ const meshes = meshCollect[uuid]
50
+ const mesh = meshes[0]
51
+
52
+ const instancedMesh = new THREE.InstancedMesh(mesh.geometry, mesh.material, meshes.length * count)
53
+ instanceCollect[uuid] = instancedMesh
54
+ this.add(instancedMesh)
55
+ })
56
+ }
57
+
58
+ setMatrixAt(index: number, matrix: THREE.Matrix4) {
59
+ Object.keys(this.meshCollect).forEach(uuid => {
60
+ const instancedMesh = this.instanceCollect[uuid]
61
+ const collect = this.meshCollect[uuid]
62
+ collect.forEach((mesh: THREE.Mesh, i: number) => {
63
+ InstancedGroupMesh.matrix.copy(mesh.matrix)
64
+ InstancedGroupMesh.matrix.premultiply(matrix)
65
+
66
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedGroupMesh.matrix);
67
+ instancedMesh.instanceMatrix.needsUpdate = true;
68
+ })
69
+ })
70
+ }
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "three 3d three-base",
5
5
  "keywords": [
6
6
  "three",