three-base 2.0.137 → 2.0.145

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 (273) hide show
  1. package/DirectionalLight.d.ts +2 -2
  2. package/DirectionalLight.js +1 -1
  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.ts +60 -0
  8. package/ExtrudeMesh.js +1 -25
  9. package/ExtrudeMesh.ts +553 -0
  10. package/FPScontrol.d.ts +448 -15
  11. package/FPScontrol.js +1448 -266
  12. package/FPScontrol.ts +1855 -0
  13. package/Fire/Fire.d.ts +4 -4
  14. package/Fire/Fire.js +8 -7
  15. package/Fire/Fire.ts +89 -0
  16. package/Fire/{FireGromtry.d.ts → FireGeometry.d.ts} +2 -2
  17. package/Fire/{FireGromtry.js → FireGeometry.js} +1 -1
  18. package/Fire/FireGeometry.ts +40 -0
  19. package/Fire/FireMaterial.d.ts +3 -3
  20. package/Fire/FireMaterial.js +1 -1
  21. package/Fire/FireMaterial.ts +89 -0
  22. package/Fire/index.d.ts +1 -0
  23. package/Fire/index.js +1 -0
  24. package/Fire/index.ts +2 -0
  25. package/GLTFDracoLoader.d.ts +71 -0
  26. package/GLTFDracoLoader.js +266 -0
  27. package/GLTFDracoLoader.ts +430 -0
  28. package/Grass/Grass.d.ts +2 -2
  29. package/Grass/Grass.ts +38 -0
  30. package/Grass/index.d.ts +1 -0
  31. package/Grass/index.js +1 -0
  32. package/Grass/index.ts +2 -0
  33. package/Ground1/Ground1.d.ts +2 -3
  34. package/Ground1/Ground1.js +64 -60
  35. package/Ground1/Ground1.ts +114 -0
  36. package/Ground1/index.ts +1 -0
  37. package/MeshBase.d.ts +1 -0
  38. package/MeshBase.js +37 -13
  39. package/MeshBase.ts +253 -0
  40. package/Mirror.d.ts +3 -3
  41. package/Mirror.js +7 -3
  42. package/Mirror.ts +203 -0
  43. package/MktMesh/MktMesh.d.ts +301 -0
  44. package/MktMesh/MktMesh.js +1064 -0
  45. package/MktMesh/MktMesh.ts +1096 -0
  46. package/MktMesh/assetUrl.d.ts +59 -0
  47. package/MktMesh/assetUrl.js +154 -0
  48. package/MktMesh/assetUrl.ts +155 -0
  49. package/MktMesh/mktMappingShader.d.ts +12 -0
  50. package/MktMesh/mktMappingShader.js +139 -0
  51. package/MktMesh/mktMappingShader.ts +139 -0
  52. package/MktMesh/mktShader.d.ts +263 -0
  53. package/MktMesh/mktShader.js +1248 -0
  54. package/MktMesh/mktShader.ts +1344 -0
  55. package/MktMesh/panoBitmapTexture.d.ts +25 -0
  56. package/MktMesh/panoBitmapTexture.js +52 -0
  57. package/MktMesh/panoBitmapTexture.ts +54 -0
  58. package/MktMesh/panoSlotBind.d.ts +29 -0
  59. package/MktMesh/panoSlotBind.js +106 -0
  60. package/MktMesh/panoSlotBind.ts +153 -0
  61. package/MktMesh/panoSlotRoster.d.ts +22 -0
  62. package/MktMesh/panoSlotRoster.js +169 -0
  63. package/MktMesh/panoSlotRoster.ts +168 -0
  64. package/MktMesh/panoTileStitcher.d.ts +119 -0
  65. package/MktMesh/panoTileStitcher.js +533 -0
  66. package/MktMesh/panoTileStitcher.ts +616 -0
  67. package/MktMesh/types.d.ts +75 -0
  68. package/MktMesh/types.js +1 -0
  69. package/MktMesh/types.ts +87 -0
  70. package/MktMesh/wallCapTessellate.d.ts +33 -0
  71. package/MktMesh/wallCapTessellate.js +97 -0
  72. package/MktMesh/wallCapTessellate.ts +68 -0
  73. package/PipeMesh/PathGeometry.js +9 -2
  74. package/PipeMesh/PathGeometry.ts +343 -0
  75. package/PipeMesh/PathPoint.ts +36 -0
  76. package/PipeMesh/PathPointList.js +1 -0
  77. package/PipeMesh/PathPointList.ts +227 -0
  78. package/PipeMesh/PathTubeGeometry.ts +146 -0
  79. package/PipeMesh/PipeMesh.d.ts +1 -1
  80. package/PipeMesh/PipeMesh.js +1 -1
  81. package/PipeMesh/PipeMesh.ts +76 -0
  82. package/PipeMesh/index.ts +1 -0
  83. package/PlaneModal1/PlaneModal1.d.ts +16 -13
  84. package/PlaneModal1/PlaneModal1.js +4 -0
  85. package/PlaneModal1/PlaneModal1.ts +138 -0
  86. package/PlaneModal1/index.ts +1 -0
  87. package/PlaneText.d.ts +2 -14
  88. package/PlaneText.js +7 -2
  89. package/PlaneText.ts +55 -0
  90. package/PlaneWithHtml.d.ts +4 -2
  91. package/PlaneWithHtml.js +2 -1
  92. package/PlaneWithHtml.ts +70 -0
  93. package/PointLight/PointLight.d.ts +3 -4
  94. package/PointLight/PointLight.js +1 -7
  95. package/PointLight/PointLight.ts +67 -0
  96. package/PointLight/index.ts +1 -0
  97. package/README.md +110 -79
  98. package/Sky/Sky.ts +22 -0
  99. package/Sky/index.ts +1 -0
  100. package/SpotLight.d.ts +2 -2
  101. package/SpotLight.js +1 -1
  102. package/SpotLight.ts +74 -0
  103. package/SpriteModal1/SpriteModal1.d.ts +16 -13
  104. package/SpriteModal1/SpriteModal1.js +8 -2
  105. package/SpriteModal1/SpriteModal1.ts +146 -0
  106. package/SpriteModal1/index.ts +1 -0
  107. package/SpriteText.d.ts +14 -13
  108. package/SpriteText.js +22 -9
  109. package/SpriteText.ts +205 -0
  110. package/SpriteText2.d.ts +9 -8
  111. package/SpriteText2.js +57 -32
  112. package/SpriteText2.ts +258 -0
  113. package/SpriteText3.d.ts +86 -0
  114. package/SpriteText3.js +519 -0
  115. package/SpriteText3.ts +599 -0
  116. package/SpriteWithHtml.d.ts +4 -2
  117. package/SpriteWithHtml.ts +50 -0
  118. package/Text3D/Text3D.js +17 -21
  119. package/Text3D/Text3D.ts +77 -0
  120. package/Text3D/fontController.d.ts +10 -9
  121. package/Text3D/fontController.js +17 -50
  122. package/Text3D/fontController.ts +209 -0
  123. package/Text3D/index.ts +1 -0
  124. package/ThreeBase.d.ts +168 -31
  125. package/ThreeBase.js +663 -117
  126. package/ThreeBase.ts +1314 -0
  127. package/ThreeMouse.d.ts +20 -7
  128. package/ThreeMouse.js +78 -32
  129. package/ThreeMouse.ts +472 -0
  130. package/Transition.d.ts +12 -7
  131. package/Transition.js +26 -27
  132. package/Transition.ts +242 -0
  133. package/Water/Water.js +21 -3
  134. package/Water/Water.ts +45 -0
  135. package/Water/index.d.ts +2 -0
  136. package/Water/index.js +2 -0
  137. package/Water/index.ts +2 -0
  138. package/WaterFlow/FloatingDuck.d.ts +88 -0
  139. package/WaterFlow/FloatingDuck.js +307 -0
  140. package/WaterFlow/FloatingDuck.ts +445 -0
  141. package/WaterFlow/GPUFluidSimulation.d.ts +56 -0
  142. package/WaterFlow/GPUFluidSimulation.js +785 -0
  143. package/WaterFlow/GPUFluidSimulation.ts +827 -0
  144. package/WaterFlow/ReservoirSystem.d.ts +106 -0
  145. package/WaterFlow/ReservoirSystem.js +621 -0
  146. package/WaterFlow/ReservoirSystem.ts +754 -0
  147. package/WaterFlow/SPHField.d.ts +35 -0
  148. package/WaterFlow/SPHField.js +204 -0
  149. package/WaterFlow/SPHField.ts +228 -0
  150. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -0
  151. package/WaterFlow/ScreenSpaceFluidPass.js +736 -0
  152. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -0
  153. package/WaterFlow/ShallowWaterSurface.d.ts +45 -0
  154. package/WaterFlow/ShallowWaterSurface.js +441 -0
  155. package/WaterFlow/ShallowWaterSurface.ts +510 -0
  156. package/WaterFlow/WaterFlow.d.ts +87 -0
  157. package/WaterFlow/WaterFlow.js +171 -0
  158. package/WaterFlow/WaterFlow.ts +219 -0
  159. package/WaterFlow/types.d.ts +161 -0
  160. package/WaterFlow/types.js +1 -0
  161. package/WaterFlow/types.ts +189 -0
  162. package/WaterFlow/waterFlowPass.d.ts +3 -0
  163. package/WaterFlow/waterFlowPass.js +20 -0
  164. package/WaterFlow/waterFlowPass.ts +40 -0
  165. package/collisionOBB.d.ts +42 -0
  166. package/collisionOBB.js +112 -0
  167. package/collisionOBB.ts +108 -0
  168. package/declare.d.ts +25 -167
  169. package/index.js +0 -1
  170. package/index.ts +1 -0
  171. package/instanced/InstancedBatch.d.ts +57 -0
  172. package/instanced/InstancedBatch.js +348 -0
  173. package/instanced/InstancedBatch.ts +406 -0
  174. package/instanced/InstancedChild.d.ts +174 -0
  175. package/instanced/InstancedChild.js +264 -0
  176. package/instanced/InstancedChild.ts +327 -0
  177. package/instanced/InstancedGroup.d.ts +43 -0
  178. package/instanced/InstancedGroup.js +126 -0
  179. package/instanced/InstancedGroup.ts +137 -0
  180. package/{InstancedMeshBase.d.ts → instanced/InstancedMeshBase.d.ts} +6 -7
  181. package/{InstancedMeshBase.js → instanced/InstancedMeshBase.js} +36 -47
  182. package/instanced/InstancedMeshBase.ts +292 -0
  183. package/instanced/InstancedTemplate.d.ts +46 -0
  184. package/instanced/InstancedTemplate.js +125 -0
  185. package/instanced/InstancedTemplate.ts +188 -0
  186. package/keyControls.d.ts +29 -0
  187. package/{keyContorls.js → keyControls.js} +10 -7
  188. package/keyControls.ts +107 -0
  189. package/lensflare/LensFlare.d.ts +39 -0
  190. package/lensflare/LensFlare.js +373 -0
  191. package/lensflare/LensFlare.ts +448 -0
  192. package/lensflare/lensflare0.png +0 -0
  193. package/lensflare/lensflare3.png +0 -0
  194. package/lensflare/lensflare_custom.png +0 -0
  195. package/lensflare/lensflare_hex_ghost.png +0 -0
  196. package/lensflare/lensflare_hex_main.png +0 -0
  197. package/package.json +104 -25
  198. package/pass/bloom_pass.d.ts +1 -1
  199. package/pass/bloom_pass.js +127 -40
  200. package/pass/bloom_pass.ts +243 -0
  201. package/pass/cover_pass.d.ts +1 -1
  202. package/pass/cover_pass.js +55 -32
  203. package/pass/cover_pass.ts +182 -0
  204. package/pass/debugDepthState.d.ts +20 -0
  205. package/pass/debugDepthState.js +9 -0
  206. package/pass/debugDepthState.ts +22 -0
  207. package/pass/depthTestBias.d.ts +12 -0
  208. package/pass/depthTestBias.js +12 -0
  209. package/pass/depthTestBias.ts +12 -0
  210. package/pass/effectUserData.d.ts +27 -0
  211. package/pass/effectUserData.js +1 -0
  212. package/pass/effectUserData.ts +46 -0
  213. package/pass/frosted_glass_pass.d.ts +1 -1
  214. package/pass/frosted_glass_pass.js +45 -64
  215. package/pass/frosted_glass_pass.ts +288 -0
  216. package/pass/fsQuad.d.ts +2 -2
  217. package/pass/fsQuad.ts +34 -0
  218. package/pass/glow_pass.d.ts +1 -1
  219. package/pass/glow_pass.js +288 -72
  220. package/pass/glow_pass.ts +654 -0
  221. package/pass/screen_bloom_pass.d.ts +11 -0
  222. package/pass/screen_bloom_pass.js +235 -0
  223. package/pass/screen_bloom_pass.ts +263 -0
  224. package/pass/ssao_pass.d.ts +78 -3
  225. package/pass/ssao_pass.js +642 -206
  226. package/pass/ssao_pass.ts +918 -0
  227. package/pass/stroke_pass.d.ts +1 -1
  228. package/pass/stroke_pass.js +187 -116
  229. package/pass/stroke_pass.ts +638 -0
  230. package/shaders/FXAAShaderMaterial.js +5 -48
  231. package/shaders/FXAAShaderMaterial.ts +201 -0
  232. package/shaders/baseDepthVisShader.d.ts +11 -0
  233. package/shaders/baseDepthVisShader.js +36 -0
  234. package/shaders/baseDepthVisShader.ts +36 -0
  235. package/shaders/baseTextureToRenderShader.js +46 -51
  236. package/shaders/baseTextureToRenderShader.ts +232 -0
  237. package/shaders/copyShader.js +2 -3
  238. package/shaders/copyShader.ts +32 -0
  239. package/shaders/debugDepthCompareShader.d.ts +14 -0
  240. package/shaders/debugDepthCompareShader.js +49 -0
  241. package/shaders/debugDepthCompareShader.ts +50 -0
  242. package/shaders/debugDepthJudgeShader.d.ts +18 -0
  243. package/shaders/debugDepthJudgeShader.js +52 -0
  244. package/shaders/debugDepthJudgeShader.ts +54 -0
  245. package/shaders/debugExcludedShader.d.ts +13 -0
  246. package/shaders/debugExcludedShader.js +46 -0
  247. package/shaders/debugExcludedShader.ts +48 -0
  248. package/spriteTextBg.d.ts +1 -0
  249. package/spriteTextBg.js +20 -0
  250. package/spriteTextBg.ts +20 -0
  251. package/threeLoader.d.ts +8 -7
  252. package/threeLoader.js +58 -67
  253. package/threeLoader.ts +525 -0
  254. package/threeUtils.d.ts +2 -1
  255. package/threeUtils.js +24 -25
  256. package/threeUtils.ts +373 -0
  257. package/types.d.ts +2 -0
  258. package/types.js +6 -0
  259. package/types.ts +8 -0
  260. package/ApngTexture.d.ts +0 -13
  261. package/ApngTexture.js +0 -61
  262. package/ApngTextureManage.d.ts +0 -18
  263. package/ApngTextureManage.js +0 -87
  264. package/KeyFrameAnimation.d.ts +0 -25
  265. package/KeyFrameAnimation.js +0 -78
  266. package/keyContorls.d.ts +0 -25
  267. package/lib/draco/draco_decoder.js +0 -34
  268. package/lib/draco/draco_decoder.wasm +0 -0
  269. package/lib/draco/draco_wasm_wrapper.js +0 -117
  270. package/pass/emissive_ao_pass.d.ts +0 -13
  271. package/pass/emissive_ao_pass.js +0 -370
  272. package/shaders.d.ts +0 -5
  273. 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,4 +1,4 @@
1
- import { BufferGeometry, Camera, Group, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
1
+ import { Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer, Scene, Camera, BufferGeometry, Material, Group, Texture } from "three";
2
2
  export default class Mirror extends Mesh {
3
3
  constructor(geometry?: BufferGeometry);
4
4
  static geometry: PlaneGeometry;
@@ -17,9 +17,9 @@ export default class Mirror extends Mesh {
17
17
  q: Vector4;
18
18
  textureMatrix: Matrix4;
19
19
  virtualCamera: PerspectiveCamera;
20
- renderTarget: WebGLRenderTarget<import("three").Texture>;
20
+ renderTarget: WebGLRenderTarget<Texture>;
21
21
  material: ShaderMaterial;
22
22
  onBeforeRender: (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => void;
23
- getRenderTarget(): WebGLRenderTarget<import("three").Texture>;
23
+ getRenderTarget(): WebGLRenderTarget<Texture>;
24
24
  dispose(): void;
25
25
  }
package/Mirror.js CHANGED
@@ -9,6 +9,10 @@ import { Color, DoubleSide, HalfFloatType, Matrix4, Mesh, PerspectiveCamera, Pla
9
9
  export default class Mirror extends Mesh {
10
10
  constructor(geometry = Mirror.geometry) {
11
11
  super(geometry);
12
+ // 镜子虽标了 transparent:true(为渲染顺序),但 fragment shader 实际输出 alpha=1.0,
13
+ // 是实心反射面、并非可透视表面 → 必须参与 AO 与描边/辉光的深度遮挡。
14
+ // SSAO 的 AO 数据准备会排除所有 transparent 物体,故此处显式声明保留。
15
+ this.userData.aoOpaque = true;
12
16
  }
13
17
  // 正方形网格
14
18
  static geometry = (() => {
@@ -30,7 +34,7 @@ export default class Mirror extends Mesh {
30
34
  q = new Vector4();
31
35
  textureMatrix = new Matrix4();
32
36
  virtualCamera = this.camera;
33
- renderTarget = new WebGLRenderTarget(2048, 2048, { samples: 0, type: HalfFloatType });
37
+ renderTarget = new WebGLRenderTarget(2048, 2048, { type: HalfFloatType });
34
38
  material = new ShaderMaterial({
35
39
  uniforms: {
36
40
  'color': {
@@ -177,8 +181,8 @@ export default class Mirror extends Mesh {
177
181
  renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
178
182
  renderer.setRenderTarget(currentRenderTarget);
179
183
  // Restore viewport
180
- //@ts-ignore
181
- const viewport = this.camera.viewport;
184
+ const cameraWithViewport = this.camera;
185
+ const viewport = cameraWithViewport.viewport;
182
186
  if (viewport !== undefined) {
183
187
  renderer.state.viewport(viewport);
184
188
  }
package/Mirror.ts ADDED
@@ -0,0 +1,203 @@
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, WebGLRenderer, Scene, Camera, BufferGeometry, Material, Group, Texture } from "three";
8
+
9
+ // 镜子模型
10
+ export default class Mirror extends Mesh {
11
+ constructor(geometry: BufferGeometry = Mirror.geometry) {
12
+ super(geometry);
13
+ // 镜子虽标了 transparent:true(为渲染顺序),但 fragment shader 实际输出 alpha=1.0,
14
+ // 是实心反射面、并非可透视表面 → 必须参与 AO 与描边/辉光的深度遮挡。
15
+ // SSAO 的 AO 数据准备会排除所有 transparent 物体,故此处显式声明保留。
16
+ this.userData.aoOpaque = true;
17
+ }
18
+
19
+ // 正方形网格
20
+ static geometry: PlaneGeometry = (() => {
21
+ const geometry = new PlaneGeometry(10, 10, 1, 1);
22
+ return geometry;
23
+ })();
24
+
25
+ isReflector: boolean = true;
26
+ type: string = 'Reflector';
27
+ camera: PerspectiveCamera = new PerspectiveCamera();
28
+ reflectorPlane: Plane = new Plane();
29
+ normal: Vector3 = new Vector3();
30
+ reflectorWorldPosition: Vector3 = new Vector3();
31
+ cameraWorldPosition: Vector3 = new Vector3();
32
+ rotationMatrix: Matrix4 = new Matrix4();
33
+ lookAtPosition: Vector3 = new Vector3(0, 0, -1);
34
+ clipPlane: Vector4 = new Vector4();
35
+ view: Vector3 = new Vector3();
36
+ target: Vector3 = new Vector3();
37
+ q: Vector4 = new Vector4();
38
+ textureMatrix: Matrix4 = new Matrix4();
39
+ virtualCamera: PerspectiveCamera = this.camera;
40
+ renderTarget: WebGLRenderTarget<Texture> = new WebGLRenderTarget(2048, 2048, { type: HalfFloatType });
41
+ material: ShaderMaterial = new ShaderMaterial({
42
+ uniforms: {
43
+ 'color': {
44
+ value: new Color("#b6b6b6")
45
+ },
46
+ 'tDiffuse': {
47
+ value: this.renderTarget.texture
48
+ },
49
+ 'textureMatrix': {
50
+ value: this.textureMatrix
51
+ },
52
+ blurAmount: {
53
+ value: 0.5
54
+ } // 模糊程度,可以根据需求调整
55
+ },
56
+ vertexShader: /* glsl */ `
57
+ uniform mat4 textureMatrix;
58
+ varying vec4 vUv;
59
+
60
+ #include <common>
61
+ #include <logdepthbuf_pars_vertex>
62
+
63
+ void main() {
64
+
65
+ vUv = textureMatrix * vec4( position, 1.0 );
66
+
67
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
68
+
69
+ #include <logdepthbuf_vertex>
70
+
71
+ }`,
72
+ fragmentShader: /* glsl */ `
73
+ uniform vec3 color;
74
+ uniform sampler2D tDiffuse;
75
+ varying vec4 vUv;
76
+ uniform float blurAmount;
77
+
78
+ #include <logdepthbuf_pars_fragment>
79
+
80
+ float blendOverlay( float base, float blend ) {
81
+
82
+ return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
83
+
84
+ }
85
+
86
+ vec3 blendOverlay( vec3 base, vec3 blend ) {
87
+
88
+ return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
89
+
90
+ }
91
+
92
+ void main() {
93
+
94
+ #include <logdepthbuf_fragment>
95
+
96
+ vec2 texelSize = 1.0 / vec2(1024,1024);
97
+ vec4 fragColor = vec4(0.0);
98
+
99
+ for (float offsetX = -blurAmount; offsetX <= blurAmount; offsetX += texelSize.x) {
100
+ for (float offsetY = -blurAmount; offsetY <= blurAmount; offsetY += texelSize.y) {
101
+ // fragColor += texture2DProj(tDiffuse, vUv + vec4(offsetX, offsetY, 0.0, 0.0) * texelSize);
102
+ fragColor += texture2DProj(tDiffuse, vUv + vec4(offsetX * texelSize.x, offsetY * texelSize.y, 0.0, 0.0) );
103
+ }
104
+ }
105
+
106
+ // fragColor /= pow(2.0 * blurAmount + 1.0, 2.0);
107
+ // gl_FragColor = color;
108
+
109
+ fragColor = texture2DProj( tDiffuse, vUv );
110
+ gl_FragColor = vec4( blendOverlay( fragColor.rgb, color ), 1.0 );
111
+
112
+ #include <tonemapping_fragment>
113
+ #include <colorspace_fragment>
114
+ }`,
115
+ depthTest: true,
116
+ depthWrite: true,
117
+ transparent: true,
118
+ side: DoubleSide
119
+ });
120
+
121
+ onBeforeRender = (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group): void => {
122
+ this.reflectorWorldPosition.setFromMatrixPosition(this.matrixWorld);
123
+ this.cameraWorldPosition.setFromMatrixPosition(camera.matrixWorld);
124
+ this.rotationMatrix.extractRotation(this.matrixWorld);
125
+ this.normal.set(0, 0, 1);
126
+ this.normal.applyMatrix4(this.rotationMatrix);
127
+ this.view.subVectors(this.reflectorWorldPosition, this.cameraWorldPosition);
128
+ // Avoid rendering when reflector is facing away
129
+ if (this.view.dot(this.normal) > 0) {
130
+ this.normal.set(0, 0, -1);
131
+ };
132
+ this.view.reflect(this.normal).negate();
133
+ this.view.add(this.reflectorWorldPosition);
134
+ this.rotationMatrix.extractRotation(camera.matrixWorld);
135
+ this.lookAtPosition.set(0, 0, -1);
136
+ this.lookAtPosition.applyMatrix4(this.rotationMatrix);
137
+ this.lookAtPosition.add(this.cameraWorldPosition);
138
+ this.target.subVectors(this.reflectorWorldPosition, this.lookAtPosition);
139
+ this.target.reflect(this.normal).negate();
140
+ this.target.add(this.reflectorWorldPosition);
141
+ this.virtualCamera.position.copy(this.view);
142
+ this.virtualCamera.up.set(0, 1, 0);
143
+ this.virtualCamera.up.applyMatrix4(this.rotationMatrix);
144
+ this.virtualCamera.up.reflect(this.normal);
145
+ this.virtualCamera.lookAt(this.target);
146
+ this.virtualCamera.far = this.camera.far; // Used in WebGLBackground
147
+ this.virtualCamera.updateMatrixWorld();
148
+ this.virtualCamera.projectionMatrix.copy(camera.projectionMatrix);
149
+ // Update the texture matrix
150
+ 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);
151
+ this.textureMatrix.multiply(this.virtualCamera.projectionMatrix);
152
+ this.textureMatrix.multiply(this.virtualCamera.matrixWorldInverse);
153
+ this.textureMatrix.multiply(this.matrixWorld);
154
+ // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
155
+ // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
156
+ this.reflectorPlane.setFromNormalAndCoplanarPoint(this.normal, this.reflectorWorldPosition);
157
+ this.reflectorPlane.applyMatrix4(this.virtualCamera.matrixWorldInverse);
158
+ this.clipPlane.set(this.reflectorPlane.normal.x, this.reflectorPlane.normal.y, this.reflectorPlane.normal.z, this.reflectorPlane.constant);
159
+ const projectionMatrix = this.virtualCamera.projectionMatrix;
160
+ this.q.x = (Math.sign(this.clipPlane.x) + projectionMatrix.elements[8]) / projectionMatrix.elements[0];
161
+ this.q.y = (Math.sign(this.clipPlane.y) + projectionMatrix.elements[9]) / projectionMatrix.elements[5];
162
+ this.q.z = -1.0;
163
+ this.q.w = (1.0 + projectionMatrix.elements[10]) / projectionMatrix.elements[14];
164
+ // Calculate the scaled plane vector
165
+ this.clipPlane.multiplyScalar(2.0 / this.clipPlane.dot(this.q));
166
+ // Replacing the third row of the projection matrix
167
+ projectionMatrix.elements[2] = this.clipPlane.x;
168
+ projectionMatrix.elements[6] = this.clipPlane.y;
169
+ projectionMatrix.elements[10] = this.clipPlane.z + 1.0 - 0.003;
170
+ projectionMatrix.elements[14] = this.clipPlane.w;
171
+ // Render
172
+ this.visible = false;
173
+ const currentRenderTarget = renderer.getRenderTarget();
174
+ const currentXrEnabled = renderer.xr.enabled;
175
+ const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
176
+ renderer.xr.enabled = false; // Avoid camera modification
177
+ renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
178
+ renderer.setRenderTarget(this.renderTarget);
179
+ renderer.state.buffers.depth.setMask(true); // make sure the depth buffer is writable so it can be properly cleared, see #18897
180
+ if (renderer.autoClear === false)
181
+ renderer.clear();
182
+ renderer.render(scene, this.virtualCamera);
183
+ renderer.xr.enabled = currentXrEnabled;
184
+ renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
185
+ renderer.setRenderTarget(currentRenderTarget);
186
+ // Restore viewport
187
+ const cameraWithViewport = this.camera as PerspectiveCamera & { viewport?: { x: number; y: number; width: number; height: number } };
188
+ const viewport = cameraWithViewport.viewport;
189
+ if (viewport !== undefined) {
190
+ renderer.state.viewport(viewport);
191
+ }
192
+ this.visible = true;
193
+ };
194
+
195
+ getRenderTarget(): WebGLRenderTarget<Texture> {
196
+ return this.renderTarget;
197
+ };
198
+
199
+ dispose(): void {
200
+ this.renderTarget.dispose();
201
+ // this.material.dispose();
202
+ };
203
+ }