three-base 2.0.137 → 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.
Files changed (307) hide show
  1. package/DirectionalLight.d.ts +6 -6
  2. package/DirectionalLight.js +68 -68
  3. package/DirectionalLight.ts +70 -0
  4. package/EventMap.d.ts +103 -0
  5. package/EventMap.js +17 -0
  6. package/EventMap.ts +139 -0
  7. package/EventMesh.d.ts +18 -18
  8. package/EventMesh.js +59 -59
  9. package/EventMesh.ts +60 -0
  10. package/ExtrudeMesh.d.ts +112 -112
  11. package/ExtrudeMesh.js +342 -366
  12. package/ExtrudeMesh.ts +553 -0
  13. package/FPScontrol.d.ts +511 -78
  14. package/FPScontrol.js +1799 -617
  15. package/FPScontrol.ts +1855 -0
  16. package/Fire/Fire.d.ts +28 -28
  17. package/Fire/Fire.js +80 -79
  18. package/Fire/Fire.ts +89 -0
  19. package/Fire/{FireGromtry.d.ts → FireGeometry.d.ts} +9 -9
  20. package/Fire/{FireGromtry.js → FireGeometry.js} +40 -40
  21. package/Fire/FireGeometry.ts +40 -0
  22. package/Fire/FireMaterial.d.ts +28 -28
  23. package/Fire/FireMaterial.js +32 -32
  24. package/Fire/FireMaterial.ts +89 -0
  25. package/Fire/index.d.ts +2 -1
  26. package/Fire/index.js +2 -1
  27. package/Fire/index.ts +2 -0
  28. package/GLTFDracoLoader.d.ts +71 -0
  29. package/GLTFDracoLoader.js +266 -0
  30. package/GLTFDracoLoader.ts +430 -0
  31. package/Grass/Grass.d.ts +8 -8
  32. package/Grass/Grass.js +38 -38
  33. package/Grass/Grass.ts +38 -0
  34. package/Grass/index.d.ts +2 -1
  35. package/Grass/index.js +2 -1
  36. package/Grass/index.ts +2 -0
  37. package/Ground1/Ground1.d.ts +10 -11
  38. package/Ground1/Ground1.js +95 -91
  39. package/Ground1/Ground1.ts +114 -0
  40. package/Ground1/index.d.ts +1 -1
  41. package/Ground1/index.js +1 -1
  42. package/Ground1/index.ts +1 -0
  43. package/MeshBase.d.ts +36 -35
  44. package/MeshBase.js +247 -223
  45. package/MeshBase.ts +253 -0
  46. package/Mirror.d.ts +25 -25
  47. package/Mirror.js +143 -139
  48. package/Mirror.ts +203 -0
  49. package/MktMesh/MktMesh.d.ts +291 -0
  50. package/MktMesh/MktMesh.js +1045 -0
  51. package/MktMesh/MktMesh.ts +1075 -0
  52. package/MktMesh/assetUrl.d.ts +35 -0
  53. package/MktMesh/assetUrl.js +69 -0
  54. package/MktMesh/assetUrl.ts +69 -0
  55. package/MktMesh/mktMappingShader.d.ts +12 -0
  56. package/MktMesh/mktMappingShader.js +139 -0
  57. package/MktMesh/mktMappingShader.ts +139 -0
  58. package/MktMesh/mktShader.d.ts +263 -0
  59. package/MktMesh/mktShader.js +1248 -0
  60. package/MktMesh/mktShader.ts +1344 -0
  61. package/MktMesh/panoBitmapTexture.d.ts +25 -0
  62. package/MktMesh/panoBitmapTexture.js +52 -0
  63. package/MktMesh/panoBitmapTexture.ts +54 -0
  64. package/MktMesh/panoSlotBind.d.ts +29 -0
  65. package/MktMesh/panoSlotBind.js +106 -0
  66. package/MktMesh/panoSlotBind.ts +153 -0
  67. package/MktMesh/panoSlotRoster.d.ts +22 -0
  68. package/MktMesh/panoSlotRoster.js +169 -0
  69. package/MktMesh/panoSlotRoster.ts +168 -0
  70. package/MktMesh/panoTileStitcher.d.ts +119 -0
  71. package/MktMesh/panoTileStitcher.js +533 -0
  72. package/MktMesh/panoTileStitcher.ts +616 -0
  73. package/MktMesh/types.d.ts +75 -0
  74. package/MktMesh/types.js +1 -0
  75. package/MktMesh/types.ts +87 -0
  76. package/MktMesh/wallCapTessellate.d.ts +33 -0
  77. package/MktMesh/wallCapTessellate.js +97 -0
  78. package/MktMesh/wallCapTessellate.ts +68 -0
  79. package/PipeMesh/PathGeometry.d.ts +44 -44
  80. package/PipeMesh/PathGeometry.js +307 -300
  81. package/PipeMesh/PathGeometry.ts +343 -0
  82. package/PipeMesh/PathPoint.d.ts +17 -17
  83. package/PipeMesh/PathPoint.js +31 -31
  84. package/PipeMesh/PathPoint.ts +36 -0
  85. package/PipeMesh/PathPointList.d.ts +31 -31
  86. package/PipeMesh/PathPointList.js +209 -208
  87. package/PipeMesh/PathPointList.ts +227 -0
  88. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  89. package/PipeMesh/PathTubeGeometry.js +130 -130
  90. package/PipeMesh/PathTubeGeometry.ts +146 -0
  91. package/PipeMesh/PipeMesh.d.ts +25 -25
  92. package/PipeMesh/PipeMesh.js +63 -63
  93. package/PipeMesh/PipeMesh.ts +76 -0
  94. package/PipeMesh/index.d.ts +1 -1
  95. package/PipeMesh/index.js +1 -1
  96. package/PipeMesh/index.ts +1 -0
  97. package/PlaneModal1/PlaneModal1.d.ts +34 -31
  98. package/PlaneModal1/PlaneModal1.js +109 -105
  99. package/PlaneModal1/PlaneModal1.ts +138 -0
  100. package/PlaneModal1/index.d.ts +1 -1
  101. package/PlaneModal1/index.js +1 -1
  102. package/PlaneModal1/index.ts +1 -0
  103. package/PlaneText.d.ts +27 -39
  104. package/PlaneText.js +35 -30
  105. package/PlaneText.ts +55 -0
  106. package/PlaneWithHtml.d.ts +13 -11
  107. package/PlaneWithHtml.js +64 -63
  108. package/PlaneWithHtml.ts +70 -0
  109. package/PointLight/PointLight.d.ts +13 -14
  110. package/PointLight/PointLight.js +64 -70
  111. package/PointLight/PointLight.ts +67 -0
  112. package/PointLight/index.d.ts +1 -1
  113. package/PointLight/index.js +1 -1
  114. package/PointLight/index.ts +1 -0
  115. package/README.md +110 -79
  116. package/Sky/Sky.d.ts +9 -9
  117. package/Sky/Sky.js +22 -22
  118. package/Sky/Sky.ts +22 -0
  119. package/Sky/index.d.ts +1 -1
  120. package/Sky/index.js +1 -1
  121. package/Sky/index.ts +1 -0
  122. package/SpotLight.d.ts +6 -6
  123. package/SpotLight.js +72 -72
  124. package/SpotLight.ts +74 -0
  125. package/SpriteModal1/SpriteModal1.d.ts +33 -30
  126. package/SpriteModal1/SpriteModal1.js +116 -110
  127. package/SpriteModal1/SpriteModal1.ts +146 -0
  128. package/SpriteModal1/index.d.ts +1 -1
  129. package/SpriteModal1/index.js +1 -1
  130. package/SpriteModal1/index.ts +1 -0
  131. package/SpriteText.d.ts +46 -45
  132. package/SpriteText.js +176 -163
  133. package/SpriteText.ts +205 -0
  134. package/SpriteText2.d.ts +48 -47
  135. package/SpriteText2.js +223 -198
  136. package/SpriteText2.ts +258 -0
  137. package/SpriteText3.d.ts +86 -0
  138. package/SpriteText3.js +519 -0
  139. package/SpriteText3.ts +599 -0
  140. package/SpriteWithHtml.d.ts +9 -7
  141. package/SpriteWithHtml.js +44 -44
  142. package/SpriteWithHtml.ts +50 -0
  143. package/Text3D/Text3D.d.ts +20 -20
  144. package/Text3D/Text3D.js +58 -62
  145. package/Text3D/Text3D.ts +77 -0
  146. package/Text3D/fontController.d.ts +60 -59
  147. package/Text3D/fontController.js +166 -199
  148. package/Text3D/fontController.ts +209 -0
  149. package/Text3D/index.d.ts +1 -1
  150. package/Text3D/index.js +1 -1
  151. package/Text3D/index.ts +1 -0
  152. package/ThreeBase.d.ts +248 -111
  153. package/ThreeBase.js +1202 -656
  154. package/ThreeBase.ts +1314 -0
  155. package/ThreeMouse.d.ts +54 -40
  156. package/ThreeMouse.js +453 -407
  157. package/ThreeMouse.ts +472 -0
  158. package/Transition.d.ts +45 -40
  159. package/Transition.js +209 -210
  160. package/Transition.ts +242 -0
  161. package/Water/Water.d.ts +5 -5
  162. package/Water/Water.js +44 -26
  163. package/Water/Water.ts +45 -0
  164. package/Water/index.d.ts +2 -0
  165. package/Water/index.js +2 -0
  166. package/Water/index.ts +2 -0
  167. package/WaterFlow/FloatingDuck.d.ts +88 -0
  168. package/WaterFlow/FloatingDuck.js +307 -0
  169. package/WaterFlow/FloatingDuck.ts +445 -0
  170. package/WaterFlow/GPUFluidSimulation.d.ts +56 -0
  171. package/WaterFlow/GPUFluidSimulation.js +785 -0
  172. package/WaterFlow/GPUFluidSimulation.ts +827 -0
  173. package/WaterFlow/ReservoirSystem.d.ts +106 -0
  174. package/WaterFlow/ReservoirSystem.js +621 -0
  175. package/WaterFlow/ReservoirSystem.ts +754 -0
  176. package/WaterFlow/SPHField.d.ts +35 -0
  177. package/WaterFlow/SPHField.js +204 -0
  178. package/WaterFlow/SPHField.ts +228 -0
  179. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -0
  180. package/WaterFlow/ScreenSpaceFluidPass.js +736 -0
  181. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -0
  182. package/WaterFlow/ShallowWaterSurface.d.ts +45 -0
  183. package/WaterFlow/ShallowWaterSurface.js +441 -0
  184. package/WaterFlow/ShallowWaterSurface.ts +510 -0
  185. package/WaterFlow/WaterFlow.d.ts +87 -0
  186. package/WaterFlow/WaterFlow.js +171 -0
  187. package/WaterFlow/WaterFlow.ts +219 -0
  188. package/WaterFlow/types.d.ts +161 -0
  189. package/WaterFlow/types.js +1 -0
  190. package/WaterFlow/types.ts +189 -0
  191. package/WaterFlow/waterFlowPass.d.ts +3 -0
  192. package/WaterFlow/waterFlowPass.js +20 -0
  193. package/WaterFlow/waterFlowPass.ts +40 -0
  194. package/collisionOBB.d.ts +42 -0
  195. package/collisionOBB.js +112 -0
  196. package/collisionOBB.ts +108 -0
  197. package/declare.d.ts +23 -165
  198. package/index.d.ts +1 -1
  199. package/index.js +1 -2
  200. package/index.ts +1 -0
  201. package/instanced/InstancedBatch.d.ts +57 -0
  202. package/instanced/InstancedBatch.js +348 -0
  203. package/instanced/InstancedBatch.ts +406 -0
  204. package/instanced/InstancedChild.d.ts +174 -0
  205. package/instanced/InstancedChild.js +264 -0
  206. package/instanced/InstancedChild.ts +327 -0
  207. package/instanced/InstancedGroup.d.ts +43 -0
  208. package/instanced/InstancedGroup.js +126 -0
  209. package/instanced/InstancedGroup.ts +137 -0
  210. package/{InstancedMeshBase.d.ts → instanced/InstancedMeshBase.d.ts} +38 -39
  211. package/{InstancedMeshBase.js → instanced/InstancedMeshBase.js} +282 -293
  212. package/instanced/InstancedMeshBase.ts +292 -0
  213. package/instanced/InstancedTemplate.d.ts +46 -0
  214. package/instanced/InstancedTemplate.js +125 -0
  215. package/instanced/InstancedTemplate.ts +188 -0
  216. package/keyControls.d.ts +30 -0
  217. package/{keyContorls.js → keyControls.js} +101 -98
  218. package/keyControls.ts +107 -0
  219. package/lensflare/LensFlare.d.ts +39 -0
  220. package/lensflare/LensFlare.js +373 -0
  221. package/lensflare/LensFlare.ts +448 -0
  222. package/lensflare/lensflare0.png +0 -0
  223. package/lensflare/lensflare3.png +0 -0
  224. package/lensflare/lensflare_custom.png +0 -0
  225. package/lensflare/lensflare_hex_ghost.png +0 -0
  226. package/lensflare/lensflare_hex_main.png +0 -0
  227. package/package.json +99 -25
  228. package/pass/bloom_pass.d.ts +2 -2
  229. package/pass/bloom_pass.js +188 -101
  230. package/pass/bloom_pass.ts +243 -0
  231. package/pass/cover_pass.d.ts +2 -2
  232. package/pass/cover_pass.js +122 -99
  233. package/pass/cover_pass.ts +182 -0
  234. package/pass/debugDepthState.d.ts +20 -0
  235. package/pass/debugDepthState.js +9 -0
  236. package/pass/debugDepthState.ts +22 -0
  237. package/pass/depthTestBias.d.ts +12 -0
  238. package/pass/depthTestBias.js +12 -0
  239. package/pass/depthTestBias.ts +12 -0
  240. package/pass/effectUserData.d.ts +27 -0
  241. package/pass/effectUserData.js +1 -0
  242. package/pass/effectUserData.ts +46 -0
  243. package/pass/frosted_glass_pass.d.ts +2 -2
  244. package/pass/frosted_glass_pass.js +167 -186
  245. package/pass/frosted_glass_pass.ts +288 -0
  246. package/pass/fsQuad.d.ts +9 -9
  247. package/pass/fsQuad.js +28 -28
  248. package/pass/fsQuad.ts +34 -0
  249. package/pass/glow_pass.d.ts +2 -2
  250. package/pass/glow_pass.js +449 -233
  251. package/pass/glow_pass.ts +654 -0
  252. package/pass/screen_bloom_pass.d.ts +11 -0
  253. package/pass/screen_bloom_pass.js +235 -0
  254. package/pass/screen_bloom_pass.ts +263 -0
  255. package/pass/ssao_pass.d.ts +88 -13
  256. package/pass/ssao_pass.js +802 -366
  257. package/pass/ssao_pass.ts +918 -0
  258. package/pass/stroke_pass.d.ts +2 -2
  259. package/pass/stroke_pass.js +329 -258
  260. package/pass/stroke_pass.ts +638 -0
  261. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  262. package/shaders/FXAAShaderMaterial.js +23 -66
  263. package/shaders/FXAAShaderMaterial.ts +201 -0
  264. package/shaders/baseDepthVisShader.d.ts +11 -0
  265. package/shaders/baseDepthVisShader.js +36 -0
  266. package/shaders/baseDepthVisShader.ts +36 -0
  267. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  268. package/shaders/baseTextureToRenderShader.js +55 -60
  269. package/shaders/baseTextureToRenderShader.ts +232 -0
  270. package/shaders/copyShader.d.ts +3 -3
  271. package/shaders/copyShader.js +13 -14
  272. package/shaders/copyShader.ts +32 -0
  273. package/shaders/debugDepthCompareShader.d.ts +14 -0
  274. package/shaders/debugDepthCompareShader.js +49 -0
  275. package/shaders/debugDepthCompareShader.ts +50 -0
  276. package/shaders/debugDepthJudgeShader.d.ts +18 -0
  277. package/shaders/debugDepthJudgeShader.js +52 -0
  278. package/shaders/debugDepthJudgeShader.ts +54 -0
  279. package/shaders/debugExcludedShader.d.ts +13 -0
  280. package/shaders/debugExcludedShader.js +46 -0
  281. package/shaders/debugExcludedShader.ts +48 -0
  282. package/spriteTextBg.d.ts +1 -0
  283. package/spriteTextBg.js +20 -0
  284. package/spriteTextBg.ts +20 -0
  285. package/threeLoader.d.ts +35 -34
  286. package/threeLoader.js +518 -527
  287. package/threeLoader.ts +525 -0
  288. package/threeUtils.d.ts +53 -52
  289. package/threeUtils.js +329 -330
  290. package/threeUtils.ts +373 -0
  291. package/types.d.ts +2 -0
  292. package/types.js +6 -0
  293. package/types.ts +8 -0
  294. package/ApngTexture.d.ts +0 -13
  295. package/ApngTexture.js +0 -61
  296. package/ApngTextureManage.d.ts +0 -18
  297. package/ApngTextureManage.js +0 -87
  298. package/KeyFrameAnimation.d.ts +0 -25
  299. package/KeyFrameAnimation.js +0 -78
  300. package/keyContorls.d.ts +0 -25
  301. package/lib/draco/draco_decoder.js +0 -34
  302. package/lib/draco/draco_decoder.wasm +0 -0
  303. package/lib/draco/draco_wasm_wrapper.js +0 -117
  304. package/pass/emissive_ao_pass.d.ts +0 -13
  305. package/pass/emissive_ao_pass.js +0 -370
  306. package/shaders.d.ts +0 -5
  307. package/shaders.js +0 -93
package/MeshBase.ts ADDED
@@ -0,0 +1,253 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-02-24 22:49:42
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2025-06-25
6
+ */
7
+ import { AnimationAction, AnimationClip, AnimationMixer, Clock, Group, Mesh, Object3D, Vector3 } from "three";
8
+ import threeUtils from "./threeUtils";
9
+ import threeLoader from "./threeLoader";
10
+ import * as SkeletonUtils from "three/examples/jsm/utils/SkeletonUtils";
11
+ import { isString } from "lodash";
12
+ import { dispatchCustomEvent } from "./EventMap";
13
+
14
+ // 模型基类
15
+ export default class MeshBase extends Group {
16
+ constructor(url?: string) {
17
+ super();
18
+ url && this.loadModel(url);
19
+ }
20
+
21
+ url: string = "";
22
+ // 模型对象
23
+ static models: Map<string, Object3D> = new Map();
24
+ // 模型是否在加载中
25
+ static loadModeling: Map<string, boolean> = new Map();
26
+ // 加载完成
27
+ loaded: boolean = false;
28
+ // 是否有动画
29
+ hasAnimation: boolean = false;
30
+ //动画混合器
31
+ mixer?: AnimationMixer;
32
+ //动画剪辑对象数组
33
+ animationActions: {
34
+ [key: string]: AnimationAction;
35
+ } = {};
36
+ // 获取文件名的后缀
37
+ getSuffix(filename: string): string {
38
+ const match = filename.match(/\.([^\.]+)$/);
39
+ return match ? match[1].toLowerCase() : "";
40
+ }
41
+ reset(...object: Object3D[]): this {
42
+ threeUtils.dipose(this);
43
+ for (const child of this.children) {
44
+ child.parent = null;
45
+ }
46
+ for (const item of object) {
47
+ this.add(item);
48
+ }
49
+ return this;
50
+ }
51
+ // 兜底:为缺失顶点法线的几何体计算法线。
52
+ // 某些 glb/stl 模型仅有 POSITION 没有 NORMAL(如 public/models/bottle.glb),
53
+ // 会让法线 pass 输出零向量 -> AO 算出 NaN -> 整片被乘黑,且 PBR 金属材质无法正确受光 -> 发黑。
54
+ // 在加载完成后统一兜底,保证光照与 AO 都正确;已含法线的模型不受影响。
55
+ protected ensureNormals(root: Object3D): void {
56
+ root.traverse((obj) => {
57
+ if (obj instanceof Mesh && obj.geometry && !obj.geometry.getAttribute("normal")) {
58
+ obj.geometry.computeVertexNormals();
59
+ }
60
+ });
61
+ }
62
+ // 如果url 是blob地址的话,就用suffix字段来制定模型的类型
63
+ async loadModel(url: string | File | Array<File>, filename: string = ""): Promise<void> {
64
+ //获取模型文件的地址
65
+ const modelurl = await (async (): Promise<string> => {
66
+ if (isString(url)) {
67
+ return url;
68
+ }
69
+ else if (url instanceof File) {
70
+ return url.name;
71
+ }
72
+ else if (url instanceof Array) {
73
+ let modelurl = "";
74
+ const suffixs = [
75
+ "zip",
76
+ "obj",
77
+ "glb",
78
+ "gltf",
79
+ "fbx",
80
+ "dae",
81
+ "stl",
82
+ "usdz",
83
+ ];
84
+ for (let file of url) {
85
+ if (suffixs.includes(threeUtils.getSuffix(file.name))) {
86
+ modelurl = file.name;
87
+ }
88
+ }
89
+ return modelurl;
90
+ }
91
+ return "";
92
+ })();
93
+ this.url = modelurl || "";
94
+ if (!modelurl) {
95
+ return;
96
+ }
97
+ this.loaded = false;
98
+ // 没有模型并且没有加载任务时就去加载模型
99
+ if (!MeshBase.models.get(modelurl) &&
100
+ !MeshBase.loadModeling.get(modelurl)) {
101
+ MeshBase.loadModeling.set(modelurl, true);
102
+ // 万能加载器
103
+ let modal = await threeLoader.loadModel(url, filename);
104
+ MeshBase.models.set(modelurl, modal);
105
+ MeshBase.loadModeling.set(modelurl, false);
106
+ }
107
+ // 如果模型还在加载中, 就等待模型加载完成
108
+ do {
109
+ await new Promise((resolve) => setTimeout(resolve, 50));
110
+ } while (MeshBase.loadModeling.get(modelurl));
111
+ if (MeshBase.models.get(modelurl)) {
112
+ const mesh = MeshBase.models.get(modelurl)!;
113
+ // 兜底:为缺失顶点法线的几何体补算法线。某些 glb/stl 模型只有 POSITION,
114
+ // 没有 NORMAL,会导致:① 法线 pass 输出零向量 -> AO 算出 NaN -> 整片被乘黑;
115
+ // ② PBR 金属材质无法正确光照 -> 颜色发黑。统一在加载后兜底,光照与 AO 都正确。
116
+ this.ensureNormals(mesh);
117
+ // 判断是否有动画
118
+ this.hasAnimation = (mesh as Group & { animations: AnimationClip[] }).animations.length > 0;
119
+ if (this.hasAnimation) {
120
+ const model = SkeletonUtils.clone(mesh);
121
+ this.mixer = new AnimationMixer(model);
122
+ this.animationActions = {};
123
+ (mesh as Group & { animations: AnimationClip[] }).animations.map((animation: AnimationClip) => {
124
+ const animationAction = this.mixer!.clipAction(animation).play();
125
+ animationAction.weight = 0;
126
+ this.animationActions[animation.name] = animationAction;
127
+ });
128
+ this.reset(model);
129
+ }
130
+ else {
131
+ this.reset(mesh.clone(true));
132
+ }
133
+ }
134
+ await new Promise((resolve) => requestAnimationFrame(resolve));
135
+ this.loaded = true;
136
+ // 加载完成事件
137
+ dispatchCustomEvent(this, { type: "loaded" });
138
+ }
139
+ // 超出绘制区域时不绘制
140
+ frustumCulled: boolean = false;
141
+ // 创建阴影
142
+ castShadow: boolean = false;
143
+ // 接受阴影
144
+ receiveShadow: boolean = false;
145
+ // promise.resole 队列
146
+ resoles: Array<(value: void | PromiseLike<void>) => void> = [];
147
+ // 等待加载加载完成
148
+ async awaitLoaded(): Promise<void> {
149
+ if (!this.loaded) {
150
+ await new Promise<void>((resolve) => {
151
+ if (this.resoles.length === 0) {
152
+ (async () => {
153
+ while (!this.loaded) {
154
+ await new Promise((resolve) => requestAnimationFrame(resolve));
155
+ }
156
+ while (this.resoles.length > 0) {
157
+ const resole = this.resoles.pop();
158
+ resole && resole();
159
+ }
160
+ })();
161
+ }
162
+ this.resoles.unshift(resolve);
163
+ });
164
+ }
165
+ }
166
+ //遍历模型
167
+ async traverseMesh(callback: (mesh: Mesh) => void): Promise<void> {
168
+ // 等待加载完成
169
+ await this.awaitLoaded();
170
+ this.traverse((obj) => {
171
+ if (obj instanceof Mesh) {
172
+ callback(obj);
173
+ }
174
+ });
175
+ }
176
+ clock: Clock = new Clock();
177
+ //设置动画的权重
178
+ async setClipActionWeight(weights: {
179
+ [key: string]: number;
180
+ } = {}): Promise<void> {
181
+ // 等待加载完成
182
+ await this.awaitLoaded();
183
+ for (let key in this.animationActions) {
184
+ this.animationActions[key].weight = weights[key] || 0;
185
+ }
186
+ }
187
+ //获取动画列表
188
+ getAnimas(): string[] {
189
+ return Object.keys(this.animationActions);
190
+ }
191
+ //播放指定动画
192
+ setAnimas(anima: string = ""): void {
193
+ this.setClipActionWeight({ [anima]: 1 });
194
+ }
195
+ //停止所有动画
196
+ stop(): void {
197
+ this.setClipActionWeight();
198
+ }
199
+ updateMatrix(): void {
200
+ //播放动画
201
+ if (this.mixer) {
202
+ this.mixer.update(this.clock.getDelta());
203
+ }
204
+ // 深度应用属性
205
+ this.traverse((obj) => {
206
+ if (obj instanceof Mesh) {
207
+ obj.frustumCulled = this.frustumCulled;
208
+ obj.castShadow = this.castShadow;
209
+ obj.receiveShadow = this.receiveShadow;
210
+ }
211
+ });
212
+ super.updateMatrix();
213
+ }
214
+ // 获取模型的尺寸
215
+ async getSize(): Promise<Vector3> {
216
+ await this.awaitLoaded();
217
+ return threeUtils.getSize(this);
218
+ }
219
+ // 居中
220
+ async center(): Promise<void> {
221
+ await this.awaitLoaded();
222
+ threeUtils.center(this);
223
+ }
224
+ //底对齐
225
+ async alignBottom(y: number = 0): Promise<void> {
226
+ await this.awaitLoaded();
227
+ threeUtils.alignBottom(this, y);
228
+ }
229
+ // 放大到指定尺寸
230
+ async scaleTo(size: number = 1, key: Array<"x" | "y" | "z"> = ["x", "y", "z"]): Promise<void> {
231
+ await this.awaitLoaded();
232
+ threeUtils.scaleTo(this, size, key);
233
+ }
234
+ //按照法线拓展
235
+ expandBynormal(length: number): void {
236
+ this.traverseMesh((mesh) => {
237
+ // 没有法线就计算法线
238
+ mesh.geometry = mesh.geometry.clone();
239
+ if (!mesh.geometry.getAttribute("normal")) {
240
+ mesh.geometry.computeVertexNormals();
241
+ }
242
+ const normal = mesh.geometry.getAttribute("normal");
243
+ const position = mesh.geometry.getAttribute("position");
244
+ const v3 = new Vector3(0, 0, 0);
245
+ for (let i = 0; i < position.count; i++) {
246
+ v3.set(normal.getX(i), normal.getY(i), normal.getZ(i));
247
+ v3.setLength(length);
248
+ position.setXYZ(i, position.getX(i) + v3.x, position.getY(i) + v3.y, position.getZ(i) + v3.z);
249
+ }
250
+ position.needsUpdate = true;
251
+ });
252
+ }
253
+ }
package/Mirror.d.ts CHANGED
@@ -1,25 +1,25 @@
1
- import { BufferGeometry, Camera, Group, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
2
- export default class Mirror extends Mesh {
3
- constructor(geometry?: BufferGeometry);
4
- static geometry: PlaneGeometry;
5
- isReflector: boolean;
6
- type: string;
7
- camera: PerspectiveCamera;
8
- reflectorPlane: Plane;
9
- normal: Vector3;
10
- reflectorWorldPosition: Vector3;
11
- cameraWorldPosition: Vector3;
12
- rotationMatrix: Matrix4;
13
- lookAtPosition: Vector3;
14
- clipPlane: Vector4;
15
- view: Vector3;
16
- target: Vector3;
17
- q: Vector4;
18
- textureMatrix: Matrix4;
19
- virtualCamera: PerspectiveCamera;
20
- renderTarget: WebGLRenderTarget<import("three").Texture>;
21
- material: ShaderMaterial;
22
- onBeforeRender: (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => void;
23
- getRenderTarget(): WebGLRenderTarget<import("three").Texture>;
24
- dispose(): void;
25
- }
1
+ import { Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer, Scene, Camera, BufferGeometry, Material, Group, Texture } from "three";
2
+ export default class Mirror extends Mesh {
3
+ constructor(geometry?: BufferGeometry);
4
+ static geometry: PlaneGeometry;
5
+ isReflector: boolean;
6
+ type: string;
7
+ camera: PerspectiveCamera;
8
+ reflectorPlane: Plane;
9
+ normal: Vector3;
10
+ reflectorWorldPosition: Vector3;
11
+ cameraWorldPosition: Vector3;
12
+ rotationMatrix: Matrix4;
13
+ lookAtPosition: Vector3;
14
+ clipPlane: Vector4;
15
+ view: Vector3;
16
+ target: Vector3;
17
+ q: Vector4;
18
+ textureMatrix: Matrix4;
19
+ virtualCamera: PerspectiveCamera;
20
+ renderTarget: WebGLRenderTarget<Texture>;
21
+ material: ShaderMaterial;
22
+ onBeforeRender: (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => void;
23
+ getRenderTarget(): WebGLRenderTarget<Texture>;
24
+ dispose(): void;
25
+ }
package/Mirror.js CHANGED
@@ -1,51 +1,55 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-08-04 17:17:15
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-20 05:33:51
6
- */
7
- import { Color, DoubleSide, HalfFloatType, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget } from "three";
8
- // 镜子模型
9
- export default class Mirror extends Mesh {
10
- constructor(geometry = Mirror.geometry) {
11
- super(geometry);
12
- }
13
- // 正方形网格
14
- static geometry = (() => {
15
- const geometry = new PlaneGeometry(10, 10, 1, 1);
16
- return geometry;
17
- })();
18
- isReflector = true;
19
- type = 'Reflector';
20
- camera = new PerspectiveCamera();
21
- reflectorPlane = new Plane();
22
- normal = new Vector3();
23
- reflectorWorldPosition = new Vector3();
24
- cameraWorldPosition = new Vector3();
25
- rotationMatrix = new Matrix4();
26
- lookAtPosition = new Vector3(0, 0, -1);
27
- clipPlane = new Vector4();
28
- view = new Vector3();
29
- target = new Vector3();
30
- q = new Vector4();
31
- textureMatrix = new Matrix4();
32
- virtualCamera = this.camera;
33
- renderTarget = new WebGLRenderTarget(2048, 2048, { samples: 0, type: HalfFloatType });
34
- material = new ShaderMaterial({
35
- uniforms: {
36
- 'color': {
37
- value: new Color("#b6b6b6")
38
- },
39
- 'tDiffuse': {
40
- value: this.renderTarget.texture
41
- },
42
- 'textureMatrix': {
43
- value: this.textureMatrix
44
- },
45
- blurAmount: {
46
- value: 0.5
47
- } // 模糊程度,可以根据需求调整
48
- },
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-08-04 17:17:15
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2024-01-20 05:33:51
6
+ */
7
+ import { Color, DoubleSide, HalfFloatType, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget } from "three";
8
+ // 镜子模型
9
+ export default class Mirror extends Mesh {
10
+ constructor(geometry = Mirror.geometry) {
11
+ super(geometry);
12
+ // 镜子虽标了 transparent:true(为渲染顺序),但 fragment shader 实际输出 alpha=1.0,
13
+ // 是实心反射面、并非可透视表面 → 必须参与 AO 与描边/辉光的深度遮挡。
14
+ // SSAO 的 AO 数据准备会排除所有 transparent 物体,故此处显式声明保留。
15
+ this.userData.aoOpaque = true;
16
+ }
17
+ // 正方形网格
18
+ static geometry = (() => {
19
+ const geometry = new PlaneGeometry(10, 10, 1, 1);
20
+ return geometry;
21
+ })();
22
+ isReflector = true;
23
+ type = 'Reflector';
24
+ camera = new PerspectiveCamera();
25
+ reflectorPlane = new Plane();
26
+ normal = new Vector3();
27
+ reflectorWorldPosition = new Vector3();
28
+ cameraWorldPosition = new Vector3();
29
+ rotationMatrix = new Matrix4();
30
+ lookAtPosition = new Vector3(0, 0, -1);
31
+ clipPlane = new Vector4();
32
+ view = new Vector3();
33
+ target = new Vector3();
34
+ q = new Vector4();
35
+ textureMatrix = new Matrix4();
36
+ virtualCamera = this.camera;
37
+ renderTarget = new WebGLRenderTarget(2048, 2048, { type: HalfFloatType });
38
+ material = new ShaderMaterial({
39
+ uniforms: {
40
+ 'color': {
41
+ value: new Color("#b6b6b6")
42
+ },
43
+ 'tDiffuse': {
44
+ value: this.renderTarget.texture
45
+ },
46
+ 'textureMatrix': {
47
+ value: this.textureMatrix
48
+ },
49
+ blurAmount: {
50
+ value: 0.5
51
+ } // 模糊程度,可以根据需求调整
52
+ },
49
53
  vertexShader: /* glsl */ `
50
54
  uniform mat4 textureMatrix;
51
55
  varying vec4 vUv;
@@ -61,7 +65,7 @@ export default class Mirror extends Mesh {
61
65
 
62
66
  #include <logdepthbuf_vertex>
63
67
 
64
- }`,
68
+ }`,
65
69
  fragmentShader: /* glsl */ `
66
70
  uniform vec3 color;
67
71
  uniform sampler2D tDiffuse;
@@ -104,93 +108,93 @@ export default class Mirror extends Mesh {
104
108
 
105
109
  #include <tonemapping_fragment>
106
110
  #include <colorspace_fragment>
107
- }`,
108
- depthTest: true,
109
- depthWrite: true,
110
- transparent: true,
111
- side: DoubleSide
112
- });
113
- onBeforeRender = (renderer, scene, camera, geometry, material, group) => {
114
- this.reflectorWorldPosition.setFromMatrixPosition(this.matrixWorld);
115
- this.cameraWorldPosition.setFromMatrixPosition(camera.matrixWorld);
116
- this.rotationMatrix.extractRotation(this.matrixWorld);
117
- this.normal.set(0, 0, 1);
118
- this.normal.applyMatrix4(this.rotationMatrix);
119
- this.view.subVectors(this.reflectorWorldPosition, this.cameraWorldPosition);
120
- // Avoid rendering when reflector is facing away
121
- if (this.view.dot(this.normal) > 0) {
122
- this.normal.set(0, 0, -1);
123
- }
124
- ;
125
- this.view.reflect(this.normal).negate();
126
- this.view.add(this.reflectorWorldPosition);
127
- this.rotationMatrix.extractRotation(camera.matrixWorld);
128
- this.lookAtPosition.set(0, 0, -1);
129
- this.lookAtPosition.applyMatrix4(this.rotationMatrix);
130
- this.lookAtPosition.add(this.cameraWorldPosition);
131
- this.target.subVectors(this.reflectorWorldPosition, this.lookAtPosition);
132
- this.target.reflect(this.normal).negate();
133
- this.target.add(this.reflectorWorldPosition);
134
- this.virtualCamera.position.copy(this.view);
135
- this.virtualCamera.up.set(0, 1, 0);
136
- this.virtualCamera.up.applyMatrix4(this.rotationMatrix);
137
- this.virtualCamera.up.reflect(this.normal);
138
- this.virtualCamera.lookAt(this.target);
139
- this.virtualCamera.far = this.camera.far; // Used in WebGLBackground
140
- this.virtualCamera.updateMatrixWorld();
141
- this.virtualCamera.projectionMatrix.copy(camera.projectionMatrix);
142
- // Update the texture matrix
143
- this.textureMatrix.set(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);
144
- this.textureMatrix.multiply(this.virtualCamera.projectionMatrix);
145
- this.textureMatrix.multiply(this.virtualCamera.matrixWorldInverse);
146
- this.textureMatrix.multiply(this.matrixWorld);
147
- // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
148
- // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
149
- this.reflectorPlane.setFromNormalAndCoplanarPoint(this.normal, this.reflectorWorldPosition);
150
- this.reflectorPlane.applyMatrix4(this.virtualCamera.matrixWorldInverse);
151
- this.clipPlane.set(this.reflectorPlane.normal.x, this.reflectorPlane.normal.y, this.reflectorPlane.normal.z, this.reflectorPlane.constant);
152
- const projectionMatrix = this.virtualCamera.projectionMatrix;
153
- this.q.x = (Math.sign(this.clipPlane.x) + projectionMatrix.elements[8]) / projectionMatrix.elements[0];
154
- this.q.y = (Math.sign(this.clipPlane.y) + projectionMatrix.elements[9]) / projectionMatrix.elements[5];
155
- this.q.z = -1.0;
156
- this.q.w = (1.0 + projectionMatrix.elements[10]) / projectionMatrix.elements[14];
157
- // Calculate the scaled plane vector
158
- this.clipPlane.multiplyScalar(2.0 / this.clipPlane.dot(this.q));
159
- // Replacing the third row of the projection matrix
160
- projectionMatrix.elements[2] = this.clipPlane.x;
161
- projectionMatrix.elements[6] = this.clipPlane.y;
162
- projectionMatrix.elements[10] = this.clipPlane.z + 1.0 - 0.003;
163
- projectionMatrix.elements[14] = this.clipPlane.w;
164
- // Render
165
- this.visible = false;
166
- const currentRenderTarget = renderer.getRenderTarget();
167
- const currentXrEnabled = renderer.xr.enabled;
168
- const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
169
- renderer.xr.enabled = false; // Avoid camera modification
170
- renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
171
- renderer.setRenderTarget(this.renderTarget);
172
- renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897
173
- if (renderer.autoClear === false)
174
- renderer.clear();
175
- renderer.render(scene, this.virtualCamera);
176
- renderer.xr.enabled = currentXrEnabled;
177
- renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
178
- renderer.setRenderTarget(currentRenderTarget);
179
- // Restore viewport
180
- //@ts-ignore
181
- const viewport = this.camera.viewport;
182
- if (viewport !== undefined) {
183
- renderer.state.viewport(viewport);
184
- }
185
- this.visible = true;
186
- };
187
- getRenderTarget() {
188
- return this.renderTarget;
189
- }
190
- ;
191
- dispose() {
192
- this.renderTarget.dispose();
193
- // this.material.dispose();
194
- }
195
- ;
196
- }
111
+ }`,
112
+ depthTest: true,
113
+ depthWrite: true,
114
+ transparent: true,
115
+ side: DoubleSide
116
+ });
117
+ onBeforeRender = (renderer, scene, camera, geometry, material, group) => {
118
+ this.reflectorWorldPosition.setFromMatrixPosition(this.matrixWorld);
119
+ this.cameraWorldPosition.setFromMatrixPosition(camera.matrixWorld);
120
+ this.rotationMatrix.extractRotation(this.matrixWorld);
121
+ this.normal.set(0, 0, 1);
122
+ this.normal.applyMatrix4(this.rotationMatrix);
123
+ this.view.subVectors(this.reflectorWorldPosition, this.cameraWorldPosition);
124
+ // Avoid rendering when reflector is facing away
125
+ if (this.view.dot(this.normal) > 0) {
126
+ this.normal.set(0, 0, -1);
127
+ }
128
+ ;
129
+ this.view.reflect(this.normal).negate();
130
+ this.view.add(this.reflectorWorldPosition);
131
+ this.rotationMatrix.extractRotation(camera.matrixWorld);
132
+ this.lookAtPosition.set(0, 0, -1);
133
+ this.lookAtPosition.applyMatrix4(this.rotationMatrix);
134
+ this.lookAtPosition.add(this.cameraWorldPosition);
135
+ this.target.subVectors(this.reflectorWorldPosition, this.lookAtPosition);
136
+ this.target.reflect(this.normal).negate();
137
+ this.target.add(this.reflectorWorldPosition);
138
+ this.virtualCamera.position.copy(this.view);
139
+ this.virtualCamera.up.set(0, 1, 0);
140
+ this.virtualCamera.up.applyMatrix4(this.rotationMatrix);
141
+ this.virtualCamera.up.reflect(this.normal);
142
+ this.virtualCamera.lookAt(this.target);
143
+ this.virtualCamera.far = this.camera.far; // Used in WebGLBackground
144
+ this.virtualCamera.updateMatrixWorld();
145
+ this.virtualCamera.projectionMatrix.copy(camera.projectionMatrix);
146
+ // Update the texture matrix
147
+ this.textureMatrix.set(0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0);
148
+ this.textureMatrix.multiply(this.virtualCamera.projectionMatrix);
149
+ this.textureMatrix.multiply(this.virtualCamera.matrixWorldInverse);
150
+ this.textureMatrix.multiply(this.matrixWorld);
151
+ // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
152
+ // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
153
+ this.reflectorPlane.setFromNormalAndCoplanarPoint(this.normal, this.reflectorWorldPosition);
154
+ this.reflectorPlane.applyMatrix4(this.virtualCamera.matrixWorldInverse);
155
+ this.clipPlane.set(this.reflectorPlane.normal.x, this.reflectorPlane.normal.y, this.reflectorPlane.normal.z, this.reflectorPlane.constant);
156
+ const projectionMatrix = this.virtualCamera.projectionMatrix;
157
+ this.q.x = (Math.sign(this.clipPlane.x) + projectionMatrix.elements[8]) / projectionMatrix.elements[0];
158
+ this.q.y = (Math.sign(this.clipPlane.y) + projectionMatrix.elements[9]) / projectionMatrix.elements[5];
159
+ this.q.z = -1.0;
160
+ this.q.w = (1.0 + projectionMatrix.elements[10]) / projectionMatrix.elements[14];
161
+ // Calculate the scaled plane vector
162
+ this.clipPlane.multiplyScalar(2.0 / this.clipPlane.dot(this.q));
163
+ // Replacing the third row of the projection matrix
164
+ projectionMatrix.elements[2] = this.clipPlane.x;
165
+ projectionMatrix.elements[6] = this.clipPlane.y;
166
+ projectionMatrix.elements[10] = this.clipPlane.z + 1.0 - 0.003;
167
+ projectionMatrix.elements[14] = this.clipPlane.w;
168
+ // Render
169
+ this.visible = false;
170
+ const currentRenderTarget = renderer.getRenderTarget();
171
+ const currentXrEnabled = renderer.xr.enabled;
172
+ const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
173
+ renderer.xr.enabled = false; // Avoid camera modification
174
+ renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
175
+ renderer.setRenderTarget(this.renderTarget);
176
+ renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897
177
+ if (renderer.autoClear === false)
178
+ renderer.clear();
179
+ renderer.render(scene, this.virtualCamera);
180
+ renderer.xr.enabled = currentXrEnabled;
181
+ renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
182
+ renderer.setRenderTarget(currentRenderTarget);
183
+ // Restore viewport
184
+ const cameraWithViewport = this.camera;
185
+ const viewport = cameraWithViewport.viewport;
186
+ if (viewport !== undefined) {
187
+ renderer.state.viewport(viewport);
188
+ }
189
+ this.visible = true;
190
+ };
191
+ getRenderTarget() {
192
+ return this.renderTarget;
193
+ }
194
+ ;
195
+ dispose() {
196
+ this.renderTarget.dispose();
197
+ // this.material.dispose();
198
+ }
199
+ ;
200
+ }