three-base 2.0.134 → 2.0.142
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/DirectionalLight.d.ts +6 -6
- package/DirectionalLight.js +68 -68
- package/DirectionalLight.ts +70 -0
- package/EventMap.d.ts +103 -0
- package/EventMap.js +17 -0
- package/EventMap.ts +139 -0
- package/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/EventMesh.ts +60 -0
- package/ExtrudeMesh.d.ts +112 -112
- package/ExtrudeMesh.js +342 -366
- package/ExtrudeMesh.ts +553 -0
- package/FPScontrol.d.ts +511 -71
- package/FPScontrol.js +1799 -505
- package/FPScontrol.ts +1855 -0
- package/Fire/Fire.d.ts +28 -28
- package/Fire/Fire.js +80 -79
- package/Fire/Fire.ts +89 -0
- package/Fire/{FireGromtry.d.ts → FireGeometry.d.ts} +9 -9
- package/Fire/{FireGromtry.js → FireGeometry.js} +40 -40
- package/Fire/FireGeometry.ts +40 -0
- package/Fire/FireMaterial.d.ts +28 -28
- package/Fire/FireMaterial.js +32 -32
- package/Fire/FireMaterial.ts +89 -0
- package/Fire/index.d.ts +2 -1
- package/Fire/index.js +2 -1
- package/Fire/index.ts +2 -0
- package/GLTFDracoLoader.d.ts +71 -0
- package/GLTFDracoLoader.js +266 -0
- package/GLTFDracoLoader.ts +430 -0
- package/Grass/Grass.d.ts +8 -8
- package/Grass/Grass.js +38 -38
- package/Grass/Grass.ts +38 -0
- package/Grass/index.d.ts +2 -1
- package/Grass/index.js +2 -1
- package/Grass/index.ts +2 -0
- package/Ground1/Ground1.d.ts +10 -11
- package/Ground1/Ground1.js +95 -91
- package/Ground1/Ground1.ts +114 -0
- package/Ground1/index.d.ts +1 -1
- package/Ground1/index.js +1 -1
- package/Ground1/index.ts +1 -0
- package/MeshBase.d.ts +36 -35
- package/MeshBase.js +247 -223
- package/MeshBase.ts +253 -0
- package/Mirror.d.ts +25 -25
- package/Mirror.js +143 -139
- package/Mirror.ts +203 -0
- package/MktMesh/MktMesh.d.ts +291 -0
- package/MktMesh/MktMesh.js +1045 -0
- package/MktMesh/MktMesh.ts +1075 -0
- package/MktMesh/assetUrl.d.ts +35 -0
- package/MktMesh/assetUrl.js +69 -0
- package/MktMesh/assetUrl.ts +69 -0
- package/MktMesh/mktMappingShader.d.ts +12 -0
- package/MktMesh/mktMappingShader.js +139 -0
- package/MktMesh/mktMappingShader.ts +139 -0
- package/MktMesh/mktShader.d.ts +263 -0
- package/MktMesh/mktShader.js +1248 -0
- package/MktMesh/mktShader.ts +1344 -0
- package/MktMesh/panoBitmapTexture.d.ts +25 -0
- package/MktMesh/panoBitmapTexture.js +52 -0
- package/MktMesh/panoBitmapTexture.ts +54 -0
- package/MktMesh/panoSlotBind.d.ts +29 -0
- package/MktMesh/panoSlotBind.js +106 -0
- package/MktMesh/panoSlotBind.ts +153 -0
- package/MktMesh/panoSlotRoster.d.ts +22 -0
- package/MktMesh/panoSlotRoster.js +169 -0
- package/MktMesh/panoSlotRoster.ts +168 -0
- package/MktMesh/panoTileStitcher.d.ts +119 -0
- package/MktMesh/panoTileStitcher.js +533 -0
- package/MktMesh/panoTileStitcher.ts +616 -0
- package/MktMesh/types.d.ts +75 -0
- package/MktMesh/types.js +1 -0
- package/MktMesh/types.ts +87 -0
- package/MktMesh/wallCapTessellate.d.ts +33 -0
- package/MktMesh/wallCapTessellate.js +97 -0
- package/MktMesh/wallCapTessellate.ts +68 -0
- package/PipeMesh/PathGeometry.d.ts +44 -44
- package/PipeMesh/PathGeometry.js +307 -300
- package/PipeMesh/PathGeometry.ts +343 -0
- package/PipeMesh/PathPoint.d.ts +17 -17
- package/PipeMesh/PathPoint.js +31 -31
- package/PipeMesh/PathPoint.ts +36 -0
- package/PipeMesh/PathPointList.d.ts +31 -31
- package/PipeMesh/PathPointList.js +209 -208
- package/PipeMesh/PathPointList.ts +227 -0
- package/PipeMesh/PathTubeGeometry.d.ts +25 -25
- package/PipeMesh/PathTubeGeometry.js +130 -130
- package/PipeMesh/PathTubeGeometry.ts +146 -0
- package/PipeMesh/PipeMesh.d.ts +25 -25
- package/PipeMesh/PipeMesh.js +63 -63
- package/PipeMesh/PipeMesh.ts +76 -0
- package/PipeMesh/index.d.ts +1 -1
- package/PipeMesh/index.js +1 -1
- package/PipeMesh/index.ts +1 -0
- package/PlaneModal1/PlaneModal1.d.ts +34 -31
- package/PlaneModal1/PlaneModal1.js +109 -105
- package/PlaneModal1/PlaneModal1.ts +138 -0
- package/PlaneModal1/index.d.ts +1 -1
- package/PlaneModal1/index.js +1 -1
- package/PlaneModal1/index.ts +1 -0
- package/PlaneText.d.ts +27 -39
- package/PlaneText.js +35 -30
- package/PlaneText.ts +55 -0
- package/PlaneWithHtml.d.ts +13 -11
- package/PlaneWithHtml.js +64 -63
- package/PlaneWithHtml.ts +70 -0
- package/PointLight/PointLight.d.ts +13 -14
- package/PointLight/PointLight.js +64 -70
- package/PointLight/PointLight.ts +67 -0
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/PointLight/index.ts +1 -0
- package/README.md +110 -79
- package/Sky/Sky.d.ts +9 -9
- package/Sky/Sky.js +22 -22
- package/Sky/Sky.ts +22 -0
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- package/Sky/index.ts +1 -0
- package/SpotLight.d.ts +6 -6
- package/SpotLight.js +72 -72
- package/SpotLight.ts +74 -0
- package/SpriteModal1/SpriteModal1.d.ts +33 -30
- package/SpriteModal1/SpriteModal1.js +116 -110
- package/SpriteModal1/SpriteModal1.ts +146 -0
- package/SpriteModal1/index.d.ts +1 -1
- package/SpriteModal1/index.js +1 -1
- package/SpriteModal1/index.ts +1 -0
- package/SpriteText.d.ts +46 -45
- package/SpriteText.js +176 -163
- package/SpriteText.ts +205 -0
- package/SpriteText2.d.ts +48 -47
- package/SpriteText2.js +223 -198
- package/SpriteText2.ts +258 -0
- package/SpriteText3.d.ts +86 -0
- package/SpriteText3.js +519 -0
- package/SpriteText3.ts +599 -0
- package/SpriteWithHtml.d.ts +9 -7
- package/SpriteWithHtml.js +44 -44
- package/SpriteWithHtml.ts +50 -0
- package/Text3D/Text3D.d.ts +20 -20
- package/Text3D/Text3D.js +58 -62
- package/Text3D/Text3D.ts +77 -0
- package/Text3D/fontController.d.ts +60 -59
- package/Text3D/fontController.js +166 -199
- package/Text3D/fontController.ts +209 -0
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/Text3D/index.ts +1 -0
- package/ThreeBase.d.ts +248 -97
- package/ThreeBase.js +1202 -613
- package/ThreeBase.ts +1314 -0
- package/ThreeMouse.d.ts +54 -40
- package/ThreeMouse.js +453 -407
- package/ThreeMouse.ts +472 -0
- package/Transition.d.ts +45 -40
- package/Transition.js +209 -210
- package/Transition.ts +242 -0
- package/Water/Water.d.ts +5 -5
- package/Water/Water.js +44 -26
- package/Water/Water.ts +45 -0
- package/Water/index.d.ts +2 -0
- package/Water/index.js +2 -0
- package/Water/index.ts +2 -0
- package/WaterFlow/FloatingDuck.d.ts +88 -0
- package/WaterFlow/FloatingDuck.js +307 -0
- package/WaterFlow/FloatingDuck.ts +445 -0
- package/WaterFlow/GPUFluidSimulation.d.ts +56 -0
- package/WaterFlow/GPUFluidSimulation.js +785 -0
- package/WaterFlow/GPUFluidSimulation.ts +827 -0
- package/WaterFlow/ReservoirSystem.d.ts +106 -0
- package/WaterFlow/ReservoirSystem.js +621 -0
- package/WaterFlow/ReservoirSystem.ts +754 -0
- package/WaterFlow/SPHField.d.ts +35 -0
- package/WaterFlow/SPHField.js +204 -0
- package/WaterFlow/SPHField.ts +228 -0
- package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -0
- package/WaterFlow/ScreenSpaceFluidPass.js +736 -0
- package/WaterFlow/ScreenSpaceFluidPass.ts +789 -0
- package/WaterFlow/ShallowWaterSurface.d.ts +45 -0
- package/WaterFlow/ShallowWaterSurface.js +441 -0
- package/WaterFlow/ShallowWaterSurface.ts +510 -0
- package/WaterFlow/WaterFlow.d.ts +87 -0
- package/WaterFlow/WaterFlow.js +171 -0
- package/WaterFlow/WaterFlow.ts +219 -0
- package/WaterFlow/types.d.ts +161 -0
- package/WaterFlow/types.js +1 -0
- package/WaterFlow/types.ts +189 -0
- package/WaterFlow/waterFlowPass.d.ts +3 -0
- package/WaterFlow/waterFlowPass.js +20 -0
- package/WaterFlow/waterFlowPass.ts +40 -0
- package/collisionOBB.d.ts +42 -0
- package/collisionOBB.js +112 -0
- package/collisionOBB.ts +108 -0
- package/declare.d.ts +23 -165
- package/index.d.ts +1 -1
- package/index.js +1 -2
- package/index.ts +1 -0
- package/instanced/InstancedBatch.d.ts +57 -0
- package/instanced/InstancedBatch.js +348 -0
- package/instanced/InstancedBatch.ts +406 -0
- package/instanced/InstancedChild.d.ts +174 -0
- package/instanced/InstancedChild.js +264 -0
- package/instanced/InstancedChild.ts +327 -0
- package/instanced/InstancedGroup.d.ts +43 -0
- package/instanced/InstancedGroup.js +126 -0
- package/instanced/InstancedGroup.ts +137 -0
- package/{InstancedMeshBase.d.ts → instanced/InstancedMeshBase.d.ts} +38 -39
- package/{InstancedMeshBase.js → instanced/InstancedMeshBase.js} +282 -293
- package/instanced/InstancedMeshBase.ts +292 -0
- package/instanced/InstancedTemplate.d.ts +46 -0
- package/instanced/InstancedTemplate.js +125 -0
- package/instanced/InstancedTemplate.ts +188 -0
- package/keyControls.d.ts +30 -0
- package/{keyContorls.js → keyControls.js} +101 -98
- package/keyControls.ts +107 -0
- package/lensflare/LensFlare.d.ts +39 -0
- package/lensflare/LensFlare.js +373 -0
- package/lensflare/LensFlare.ts +448 -0
- package/lensflare/lensflare0.png +0 -0
- package/lensflare/lensflare3.png +0 -0
- package/lensflare/lensflare_custom.png +0 -0
- package/lensflare/lensflare_hex_ghost.png +0 -0
- package/lensflare/lensflare_hex_main.png +0 -0
- package/package.json +101 -26
- package/pass/bloom_pass.d.ts +2 -2
- package/pass/bloom_pass.js +188 -101
- package/pass/bloom_pass.ts +243 -0
- package/pass/cover_pass.d.ts +2 -2
- package/pass/cover_pass.js +122 -99
- package/pass/cover_pass.ts +182 -0
- package/pass/debugDepthState.d.ts +20 -0
- package/pass/debugDepthState.js +9 -0
- package/pass/debugDepthState.ts +22 -0
- package/pass/depthTestBias.d.ts +12 -0
- package/pass/depthTestBias.js +12 -0
- package/pass/depthTestBias.ts +12 -0
- package/pass/effectUserData.d.ts +27 -0
- package/pass/effectUserData.js +1 -0
- package/pass/effectUserData.ts +46 -0
- package/pass/frosted_glass_pass.d.ts +2 -2
- package/pass/frosted_glass_pass.js +167 -186
- package/pass/frosted_glass_pass.ts +288 -0
- package/pass/fsQuad.d.ts +9 -9
- package/pass/fsQuad.js +28 -28
- package/pass/fsQuad.ts +34 -0
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +449 -233
- package/pass/glow_pass.ts +654 -0
- package/pass/screen_bloom_pass.d.ts +11 -0
- package/pass/screen_bloom_pass.js +235 -0
- package/pass/screen_bloom_pass.ts +263 -0
- package/pass/ssao_pass.d.ts +88 -11
- package/pass/ssao_pass.js +802 -288
- package/pass/ssao_pass.ts +918 -0
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +329 -258
- package/pass/stroke_pass.ts +638 -0
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +23 -66
- package/shaders/FXAAShaderMaterial.ts +201 -0
- package/shaders/baseDepthVisShader.d.ts +11 -0
- package/shaders/baseDepthVisShader.js +36 -0
- package/shaders/baseDepthVisShader.ts +36 -0
- package/shaders/baseTextureToRenderShader.d.ts +2 -2
- package/shaders/baseTextureToRenderShader.js +55 -60
- package/shaders/baseTextureToRenderShader.ts +232 -0
- package/shaders/copyShader.d.ts +3 -3
- package/shaders/copyShader.js +13 -14
- package/shaders/copyShader.ts +32 -0
- package/shaders/debugDepthCompareShader.d.ts +14 -0
- package/shaders/debugDepthCompareShader.js +49 -0
- package/shaders/debugDepthCompareShader.ts +50 -0
- package/shaders/debugDepthJudgeShader.d.ts +18 -0
- package/shaders/debugDepthJudgeShader.js +52 -0
- package/shaders/debugDepthJudgeShader.ts +54 -0
- package/shaders/debugExcludedShader.d.ts +13 -0
- package/shaders/debugExcludedShader.js +46 -0
- package/shaders/debugExcludedShader.ts +48 -0
- package/spriteTextBg.d.ts +1 -0
- package/spriteTextBg.js +20 -0
- package/spriteTextBg.ts +20 -0
- package/threeLoader.d.ts +35 -34
- package/threeLoader.js +518 -527
- package/threeLoader.ts +525 -0
- package/threeUtils.d.ts +53 -52
- package/threeUtils.js +329 -330
- package/threeUtils.ts +373 -0
- package/types.d.ts +2 -0
- package/types.js +6 -0
- package/types.ts +8 -0
- package/ApngTexture.d.ts +0 -13
- package/ApngTexture.js +0 -61
- package/ApngTextureManage.d.ts +0 -18
- package/ApngTextureManage.js +0 -87
- package/KeyFrameAnimation.d.ts +0 -25
- package/KeyFrameAnimation.js +0 -78
- package/keyContorls.d.ts +0 -25
- package/lib/draco/draco_decoder.js +0 -34
- package/lib/draco/draco_decoder.wasm +0 -0
- package/lib/draco/draco_wasm_wrapper.js +0 -117
- package/shaders.d.ts +0 -5
- package/shaders.js +0 -93
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2024-02-05 09:31:57
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2025-06-25
|
|
6
|
+
*/
|
|
7
|
+
import { first, isString, throttle } from "lodash";
|
|
8
|
+
import { Color, Event, Euler, InstancedBufferAttribute, InstancedMesh, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from "three";
|
|
9
|
+
import MeshBase from "../MeshBase";
|
|
10
|
+
import threeUtils from "../threeUtils";
|
|
11
|
+
import threeLoader from "../threeLoader";
|
|
12
|
+
import { DebouncedFuncLeading } from "lodash";
|
|
13
|
+
import { dispatchCustomEvent, type InstancedMeshBaseEventMap } from "../EventMap";
|
|
14
|
+
|
|
15
|
+
// 实例模型组对象
|
|
16
|
+
export default class InstancedMeshBase extends MeshBase {
|
|
17
|
+
constructor(url?: string) {
|
|
18
|
+
super(url);
|
|
19
|
+
}
|
|
20
|
+
count: number = 0;
|
|
21
|
+
size: Vector3 = new Vector3(1, 1, 1);
|
|
22
|
+
meshCollect: Record<string, Mesh[]> = {};
|
|
23
|
+
instanceCollect: Record<string, InstancedMesh> = {};
|
|
24
|
+
colorMap: Map<String, Color> = new Map();
|
|
25
|
+
static _position: Vector3 = new Vector3();
|
|
26
|
+
static _scale: Vector3 = new Vector3(1, 1, 1);
|
|
27
|
+
static _rotation: Euler = new Euler();
|
|
28
|
+
static _Quaternion: Quaternion = new Quaternion();
|
|
29
|
+
static _matrix: Matrix4 = new Matrix4();
|
|
30
|
+
instanceMatrix: InstancedBufferAttribute = new InstancedBufferAttribute(new Float32Array(0 * 16), 16);
|
|
31
|
+
instanceColor: InstancedBufferAttribute = new InstancedBufferAttribute(new Float32Array(0 * 3), 3);
|
|
32
|
+
// 重置子元素
|
|
33
|
+
reset(...object: Object3D[]): this {
|
|
34
|
+
super.clear();
|
|
35
|
+
super.add(...object);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
// 重构加载模型的方法
|
|
39
|
+
async loadModel(url: string | File | Array<File>, filename: string = ""): Promise<void> {
|
|
40
|
+
//获取模型文件的地址
|
|
41
|
+
const modelurl = await (async (): Promise<string> => {
|
|
42
|
+
if (isString(url)) {
|
|
43
|
+
return url;
|
|
44
|
+
}
|
|
45
|
+
else if (url instanceof File) {
|
|
46
|
+
return url.name;
|
|
47
|
+
}
|
|
48
|
+
else if (url instanceof Array) {
|
|
49
|
+
let modelurl = "";
|
|
50
|
+
const suffixs = [
|
|
51
|
+
"zip",
|
|
52
|
+
"obj",
|
|
53
|
+
"glb",
|
|
54
|
+
"gltf",
|
|
55
|
+
"fbx",
|
|
56
|
+
"dae",
|
|
57
|
+
"stl",
|
|
58
|
+
"usdz",
|
|
59
|
+
];
|
|
60
|
+
for (let file of url) {
|
|
61
|
+
if (suffixs.includes(threeUtils.getSuffix(file.name))) {
|
|
62
|
+
modelurl = file.name;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return modelurl;
|
|
66
|
+
}
|
|
67
|
+
return "";
|
|
68
|
+
})();
|
|
69
|
+
this.url = modelurl || "";
|
|
70
|
+
if (!url) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
this.loaded = false;
|
|
74
|
+
// 没有模型并且没有加载任务时就去加载模型
|
|
75
|
+
if (!MeshBase.models.get(this.url) &&
|
|
76
|
+
!MeshBase.loadModeling.get(this.url)) {
|
|
77
|
+
MeshBase.loadModeling.set(this.url, true);
|
|
78
|
+
// 万能加载器
|
|
79
|
+
let modal = await threeLoader.loadModel(url, filename);
|
|
80
|
+
MeshBase.models.set(this.url, modal);
|
|
81
|
+
MeshBase.loadModeling.set(this.url, false);
|
|
82
|
+
}
|
|
83
|
+
// 如果模型还在加载中, 就等待模型加载完成
|
|
84
|
+
do {
|
|
85
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
86
|
+
} while (MeshBase.loadModeling.get(this.url));
|
|
87
|
+
if (MeshBase.models.get(this.url)) {
|
|
88
|
+
const mesh = MeshBase.models.get(this.url)!;
|
|
89
|
+
// 兜底:与 MeshBase 一致,为缺失顶点法线的几何体补算法线,避免 AO 全黑与金属材质发黑
|
|
90
|
+
this.ensureNormals(mesh);
|
|
91
|
+
this.size.copy(threeUtils.getSize(mesh));
|
|
92
|
+
const { meshCollect, instanceCollect } = this;
|
|
93
|
+
mesh.updateMatrixWorld(true);
|
|
94
|
+
// collect all mesh counts
|
|
95
|
+
mesh.traverse((obj) => {
|
|
96
|
+
const mesh = obj as Mesh;
|
|
97
|
+
if (mesh.isMesh) {
|
|
98
|
+
mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
|
|
99
|
+
mesh.rotation.setFromQuaternion(mesh.quaternion);
|
|
100
|
+
mesh.updateMatrix();
|
|
101
|
+
const materialUuid = mesh.material instanceof Array ? mesh.material[0].uuid : mesh.material.uuid;
|
|
102
|
+
const uuid = mesh.geometry.uuid + "/" + materialUuid;
|
|
103
|
+
if (meshCollect[uuid]) {
|
|
104
|
+
meshCollect[uuid].push(mesh);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
meshCollect[uuid] = [mesh];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
obj.position.set(0, 0, 0);
|
|
112
|
+
obj.scale.set(1, 1, 1);
|
|
113
|
+
obj.rotation.set(0, 0, 0);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
this.clear();
|
|
117
|
+
// create all instances
|
|
118
|
+
Object.keys(meshCollect).forEach((uuid) => {
|
|
119
|
+
const meshes = meshCollect[uuid];
|
|
120
|
+
const mesh = meshes[0];
|
|
121
|
+
const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * this.count);
|
|
122
|
+
instancedMesh.name = mesh.name;
|
|
123
|
+
instancedMesh.frustumCulled = false;
|
|
124
|
+
instanceCollect[uuid] = instancedMesh;
|
|
125
|
+
this.add(instancedMesh);
|
|
126
|
+
});
|
|
127
|
+
// 加载完成事件
|
|
128
|
+
await new Promise((resolve) => setTimeout(resolve, 30));
|
|
129
|
+
this.loaded = true;
|
|
130
|
+
dispatchCustomEvent(this, { type: "loaded" });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// 更新包围盒
|
|
134
|
+
computeBoundingSphere: DebouncedFuncLeading<() => void> = throttle(() => {
|
|
135
|
+
for (const child of this.children) {
|
|
136
|
+
const instancedMesh = child as InstancedMesh;
|
|
137
|
+
instancedMesh.computeBoundingSphere && instancedMesh.computeBoundingSphere();
|
|
138
|
+
}
|
|
139
|
+
}, 1000);
|
|
140
|
+
//初始实例矩阵的数量
|
|
141
|
+
async setCount(count: number = 0): Promise<void> {
|
|
142
|
+
if (this.count === count) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
await this.awaitLoaded();
|
|
146
|
+
const instanceMatrix = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
147
|
+
const instanceColor = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
148
|
+
for (let i = 0; i < count; i++) {
|
|
149
|
+
instanceMatrix.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
150
|
+
instanceColor.set([1, 1, 1], i * 3);
|
|
151
|
+
}
|
|
152
|
+
for (let i = 0; i < this.count; i++) {
|
|
153
|
+
instanceMatrix.copyAt(i, this.instanceMatrix, i);
|
|
154
|
+
instanceColor.copyAt(i, this.instanceColor, i);
|
|
155
|
+
}
|
|
156
|
+
this.instanceMatrix = instanceMatrix;
|
|
157
|
+
this.instanceColor = instanceColor;
|
|
158
|
+
this.count = count;
|
|
159
|
+
for (const child of this.children) {
|
|
160
|
+
const instanceMesh = child as InstancedMesh;
|
|
161
|
+
const im = new InstancedBufferAttribute(new Float32Array(count * 16), 16);
|
|
162
|
+
const ic = new InstancedBufferAttribute(new Float32Array(count * 3).fill(1), 3);
|
|
163
|
+
for (let i = 0; i < count; i++) {
|
|
164
|
+
im.set(InstancedMeshBase._matrix.identity().toArray(), i * 16);
|
|
165
|
+
ic.set([1, 1, 1], i * 3);
|
|
166
|
+
}
|
|
167
|
+
for (let i = 0; i < instanceMesh.count; i++) {
|
|
168
|
+
im.copyAt(i, instanceMesh.instanceMatrix, i);
|
|
169
|
+
if (instanceMesh.instanceColor) {
|
|
170
|
+
ic.copyAt(i, instanceMesh.instanceColor, i);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
instanceMesh.instanceMatrix = im;
|
|
174
|
+
instanceMesh.instanceColor = ic;
|
|
175
|
+
instanceMesh.count = count;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// 设置矩阵
|
|
179
|
+
async setMatrixAt(index: number, matrix: Matrix4 = new Matrix4().identity()): Promise<void> {
|
|
180
|
+
const new_matrix = matrix.clone();
|
|
181
|
+
await this.awaitLoaded();
|
|
182
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
183
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
184
|
+
const collect = this.meshCollect[uuid];
|
|
185
|
+
collect.forEach((mesh, i) => {
|
|
186
|
+
InstancedMeshBase._matrix.copy(mesh.matrix);
|
|
187
|
+
InstancedMeshBase._matrix.premultiply(new_matrix);
|
|
188
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
189
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
this.computeBoundingSphere();
|
|
193
|
+
}
|
|
194
|
+
//设置移动旋转缩放
|
|
195
|
+
async setTRS(index: number, { position, rotation, scale, }: {
|
|
196
|
+
position?: Vector3;
|
|
197
|
+
rotation?: Euler;
|
|
198
|
+
scale?: Vector3;
|
|
199
|
+
}): Promise<void> {
|
|
200
|
+
const new_position = position ? position.clone() : undefined;
|
|
201
|
+
const new_rotation = rotation ? rotation.clone() : undefined;
|
|
202
|
+
const new_scale = scale ? scale.clone() : undefined;
|
|
203
|
+
await this.awaitLoaded();
|
|
204
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
205
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
206
|
+
const collect = this.meshCollect[uuid];
|
|
207
|
+
collect.forEach((mesh, i) => {
|
|
208
|
+
instancedMesh.getMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
209
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix.clone().invert());
|
|
210
|
+
InstancedMeshBase._matrix.decompose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
211
|
+
InstancedMeshBase._rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
212
|
+
new_position && InstancedMeshBase._position.copy(new_position);
|
|
213
|
+
new_rotation && InstancedMeshBase._rotation.copy(new_rotation);
|
|
214
|
+
new_scale && InstancedMeshBase._scale.copy(new_scale);
|
|
215
|
+
InstancedMeshBase._Quaternion.setFromEuler(InstancedMeshBase._rotation);
|
|
216
|
+
InstancedMeshBase._matrix.compose(InstancedMeshBase._position, InstancedMeshBase._Quaternion, InstancedMeshBase._scale);
|
|
217
|
+
InstancedMeshBase._matrix.premultiply(mesh.matrix);
|
|
218
|
+
instancedMesh.setMatrixAt(collect.length * index + i, InstancedMeshBase._matrix);
|
|
219
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
this.computeBoundingSphere();
|
|
223
|
+
}
|
|
224
|
+
//获取指定位置的移动旋转缩放
|
|
225
|
+
async getTRS(index: number = 0): Promise<{
|
|
226
|
+
position: Vector3;
|
|
227
|
+
rotation: Euler;
|
|
228
|
+
scale: Vector3;
|
|
229
|
+
}> {
|
|
230
|
+
await this.awaitLoaded();
|
|
231
|
+
const position = new Vector3();
|
|
232
|
+
const rotation = new Euler();
|
|
233
|
+
const scale = new Vector3();
|
|
234
|
+
const uuid = first(Object.keys(this.instanceCollect));
|
|
235
|
+
const instancedMesh = this.instanceCollect[uuid!];
|
|
236
|
+
if (instancedMesh) {
|
|
237
|
+
instancedMesh.getMatrixAt(index, InstancedMeshBase._matrix);
|
|
238
|
+
InstancedMeshBase._matrix.decompose(position, InstancedMeshBase._Quaternion, scale);
|
|
239
|
+
rotation.setFromQuaternion(InstancedMeshBase._Quaternion);
|
|
240
|
+
}
|
|
241
|
+
return { position, rotation, scale };
|
|
242
|
+
}
|
|
243
|
+
// 设置颜色
|
|
244
|
+
async setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): Promise<void> {
|
|
245
|
+
await this.awaitLoaded();
|
|
246
|
+
Object.keys(this.meshCollect).forEach((uuid) => {
|
|
247
|
+
const instancedMesh = this.instanceCollect[uuid];
|
|
248
|
+
const collect = this.meshCollect[uuid];
|
|
249
|
+
collect.forEach((mesh, i) => {
|
|
250
|
+
const c = color(instancedMesh);
|
|
251
|
+
if (c) {
|
|
252
|
+
instancedMesh.setColorAt(collect.length * index + i, c);
|
|
253
|
+
instancedMesh.instanceColor.needsUpdate = true;
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
// 获取颜色
|
|
259
|
+
async getColorAt(index: number = 0): Promise<Color> {
|
|
260
|
+
await this.awaitLoaded();
|
|
261
|
+
const color = new Color();
|
|
262
|
+
const uuid = first(Object.keys(this.instanceCollect));
|
|
263
|
+
const instancedMesh = this.instanceCollect[uuid!];
|
|
264
|
+
if (instancedMesh) {
|
|
265
|
+
instancedMesh.getColorAt(index, color);
|
|
266
|
+
}
|
|
267
|
+
return color;
|
|
268
|
+
}
|
|
269
|
+
// 同步属性
|
|
270
|
+
updateMatrixWorld(force?: boolean): void {
|
|
271
|
+
super.updateMatrixWorld(force);
|
|
272
|
+
const { cursor, enableEvent } = this.userData;
|
|
273
|
+
this.traverse((obj) => {
|
|
274
|
+
Object.assign(obj.userData, { cursor, enableEvent });
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// 事件监听
|
|
278
|
+
async addEventListener<T extends string>(name: T, callback: (e: Event<T, this>) => void): Promise<void> {
|
|
279
|
+
super.addEventListener(name, callback);
|
|
280
|
+
await this.awaitLoaded();
|
|
281
|
+
for (const child of this.children) {
|
|
282
|
+
(child as InstancedMesh).addEventListener(name, callback as unknown as (e: Event<T, InstancedMesh>) => void);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// 取消事件的监听
|
|
286
|
+
removeEventListener<T extends string>(name: T, callback: (e: Event<T, this>) => void): void {
|
|
287
|
+
super.removeEventListener(name, callback);
|
|
288
|
+
for (const child of this.children) {
|
|
289
|
+
(child as InstancedMesh).removeEventListener(name, callback as unknown as (e: Event<T, InstancedMesh>) => void);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { AnimationClip, BufferGeometry, Material, Matrix4, Object3D, Skeleton } from "three";
|
|
2
|
+
import InstancedChild from "./InstancedChild";
|
|
3
|
+
/** 一个 submesh 的描述:几何 + 材质 + 是否骨骼网格 */
|
|
4
|
+
export interface InstancedSubmesh {
|
|
5
|
+
geometry: BufferGeometry;
|
|
6
|
+
material: Material;
|
|
7
|
+
skinned: boolean;
|
|
8
|
+
/** 非骨骼动画:拥有该几何的节点(取世界变换烘焙进几何) */
|
|
9
|
+
node?: Object3D;
|
|
10
|
+
/** 非骨骼动画:原始局部几何副本(每帧重新变换的源,避免累积误差) */
|
|
11
|
+
localGeometry?: BufferGeometry;
|
|
12
|
+
/**
|
|
13
|
+
* 该 submesh 在 GLB 场景图里的世界变换(加载时快照)。
|
|
14
|
+
* GLTF 常把模型整体包在一层带缩放/位移的根节点里(如 Soldier.glb 根节点 scale=0.01),
|
|
15
|
+
* 只克隆几何会丢掉这层变换导致模型大小/位置错乱。写实例矩阵时把它乘到 child.matrixWorld 上,
|
|
16
|
+
* 使实例化渲染与"把整棵 GLB 场景图加进场景"的效果一致(对齐 MeshBase 行为)。
|
|
17
|
+
*/
|
|
18
|
+
nodeWorldMatrix?: Matrix4;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 模板:描述"一类实例"的渲染数据。
|
|
22
|
+
* - 内置几何:submeshes 含一份几何+材质;
|
|
23
|
+
* - GLB:submeshes 按 geometry/material uuid 分组(多 submesh),并可能带 skeleton/animations。
|
|
24
|
+
*/
|
|
25
|
+
export interface InstancedTemplate {
|
|
26
|
+
submeshes: InstancedSubmesh[];
|
|
27
|
+
skeleton?: Skeleton;
|
|
28
|
+
animations: AnimationClip[];
|
|
29
|
+
/** 含骨骼时:每帧把骨骼姿态烘焙进 sharedGeometry,所有实例复用(共享姿态) */
|
|
30
|
+
sharedGeometry?: BufferGeometry;
|
|
31
|
+
/**
|
|
32
|
+
* 含骨骼时:原始 bind-pose 几何(只读,永不修改)。
|
|
33
|
+
* bakeSkinning 必须每帧从这里读"绑定姿态顶点",再写到 sharedGeometry,
|
|
34
|
+
* 绝不能原地读改写 sharedGeometry(否则上一帧的烘焙结果被当成 bind-pose 再烤一遍 → 蒙皮累积错位、越动越乱)。
|
|
35
|
+
*/
|
|
36
|
+
sourceGeometry?: BufferGeometry;
|
|
37
|
+
/** 含骨骼时:动画驱动用根对象(含 skeleton 层级,不渲染,仅用于 mixer 更新骨骼) */
|
|
38
|
+
animRoot?: Object3D;
|
|
39
|
+
/** 含骨骼时:SkinnedMesh 的 bindMatrix / bindMatrixInverse,烘焙蒙皮用 */
|
|
40
|
+
bindMatrix?: Matrix4;
|
|
41
|
+
bindMatrixInverse?: Matrix4;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 解析某实例的模板:内置几何同步返回;GLB 异步加载并按 batchKey 缓存。
|
|
45
|
+
*/
|
|
46
|
+
export declare function loadTemplate(child: InstancedChild): Promise<InstancedTemplate>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Matrix4, } from "three";
|
|
2
|
+
import threeLoader from "../threeLoader";
|
|
3
|
+
/** 按 batchKey 缓存模板加载 Promise,避免同一 GLB 重复加载 */
|
|
4
|
+
const _templateCache = new Map();
|
|
5
|
+
function uuidOf(geometry, material) {
|
|
6
|
+
return `${geometry.uuid}/${material.uuid}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 解析某实例的模板:内置几何同步返回;GLB 异步加载并按 batchKey 缓存。
|
|
10
|
+
*/
|
|
11
|
+
export function loadTemplate(child) {
|
|
12
|
+
const ctor = child.constructor;
|
|
13
|
+
// 内置几何:同步构造
|
|
14
|
+
if (ctor.geometry && ctor.material) {
|
|
15
|
+
return Promise.resolve({
|
|
16
|
+
submeshes: [
|
|
17
|
+
{ geometry: ctor.geometry, material: ctor.material, skinned: false },
|
|
18
|
+
],
|
|
19
|
+
animations: [],
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
// GLB:异步加载 + 按 batchKey 缓存
|
|
23
|
+
if (ctor.instanceUrl) {
|
|
24
|
+
const key = child.getBatchKey();
|
|
25
|
+
const cached = _templateCache.get(key);
|
|
26
|
+
if (cached) {
|
|
27
|
+
return cached;
|
|
28
|
+
}
|
|
29
|
+
const p = resolveGLB(ctor.instanceUrl);
|
|
30
|
+
_templateCache.set(key, p);
|
|
31
|
+
return p;
|
|
32
|
+
}
|
|
33
|
+
return Promise.reject(new Error("[loadTemplate] 子类未定义 geometry+material 或 instanceUrl"));
|
|
34
|
+
}
|
|
35
|
+
/** 加载 GLB 并按 geometry/material 分组,提取首个 skeleton 与动画剪辑 */
|
|
36
|
+
function resolveGLB(url) {
|
|
37
|
+
return threeLoader.loadModel(url).then((root) => {
|
|
38
|
+
if (!root) {
|
|
39
|
+
throw new Error(`[resolveGLB] 加载失败: ${url}`);
|
|
40
|
+
}
|
|
41
|
+
// 确保场景图世界矩阵已就绪,下面快照每个 submesh 的 nodeWorldMatrix 才准确
|
|
42
|
+
root.updateMatrixWorld(true);
|
|
43
|
+
const geometryMap = new Map();
|
|
44
|
+
let skeleton;
|
|
45
|
+
let firstSkinned;
|
|
46
|
+
let animations = [];
|
|
47
|
+
root.traverse((obj) => {
|
|
48
|
+
const mesh = obj;
|
|
49
|
+
const isMesh = mesh.isMesh === true;
|
|
50
|
+
if (!isMesh) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const geom = mesh.geometry;
|
|
54
|
+
const rawMat = mesh.material;
|
|
55
|
+
const mat = Array.isArray(rawMat) ? rawMat[0] : rawMat;
|
|
56
|
+
const skinned = mesh.isSkinnedMesh === true;
|
|
57
|
+
const key = uuidOf(geom, mat);
|
|
58
|
+
if (!geometryMap.has(key)) {
|
|
59
|
+
geometryMap.set(key, {
|
|
60
|
+
geometry: geom,
|
|
61
|
+
material: mat,
|
|
62
|
+
skinned,
|
|
63
|
+
node: mesh,
|
|
64
|
+
// nodeWorldMatrix 先留空,下方按 骨骼/节点动画 类型统一确定
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (skinned && !firstSkinned) {
|
|
68
|
+
firstSkinned = mesh;
|
|
69
|
+
skeleton = mesh.skeleton;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
// GLTF 加载器把 animations 挂在 scene 上(threeLoader 已 res.scene.animations = res.animations)
|
|
73
|
+
const rootAnims = root.animations;
|
|
74
|
+
if (rootAnims && rootAnims.length) {
|
|
75
|
+
animations = rootAnims;
|
|
76
|
+
}
|
|
77
|
+
const template = {
|
|
78
|
+
submeshes: Array.from(geometryMap.values()),
|
|
79
|
+
skeleton,
|
|
80
|
+
animations,
|
|
81
|
+
};
|
|
82
|
+
// 含骨骼:保留原始 bind-pose 几何(sourceGeometry,只读) + 一份渲染用几何(sharedGeometry,每帧被烘焙覆盖)。
|
|
83
|
+
// 二者必须分开:bakeSkinning 从 sourceGeometry 读、写 sharedGeometry,避免原地累积烘焙导致动画越动越乱。
|
|
84
|
+
if (firstSkinned && skeleton) {
|
|
85
|
+
const src = firstSkinned.geometry;
|
|
86
|
+
template.sourceGeometry = src;
|
|
87
|
+
template.sharedGeometry = src.clone();
|
|
88
|
+
template.animRoot = root;
|
|
89
|
+
template.bindMatrix = firstSkinned.bindMatrix;
|
|
90
|
+
template.bindMatrixInverse = firstSkinned.bindMatrixInverse;
|
|
91
|
+
}
|
|
92
|
+
// 非骨骼动画(节点变换动画,如 point1.glb):克隆几何,每帧把节点相对根的世界变换烘焙进几何。
|
|
93
|
+
// 与骨骼 LBS 同理 —— 把"节点层级动画"降级为每帧 CPU 几何变换,所有实例复用一份变换后的几何,
|
|
94
|
+
// 天然兼容项目所有材质与后处理。
|
|
95
|
+
if (!skeleton && animations.length > 0) {
|
|
96
|
+
template.animRoot = root;
|
|
97
|
+
for (const sub of template.submeshes) {
|
|
98
|
+
sub.localGeometry = sub.geometry.clone();
|
|
99
|
+
sub.geometry = sub.geometry.clone();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// 统一确定每个 submesh 的 nodeWorldMatrix:
|
|
103
|
+
// - 骨骼 / 静态:用 submesh 节点(mesh) 自身的完整世界变换(含祖先)。
|
|
104
|
+
// bakeSkinning 输出在 skinned mesh 局部空间、静态几何即 GLTF 原始局部空间,都需完整世界变换才能落到位;
|
|
105
|
+
// Soldier.glb 根节点 scale=0.01 这类"整模型套一层缩放根"的情形,靠这里补回
|
|
106
|
+
// (对齐 MeshBase 把整棵 GLB 场景图加进场景的行为)。
|
|
107
|
+
// - 非骨骼节点动画(如 point1.glb):bakeNodeTransform 已把"节点相对根的世界变换"
|
|
108
|
+
// 烘焙进几何(抵消了 animRoot),所以只需补 animRoot 的世界变换即可,
|
|
109
|
+
// 绝不能再用 mesh.matrixWorld(否则节点自身变换被重复应用 → 多节点模型整体扭曲)。
|
|
110
|
+
const animRootWorld = template.animRoot
|
|
111
|
+
? template.animRoot.matrixWorld.clone()
|
|
112
|
+
: new Matrix4();
|
|
113
|
+
for (const sub of template.submeshes) {
|
|
114
|
+
if (!skeleton && animations.length > 0) {
|
|
115
|
+
sub.nodeWorldMatrix = animRootWorld;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
sub.nodeWorldMatrix = sub.node
|
|
119
|
+
? sub.node.matrixWorld.clone()
|
|
120
|
+
: new Matrix4();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return template;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AnimationClip,
|
|
3
|
+
BufferGeometry,
|
|
4
|
+
Material,
|
|
5
|
+
Matrix4,
|
|
6
|
+
Object3D,
|
|
7
|
+
Skeleton,
|
|
8
|
+
SkinnedMesh,
|
|
9
|
+
} from "three";
|
|
10
|
+
import threeLoader from "../threeLoader";
|
|
11
|
+
import InstancedChild from "./InstancedChild";
|
|
12
|
+
|
|
13
|
+
/** 一个 submesh 的描述:几何 + 材质 + 是否骨骼网格 */
|
|
14
|
+
export interface InstancedSubmesh {
|
|
15
|
+
geometry: BufferGeometry;
|
|
16
|
+
material: Material;
|
|
17
|
+
skinned: boolean;
|
|
18
|
+
/** 非骨骼动画:拥有该几何的节点(取世界变换烘焙进几何) */
|
|
19
|
+
node?: Object3D;
|
|
20
|
+
/** 非骨骼动画:原始局部几何副本(每帧重新变换的源,避免累积误差) */
|
|
21
|
+
localGeometry?: BufferGeometry;
|
|
22
|
+
/**
|
|
23
|
+
* 该 submesh 在 GLB 场景图里的世界变换(加载时快照)。
|
|
24
|
+
* GLTF 常把模型整体包在一层带缩放/位移的根节点里(如 Soldier.glb 根节点 scale=0.01),
|
|
25
|
+
* 只克隆几何会丢掉这层变换导致模型大小/位置错乱。写实例矩阵时把它乘到 child.matrixWorld 上,
|
|
26
|
+
* 使实例化渲染与"把整棵 GLB 场景图加进场景"的效果一致(对齐 MeshBase 行为)。
|
|
27
|
+
*/
|
|
28
|
+
nodeWorldMatrix?: Matrix4;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 模板:描述"一类实例"的渲染数据。
|
|
33
|
+
* - 内置几何:submeshes 含一份几何+材质;
|
|
34
|
+
* - GLB:submeshes 按 geometry/material uuid 分组(多 submesh),并可能带 skeleton/animations。
|
|
35
|
+
*/
|
|
36
|
+
export interface InstancedTemplate {
|
|
37
|
+
submeshes: InstancedSubmesh[];
|
|
38
|
+
skeleton?: Skeleton;
|
|
39
|
+
animations: AnimationClip[];
|
|
40
|
+
/** 含骨骼时:每帧把骨骼姿态烘焙进 sharedGeometry,所有实例复用(共享姿态) */
|
|
41
|
+
sharedGeometry?: BufferGeometry;
|
|
42
|
+
/**
|
|
43
|
+
* 含骨骼时:原始 bind-pose 几何(只读,永不修改)。
|
|
44
|
+
* bakeSkinning 必须每帧从这里读"绑定姿态顶点",再写到 sharedGeometry,
|
|
45
|
+
* 绝不能原地读改写 sharedGeometry(否则上一帧的烘焙结果被当成 bind-pose 再烤一遍 → 蒙皮累积错位、越动越乱)。
|
|
46
|
+
*/
|
|
47
|
+
sourceGeometry?: BufferGeometry;
|
|
48
|
+
/** 含骨骼时:动画驱动用根对象(含 skeleton 层级,不渲染,仅用于 mixer 更新骨骼) */
|
|
49
|
+
animRoot?: Object3D;
|
|
50
|
+
/** 含骨骼时:SkinnedMesh 的 bindMatrix / bindMatrixInverse,烘焙蒙皮用 */
|
|
51
|
+
bindMatrix?: Matrix4;
|
|
52
|
+
bindMatrixInverse?: Matrix4;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 按 batchKey 缓存模板加载 Promise,避免同一 GLB 重复加载 */
|
|
56
|
+
const _templateCache = new Map<string, Promise<InstancedTemplate>>();
|
|
57
|
+
|
|
58
|
+
function uuidOf(geometry: BufferGeometry, material: Material): string {
|
|
59
|
+
return `${geometry.uuid}/${material.uuid}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 解析某实例的模板:内置几何同步返回;GLB 异步加载并按 batchKey 缓存。
|
|
64
|
+
*/
|
|
65
|
+
export function loadTemplate(child: InstancedChild): Promise<InstancedTemplate> {
|
|
66
|
+
const ctor = child.constructor as typeof InstancedChild;
|
|
67
|
+
// 内置几何:同步构造
|
|
68
|
+
if (ctor.geometry && ctor.material) {
|
|
69
|
+
return Promise.resolve({
|
|
70
|
+
submeshes: [
|
|
71
|
+
{ geometry: ctor.geometry, material: ctor.material, skinned: false },
|
|
72
|
+
],
|
|
73
|
+
animations: [],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// GLB:异步加载 + 按 batchKey 缓存
|
|
77
|
+
if (ctor.instanceUrl) {
|
|
78
|
+
const key = child.getBatchKey();
|
|
79
|
+
const cached = _templateCache.get(key);
|
|
80
|
+
if (cached) {
|
|
81
|
+
return cached;
|
|
82
|
+
}
|
|
83
|
+
const p = resolveGLB(ctor.instanceUrl);
|
|
84
|
+
_templateCache.set(key, p);
|
|
85
|
+
return p;
|
|
86
|
+
}
|
|
87
|
+
return Promise.reject(
|
|
88
|
+
new Error("[loadTemplate] 子类未定义 geometry+material 或 instanceUrl")
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 加载 GLB 并按 geometry/material 分组,提取首个 skeleton 与动画剪辑 */
|
|
93
|
+
function resolveGLB(url: string): Promise<InstancedTemplate> {
|
|
94
|
+
return threeLoader.loadModel(url).then((root) => {
|
|
95
|
+
if (!root) {
|
|
96
|
+
throw new Error(`[resolveGLB] 加载失败: ${url}`);
|
|
97
|
+
}
|
|
98
|
+
// 确保场景图世界矩阵已就绪,下面快照每个 submesh 的 nodeWorldMatrix 才准确
|
|
99
|
+
root.updateMatrixWorld(true);
|
|
100
|
+
const geometryMap = new Map<string, InstancedSubmesh>();
|
|
101
|
+
let skeleton: Skeleton | undefined;
|
|
102
|
+
let firstSkinned: SkinnedMesh | undefined;
|
|
103
|
+
let animations: AnimationClip[] = [];
|
|
104
|
+
|
|
105
|
+
root.traverse((obj: Object3D) => {
|
|
106
|
+
const mesh = obj as SkinnedMesh;
|
|
107
|
+
const isMesh = (mesh as any).isMesh === true;
|
|
108
|
+
if (!isMesh) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const geom = (mesh as any).geometry as BufferGeometry;
|
|
112
|
+
const rawMat = (mesh as any).material;
|
|
113
|
+
const mat: Material = Array.isArray(rawMat) ? rawMat[0] : rawMat;
|
|
114
|
+
const skinned = (mesh as any).isSkinnedMesh === true;
|
|
115
|
+
const key = uuidOf(geom, mat);
|
|
116
|
+
if (!geometryMap.has(key)) {
|
|
117
|
+
geometryMap.set(key, {
|
|
118
|
+
geometry: geom,
|
|
119
|
+
material: mat,
|
|
120
|
+
skinned,
|
|
121
|
+
node: mesh,
|
|
122
|
+
// nodeWorldMatrix 先留空,下方按 骨骼/节点动画 类型统一确定
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (skinned && !firstSkinned) {
|
|
126
|
+
firstSkinned = mesh;
|
|
127
|
+
skeleton = (mesh as any).skeleton as Skeleton | undefined;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// GLTF 加载器把 animations 挂在 scene 上(threeLoader 已 res.scene.animations = res.animations)
|
|
132
|
+
const rootAnims = (root as any).animations as AnimationClip[] | undefined;
|
|
133
|
+
if (rootAnims && rootAnims.length) {
|
|
134
|
+
animations = rootAnims;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const template: InstancedTemplate = {
|
|
138
|
+
submeshes: Array.from(geometryMap.values()),
|
|
139
|
+
skeleton,
|
|
140
|
+
animations,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// 含骨骼:保留原始 bind-pose 几何(sourceGeometry,只读) + 一份渲染用几何(sharedGeometry,每帧被烘焙覆盖)。
|
|
144
|
+
// 二者必须分开:bakeSkinning 从 sourceGeometry 读、写 sharedGeometry,避免原地累积烘焙导致动画越动越乱。
|
|
145
|
+
if (firstSkinned && skeleton) {
|
|
146
|
+
const src = firstSkinned.geometry as BufferGeometry;
|
|
147
|
+
template.sourceGeometry = src;
|
|
148
|
+
template.sharedGeometry = src.clone();
|
|
149
|
+
template.animRoot = root;
|
|
150
|
+
template.bindMatrix = firstSkinned.bindMatrix;
|
|
151
|
+
template.bindMatrixInverse = firstSkinned.bindMatrixInverse;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// 非骨骼动画(节点变换动画,如 point1.glb):克隆几何,每帧把节点相对根的世界变换烘焙进几何。
|
|
155
|
+
// 与骨骼 LBS 同理 —— 把"节点层级动画"降级为每帧 CPU 几何变换,所有实例复用一份变换后的几何,
|
|
156
|
+
// 天然兼容项目所有材质与后处理。
|
|
157
|
+
if (!skeleton && animations.length > 0) {
|
|
158
|
+
template.animRoot = root;
|
|
159
|
+
for (const sub of template.submeshes) {
|
|
160
|
+
sub.localGeometry = sub.geometry.clone();
|
|
161
|
+
sub.geometry = sub.geometry.clone();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 统一确定每个 submesh 的 nodeWorldMatrix:
|
|
166
|
+
// - 骨骼 / 静态:用 submesh 节点(mesh) 自身的完整世界变换(含祖先)。
|
|
167
|
+
// bakeSkinning 输出在 skinned mesh 局部空间、静态几何即 GLTF 原始局部空间,都需完整世界变换才能落到位;
|
|
168
|
+
// Soldier.glb 根节点 scale=0.01 这类"整模型套一层缩放根"的情形,靠这里补回
|
|
169
|
+
// (对齐 MeshBase 把整棵 GLB 场景图加进场景的行为)。
|
|
170
|
+
// - 非骨骼节点动画(如 point1.glb):bakeNodeTransform 已把"节点相对根的世界变换"
|
|
171
|
+
// 烘焙进几何(抵消了 animRoot),所以只需补 animRoot 的世界变换即可,
|
|
172
|
+
// 绝不能再用 mesh.matrixWorld(否则节点自身变换被重复应用 → 多节点模型整体扭曲)。
|
|
173
|
+
const animRootWorld = template.animRoot
|
|
174
|
+
? template.animRoot.matrixWorld.clone()
|
|
175
|
+
: new Matrix4();
|
|
176
|
+
for (const sub of template.submeshes) {
|
|
177
|
+
if (!skeleton && animations.length > 0) {
|
|
178
|
+
sub.nodeWorldMatrix = animRootWorld;
|
|
179
|
+
} else {
|
|
180
|
+
sub.nodeWorldMatrix = sub.node
|
|
181
|
+
? sub.node.matrixWorld.clone()
|
|
182
|
+
: new Matrix4();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return template;
|
|
187
|
+
});
|
|
188
|
+
}
|
package/keyControls.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="lodash" />
|
|
2
|
+
import { EventDispatcher, EventListener, Vector3 } from "three";
|
|
3
|
+
import type { KeyControlsEventTypeMap, KeyAddEvent, KeyDeleteEvent, KeyEnterEvent } from "./EventMap";
|
|
4
|
+
/**
|
|
5
|
+
* 键盘 使用 ↑↓←→ 控制 x z 方向, 小键盘 1 0 控制 y 方向
|
|
6
|
+
*
|
|
7
|
+
* @class KeyControls
|
|
8
|
+
* @extends {EventDispatcher<KeyControlsEventTypeMap>}
|
|
9
|
+
*/
|
|
10
|
+
declare class KeyControls extends EventDispatcher<KeyControlsEventTypeMap> {
|
|
11
|
+
constructor();
|
|
12
|
+
uuid: string;
|
|
13
|
+
direction: Vector3;
|
|
14
|
+
_listeners?: {
|
|
15
|
+
[key: string]: Array<(event: {
|
|
16
|
+
type: string;
|
|
17
|
+
}) => void>;
|
|
18
|
+
};
|
|
19
|
+
add(e: KeyAddEvent): void;
|
|
20
|
+
delete(e: KeyboardEvent | KeyDeleteEvent): void;
|
|
21
|
+
enter(e: KeyboardEvent | KeyEnterEvent): void;
|
|
22
|
+
keyDown: (e: KeyboardEvent) => void;
|
|
23
|
+
removeEvent: import("lodash").DebouncedFuncLeading<(type: string) => void>;
|
|
24
|
+
addEventListener<T extends string>(type: T, listener: EventListener<{}, T, this>): void;
|
|
25
|
+
addEvent(): void;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
}
|
|
28
|
+
declare const keyControls: KeyControls;
|
|
29
|
+
export { KeyControls, keyControls };
|
|
30
|
+
export default keyControls;
|