three-base 2.0.142 → 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 (227) hide show
  1. package/DirectionalLight.d.ts +6 -6
  2. package/DirectionalLight.js +68 -68
  3. package/EventMap.d.ts +103 -103
  4. package/EventMap.js +17 -17
  5. package/EventMesh.d.ts +18 -18
  6. package/EventMesh.js +59 -59
  7. package/ExtrudeMesh.d.ts +112 -112
  8. package/ExtrudeMesh.js +342 -342
  9. package/FPScontrol.d.ts +511 -511
  10. package/FPScontrol.js +1799 -1799
  11. package/FPScontrol.ts +1855 -1855
  12. package/Fire/Fire.d.ts +28 -28
  13. package/Fire/Fire.js +80 -80
  14. package/Fire/FireGeometry.d.ts +9 -9
  15. package/Fire/FireGeometry.js +40 -40
  16. package/Fire/FireMaterial.d.ts +28 -28
  17. package/Fire/FireMaterial.js +32 -32
  18. package/Fire/index.d.ts +2 -2
  19. package/Fire/index.js +2 -2
  20. package/GLTFDracoLoader.d.ts +71 -71
  21. package/GLTFDracoLoader.js +266 -266
  22. package/GLTFDracoLoader.ts +1 -1
  23. package/Grass/Grass.d.ts +8 -8
  24. package/Grass/Grass.js +38 -38
  25. package/Grass/index.d.ts +2 -2
  26. package/Grass/index.js +2 -2
  27. package/Ground1/Ground1.d.ts +10 -10
  28. package/Ground1/Ground1.js +71 -71
  29. package/Ground1/index.d.ts +1 -1
  30. package/Ground1/index.js +1 -1
  31. package/MeshBase.d.ts +36 -36
  32. package/MeshBase.js +247 -247
  33. package/Mirror.d.ts +25 -25
  34. package/Mirror.js +143 -143
  35. package/MktMesh/MktMesh.d.ts +301 -291
  36. package/MktMesh/MktMesh.js +1064 -1045
  37. package/MktMesh/MktMesh.ts +32 -11
  38. package/MktMesh/assetUrl.d.ts +59 -35
  39. package/MktMesh/assetUrl.js +154 -69
  40. package/MktMesh/assetUrl.ts +155 -69
  41. package/MktMesh/mktMappingShader.d.ts +12 -12
  42. package/MktMesh/mktMappingShader.js +138 -138
  43. package/MktMesh/mktMappingShader.ts +139 -139
  44. package/MktMesh/mktShader.d.ts +263 -263
  45. package/MktMesh/mktShader.js +1236 -1236
  46. package/MktMesh/mktShader.ts +1344 -1344
  47. package/MktMesh/panoBitmapTexture.d.ts +25 -25
  48. package/MktMesh/panoBitmapTexture.js +52 -52
  49. package/MktMesh/panoBitmapTexture.ts +54 -54
  50. package/MktMesh/panoSlotBind.d.ts +29 -29
  51. package/MktMesh/panoSlotBind.js +106 -106
  52. package/MktMesh/panoSlotBind.ts +153 -153
  53. package/MktMesh/panoSlotRoster.d.ts +22 -22
  54. package/MktMesh/panoSlotRoster.js +169 -169
  55. package/MktMesh/panoSlotRoster.ts +168 -168
  56. package/MktMesh/panoTileStitcher.d.ts +119 -119
  57. package/MktMesh/panoTileStitcher.js +529 -529
  58. package/MktMesh/panoTileStitcher.ts +616 -616
  59. package/MktMesh/types.d.ts +75 -75
  60. package/MktMesh/types.js +1 -1
  61. package/MktMesh/wallCapTessellate.d.ts +33 -33
  62. package/MktMesh/wallCapTessellate.js +97 -97
  63. package/MktMesh/wallCapTessellate.ts +68 -68
  64. package/PipeMesh/PathGeometry.d.ts +44 -44
  65. package/PipeMesh/PathGeometry.js +307 -307
  66. package/PipeMesh/PathPoint.d.ts +17 -17
  67. package/PipeMesh/PathPoint.js +31 -31
  68. package/PipeMesh/PathPointList.d.ts +31 -31
  69. package/PipeMesh/PathPointList.js +209 -209
  70. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  71. package/PipeMesh/PathTubeGeometry.js +130 -130
  72. package/PipeMesh/PipeMesh.d.ts +25 -25
  73. package/PipeMesh/PipeMesh.js +63 -63
  74. package/PipeMesh/index.d.ts +1 -1
  75. package/PipeMesh/index.js +1 -1
  76. package/PlaneModal1/PlaneModal1.d.ts +34 -34
  77. package/PlaneModal1/PlaneModal1.js +109 -109
  78. package/PlaneModal1/index.d.ts +1 -1
  79. package/PlaneModal1/index.js +1 -1
  80. package/PlaneText.d.ts +27 -27
  81. package/PlaneText.js +35 -35
  82. package/PlaneWithHtml.d.ts +13 -13
  83. package/PlaneWithHtml.js +64 -64
  84. package/PointLight/PointLight.d.ts +13 -13
  85. package/PointLight/PointLight.js +64 -64
  86. package/PointLight/index.d.ts +1 -1
  87. package/PointLight/index.js +1 -1
  88. package/Sky/Sky.d.ts +9 -9
  89. package/Sky/Sky.js +22 -22
  90. package/Sky/index.d.ts +1 -1
  91. package/Sky/index.js +1 -1
  92. package/SpotLight.d.ts +6 -6
  93. package/SpotLight.js +72 -72
  94. package/SpriteModal1/SpriteModal1.d.ts +33 -33
  95. package/SpriteModal1/SpriteModal1.js +116 -116
  96. package/SpriteModal1/index.d.ts +1 -1
  97. package/SpriteModal1/index.js +1 -1
  98. package/SpriteText.d.ts +46 -46
  99. package/SpriteText.js +176 -176
  100. package/SpriteText2.d.ts +48 -48
  101. package/SpriteText2.js +223 -223
  102. package/SpriteText3.d.ts +86 -86
  103. package/SpriteText3.js +517 -517
  104. package/SpriteText3.ts +599 -599
  105. package/SpriteWithHtml.d.ts +9 -9
  106. package/SpriteWithHtml.js +44 -44
  107. package/Text3D/Text3D.d.ts +20 -20
  108. package/Text3D/Text3D.js +58 -58
  109. package/Text3D/fontController.d.ts +60 -60
  110. package/Text3D/fontController.js +166 -166
  111. package/Text3D/index.d.ts +1 -1
  112. package/Text3D/index.js +1 -1
  113. package/ThreeBase.d.ts +248 -248
  114. package/ThreeBase.js +1202 -1202
  115. package/ThreeBase.ts +1314 -1314
  116. package/ThreeMouse.d.ts +53 -54
  117. package/ThreeMouse.js +453 -453
  118. package/Transition.d.ts +45 -45
  119. package/Transition.js +209 -209
  120. package/Water/Water.d.ts +5 -5
  121. package/Water/Water.js +44 -44
  122. package/Water/index.d.ts +2 -2
  123. package/Water/index.js +2 -2
  124. package/WaterFlow/FloatingDuck.d.ts +88 -88
  125. package/WaterFlow/FloatingDuck.js +307 -307
  126. package/WaterFlow/FloatingDuck.ts +445 -445
  127. package/WaterFlow/GPUFluidSimulation.d.ts +56 -56
  128. package/WaterFlow/GPUFluidSimulation.js +781 -781
  129. package/WaterFlow/GPUFluidSimulation.ts +827 -827
  130. package/WaterFlow/ReservoirSystem.d.ts +106 -106
  131. package/WaterFlow/ReservoirSystem.js +611 -611
  132. package/WaterFlow/ReservoirSystem.ts +754 -754
  133. package/WaterFlow/SPHField.d.ts +35 -35
  134. package/WaterFlow/SPHField.js +203 -203
  135. package/WaterFlow/SPHField.ts +228 -228
  136. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -69
  137. package/WaterFlow/ScreenSpaceFluidPass.js +728 -728
  138. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -789
  139. package/WaterFlow/ShallowWaterSurface.d.ts +45 -45
  140. package/WaterFlow/ShallowWaterSurface.js +438 -438
  141. package/WaterFlow/ShallowWaterSurface.ts +510 -510
  142. package/WaterFlow/WaterFlow.d.ts +87 -87
  143. package/WaterFlow/WaterFlow.js +171 -171
  144. package/WaterFlow/WaterFlow.ts +219 -219
  145. package/WaterFlow/types.d.ts +161 -161
  146. package/WaterFlow/types.js +1 -1
  147. package/WaterFlow/types.ts +189 -189
  148. package/WaterFlow/waterFlowPass.d.ts +3 -3
  149. package/WaterFlow/waterFlowPass.js +20 -20
  150. package/WaterFlow/waterFlowPass.ts +40 -40
  151. package/collisionOBB.d.ts +42 -42
  152. package/collisionOBB.js +112 -112
  153. package/collisionOBB.ts +108 -108
  154. package/declare.d.ts +25 -25
  155. package/index.d.ts +1 -1
  156. package/index.js +1 -1
  157. package/instanced/InstancedBatch.d.ts +57 -57
  158. package/instanced/InstancedBatch.js +348 -348
  159. package/instanced/InstancedBatch.ts +406 -406
  160. package/instanced/InstancedChild.d.ts +174 -174
  161. package/instanced/InstancedChild.js +264 -264
  162. package/instanced/InstancedChild.ts +327 -327
  163. package/instanced/InstancedGroup.d.ts +43 -43
  164. package/instanced/InstancedGroup.js +126 -126
  165. package/instanced/InstancedGroup.ts +137 -137
  166. package/instanced/InstancedMeshBase.d.ts +38 -38
  167. package/instanced/InstancedMeshBase.js +282 -282
  168. package/instanced/InstancedTemplate.d.ts +46 -46
  169. package/instanced/InstancedTemplate.js +125 -125
  170. package/keyControls.d.ts +29 -30
  171. package/keyControls.js +101 -101
  172. package/lensflare/LensFlare.d.ts +39 -39
  173. package/lensflare/LensFlare.js +284 -284
  174. package/package.json +6 -1
  175. package/pass/bloom_pass.d.ts +2 -2
  176. package/pass/bloom_pass.js +174 -174
  177. package/pass/cover_pass.d.ts +2 -2
  178. package/pass/cover_pass.js +108 -108
  179. package/pass/debugDepthState.d.ts +20 -20
  180. package/pass/debugDepthState.js +9 -9
  181. package/pass/debugDepthState.ts +22 -22
  182. package/pass/depthTestBias.d.ts +12 -12
  183. package/pass/depthTestBias.js +12 -12
  184. package/pass/depthTestBias.ts +12 -12
  185. package/pass/effectUserData.d.ts +27 -27
  186. package/pass/effectUserData.js +1 -1
  187. package/pass/effectUserData.ts +46 -46
  188. package/pass/frosted_glass_pass.d.ts +2 -2
  189. package/pass/frosted_glass_pass.js +150 -150
  190. package/pass/fsQuad.d.ts +9 -9
  191. package/pass/fsQuad.js +28 -28
  192. package/pass/glow_pass.d.ts +2 -2
  193. package/pass/glow_pass.js +350 -350
  194. package/pass/screen_bloom_pass.d.ts +11 -11
  195. package/pass/screen_bloom_pass.js +136 -136
  196. package/pass/ssao_pass.d.ts +88 -88
  197. package/pass/ssao_pass.js +799 -799
  198. package/pass/ssao_pass.ts +918 -918
  199. package/pass/stroke_pass.d.ts +2 -2
  200. package/pass/stroke_pass.js +267 -267
  201. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  202. package/shaders/FXAAShaderMaterial.js +20 -20
  203. package/shaders/baseDepthVisShader.d.ts +11 -11
  204. package/shaders/baseDepthVisShader.js +36 -36
  205. package/shaders/baseDepthVisShader.ts +36 -36
  206. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  207. package/shaders/baseTextureToRenderShader.js +19 -19
  208. package/shaders/copyShader.d.ts +3 -3
  209. package/shaders/copyShader.js +13 -13
  210. package/shaders/debugDepthCompareShader.d.ts +14 -14
  211. package/shaders/debugDepthCompareShader.js +49 -49
  212. package/shaders/debugDepthCompareShader.ts +50 -50
  213. package/shaders/debugDepthJudgeShader.d.ts +18 -18
  214. package/shaders/debugDepthJudgeShader.js +52 -52
  215. package/shaders/debugDepthJudgeShader.ts +54 -54
  216. package/shaders/debugExcludedShader.d.ts +13 -13
  217. package/shaders/debugExcludedShader.js +46 -46
  218. package/shaders/debugExcludedShader.ts +48 -48
  219. package/spriteTextBg.d.ts +1 -1
  220. package/spriteTextBg.js +20 -20
  221. package/spriteTextBg.ts +20 -20
  222. package/threeLoader.d.ts +35 -35
  223. package/threeLoader.js +518 -518
  224. package/threeUtils.d.ts +53 -53
  225. package/threeUtils.js +329 -329
  226. package/types.d.ts +2 -2
  227. package/types.js +6 -6
package/threeUtils.js CHANGED
@@ -1,329 +1,329 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-02-24 22:49:42
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 00:36:18
6
- */
7
- import { Box3, BufferGeometry, Clock, EventDispatcher, Material, Mesh, MeshBasicMaterial, MeshLambertMaterial, Object3D, Vector3 } from "three";
8
- import { MathUtils } from "three";
9
- const { generateUUID } = MathUtils;
10
- import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter";
11
- class ThreeUtils extends EventDispatcher {
12
- constructor() {
13
- super();
14
- this.requestAnimationFrame();
15
- }
16
- isProd = typeof process !== 'undefined' && process.env?.NODE_ENV === "production";
17
- isDev = typeof process !== 'undefined' && process.env?.NODE_ENV === "development";
18
- _timeTemp = performance.now();
19
- _accumulate = 0; // 事件分发次数
20
- _box3 = new Box3();
21
- _size = new Vector3();
22
- _center = new Vector3();
23
- _clock = new Clock();
24
- requestAnimationFrame() {
25
- this._accumulate += 1;
26
- const timeTemp = (this._timeTemp = performance.now());
27
- this.dispatchEvent({
28
- type: "requestAnimationFrame",
29
- timeTemp,
30
- accumulate: this._accumulate,
31
- });
32
- this._computerDomZoom();
33
- //执行一个计划任务
34
- let work = this.workList.splice(0, 1)[0];
35
- work && work();
36
- setTimeout(() => {
37
- this.requestAnimationFrame();
38
- }, 30 - this._clock.getDelta());
39
- }
40
- // 需要保持宽高比的元素列表
41
- _keepZoomDoms = [];
42
- /**
43
- *计算需要保持比例的元素
44
- */
45
- _computerDomZoom() {
46
- //1. 踢除没有出现在页面上的 dom 元素
47
- this._keepZoomDoms = this._keepZoomDoms.filter((l) => this.inDocument(l.dom));
48
- //2.设置dom的缩放值样式
49
- this._keepZoomDoms.map((item, i) => {
50
- let parentDom = item.dom.parentNode;
51
- let scale = Math.min(parentDom.clientWidth / item.width, parentDom.clientHeight / item.height);
52
- Object.assign(item.dom.style, {
53
- position: "absolute",
54
- top: "50%",
55
- left: "50%",
56
- transform: `translate(-50%, -50%) scale(${scale})`,
57
- width: `${item.width}px`,
58
- height: `${item.height}px`,
59
- });
60
- });
61
- }
62
- /**
63
- * dom元素保持比例
64
- */
65
- keepZoom(dom, width = 1920, height = 1080) {
66
- if (dom) {
67
- // 去掉相同的dom存储的信息
68
- this._keepZoomDoms = this._keepZoomDoms.filter((l) => l.dom !== dom);
69
- // 把配置加入到列表中
70
- this._keepZoomDoms.push({ dom, width, height });
71
- dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
72
- this._computerDomZoom();
73
- }
74
- }
75
- workList = [];
76
- // 时钟对象
77
- clock = new Clock();
78
- // 按照任务队列依次执行计划任务 性能优化
79
- /**
80
- * @param {() => void} [callback]
81
- * @param {boolean} [first=true] 需要优先执行
82
- */
83
- addWork(work, first = false) {
84
- // 加入任务队列
85
- if (work && !this.workList.includes(work)) {
86
- if (first) {
87
- this.workList.splice(1, 0, work); // 优先执行的任务回调
88
- }
89
- else {
90
- this.workList.push(work);
91
- }
92
- }
93
- }
94
- /**
95
- *判断一个元素是否在页面上
96
- */
97
- inDocument(dom) {
98
- if (dom === null) {
99
- return false;
100
- }
101
- else if (dom.parentNode === null) {
102
- return false;
103
- }
104
- else if (dom.parentNode === document) {
105
- return true;
106
- }
107
- else {
108
- return this.inDocument(dom.parentNode);
109
- }
110
- }
111
- // 获取中心点
112
- getCenter(object3d) {
113
- if (object3d instanceof Object3D) {
114
- this._box3.setFromObject(object3d);
115
- this._box3.getCenter(this._center);
116
- }
117
- else if (object3d instanceof BufferGeometry) {
118
- object3d.computeBoundingBox();
119
- object3d.boundingBox.getCenter(this._center);
120
- }
121
- return this._center.clone();
122
- }
123
- // 获取模型大小
124
- getSize(object3d) {
125
- if (object3d instanceof Object3D) {
126
- this._box3.setFromObject(object3d);
127
- this._box3.getSize(this._size);
128
- }
129
- else if (object3d instanceof BufferGeometry) {
130
- object3d.computeBoundingBox();
131
- object3d.boundingBox.getSize(this._size);
132
- }
133
- return this._size.clone();
134
- }
135
- // 模型对齐底面
136
- alignBottom(object3d, y = 0) {
137
- if (object3d instanceof Object3D) {
138
- this._box3.setFromObject(object3d);
139
- this._box3.getCenter(this._center);
140
- this._box3.getSize(this._size);
141
- object3d.position.y -= this._center.y - this._size.y * 0.5;
142
- object3d.position.y += y;
143
- object3d.updateMatrixWorld(true);
144
- }
145
- else if (object3d instanceof BufferGeometry) {
146
- object3d.computeBoundingBox();
147
- object3d.translate(0, -object3d.boundingBox.min.y + y, 0);
148
- }
149
- }
150
- // 居中对象
151
- centerObject3D(object3d) {
152
- if (object3d instanceof Object3D) {
153
- this._box3.setFromObject(object3d);
154
- this._box3.getCenter(this._center);
155
- object3d.position.sub(this._center);
156
- object3d.updateMatrixWorld(true);
157
- }
158
- else if (object3d instanceof BufferGeometry) {
159
- object3d.center();
160
- }
161
- }
162
- //居中对象
163
- center(object3d) {
164
- this.centerObject3D(object3d);
165
- }
166
- // 使用指定的维度(xyz)计算, 缩放至指定大小;
167
- scaleTo(object3d, size = 1, key = ["x", "y", "z"]) {
168
- const originSize = this.getSize(object3d);
169
- const scale = size / Math.max(...key.map((k) => originSize[k]));
170
- if (object3d.isObject3D) {
171
- object3d.scale.multiplyScalar(scale);
172
- object3d.updateMatrixWorld(true);
173
- }
174
- else if (object3d.isBufferGeometry) {
175
- object3d.scale(scale, scale, scale);
176
- }
177
- }
178
- // 应用全部变化
179
- applyAllTranstion(object3d) {
180
- object3d.updateMatrixWorld(true);
181
- // 应用全部变换
182
- object3d.traverse((obj) => {
183
- obj.position.set(0, 0, 0);
184
- obj.rotation.set(0, 0, 0);
185
- obj.scale.set(1, 1, 1);
186
- if (obj instanceof Mesh) {
187
- obj.geometry.applyMatrix4(obj.matrixWorld);
188
- }
189
- });
190
- }
191
- // 导出 glb
192
- exportGLB(input, fileName = "threeUtils.exportGLB", maxTextureSize = 4096) {
193
- const gltfExporter = new GLTFExporter();
194
- gltfExporter.parse(input, (result) => {
195
- if (result instanceof ArrayBuffer) {
196
- const link = document.createElement("a");
197
- link.style.display = "none";
198
- document.body.appendChild(link); // Firefox workaround, see #6594
199
- link.href = URL.createObjectURL(new Blob([result], { type: "application/octet-stream" }));
200
- link.download = `${fileName}.glb`;
201
- link.click();
202
- }
203
- }, (error) => { }, {
204
- trs: true,
205
- onlyVisible: true,
206
- binary: true,
207
- maxTextureSize: maxTextureSize,
208
- });
209
- }
210
- //导出usdz
211
- exportUsdz() { }
212
- // 计算网格的体积
213
- getVolume(geometry) {
214
- if (!geometry.index)
215
- return 0;
216
- if (!geometry.isBufferGeometry) {
217
- console.warn("'geometry' must be an indexed or non-indexed buffer geometry");
218
- return 0;
219
- }
220
- const isIndexed = geometry.index !== null;
221
- const position = geometry.attributes.position;
222
- let sum = 0;
223
- const p1 = new Vector3(), p2 = new Vector3(), p3 = new Vector3();
224
- if (!isIndexed) {
225
- const faces = position.count / 3;
226
- for (let i = 0; i < faces; i++) {
227
- p1.fromBufferAttribute(position, i * 3 + 0);
228
- p2.fromBufferAttribute(position, i * 3 + 1);
229
- p3.fromBufferAttribute(position, i * 3 + 2);
230
- sum += this.signedVolumeOfTriangle(p1, p2, p3);
231
- }
232
- }
233
- else {
234
- const index = geometry.index;
235
- const faces = index.count / 3;
236
- for (let i = 0; i < faces; i++) {
237
- p1.fromBufferAttribute(position, index.array[i * 3 + 0]);
238
- p2.fromBufferAttribute(position, index.array[i * 3 + 1]);
239
- p3.fromBufferAttribute(position, index.array[i * 3 + 2]);
240
- sum += this.signedVolumeOfTriangle(p1, p2, p3);
241
- }
242
- }
243
- return sum > 0 ? sum : 0;
244
- }
245
- signedVolumeOfTriangle(p1, p2, p3) {
246
- return p1.dot(p2.cross(p3)) / 6.0;
247
- }
248
- //清理对象的网格和材质的缓存
249
- dipose(object3d) {
250
- object3d.traverse((obj) => {
251
- if (obj instanceof Mesh) {
252
- obj.geometry.dispose();
253
- if (obj.material instanceof Material) {
254
- obj.material.dispose();
255
- }
256
- else if (obj.material instanceof Array) {
257
- obj.material.map((m) => {
258
- m.dispose();
259
- });
260
- }
261
- }
262
- });
263
- }
264
- // 获取文件名的后缀
265
- getSuffix(filename) {
266
- const match = filename.match(/([^\.]+)$/);
267
- return match ? match[1].toLowerCase() : "";
268
- }
269
- // 从url 中提取文件名
270
- getFilename(url) {
271
- // 移除URL的查询字符串和哈希部分
272
- const path = url.split("?")[0].split("#")[0];
273
- // 使用lastIndexOf和substring从路径中提取文件名
274
- const filename = path.substring(path.lastIndexOf("/") + 1);
275
- return filename;
276
- }
277
- //文件转blob
278
- fileToBloburl(file) {
279
- return new Promise((resolve, reject) => {
280
- const reader = new FileReader();
281
- reader.onload = function (event) {
282
- const arrayBuffer = event.target.result;
283
- const blob = new Blob([arrayBuffer]);
284
- const bloburl = URL.createObjectURL(blob);
285
- resolve(bloburl);
286
- };
287
- reader.onerror = function (error) {
288
- throw new Error(`文件转blbo 失败,${file.name}`);
289
- };
290
- reader.readAsArrayBuffer(file);
291
- });
292
- }
293
- //材质转为基础材质
294
- toMeshBasicMaterial(material) {
295
- if (material instanceof Array) {
296
- return material.map((m) => this.toMeshBasicMaterial(m));
297
- }
298
- else {
299
- const meshBasicMaterial = new MeshBasicMaterial();
300
- for (let i in meshBasicMaterial) {
301
- if (!["type"].includes(i) &&
302
- meshBasicMaterial.hasOwnProperty(i) &&
303
- material.hasOwnProperty(i)) {
304
- meshBasicMaterial[i] = material[i];
305
- }
306
- }
307
- return meshBasicMaterial;
308
- }
309
- }
310
- toMeshLambertMaterial(material) {
311
- if (material instanceof Array) {
312
- return material.map((m) => this.toMeshLambertMaterial(m));
313
- }
314
- else {
315
- const meshLambertMaterial = new MeshLambertMaterial();
316
- for (let i in meshLambertMaterial) {
317
- if (!["type"].includes(i) &&
318
- meshLambertMaterial.hasOwnProperty(i) &&
319
- material.hasOwnProperty(i)) {
320
- meshLambertMaterial[i] = material[i];
321
- }
322
- }
323
- return meshLambertMaterial;
324
- }
325
- }
326
- }
327
- const threeUtils = new ThreeUtils();
328
- window.threeUtils = threeUtils;
329
- export default threeUtils;
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-02-24 22:49:42
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-05-03 00:36:18
6
+ */
7
+ import { Box3, BufferGeometry, Clock, EventDispatcher, Material, Mesh, MeshBasicMaterial, MeshLambertMaterial, Object3D, Vector3 } from "three";
8
+ import { MathUtils } from "three";
9
+ const { generateUUID } = MathUtils;
10
+ import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter";
11
+ class ThreeUtils extends EventDispatcher {
12
+ constructor() {
13
+ super();
14
+ this.requestAnimationFrame();
15
+ }
16
+ isProd = typeof process !== 'undefined' && process.env?.NODE_ENV === "production";
17
+ isDev = typeof process !== 'undefined' && process.env?.NODE_ENV === "development";
18
+ _timeTemp = performance.now();
19
+ _accumulate = 0; // 事件分发次数
20
+ _box3 = new Box3();
21
+ _size = new Vector3();
22
+ _center = new Vector3();
23
+ _clock = new Clock();
24
+ requestAnimationFrame() {
25
+ this._accumulate += 1;
26
+ const timeTemp = (this._timeTemp = performance.now());
27
+ this.dispatchEvent({
28
+ type: "requestAnimationFrame",
29
+ timeTemp,
30
+ accumulate: this._accumulate,
31
+ });
32
+ this._computerDomZoom();
33
+ //执行一个计划任务
34
+ let work = this.workList.splice(0, 1)[0];
35
+ work && work();
36
+ setTimeout(() => {
37
+ this.requestAnimationFrame();
38
+ }, 30 - this._clock.getDelta());
39
+ }
40
+ // 需要保持宽高比的元素列表
41
+ _keepZoomDoms = [];
42
+ /**
43
+ *计算需要保持比例的元素
44
+ */
45
+ _computerDomZoom() {
46
+ //1. 踢除没有出现在页面上的 dom 元素
47
+ this._keepZoomDoms = this._keepZoomDoms.filter((l) => this.inDocument(l.dom));
48
+ //2.设置dom的缩放值样式
49
+ this._keepZoomDoms.map((item, i) => {
50
+ let parentDom = item.dom.parentNode;
51
+ let scale = Math.min(parentDom.clientWidth / item.width, parentDom.clientHeight / item.height);
52
+ Object.assign(item.dom.style, {
53
+ position: "absolute",
54
+ top: "50%",
55
+ left: "50%",
56
+ transform: `translate(-50%, -50%) scale(${scale})`,
57
+ width: `${item.width}px`,
58
+ height: `${item.height}px`,
59
+ });
60
+ });
61
+ }
62
+ /**
63
+ * dom元素保持比例
64
+ */
65
+ keepZoom(dom, width = 1920, height = 1080) {
66
+ if (dom) {
67
+ // 去掉相同的dom存储的信息
68
+ this._keepZoomDoms = this._keepZoomDoms.filter((l) => l.dom !== dom);
69
+ // 把配置加入到列表中
70
+ this._keepZoomDoms.push({ dom, width, height });
71
+ dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
72
+ this._computerDomZoom();
73
+ }
74
+ }
75
+ workList = [];
76
+ // 时钟对象
77
+ clock = new Clock();
78
+ // 按照任务队列依次执行计划任务 性能优化
79
+ /**
80
+ * @param {() => void} [callback]
81
+ * @param {boolean} [first=true] 需要优先执行
82
+ */
83
+ addWork(work, first = false) {
84
+ // 加入任务队列
85
+ if (work && !this.workList.includes(work)) {
86
+ if (first) {
87
+ this.workList.splice(1, 0, work); // 优先执行的任务回调
88
+ }
89
+ else {
90
+ this.workList.push(work);
91
+ }
92
+ }
93
+ }
94
+ /**
95
+ *判断一个元素是否在页面上
96
+ */
97
+ inDocument(dom) {
98
+ if (dom === null) {
99
+ return false;
100
+ }
101
+ else if (dom.parentNode === null) {
102
+ return false;
103
+ }
104
+ else if (dom.parentNode === document) {
105
+ return true;
106
+ }
107
+ else {
108
+ return this.inDocument(dom.parentNode);
109
+ }
110
+ }
111
+ // 获取中心点
112
+ getCenter(object3d) {
113
+ if (object3d instanceof Object3D) {
114
+ this._box3.setFromObject(object3d);
115
+ this._box3.getCenter(this._center);
116
+ }
117
+ else if (object3d instanceof BufferGeometry) {
118
+ object3d.computeBoundingBox();
119
+ object3d.boundingBox.getCenter(this._center);
120
+ }
121
+ return this._center.clone();
122
+ }
123
+ // 获取模型大小
124
+ getSize(object3d) {
125
+ if (object3d instanceof Object3D) {
126
+ this._box3.setFromObject(object3d);
127
+ this._box3.getSize(this._size);
128
+ }
129
+ else if (object3d instanceof BufferGeometry) {
130
+ object3d.computeBoundingBox();
131
+ object3d.boundingBox.getSize(this._size);
132
+ }
133
+ return this._size.clone();
134
+ }
135
+ // 模型对齐底面
136
+ alignBottom(object3d, y = 0) {
137
+ if (object3d instanceof Object3D) {
138
+ this._box3.setFromObject(object3d);
139
+ this._box3.getCenter(this._center);
140
+ this._box3.getSize(this._size);
141
+ object3d.position.y -= this._center.y - this._size.y * 0.5;
142
+ object3d.position.y += y;
143
+ object3d.updateMatrixWorld(true);
144
+ }
145
+ else if (object3d instanceof BufferGeometry) {
146
+ object3d.computeBoundingBox();
147
+ object3d.translate(0, -object3d.boundingBox.min.y + y, 0);
148
+ }
149
+ }
150
+ // 居中对象
151
+ centerObject3D(object3d) {
152
+ if (object3d instanceof Object3D) {
153
+ this._box3.setFromObject(object3d);
154
+ this._box3.getCenter(this._center);
155
+ object3d.position.sub(this._center);
156
+ object3d.updateMatrixWorld(true);
157
+ }
158
+ else if (object3d instanceof BufferGeometry) {
159
+ object3d.center();
160
+ }
161
+ }
162
+ //居中对象
163
+ center(object3d) {
164
+ this.centerObject3D(object3d);
165
+ }
166
+ // 使用指定的维度(xyz)计算, 缩放至指定大小;
167
+ scaleTo(object3d, size = 1, key = ["x", "y", "z"]) {
168
+ const originSize = this.getSize(object3d);
169
+ const scale = size / Math.max(...key.map((k) => originSize[k]));
170
+ if (object3d.isObject3D) {
171
+ object3d.scale.multiplyScalar(scale);
172
+ object3d.updateMatrixWorld(true);
173
+ }
174
+ else if (object3d.isBufferGeometry) {
175
+ object3d.scale(scale, scale, scale);
176
+ }
177
+ }
178
+ // 应用全部变化
179
+ applyAllTranstion(object3d) {
180
+ object3d.updateMatrixWorld(true);
181
+ // 应用全部变换
182
+ object3d.traverse((obj) => {
183
+ obj.position.set(0, 0, 0);
184
+ obj.rotation.set(0, 0, 0);
185
+ obj.scale.set(1, 1, 1);
186
+ if (obj instanceof Mesh) {
187
+ obj.geometry.applyMatrix4(obj.matrixWorld);
188
+ }
189
+ });
190
+ }
191
+ // 导出 glb
192
+ exportGLB(input, fileName = "threeUtils.exportGLB", maxTextureSize = 4096) {
193
+ const gltfExporter = new GLTFExporter();
194
+ gltfExporter.parse(input, (result) => {
195
+ if (result instanceof ArrayBuffer) {
196
+ const link = document.createElement("a");
197
+ link.style.display = "none";
198
+ document.body.appendChild(link); // Firefox workaround, see #6594
199
+ link.href = URL.createObjectURL(new Blob([result], { type: "application/octet-stream" }));
200
+ link.download = `${fileName}.glb`;
201
+ link.click();
202
+ }
203
+ }, (error) => { }, {
204
+ trs: true,
205
+ onlyVisible: true, // 只导出可见的
206
+ binary: true, // glb
207
+ maxTextureSize: maxTextureSize,
208
+ });
209
+ }
210
+ //导出usdz
211
+ exportUsdz() { }
212
+ // 计算网格的体积
213
+ getVolume(geometry) {
214
+ if (!geometry.index)
215
+ return 0;
216
+ if (!geometry.isBufferGeometry) {
217
+ console.warn("'geometry' must be an indexed or non-indexed buffer geometry");
218
+ return 0;
219
+ }
220
+ const isIndexed = geometry.index !== null;
221
+ const position = geometry.attributes.position;
222
+ let sum = 0;
223
+ const p1 = new Vector3(), p2 = new Vector3(), p3 = new Vector3();
224
+ if (!isIndexed) {
225
+ const faces = position.count / 3;
226
+ for (let i = 0; i < faces; i++) {
227
+ p1.fromBufferAttribute(position, i * 3 + 0);
228
+ p2.fromBufferAttribute(position, i * 3 + 1);
229
+ p3.fromBufferAttribute(position, i * 3 + 2);
230
+ sum += this.signedVolumeOfTriangle(p1, p2, p3);
231
+ }
232
+ }
233
+ else {
234
+ const index = geometry.index;
235
+ const faces = index.count / 3;
236
+ for (let i = 0; i < faces; i++) {
237
+ p1.fromBufferAttribute(position, index.array[i * 3 + 0]);
238
+ p2.fromBufferAttribute(position, index.array[i * 3 + 1]);
239
+ p3.fromBufferAttribute(position, index.array[i * 3 + 2]);
240
+ sum += this.signedVolumeOfTriangle(p1, p2, p3);
241
+ }
242
+ }
243
+ return sum > 0 ? sum : 0;
244
+ }
245
+ signedVolumeOfTriangle(p1, p2, p3) {
246
+ return p1.dot(p2.cross(p3)) / 6.0;
247
+ }
248
+ //清理对象的网格和材质的缓存
249
+ dipose(object3d) {
250
+ object3d.traverse((obj) => {
251
+ if (obj instanceof Mesh) {
252
+ obj.geometry.dispose();
253
+ if (obj.material instanceof Material) {
254
+ obj.material.dispose();
255
+ }
256
+ else if (obj.material instanceof Array) {
257
+ obj.material.map((m) => {
258
+ m.dispose();
259
+ });
260
+ }
261
+ }
262
+ });
263
+ }
264
+ // 获取文件名的后缀
265
+ getSuffix(filename) {
266
+ const match = filename.match(/([^\.]+)$/);
267
+ return match ? match[1].toLowerCase() : "";
268
+ }
269
+ // 从url 中提取文件名
270
+ getFilename(url) {
271
+ // 移除URL的查询字符串和哈希部分
272
+ const path = url.split("?")[0].split("#")[0];
273
+ // 使用lastIndexOf和substring从路径中提取文件名
274
+ const filename = path.substring(path.lastIndexOf("/") + 1);
275
+ return filename;
276
+ }
277
+ //文件转blob
278
+ fileToBloburl(file) {
279
+ return new Promise((resolve, reject) => {
280
+ const reader = new FileReader();
281
+ reader.onload = function (event) {
282
+ const arrayBuffer = event.target.result;
283
+ const blob = new Blob([arrayBuffer]);
284
+ const bloburl = URL.createObjectURL(blob);
285
+ resolve(bloburl);
286
+ };
287
+ reader.onerror = function (error) {
288
+ throw new Error(`文件转blbo 失败,${file.name}`);
289
+ };
290
+ reader.readAsArrayBuffer(file);
291
+ });
292
+ }
293
+ //材质转为基础材质
294
+ toMeshBasicMaterial(material) {
295
+ if (material instanceof Array) {
296
+ return material.map((m) => this.toMeshBasicMaterial(m));
297
+ }
298
+ else {
299
+ const meshBasicMaterial = new MeshBasicMaterial();
300
+ for (let i in meshBasicMaterial) {
301
+ if (!["type"].includes(i) &&
302
+ meshBasicMaterial.hasOwnProperty(i) &&
303
+ material.hasOwnProperty(i)) {
304
+ meshBasicMaterial[i] = material[i];
305
+ }
306
+ }
307
+ return meshBasicMaterial;
308
+ }
309
+ }
310
+ toMeshLambertMaterial(material) {
311
+ if (material instanceof Array) {
312
+ return material.map((m) => this.toMeshLambertMaterial(m));
313
+ }
314
+ else {
315
+ const meshLambertMaterial = new MeshLambertMaterial();
316
+ for (let i in meshLambertMaterial) {
317
+ if (!["type"].includes(i) &&
318
+ meshLambertMaterial.hasOwnProperty(i) &&
319
+ material.hasOwnProperty(i)) {
320
+ meshLambertMaterial[i] = material[i];
321
+ }
322
+ }
323
+ return meshLambertMaterial;
324
+ }
325
+ }
326
+ }
327
+ const threeUtils = new ThreeUtils();
328
+ window.threeUtils = threeUtils;
329
+ export default threeUtils;
package/types.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- /** 颜色字符串:十六进制或 rgb/rgba */
2
- export type ColorString = `rgb(${string})` | `rgba(${string})` | `#${string}`;
1
+ /** 颜色字符串:十六进制或 rgb/rgba */
2
+ export type ColorString = `rgb(${string})` | `rgba(${string})` | `#${string}`;