three-base 2.0.72 → 2.0.74
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/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/ExtrudeMesh.d.ts +111 -111
- package/ExtrudeMesh.js +363 -363
- package/FPScontrol.d.ts +45 -45
- package/FPScontrol.js +171 -171
- 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/InstancedMeshBase.d.ts +32 -32
- package/InstancedMeshBase.js +236 -219
- package/InstancedMeshBase.ts +36 -20
- package/KeyFrameAnimation.d.ts +25 -25
- package/KeyFrameAnimation.js +78 -78
- package/MeshBase.d.ts +34 -33
- package/MeshBase.js +185 -174
- package/MeshBase.ts +16 -5
- package/Mirror.d.ts +25 -25
- package/Mirror.js +139 -139
- 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 +44 -44
- package/PlaneText.js +30 -30
- package/PlaneWithHtml.d.ts +11 -11
- package/PlaneWithHtml.js +68 -68
- package/PointLight/PointLight.d.ts +13 -13
- package/PointLight/PointLight.js +64 -64
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/Sky/Sky.d.ts +13 -13
- package/Sky/Sky.js +28 -28
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- 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 +49 -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 +200 -200
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/ThreeBase.d.ts +92 -91
- package/ThreeBase.js +619 -611
- package/ThreeBase.ts +15 -6
- package/ThreeMouse.d.ts +39 -39
- package/ThreeMouse.js +359 -359
- package/Transition.d.ts +40 -40
- package/Transition.js +207 -207
- 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 +1 -1
- package/pass/FullScreenQuad.d.ts +7 -7
- package/pass/FullScreenQuad.js +25 -25
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +236 -236
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +202 -202
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +21 -21
- package/shaders.d.ts +17 -17
- package/shaders.js +99 -99
- package/threeLoader.d.ts +26 -26
- package/threeLoader.js +189 -189
- package/threeUtils.d.ts +47 -47
- package/threeUtils.js +266 -266
package/InstancedMeshBase.js
CHANGED
|
@@ -1,219 +1,236 @@
|
|
|
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 { throttle } from "lodash";
|
|
8
|
-
import { 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
|
|
28
|
-
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
29
|
-
instanceColor = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
30
|
-
// 重构加载模型的方法
|
|
31
|
-
async loadModel(url, filename = "") {
|
|
32
|
-
this.url = url;
|
|
33
|
-
if (!url) {
|
|
34
|
-
this.clear();
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
this.loaded = false;
|
|
38
|
-
// 没有模型并且没有加载任务时就去加载模型
|
|
39
|
-
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
40
|
-
MeshBase.loadModeling.set(url, true);
|
|
41
|
-
// 万能加载器
|
|
42
|
-
let modal = await threeLoader.loadModel(url, filename);
|
|
43
|
-
threeUtils.center(modal);
|
|
44
|
-
modal.updateMatrixWorld(true);
|
|
45
|
-
MeshBase.models.set(url, modal);
|
|
46
|
-
MeshBase.loadModeling.set(url, false);
|
|
47
|
-
}
|
|
48
|
-
// 如果模型还在加载中, 就等待模型加载完成
|
|
49
|
-
do {
|
|
50
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
51
|
-
} while (MeshBase.loadModeling.get(url));
|
|
52
|
-
if (MeshBase.models.get(url)) {
|
|
53
|
-
this.clear();
|
|
54
|
-
const mesh = MeshBase.models.get(url);
|
|
55
|
-
// // 判断是否有动画
|
|
56
|
-
// this.hasAnimation = mesh.animations.length > 0;
|
|
57
|
-
// if (this.hasAnimation) {
|
|
58
|
-
// const model = SkeletonUtils.clone(mesh);
|
|
59
|
-
// this.mixer = new AnimationMixer(model);
|
|
60
|
-
// this.animationActions = {};
|
|
61
|
-
// mesh.animations.map(animation => {
|
|
62
|
-
// const animationAction = this.mixer!.clipAction(animation).play();
|
|
63
|
-
// animationAction.weight = 0;
|
|
64
|
-
// this.animationActions[animation.name] = animationAction;
|
|
65
|
-
// });
|
|
66
|
-
// this.add(model);
|
|
67
|
-
// } else {
|
|
68
|
-
// this.add(mesh.clone(true));
|
|
69
|
-
// }
|
|
70
|
-
this.size.copy(threeUtils.getSize(mesh));
|
|
71
|
-
const { meshCollect, instanceCollect } = this;
|
|
72
|
-
mesh.updateMatrixWorld(true);
|
|
73
|
-
// collect all mesh counts, in case there are more than one copy of the same mesh in this group
|
|
74
|
-
mesh.traverse((obj) => {
|
|
75
|
-
const mesh = obj;
|
|
76
|
-
if (mesh.isMesh) {
|
|
77
|
-
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
78
|
-
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
79
|
-
mesh.updateMatrix();
|
|
80
|
-
// same geometry and same material consider as the same mesh
|
|
81
|
-
const uuid = mesh.geometry.uuid + '/' + mesh.material.uuid;
|
|
82
|
-
if (meshCollect[uuid]) {
|
|
83
|
-
meshCollect[uuid].push(mesh);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
meshCollect[uuid] = [mesh];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
obj.position.set(0, 0, 0);
|
|
91
|
-
obj.scale.set(1, 1, 1);
|
|
92
|
-
obj.rotation.set(0, 0, 0);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
// create all instances
|
|
96
|
-
Object.keys(meshCollect).forEach(uuid => {
|
|
97
|
-
const meshes = meshCollect[uuid];
|
|
98
|
-
const mesh = meshes[0];
|
|
99
|
-
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
|
|
100
|
-
instancedMesh.name = mesh.name;
|
|
101
|
-
instancedMesh.frustumCulled = false;
|
|
102
|
-
instanceCollect[uuid] = instancedMesh;
|
|
103
|
-
this.add(instancedMesh);
|
|
104
|
-
});
|
|
105
|
-
// 加载完成事件
|
|
106
|
-
await new Promise(resolve => setTimeout(resolve,
|
|
107
|
-
// 加载完成事件
|
|
108
|
-
this.dispatchEvent({ type: "loaded" });
|
|
109
|
-
this.loaded = true;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// 更新包围盒
|
|
113
|
-
computeBoundingSphere = throttle(() => {
|
|
114
|
-
this.children.map(instancedMesh => {
|
|
115
|
-
instancedMesh.computeBoundingSphere();
|
|
116
|
-
});
|
|
117
|
-
}, 1000);
|
|
118
|
-
//初始实例矩阵的数量
|
|
119
|
-
async setCount(count = 0) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
this.
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
//
|
|
213
|
-
|
|
214
|
-
super.
|
|
215
|
-
this.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
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 { throttle } from "lodash";
|
|
8
|
+
import { 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
|
+
async loadModel(url, filename = "") {
|
|
32
|
+
this.url = url;
|
|
33
|
+
if (!url) {
|
|
34
|
+
this.clear();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.loaded = false;
|
|
38
|
+
// 没有模型并且没有加载任务时就去加载模型
|
|
39
|
+
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
40
|
+
MeshBase.loadModeling.set(url, true);
|
|
41
|
+
// 万能加载器
|
|
42
|
+
let modal = await threeLoader.loadModel(url, filename);
|
|
43
|
+
threeUtils.center(modal);
|
|
44
|
+
modal.updateMatrixWorld(true);
|
|
45
|
+
MeshBase.models.set(url, modal);
|
|
46
|
+
MeshBase.loadModeling.set(url, false);
|
|
47
|
+
}
|
|
48
|
+
// 如果模型还在加载中, 就等待模型加载完成
|
|
49
|
+
do {
|
|
50
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
51
|
+
} while (MeshBase.loadModeling.get(url));
|
|
52
|
+
if (MeshBase.models.get(url)) {
|
|
53
|
+
this.clear();
|
|
54
|
+
const mesh = MeshBase.models.get(url);
|
|
55
|
+
// // 判断是否有动画
|
|
56
|
+
// this.hasAnimation = mesh.animations.length > 0;
|
|
57
|
+
// if (this.hasAnimation) {
|
|
58
|
+
// const model = SkeletonUtils.clone(mesh);
|
|
59
|
+
// this.mixer = new AnimationMixer(model);
|
|
60
|
+
// this.animationActions = {};
|
|
61
|
+
// mesh.animations.map(animation => {
|
|
62
|
+
// const animationAction = this.mixer!.clipAction(animation).play();
|
|
63
|
+
// animationAction.weight = 0;
|
|
64
|
+
// this.animationActions[animation.name] = animationAction;
|
|
65
|
+
// });
|
|
66
|
+
// this.add(model);
|
|
67
|
+
// } else {
|
|
68
|
+
// this.add(mesh.clone(true));
|
|
69
|
+
// }
|
|
70
|
+
this.size.copy(threeUtils.getSize(mesh));
|
|
71
|
+
const { meshCollect, instanceCollect } = this;
|
|
72
|
+
mesh.updateMatrixWorld(true);
|
|
73
|
+
// collect all mesh counts, in case there are more than one copy of the same mesh in this group
|
|
74
|
+
mesh.traverse((obj) => {
|
|
75
|
+
const mesh = obj;
|
|
76
|
+
if (mesh.isMesh) {
|
|
77
|
+
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
78
|
+
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
79
|
+
mesh.updateMatrix();
|
|
80
|
+
// same geometry and same material consider as the same mesh
|
|
81
|
+
const uuid = mesh.geometry.uuid + '/' + mesh.material.uuid;
|
|
82
|
+
if (meshCollect[uuid]) {
|
|
83
|
+
meshCollect[uuid].push(mesh);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
meshCollect[uuid] = [mesh];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
obj.position.set(0, 0, 0);
|
|
91
|
+
obj.scale.set(1, 1, 1);
|
|
92
|
+
obj.rotation.set(0, 0, 0);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
// create all instances
|
|
96
|
+
Object.keys(meshCollect).forEach(uuid => {
|
|
97
|
+
const meshes = meshCollect[uuid];
|
|
98
|
+
const mesh = meshes[0];
|
|
99
|
+
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
|
|
100
|
+
instancedMesh.name = mesh.name;
|
|
101
|
+
instancedMesh.frustumCulled = false;
|
|
102
|
+
instanceCollect[uuid] = instancedMesh;
|
|
103
|
+
this.add(instancedMesh);
|
|
104
|
+
});
|
|
105
|
+
// 加载完成事件
|
|
106
|
+
await new Promise(resolve => setTimeout(resolve, 30));
|
|
107
|
+
// 加载完成事件
|
|
108
|
+
this.dispatchEvent({ type: "loaded" });
|
|
109
|
+
this.loaded = true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// 更新包围盒
|
|
113
|
+
computeBoundingSphere = throttle(() => {
|
|
114
|
+
this.children.map(instancedMesh => {
|
|
115
|
+
instancedMesh.computeBoundingSphere();
|
|
116
|
+
});
|
|
117
|
+
}, 1000);
|
|
118
|
+
//初始实例矩阵的数量
|
|
119
|
+
async setCount(count = 0) {
|
|
120
|
+
if (this.count === count) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
await this.awaitLoaded();
|
|
124
|
+
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
125
|
+
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
126
|
+
for (let i = 0; i < count; i++) {
|
|
127
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
128
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
129
|
+
}
|
|
130
|
+
for (let i = 0; i < this.count; i++) {
|
|
131
|
+
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
132
|
+
instanceColor.copyAt(i, this.instanceColor, i);
|
|
133
|
+
}
|
|
134
|
+
this.instanceMatrix = instanceMatrix;
|
|
135
|
+
this.instanceColor = instanceColor;
|
|
136
|
+
this.count = count;
|
|
137
|
+
this.children.map((instanceMesh) => {
|
|
138
|
+
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
139
|
+
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
140
|
+
for (let i = 0; i < count; i++) {
|
|
141
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
142
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
143
|
+
}
|
|
144
|
+
for (let i = 0; i < instanceMesh.count; i++) {
|
|
145
|
+
instanceMatrix.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
146
|
+
instanceColor.copyAt(i, instanceMesh.instanceColor, i);
|
|
147
|
+
}
|
|
148
|
+
instanceMesh.instanceMatrix = instanceMatrix;
|
|
149
|
+
instanceMesh.instanceColor = instanceColor;
|
|
150
|
+
instanceMesh.count = count;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// 设置矩阵
|
|
154
|
+
async setMatrixAt(index, matrix) {
|
|
155
|
+
const new_matrix = matrix.clone();
|
|
156
|
+
await this.awaitLoaded();
|
|
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(new_matrix);
|
|
163
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
164
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
this.computeBoundingSphere();
|
|
168
|
+
}
|
|
169
|
+
//设置移动旋转缩放
|
|
170
|
+
async setTRS(index, { position = InstancedMeshBase._position, rotation = InstancedMeshBase._rotation, scale = InstancedMeshBase._scale }) {
|
|
171
|
+
const new_position = position.clone();
|
|
172
|
+
const new_rotation = rotation.clone();
|
|
173
|
+
const new_scale = scale.clone();
|
|
174
|
+
await this.awaitLoaded();
|
|
175
|
+
console.log("setTRS", index);
|
|
176
|
+
Object.keys(this.meshCollect).forEach(uuid => {
|
|
177
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
178
|
+
const collect = this.meshCollect[uuid];
|
|
179
|
+
collect.forEach((mesh, i) => {
|
|
180
|
+
//获取对应的模型的矩阵
|
|
181
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
182
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
183
|
+
InstancedMeshBase._matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
184
|
+
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
185
|
+
InstancedMeshBase._position.copy(new_position);
|
|
186
|
+
InstancedMeshBase._rotation.copy(new_rotation);
|
|
187
|
+
InstancedMeshBase._scale.copy(new_scale);
|
|
188
|
+
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
189
|
+
InstancedMeshBase._matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
190
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
191
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
192
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
this.computeBoundingSphere();
|
|
196
|
+
}
|
|
197
|
+
// 设置颜色
|
|
198
|
+
async setColorAt(index, color) {
|
|
199
|
+
await this.awaitLoaded();
|
|
200
|
+
Object.keys(this.meshCollect).forEach(uuid => {
|
|
201
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
202
|
+
const collect = this.meshCollect[uuid];
|
|
203
|
+
collect.forEach((mesh, i) => {
|
|
204
|
+
const c = color(instancedMesh);
|
|
205
|
+
if (c) {
|
|
206
|
+
instancedMesh.setColorAt(collect.length * index + i, c);
|
|
207
|
+
instancedMesh.instanceColor.needsUpdate = true;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
// 同步属性
|
|
213
|
+
updateMatrixWorld(force) {
|
|
214
|
+
super.updateMatrixWorld(force);
|
|
215
|
+
const { cursor, enableEvent } = this.userData;
|
|
216
|
+
this.traverse(obj => {
|
|
217
|
+
Object.assign(obj.userData, { cursor, enableEvent });
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
// 事件监听
|
|
221
|
+
async addEventListener(name, callback) {
|
|
222
|
+
super.addEventListener(name, callback);
|
|
223
|
+
await this.awaitLoaded();
|
|
224
|
+
super.addEventListener(name, callback);
|
|
225
|
+
this.children.map(mesh => {
|
|
226
|
+
mesh.addEventListener(name, callback);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
// 取消事件的监听
|
|
230
|
+
removeEventListener(name, callback) {
|
|
231
|
+
super.removeEventListener(name, callback);
|
|
232
|
+
this.children.map(mesh => {
|
|
233
|
+
mesh.removeEventListener(name, callback);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
package/InstancedMeshBase.ts
CHANGED
|
@@ -35,7 +35,7 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
35
35
|
static _scale: Vector3 = new Vector3(1, 1, 1);
|
|
36
36
|
static _rotation: Euler = new Euler();
|
|
37
37
|
static _Quaternion: Quaternion = new Quaternion();
|
|
38
|
-
static
|
|
38
|
+
static _matrix = new Matrix4()
|
|
39
39
|
|
|
40
40
|
instanceMatrix = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
41
41
|
|
|
@@ -43,7 +43,6 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
43
43
|
|
|
44
44
|
// 重构加载模型的方法
|
|
45
45
|
async loadModel(url: string, filename: string = "") {
|
|
46
|
-
|
|
47
46
|
this.url = url;
|
|
48
47
|
|
|
49
48
|
if (!url) {
|
|
@@ -138,7 +137,7 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
138
137
|
})
|
|
139
138
|
|
|
140
139
|
// 加载完成事件
|
|
141
|
-
await new Promise(resolve => setTimeout(resolve,
|
|
140
|
+
await new Promise(resolve => setTimeout(resolve, 30));
|
|
142
141
|
// 加载完成事件
|
|
143
142
|
this.dispatchEvent({ type: "loaded" } as never);
|
|
144
143
|
this.loaded = true;
|
|
@@ -155,11 +154,19 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
155
154
|
|
|
156
155
|
//初始实例矩阵的数量
|
|
157
156
|
async setCount(count: number = 0) {
|
|
157
|
+
if (this.count === count) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
158
160
|
await this.awaitLoaded();
|
|
159
161
|
|
|
160
162
|
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
161
163
|
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
162
164
|
|
|
165
|
+
for (let i = 0; i < count; i++) {
|
|
166
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
167
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
168
|
+
}
|
|
169
|
+
|
|
163
170
|
for (let i = 0; i < this.count; i++) {
|
|
164
171
|
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
165
172
|
instanceColor.copyAt(i, this.instanceColor, i);
|
|
@@ -167,6 +174,7 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
167
174
|
|
|
168
175
|
this.instanceMatrix = instanceMatrix;
|
|
169
176
|
this.instanceColor = instanceColor;
|
|
177
|
+
|
|
170
178
|
this.count = count;
|
|
171
179
|
|
|
172
180
|
this.children.map((instanceMesh: InstancedMesh) => {
|
|
@@ -174,9 +182,14 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
174
182
|
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
175
183
|
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
176
184
|
|
|
185
|
+
for (let i = 0; i < count; i++) {
|
|
186
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
187
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
188
|
+
}
|
|
189
|
+
|
|
177
190
|
for (let i = 0; i < instanceMesh.count; i++) {
|
|
178
|
-
instanceMatrix.copyAt(i,
|
|
179
|
-
instanceColor.copyAt(i,
|
|
191
|
+
instanceMatrix.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
192
|
+
instanceColor.copyAt(i, instanceMesh.instanceColor, i);
|
|
180
193
|
}
|
|
181
194
|
|
|
182
195
|
instanceMesh.instanceMatrix = instanceMatrix;
|
|
@@ -188,16 +201,16 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
188
201
|
|
|
189
202
|
// 设置矩阵
|
|
190
203
|
async setMatrixAt(index: number, matrix: Matrix4) {
|
|
204
|
+
const new_matrix = matrix.clone();
|
|
191
205
|
await this.awaitLoaded();
|
|
192
|
-
|
|
193
206
|
Object.keys(this.meshCollect).forEach(uuid => {
|
|
194
207
|
const instancedMesh = this.instanceCollect[uuid]
|
|
195
208
|
const collect = this.meshCollect[uuid]
|
|
196
209
|
collect.forEach((mesh: Mesh, i: number) => {
|
|
197
|
-
InstancedMeshBase.
|
|
198
|
-
InstancedMeshBase.
|
|
210
|
+
InstancedMeshBase._matrix.copy(mesh.matrix)
|
|
211
|
+
InstancedMeshBase._matrix.premultiply(new_matrix);
|
|
199
212
|
|
|
200
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.
|
|
213
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
201
214
|
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
202
215
|
})
|
|
203
216
|
})
|
|
@@ -218,38 +231,41 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
218
231
|
scale?: Vector3
|
|
219
232
|
}
|
|
220
233
|
) {
|
|
234
|
+
const new_position = position.clone();
|
|
235
|
+
const new_rotation = rotation.clone();
|
|
236
|
+
const new_scale = scale.clone();
|
|
221
237
|
await this.awaitLoaded();
|
|
222
|
-
|
|
238
|
+
console.log("setTRS", index);
|
|
223
239
|
Object.keys(this.meshCollect).forEach(uuid => {
|
|
224
240
|
const instancedMesh = this.instanceCollect[uuid]
|
|
225
241
|
const collect = this.meshCollect[uuid]
|
|
226
242
|
collect.forEach((mesh: Mesh, i: number) => {
|
|
227
243
|
|
|
228
244
|
//获取对应的模型的矩阵
|
|
229
|
-
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase.
|
|
245
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
230
246
|
|
|
231
|
-
InstancedMeshBase.
|
|
247
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
232
248
|
|
|
233
|
-
InstancedMeshBase.
|
|
249
|
+
InstancedMeshBase._matrix.decompose(
|
|
234
250
|
InstancedMeshBase._position,
|
|
235
251
|
InstancedMeshBase._Quaternion,
|
|
236
252
|
InstancedMeshBase._scale
|
|
237
253
|
);
|
|
238
254
|
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
239
|
-
InstancedMeshBase._position.copy(
|
|
240
|
-
InstancedMeshBase._rotation.copy(
|
|
241
|
-
InstancedMeshBase._scale.copy(
|
|
255
|
+
InstancedMeshBase._position.copy(new_position);
|
|
256
|
+
InstancedMeshBase._rotation.copy(new_rotation);
|
|
257
|
+
InstancedMeshBase._scale.copy(new_scale);
|
|
242
258
|
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
243
259
|
|
|
244
|
-
InstancedMeshBase.
|
|
260
|
+
InstancedMeshBase._matrix.compose(
|
|
245
261
|
InstancedMeshBase._position,
|
|
246
262
|
InstancedMeshBase._Quaternion,
|
|
247
263
|
InstancedMeshBase._scale
|
|
248
264
|
);
|
|
249
265
|
|
|
250
|
-
InstancedMeshBase.
|
|
266
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
251
267
|
|
|
252
|
-
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase.
|
|
268
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
253
269
|
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
254
270
|
})
|
|
255
271
|
})
|
|
@@ -285,8 +301,8 @@ export default class InstancedMeshBase extends MeshBase {
|
|
|
285
301
|
|
|
286
302
|
// 事件监听
|
|
287
303
|
async addEventListener(name: string, callback: (e: any) => void) {
|
|
304
|
+
super.addEventListener(name, callback);
|
|
288
305
|
await this.awaitLoaded();
|
|
289
|
-
|
|
290
306
|
super.addEventListener(name, callback);
|
|
291
307
|
this.children.map(mesh => {
|
|
292
308
|
mesh.addEventListener(name, callback);
|