three-base 2.0.41 → 2.0.43

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.
@@ -1,13 +1,21 @@
1
- import { Color, Group, InstancedMesh, Matrix4, Mesh } from 'three';
2
- export default class InstancedGroupMesh extends Mesh {
3
- children: InstancedMesh[];
1
+ import { Color, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Vector3 } from "three";
2
+ import MeshBase from "./MeshBase";
3
+ export default class InstancedMeshBase extends MeshBase {
4
+ constructor(url?: string);
5
+ url: string;
4
6
  isInstancedGroupMesh: boolean;
5
7
  count: number;
8
+ size: Vector3;
6
9
  meshCollect: Record<string, Mesh[]>;
7
10
  instanceCollect: Record<string, InstancedMesh>;
11
+ colorMap: Map<String, Color>;
12
+ children: InstancedMesh[];
8
13
  static matrix: Matrix4;
9
- constructor(group: Group, count: number);
14
+ instanceMatrix: InstancedBufferAttribute;
15
+ instanceColor: InstancedBufferAttribute;
16
+ loadModel(url: string): Promise<void>;
10
17
  computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
18
+ setCount(count?: number): void;
11
19
  setMatrixAt(index: number, matrix: Matrix4): void;
12
20
  setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): void;
13
21
  updateMatrixWorld(force?: boolean): void;
@@ -0,0 +1,205 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2024-02-05 09:31:57
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2024-02-05 09:40:12
6
+ */
7
+ import { throttle } from "lodash";
8
+ import { InstancedBufferAttribute, InstancedMesh, Matrix4, Object3D, 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
+ url = "";
18
+ isInstancedGroupMesh = true;
19
+ count = 0;
20
+ size = new Vector3(1, 1, 1);
21
+ meshCollect = {};
22
+ instanceCollect = {};
23
+ colorMap = new Map();
24
+ static matrix = new Matrix4();
25
+ instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
26
+ instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
27
+ // 重构加载模型的方法
28
+ async loadModel(url) {
29
+ if (!url) {
30
+ this.clear();
31
+ return;
32
+ }
33
+ this.loaded = false;
34
+ // 没有模型并且没有加载任务时就去加载模型
35
+ if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
36
+ MeshBase.loadModeling.set(url, true);
37
+ let modal = new Object3D();
38
+ if (/\.fbx$/i.test(url)) {
39
+ await threeLoader.fbxLoader.loadAsync(url).then(res => {
40
+ modal = res;
41
+ }).catch(() => {
42
+ console.error(`${url},加载失败`);
43
+ });
44
+ }
45
+ else if (/\.(gltf|glb)$/i.test(url)) {
46
+ await threeLoader.gltfLoader.loadAsync(url).then(res => {
47
+ modal = res.scene;
48
+ modal.animations = res.animations;
49
+ }).catch(() => {
50
+ console.error(`${url},加载失败`);
51
+ });
52
+ }
53
+ else {
54
+ console.error(`${url},不支持的格式`);
55
+ }
56
+ threeUtils.center(modal);
57
+ threeUtils.alignBottom(modal);
58
+ modal.updateMatrixWorld(true);
59
+ MeshBase.models.set(url, modal);
60
+ MeshBase.loadModeling.set(url, false);
61
+ }
62
+ // 如果模型还在加载中, 就等待模型加载完成
63
+ do {
64
+ await new Promise(resolve => setTimeout(resolve, 50));
65
+ } while (MeshBase.loadModeling.get(url));
66
+ if (MeshBase.models.get(url)) {
67
+ this.clear();
68
+ const mesh = MeshBase.models.get(url);
69
+ // // 判断是否有动画
70
+ // this.hasAnimation = mesh.animations.length > 0;
71
+ // if (this.hasAnimation) {
72
+ // const model = SkeletonUtils.clone(mesh);
73
+ // this.mixer = new AnimationMixer(model);
74
+ // this.animationActions = {};
75
+ // mesh.animations.map(animation => {
76
+ // const animationAction = this.mixer!.clipAction(animation).play();
77
+ // animationAction.weight = 0;
78
+ // this.animationActions[animation.name] = animationAction;
79
+ // });
80
+ // this.add(model);
81
+ // } else {
82
+ // this.add(mesh.clone(true));
83
+ // }
84
+ this.size.copy(threeUtils.getSize(mesh));
85
+ const { meshCollect, instanceCollect } = this;
86
+ mesh.updateMatrixWorld(true);
87
+ // collect all mesh counts, in case there are more than one copy of the same mesh in this group
88
+ mesh.traverse((obj) => {
89
+ const mesh = obj;
90
+ if (mesh.isMesh) {
91
+ mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
92
+ mesh.rotation.setFromQuaternion(mesh.quaternion);
93
+ mesh.updateMatrix();
94
+ // same geometry and same material consider as the same mesh
95
+ const uuid = mesh.geometry.uuid + '/' + mesh.material.uuid;
96
+ if (meshCollect[uuid]) {
97
+ meshCollect[uuid].push(mesh);
98
+ }
99
+ else {
100
+ meshCollect[uuid] = [mesh];
101
+ }
102
+ }
103
+ else {
104
+ obj.position.set(0, 0, 0);
105
+ obj.scale.set(1, 1, 1);
106
+ obj.rotation.set(0, 0, 0);
107
+ }
108
+ });
109
+ // create all instances
110
+ Object.keys(meshCollect).forEach(uuid => {
111
+ const meshes = meshCollect[uuid];
112
+ const mesh = meshes[0];
113
+ const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
114
+ instancedMesh.name = mesh.name;
115
+ instancedMesh.frustumCulled = false;
116
+ instanceCollect[uuid] = instancedMesh;
117
+ this.add(instancedMesh);
118
+ });
119
+ // 加载完成事件
120
+ await new Promise(resolve => setTimeout(resolve, 100));
121
+ // 加载完成事件
122
+ this.dispatchEvent({ type: "loaded" });
123
+ this.loaded = true;
124
+ }
125
+ }
126
+ // 更新包围盒
127
+ computeBoundingSphere = throttle(() => {
128
+ this.traverse(m => {
129
+ m.computeBoundingSphere();
130
+ });
131
+ }, 1000);
132
+ //初始实例矩阵的数量
133
+ setCount(count = 0) {
134
+ const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
135
+ const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
136
+ for (let i = 0; i < this.count; i++) {
137
+ instanceMatrix.copyAt(i, this.instanceMatrix, i);
138
+ instanceColor.copyAt(i, this.instanceColor, i);
139
+ }
140
+ this.instanceMatrix = instanceMatrix;
141
+ this.instanceColor = instanceColor;
142
+ this.count = count;
143
+ this.children.map((instanceMesh) => {
144
+ const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
145
+ const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
146
+ for (let i = 0; i < instanceMesh.count; i++) {
147
+ instanceMatrix.copyAt(i, this.instanceMatrix, i);
148
+ instanceColor.copyAt(i, this.instanceColor, i);
149
+ }
150
+ instanceMesh.instanceMatrix = instanceMatrix;
151
+ instanceMesh.instanceColor = instanceColor;
152
+ instanceMesh.count = count;
153
+ });
154
+ }
155
+ // 设置矩阵
156
+ setMatrixAt(index, matrix) {
157
+ Object.keys(this.meshCollect).forEach(uuid => {
158
+ const instancedMesh = this.instanceCollect[uuid];
159
+ const collect = this.meshCollect[uuid];
160
+ collect.forEach((mesh, i) => {
161
+ InstancedMeshBase.matrix.copy(mesh.matrix);
162
+ InstancedMeshBase.matrix.premultiply(matrix);
163
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
164
+ instancedMesh.instanceMatrix.needsUpdate = true;
165
+ });
166
+ });
167
+ this.computeBoundingSphere();
168
+ }
169
+ // 设置颜色
170
+ setColorAt(index, color) {
171
+ Object.keys(this.meshCollect).forEach(uuid => {
172
+ const instancedMesh = this.instanceCollect[uuid];
173
+ const collect = this.meshCollect[uuid];
174
+ collect.forEach((mesh, i) => {
175
+ const c = color(instancedMesh);
176
+ if (c) {
177
+ instancedMesh.setColorAt(collect.length * index + i, c);
178
+ instancedMesh.instanceColor.needsUpdate = true;
179
+ }
180
+ });
181
+ });
182
+ }
183
+ // 同步属性
184
+ updateMatrixWorld(force) {
185
+ super.updateMatrixWorld(force);
186
+ const { cursor, enableEvent } = this.userData;
187
+ this.traverse(obj => {
188
+ Object.assign(obj.userData, { cursor, enableEvent });
189
+ });
190
+ }
191
+ // 事件监听
192
+ addEventListener(name, callback) {
193
+ super.addEventListener(name, callback);
194
+ this.children.map(mesh => {
195
+ mesh.addEventListener(name, callback);
196
+ });
197
+ }
198
+ // 取消事件的监听
199
+ removeEventListener(name, callback) {
200
+ super.removeEventListener(name, callback);
201
+ this.children.map(mesh => {
202
+ mesh.removeEventListener(name, callback);
203
+ });
204
+ }
205
+ }
@@ -0,0 +1,259 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2024-02-05 09:31:57
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2024-02-05 09:40:12
6
+ */
7
+
8
+ import { throttle } from "lodash";
9
+ import { Color, InstancedBufferAttribute, InstancedMesh, Material, Matrix4, Mesh, Object3D, 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
+ url = "";
21
+
22
+ isInstancedGroupMesh = true
23
+
24
+ count: number = 0;
25
+
26
+ size = new Vector3(1, 1, 1);
27
+
28
+ meshCollect: Record<string, Mesh[]> = {}
29
+
30
+ instanceCollect: Record<string, InstancedMesh> = {}
31
+
32
+ colorMap = new Map<String, Color>();
33
+
34
+ declare children: InstancedMesh[];
35
+
36
+ static matrix = new Matrix4()
37
+
38
+ instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
39
+
40
+ instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
41
+
42
+ // 重构加载模型的方法
43
+ async loadModel(url: string) {
44
+ if (!url) {
45
+ this.clear();
46
+ return;
47
+ }
48
+
49
+ this.loaded = false;
50
+
51
+ // 没有模型并且没有加载任务时就去加载模型
52
+ if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
53
+ MeshBase.loadModeling.set(url, true);
54
+
55
+ let modal = new Object3D();
56
+
57
+ if (/\.fbx$/i.test(url)) {
58
+ await threeLoader.fbxLoader.loadAsync(url).then(res => {
59
+ modal = res;
60
+ }).catch(() => {
61
+ console.error(`${url},加载失败`);
62
+ })
63
+ } else if (/\.(gltf|glb)$/i.test(url)) {
64
+ await threeLoader.gltfLoader.loadAsync(url).then(res => {
65
+ modal = res.scene;
66
+ modal.animations = res.animations;
67
+ }).catch(() => {
68
+ console.error(`${url},加载失败`);
69
+ })
70
+ } else {
71
+ console.error(`${url},不支持的格式`);
72
+ }
73
+
74
+ threeUtils.center(modal);
75
+ threeUtils.alignBottom(modal);
76
+ modal.updateMatrixWorld(true);
77
+
78
+ MeshBase.models.set(url, modal);
79
+ MeshBase.loadModeling.set(url, false);
80
+ }
81
+
82
+ // 如果模型还在加载中, 就等待模型加载完成
83
+ do {
84
+ await new Promise(resolve => setTimeout(resolve, 50));
85
+ } while (MeshBase.loadModeling.get(url));
86
+
87
+ if (MeshBase.models.get(url)) {
88
+
89
+ this.clear();
90
+ const mesh = MeshBase.models.get(url)!;
91
+ // // 判断是否有动画
92
+ // this.hasAnimation = mesh.animations.length > 0;
93
+
94
+ // if (this.hasAnimation) {
95
+ // const model = SkeletonUtils.clone(mesh);
96
+ // this.mixer = new AnimationMixer(model);
97
+
98
+ // this.animationActions = {};
99
+ // mesh.animations.map(animation => {
100
+ // const animationAction = this.mixer!.clipAction(animation).play();
101
+ // animationAction.weight = 0;
102
+ // this.animationActions[animation.name] = animationAction;
103
+ // });
104
+
105
+ // this.add(model);
106
+ // } else {
107
+ // this.add(mesh.clone(true));
108
+ // }
109
+
110
+ this.size.copy(threeUtils.getSize(mesh));
111
+
112
+ const { meshCollect, instanceCollect } = this
113
+
114
+ mesh.updateMatrixWorld(true);
115
+
116
+ // collect all mesh counts, in case there are more than one copy of the same mesh in this group
117
+ mesh.traverse((obj) => {
118
+ const mesh = obj as Mesh
119
+ if (mesh.isMesh) {
120
+
121
+ mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
122
+ mesh.rotation.setFromQuaternion(mesh.quaternion);
123
+ mesh.updateMatrix();
124
+
125
+ // same geometry and same material consider as the same mesh
126
+ const uuid = mesh.geometry.uuid + '/' + (mesh.material as Material).uuid
127
+
128
+ if (meshCollect[uuid]) {
129
+ meshCollect[uuid].push(mesh)
130
+ } else {
131
+ meshCollect[uuid] = [mesh]
132
+ }
133
+ } else {
134
+ obj.position.set(0, 0, 0);
135
+ obj.scale.set(1, 1, 1);
136
+ obj.rotation.set(0, 0, 0);
137
+ }
138
+
139
+ })
140
+
141
+ // create all instances
142
+ Object.keys(meshCollect).forEach(uuid => {
143
+ const meshes = meshCollect[uuid]
144
+ const mesh = meshes[0]
145
+ const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count)
146
+ instancedMesh.name = mesh.name;
147
+ instancedMesh.frustumCulled = false;
148
+ instanceCollect[uuid] = instancedMesh
149
+ this.add(instancedMesh)
150
+ })
151
+
152
+ // 加载完成事件
153
+ await new Promise(resolve => setTimeout(resolve, 100));
154
+ // 加载完成事件
155
+ this.dispatchEvent({ type: "loaded" } as never);
156
+ this.loaded = true;
157
+ }
158
+
159
+ }
160
+
161
+
162
+ // 更新包围盒
163
+ computeBoundingSphere = throttle(() => {
164
+ this.traverse(m => {
165
+ (m as InstancedMesh).computeBoundingSphere();
166
+ })
167
+ }, 1000)
168
+
169
+ //初始实例矩阵的数量
170
+ setCount(count: number = 0) {
171
+
172
+
173
+ const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
174
+ const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
175
+
176
+ for (let i = 0; i < this.count; i++) {
177
+ instanceMatrix.copyAt(i, this.instanceMatrix, i);
178
+ instanceColor.copyAt(i, this.instanceColor, i);
179
+ }
180
+
181
+ this.instanceMatrix = instanceMatrix;
182
+ this.instanceColor = instanceColor;
183
+ this.count = count;
184
+
185
+ this.children.map((instanceMesh: InstancedMesh) => {
186
+
187
+ const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
188
+ const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
189
+
190
+ for (let i = 0; i < instanceMesh.count; i++) {
191
+ instanceMatrix.copyAt(i, this.instanceMatrix, i);
192
+ instanceColor.copyAt(i, this.instanceColor, i);
193
+ }
194
+
195
+ instanceMesh.instanceMatrix = instanceMatrix;
196
+ instanceMesh.instanceColor = instanceColor;
197
+ instanceMesh.count = count;
198
+ });
199
+
200
+ }
201
+
202
+ // 设置矩阵
203
+ setMatrixAt(index: number, matrix: Matrix4) {
204
+ Object.keys(this.meshCollect).forEach(uuid => {
205
+ const instancedMesh = this.instanceCollect[uuid]
206
+ const collect = this.meshCollect[uuid]
207
+ collect.forEach((mesh: Mesh, i: number) => {
208
+ InstancedMeshBase.matrix.copy(mesh.matrix)
209
+ InstancedMeshBase.matrix.premultiply(matrix)
210
+
211
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.matrix);
212
+ instancedMesh.instanceMatrix.needsUpdate = true;
213
+ })
214
+ })
215
+
216
+ this.computeBoundingSphere();
217
+ }
218
+
219
+ // 设置颜色
220
+ setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
221
+ Object.keys(this.meshCollect).forEach(uuid => {
222
+ const instancedMesh = this.instanceCollect[uuid]
223
+ const collect = this.meshCollect[uuid]
224
+ collect.forEach((mesh: Mesh, i: number) => {
225
+ const c = color(instancedMesh);
226
+ if (c) {
227
+ instancedMesh.setColorAt(collect.length * index + i, c);
228
+ instancedMesh.instanceColor!.needsUpdate = true;
229
+ }
230
+ })
231
+ })
232
+ }
233
+
234
+ // 同步属性
235
+ updateMatrixWorld(force?: boolean): void {
236
+ super.updateMatrixWorld(force);
237
+ const { cursor, enableEvent } = this.userData;
238
+ this.traverse(obj => {
239
+ Object.assign(obj.userData, { cursor, enableEvent });
240
+ });
241
+ }
242
+
243
+ // 事件监听
244
+ addEventListener(name: string, callback: (e: any) => void) {
245
+ super.addEventListener(name, callback);
246
+ this.children.map(mesh => {
247
+ mesh.addEventListener(name, callback);
248
+ });
249
+ }
250
+
251
+ // 取消事件的监听
252
+ removeEventListener(name: string, callback: (e: any) => void) {
253
+ super.removeEventListener(name, callback);
254
+ this.children.map(mesh => {
255
+ mesh.removeEventListener(name, callback);
256
+ });
257
+ }
258
+
259
+ }
package/ThreeBase.d.ts CHANGED
@@ -52,7 +52,7 @@ export default class ThreeBase extends EventDispatcher {
52
52
  needRender: boolean;
53
53
  stopRender(): void;
54
54
  hasStrokeOrPass(): boolean;
55
- render(): boolean;
55
+ render(): boolean | undefined;
56
56
  private renderBindThis;
57
57
  download(fileName?: string): void;
58
58
  private _preMesh;
package/ThreeBase.js CHANGED
@@ -355,7 +355,7 @@ export default class ThreeBase extends EventDispatcher {
355
355
  // canvas 没有在页面上面的话 就停止渲染
356
356
  if (!threeUtils.inDocument(this.renderer.domElement)) {
357
357
  this.stopRender();
358
- return;
358
+ return false;
359
359
  }
360
360
  //透视相机和正交相机参数的时时转换
361
361
  if (this.cameraType === "perspectiveCamera") {
@@ -534,9 +534,10 @@ export default class ThreeBase extends EventDispatcher {
534
534
  this.renderTargetgaussBlur(renderTarget, 1, 1);
535
535
  this.renderTargetgaussBlur(renderTarget, 1, 1);
536
536
  }
537
- // 需要做康锯齿处理 渲染 渲染目标
537
+ // 需要做抗锯齿处理 渲染 渲染目标
538
538
  renderRendererTarget(renderTarget, alpha = false) {
539
539
  FXAAShaderMaterial.uniforms.tDiffuse.value = renderTarget.texture;
540
+ FXAAShaderMaterial.uniforms.resolution.value.set(1 / this.size.x, 1 / this.size.y);
540
541
  fullScreenMesh.material = FXAAShaderMaterial;
541
542
  fullScreenMesh.render(this.renderer);
542
543
  // const FXAAShaderMaterial = new ShaderMaterial({
package/ThreeBase.ts CHANGED
@@ -424,11 +424,11 @@ export default class ThreeBase extends EventDispatcher {
424
424
  }
425
425
 
426
426
  // 渲染场景
427
- render() {
427
+ render(): boolean | undefined {
428
428
  // canvas 没有在页面上面的话 就停止渲染
429
429
  if (!threeUtils.inDocument(this.renderer.domElement)) {
430
430
  this.stopRender();
431
- return;
431
+ return false;
432
432
  }
433
433
 
434
434
  //透视相机和正交相机参数的时时转换
@@ -642,10 +642,11 @@ export default class ThreeBase extends EventDispatcher {
642
642
  this.renderTargetgaussBlur(renderTarget, 1, 1);
643
643
  }
644
644
 
645
- // 需要做康锯齿处理 渲染 渲染目标
645
+ // 需要做抗锯齿处理 渲染 渲染目标
646
646
  renderRendererTarget(renderTarget: WebGLRenderTarget, alpha: boolean = false) {
647
647
 
648
648
  FXAAShaderMaterial.uniforms.tDiffuse.value = renderTarget.texture;
649
+ FXAAShaderMaterial.uniforms.resolution.value.set(1 / this.size.x, 1 / this.size.y);
649
650
  fullScreenMesh.material = FXAAShaderMaterial;
650
651
  fullScreenMesh.render(this.renderer);
651
652
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.41",
3
+ "version": "2.0.43",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -1,106 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-02-21 09:55:52
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-27 17:41:26
6
- */
7
- import { throttle } from 'lodash';
8
- import { InstancedMesh, Matrix4, Mesh } from 'three';
9
- // 模型组转成 实例对象
10
- export default class InstancedGroupMesh extends Mesh {
11
- isInstancedGroupMesh = true;
12
- count = 0;
13
- meshCollect = {};
14
- instanceCollect = {};
15
- static matrix = new Matrix4();
16
- constructor(group, count) {
17
- super();
18
- this.count = count;
19
- const { meshCollect, instanceCollect } = this;
20
- group.updateMatrixWorld(true);
21
- // collect all mesh counts, in case there are more than one copy of the same mesh in this group
22
- group.traverse((obj) => {
23
- const mesh = obj;
24
- obj.userData = this.userData;
25
- if (mesh.isMesh) {
26
- mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
27
- mesh.rotation.setFromQuaternion(mesh.quaternion);
28
- mesh.updateMatrix();
29
- // same geometry and same material consider as the same mesh
30
- const uuid = mesh.geometry.uuid + '/' + mesh.material.uuid;
31
- if (meshCollect[uuid]) {
32
- meshCollect[uuid].push(mesh);
33
- }
34
- else {
35
- meshCollect[uuid] = [mesh];
36
- }
37
- }
38
- else {
39
- obj.position.set(0, 0, 0);
40
- obj.scale.set(1, 1, 1);
41
- obj.rotation.set(0, 0, 0);
42
- }
43
- });
44
- // create all instances
45
- Object.keys(meshCollect).forEach(uuid => {
46
- const meshes = meshCollect[uuid];
47
- const mesh = meshes[0];
48
- const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * count);
49
- instanceCollect[uuid] = instancedMesh;
50
- this.add(instancedMesh);
51
- });
52
- }
53
- // 更新包围盒
54
- computeBoundingSphere = throttle(() => {
55
- this.traverse(m => {
56
- m.computeBoundingSphere();
57
- });
58
- }, 1000);
59
- setMatrixAt(index, matrix) {
60
- Object.keys(this.meshCollect).forEach(uuid => {
61
- const instancedMesh = this.instanceCollect[uuid];
62
- const collect = this.meshCollect[uuid];
63
- collect.forEach((mesh, i) => {
64
- InstancedGroupMesh.matrix.copy(mesh.matrix);
65
- InstancedGroupMesh.matrix.premultiply(matrix);
66
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedGroupMesh.matrix);
67
- instancedMesh.instanceMatrix.needsUpdate = true;
68
- });
69
- });
70
- this.computeBoundingSphere();
71
- }
72
- // 设置颜色
73
- setColorAt(index, color) {
74
- Object.keys(this.meshCollect).forEach(uuid => {
75
- const instancedMesh = this.instanceCollect[uuid];
76
- const collect = this.meshCollect[uuid];
77
- collect.forEach((mesh, i) => {
78
- const c = color(instancedMesh);
79
- if (c) {
80
- instancedMesh.setColorAt(collect.length * index + i, c);
81
- instancedMesh.instanceColor.needsUpdate = true;
82
- }
83
- });
84
- });
85
- }
86
- // 同步属性
87
- updateMatrixWorld(force) {
88
- super.updateMatrixWorld(force);
89
- const { cursor, enableEvent } = this.userData;
90
- this.traverse(obj => {
91
- Object.assign(obj.userData, { cursor, enableEvent });
92
- });
93
- }
94
- // 事件监听
95
- addEventListener(name, callback) {
96
- this.children.map(mesh => {
97
- mesh.addEventListener(name, callback);
98
- });
99
- }
100
- // 取消事件的监听
101
- removeEventListener(name, callback) {
102
- this.children.map(mesh => {
103
- mesh.removeEventListener(name, callback);
104
- });
105
- }
106
- }
@@ -1,133 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-02-21 09:55:52
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-27 17:41:26
6
- */
7
-
8
- import { throttle } from 'lodash';
9
- import { Color, Group, InstancedMesh, Material, Matrix4, Mesh } from 'three';
10
-
11
- // 模型组转成 实例对象
12
- export default class InstancedGroupMesh extends Mesh {
13
-
14
- declare children: InstancedMesh[];
15
-
16
- isInstancedGroupMesh = true
17
-
18
- count: number = 0;
19
-
20
- meshCollect: Record<string, Mesh[]> = {}
21
-
22
- instanceCollect: Record<string, InstancedMesh> = {}
23
-
24
- static matrix = new Matrix4()
25
-
26
- constructor(group: Group, count: number) {
27
- super()
28
- this.count = count
29
-
30
- const { meshCollect, instanceCollect } = this
31
-
32
- group.updateMatrixWorld(true);
33
-
34
- // collect all mesh counts, in case there are more than one copy of the same mesh in this group
35
- group.traverse((obj) => {
36
- const mesh = obj as Mesh;
37
-
38
- obj.userData = this.userData;
39
-
40
- if (mesh.isMesh) {
41
-
42
- mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
43
- mesh.rotation.setFromQuaternion(mesh.quaternion);
44
- mesh.updateMatrix();
45
-
46
- // same geometry and same material consider as the same mesh
47
- const uuid = mesh.geometry.uuid + '/' + (mesh.material as Material).uuid
48
-
49
- if (meshCollect[uuid]) {
50
- meshCollect[uuid].push(mesh)
51
- } else {
52
- meshCollect[uuid] = [mesh]
53
- }
54
- } else {
55
- obj.position.set(0, 0, 0);
56
- obj.scale.set(1, 1, 1);
57
- obj.rotation.set(0, 0, 0);
58
- }
59
-
60
- })
61
-
62
- // create all instances
63
- Object.keys(meshCollect).forEach(uuid => {
64
- const meshes = meshCollect[uuid]
65
- const mesh = meshes[0]
66
-
67
- const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * count)
68
- instanceCollect[uuid] = instancedMesh;
69
- this.add(instancedMesh)
70
- })
71
- }
72
-
73
- // 更新包围盒
74
- computeBoundingSphere = throttle(() => {
75
- this.traverse(m => {
76
- (m as InstancedMesh).computeBoundingSphere();
77
- })
78
- }, 1000)
79
-
80
- setMatrixAt(index: number, matrix: Matrix4) {
81
- Object.keys(this.meshCollect).forEach(uuid => {
82
- const instancedMesh = this.instanceCollect[uuid]
83
- const collect = this.meshCollect[uuid]
84
- collect.forEach((mesh: Mesh, i: number) => {
85
- InstancedGroupMesh.matrix.copy(mesh.matrix)
86
- InstancedGroupMesh.matrix.premultiply(matrix)
87
- instancedMesh.setMatrixAt(collect.length * index + i, InstancedGroupMesh.matrix);
88
- instancedMesh.instanceMatrix.needsUpdate = true;
89
- })
90
- })
91
- this.computeBoundingSphere();
92
- }
93
-
94
- // 设置颜色
95
- setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined) {
96
- Object.keys(this.meshCollect).forEach(uuid => {
97
- const instancedMesh = this.instanceCollect[uuid]
98
- const collect = this.meshCollect[uuid]
99
- collect.forEach((mesh: Mesh, i: number) => {
100
- const c = color(instancedMesh);
101
- if (c) {
102
- instancedMesh.setColorAt(collect.length * index + i, c);
103
- instancedMesh.instanceColor!.needsUpdate = true;
104
- }
105
- })
106
- })
107
- }
108
-
109
- // 同步属性
110
- updateMatrixWorld(force?: boolean): void {
111
- super.updateMatrixWorld(force);
112
- const { cursor, enableEvent } = this.userData;
113
- this.traverse(obj => {
114
- Object.assign(obj.userData, { cursor, enableEvent });
115
- });
116
- }
117
-
118
- // 事件监听
119
- addEventListener(name: string, callback: (e: any) => void) {
120
- this.children.map(mesh => {
121
- mesh.addEventListener(name, callback);
122
- });
123
- }
124
-
125
- // 取消事件的监听
126
- removeEventListener(name: string, callback: (e: any) => void) {
127
- this.children.map(mesh => {
128
- mesh.removeEventListener(name, callback);
129
- });
130
- }
131
-
132
- }
133
-
@@ -1,4 +0,0 @@
1
- import { Group } from "three";
2
- export default class InstancedMeshBase extends Group {
3
- constructor();
4
- }
@@ -1,13 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2024-02-05 09:31:57
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-02-05 09:40:12
6
- */
7
- import { Group } from "three";
8
- // 数组实例模型
9
- export default class InstancedMeshBase extends Group {
10
- constructor() {
11
- super();
12
- }
13
- }
@@ -1,18 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2024-02-05 09:31:57
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-02-05 09:40:12
6
- */
7
-
8
- import { Group } from "three";
9
-
10
- // 数组实例模型
11
- export default class InstancedMeshBase extends Group {
12
- constructor() {
13
- super();
14
- }
15
-
16
-
17
-
18
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes