three-base 2.0.130 → 2.0.137

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 (125) hide show
  1. package/ApngTexture.d.ts +13 -13
  2. package/ApngTexture.js +61 -61
  3. package/ApngTextureManage.d.ts +18 -18
  4. package/ApngTextureManage.js +87 -87
  5. package/DirectionalLight.d.ts +6 -0
  6. package/DirectionalLight.js +68 -0
  7. package/EventMesh.d.ts +18 -18
  8. package/EventMesh.js +59 -59
  9. package/ExtrudeMesh.d.ts +112 -112
  10. package/ExtrudeMesh.js +366 -366
  11. package/FPScontrol.d.ts +78 -50
  12. package/FPScontrol.js +617 -232
  13. package/Fire/Fire.d.ts +28 -28
  14. package/Fire/Fire.js +79 -79
  15. package/Fire/FireGromtry.d.ts +9 -9
  16. package/Fire/FireGromtry.js +40 -40
  17. package/Fire/FireMaterial.d.ts +28 -28
  18. package/Fire/FireMaterial.js +32 -32
  19. package/Fire/index.d.ts +1 -1
  20. package/Fire/index.js +1 -1
  21. package/Grass/Grass.d.ts +8 -8
  22. package/Grass/Grass.js +38 -38
  23. package/Grass/index.d.ts +1 -1
  24. package/Grass/index.js +1 -1
  25. package/Ground1/Ground1.d.ts +11 -11
  26. package/Ground1/Ground1.js +54 -54
  27. package/Ground1/index.d.ts +1 -1
  28. package/Ground1/index.js +1 -1
  29. package/InstancedMeshBase.d.ts +39 -39
  30. package/InstancedMeshBase.js +293 -293
  31. package/KeyFrameAnimation.d.ts +25 -25
  32. package/KeyFrameAnimation.js +78 -78
  33. package/MeshBase.d.ts +35 -35
  34. package/MeshBase.js +223 -223
  35. package/Mirror.d.ts +25 -25
  36. package/Mirror.js +139 -139
  37. package/PipeMesh/PathGeometry.d.ts +44 -44
  38. package/PipeMesh/PathGeometry.js +300 -300
  39. package/PipeMesh/PathPoint.d.ts +17 -17
  40. package/PipeMesh/PathPoint.js +31 -31
  41. package/PipeMesh/PathPointList.d.ts +31 -31
  42. package/PipeMesh/PathPointList.js +208 -208
  43. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  44. package/PipeMesh/PathTubeGeometry.js +130 -130
  45. package/PipeMesh/PipeMesh.d.ts +25 -25
  46. package/PipeMesh/PipeMesh.js +63 -63
  47. package/PipeMesh/index.d.ts +1 -1
  48. package/PipeMesh/index.js +1 -1
  49. package/PlaneModal1/PlaneModal1.d.ts +31 -31
  50. package/PlaneModal1/PlaneModal1.js +105 -105
  51. package/PlaneModal1/index.d.ts +1 -1
  52. package/PlaneModal1/index.js +1 -1
  53. package/PlaneText.d.ts +39 -39
  54. package/PlaneText.js +30 -30
  55. package/PlaneWithHtml.d.ts +11 -11
  56. package/PlaneWithHtml.js +63 -68
  57. package/PointLight/PointLight.d.ts +14 -13
  58. package/PointLight/PointLight.js +70 -64
  59. package/PointLight/index.d.ts +1 -1
  60. package/PointLight/index.js +1 -1
  61. package/Sky/Sky.d.ts +9 -9
  62. package/Sky/Sky.js +22 -22
  63. package/Sky/index.d.ts +1 -1
  64. package/Sky/index.js +1 -1
  65. package/SpotLight.d.ts +6 -0
  66. package/SpotLight.js +72 -0
  67. package/SpriteModal1/SpriteModal1.d.ts +30 -30
  68. package/SpriteModal1/SpriteModal1.js +110 -110
  69. package/SpriteModal1/index.d.ts +1 -1
  70. package/SpriteModal1/index.js +1 -1
  71. package/SpriteText.d.ts +45 -45
  72. package/SpriteText.js +163 -163
  73. package/SpriteText2.d.ts +47 -47
  74. package/SpriteText2.js +198 -198
  75. package/SpriteWithHtml.d.ts +7 -7
  76. package/SpriteWithHtml.js +44 -49
  77. package/Text3D/Text3D.d.ts +20 -20
  78. package/Text3D/Text3D.js +62 -62
  79. package/Text3D/fontController.d.ts +59 -59
  80. package/Text3D/fontController.js +199 -199
  81. package/Text3D/index.d.ts +1 -1
  82. package/Text3D/index.js +1 -1
  83. package/ThreeBase.d.ts +111 -81
  84. package/ThreeBase.js +656 -560
  85. package/ThreeMouse.d.ts +40 -40
  86. package/ThreeMouse.js +407 -407
  87. package/Transition.d.ts +40 -40
  88. package/Transition.js +210 -210
  89. package/Water/Water.d.ts +5 -0
  90. package/Water/Water.js +26 -0
  91. package/Water/waternormals.jpg +0 -0
  92. package/declare.d.ts +167 -0
  93. package/index.d.ts +1 -1
  94. package/index.js +2 -2
  95. package/keyContorls.d.ts +25 -25
  96. package/keyContorls.js +98 -98
  97. package/package.json +3 -2
  98. package/pass/bloom_pass.d.ts +2 -2
  99. package/pass/bloom_pass.js +99 -99
  100. package/pass/cover_pass.d.ts +2 -2
  101. package/pass/cover_pass.js +95 -95
  102. package/pass/emissive_ao_pass.d.ts +13 -0
  103. package/pass/emissive_ao_pass.js +370 -0
  104. package/pass/frosted_glass_pass.d.ts +2 -0
  105. package/pass/frosted_glass_pass.js +275 -0
  106. package/pass/fsQuad.d.ts +9 -9
  107. package/pass/fsQuad.js +28 -28
  108. package/pass/glow_pass.d.ts +2 -2
  109. package/pass/glow_pass.js +310 -254
  110. package/pass/ssao_pass.d.ts +13 -0
  111. package/pass/ssao_pass.js +372 -0
  112. package/pass/stroke_pass.d.ts +2 -2
  113. package/pass/stroke_pass.js +199 -199
  114. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  115. package/shaders/FXAAShaderMaterial.js +21 -21
  116. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  117. package/shaders/baseTextureToRenderShader.js +14 -14
  118. package/shaders/copyShader.d.ts +3 -3
  119. package/shaders/copyShader.js +14 -14
  120. package/shaders.d.ts +5 -5
  121. package/shaders.js +42 -42
  122. package/threeLoader.d.ts +34 -34
  123. package/threeLoader.js +527 -527
  124. package/threeUtils.d.ts +52 -52
  125. package/threeUtils.js +330 -330
package/ExtrudeMesh.js CHANGED
@@ -1,366 +1,366 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-06-08 14:20:52
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2025-01-17 20:42:22
6
- */
7
- import { clamp, cloneDeep, filter, first, get, isNumber, last } from "lodash";
8
- import { BufferAttribute, BufferGeometry, Matrix4, Mesh, Vector2, Vector3 } from "three";
9
- /**
10
- * 挤出模型
11
- * 此组件适合自动生成 公路 河流山川等 2d 平面上的模型
12
- *
13
- * @export
14
- * @class ExtrudeMesh
15
- * @extends {Mesh<BufferGeometry, Material>}
16
- */
17
- export default class ExtrudeMesh extends Mesh {
18
- // 临时使用的二维顶点对象
19
- static temp1Vector2 = new Vector2();
20
- static temp2Vector2 = new Vector2();
21
- static temp3Vector2 = new Vector2();
22
- // 临时使用的3维顶点对象
23
- static tempVector3 = new Vector3();
24
- static temp1Vector3 = new Vector3();
25
- static temp2Vector3 = new Vector3();
26
- static temp3Vector3 = new Vector3();
27
- // 临时使用的矩阵
28
- static temp1Matrix4 = new Matrix4();
29
- static temp2Matrix4 = new Matrix4();
30
- static temp3Matrix4 = new Matrix4();
31
- constructor(props, material) {
32
- super(undefined, material);
33
- this.updateGeometry(props);
34
- }
35
- //宽度
36
- shapePathWidth = 1;
37
- // 通过 props.paths 和圆角属性 计算出来的 最终路径
38
- pointerWidthRadius = [];
39
- // 通过2个顶点和缩放值计算矩阵
40
- computerMatrix(vector1, vector2, scale) {
41
- ExtrudeMesh.temp1Matrix4.identity();
42
- ExtrudeMesh.temp1Matrix4.multiply(ExtrudeMesh.temp2Matrix4.lookAt(ExtrudeMesh.temp1Vector3.set(vector1[0], vector1[1], vector1[2]), ExtrudeMesh.temp2Vector3.set(vector2[0], vector1[1], vector2[2]), ExtrudeMesh.temp3Vector3.set(0, 1, 0)));
43
- ExtrudeMesh.temp1Matrix4.scale(ExtrudeMesh.temp1Vector3.set(...scale));
44
- return ExtrudeMesh.temp1Matrix4.transpose().toArray();
45
- }
46
- /**
47
- * @计算渐变值 computerGradient([3,2,4], 0.3);
48
- * @渐变区间 values:number[]
49
- * @百分比 percentage:number
50
- */
51
- computerGradient(values, percentage) {
52
- // 过滤有效数字类型
53
- values = filter(values, v => isNumber(v));
54
- // 限定取值范围
55
- let P = clamp(percentage, 0, 1);
56
- let VL = values.length;
57
- for (let i = (VL - 1); i >= 0; i--) {
58
- if (P === (i / (VL - 1))) {
59
- return values[i];
60
- }
61
- else if (P > (i / (VL - 1))) {
62
- P -= (i / (VL - 1));
63
- P *= 1 / ((i + 1) / (VL - 1) - (i / (VL - 1)));
64
- return (values[i + 1] - values[i]) * P + values[i];
65
- }
66
- }
67
- return 0;
68
- }
69
- // 计算圆角路径 并存到 this.pointerWidthRadius 上
70
- computerPointerWidthRadius(paths, closed) {
71
- // 路径小于2个就不执行计算
72
- if (paths.length < 2) {
73
- return;
74
- }
75
- // 路径少于3个闭合路径参数不起作用
76
- if (paths.length < 3) {
77
- closed = false;
78
- }
79
- // 清空圆角路径
80
- this.pointerWidthRadius = [];
81
- paths = cloneDeep(paths);
82
- // 如果是闭合路径的话就头尾相连
83
- if (closed) {
84
- Object.defineProperties(paths, {
85
- [-1]: {
86
- value: last(paths),
87
- configurable: false,
88
- enumerable: false,
89
- writable: false // 此属性的值能否修改
90
- },
91
- [paths.length]: {
92
- value: first(paths),
93
- configurable: false,
94
- enumerable: false,
95
- writable: false // 此属性的值能否修改
96
- }
97
- });
98
- }
99
- paths.map((P, i) => {
100
- const { vector3, radius = 0.1, segments = 0, scale = { x: 1, y: 1 } } = P;
101
- //当前点位置前后都有点时就可以做圆角计算
102
- if (paths[i - 1] && paths[i + 1]) {
103
- // 第一个向量
104
- let v1 = ExtrudeMesh.temp1Vector3
105
- .set(...vector3)
106
- .sub(ExtrudeMesh.tempVector3.set(...paths[i - 1].vector3))
107
- .normalize()
108
- .clone();
109
- // 第二个向量
110
- let v2 = ExtrudeMesh.temp2Vector3
111
- .set(...paths[i + 1].vector3)
112
- .sub(ExtrudeMesh.tempVector3.set(...vector3))
113
- .normalize()
114
- .clone();
115
- // 夹角
116
- let innerAngle = v1.clone().multiplyScalar(-1).angleTo(v2);
117
- // 边长
118
- let sideLength = radius / Math.tan(innerAngle / 2);
119
- // 圆角的起点
120
- let startP = new Vector3(...vector3).sub(v1.clone().multiplyScalar(sideLength));
121
- // 圆角的终点
122
- let endP = new Vector3(...vector3).add(v2.clone().multiplyScalar(sideLength));
123
- // 圆角起点到终点的向量
124
- let StoE = endP.clone().sub(startP);
125
- // 弦长
126
- let chordLength = radius / Math.sin(innerAngle / 2);
127
- // 圆心位置
128
- let circleCenter = v2
129
- .clone()
130
- .add(v1)
131
- .multiplyScalar(0.5)
132
- .sub(v1)
133
- .setLength(chordLength)
134
- .add(ExtrudeMesh.tempVector3.set(...vector3));
135
- //向量2是否在 向量1的左边
136
- let v2Inv1Left = ExtrudeMesh.temp1Vector3
137
- .set(-v1.z, 0, v1.x)
138
- .angleTo(ExtrudeMesh.temp2Vector3.set(v2.x, 0, v2.z))
139
- < (Math.PI / 2);
140
- // XZ平面上的夹角
141
- let innerAngleXZ = ExtrudeMesh.temp1Vector3
142
- .set(v1.x, 0, v1.z)
143
- .angleTo(ExtrudeMesh.temp2Vector3.set(v2.x, 0, v2.z));
144
- // 如果当前点的位置在前后2点的连线上,就不进行分段计算 并且应用上一个点的矩阵
145
- if (innerAngleXZ === 0) {
146
- let { x = 1, y = 1 } = scale;
147
- let lastPointMatrix = last(this.pointerWidthRadius).matrix;
148
- this.pointerWidthRadius.push({
149
- vector3,
150
- matrix: ExtrudeMesh.temp1Matrix4
151
- .identity()
152
- .extractRotation(ExtrudeMesh.temp2Matrix4.set(...lastPointMatrix))
153
- .multiply(ExtrudeMesh.temp3Matrix4
154
- .identity()
155
- .scale(ExtrudeMesh.temp1Vector3.set(1, y, x)))
156
- .transpose()
157
- .toArray()
158
- });
159
- }
160
- else if (segments === 0) { // 分段数为0时不分段 并在前后各加一个点
161
- let { x = 1, y = 1 } = scale;
162
- this.pointerWidthRadius.push({
163
- vector3,
164
- matrix: this.computerMatrix((v2Inv1Left ? circleCenter.toArray() : vector3), (v2Inv1Left ? vector3 : circleCenter.toArray()), [1, y, x / Math.sin(innerAngle / 2)])
165
- });
166
- }
167
- else {
168
- for (let ii = 0; ii <= segments; ii++) {
169
- // 圆角计算的第n个点的角度
170
- let Aa = ii * ((Math.PI - innerAngle) / segments);
171
- //半径对角
172
- let Cc = Math.PI - Aa - (innerAngle / 2);
173
- //圆角分段的弦长
174
- let a = radius * Math.sin(Aa) / Math.sin(Cc);
175
- // 每段圆角的弦长
176
- let StoEvector = StoE.clone().setLength(a).add(startP);
177
- // 当前点的位置
178
- let ridiusP = StoEvector
179
- .clone()
180
- .sub(circleCenter)
181
- .setLength(radius)
182
- .add(circleCenter);
183
- let { x: preX = 1, y: preY = 1 } = get(paths[i - 1], ["scale"]) || {};
184
- let { x: nextX = 1, y: nextY = 1 } = get(paths[i + 1], ["scale"]) || {};
185
- let { x = 1, y = 1 } = scale;
186
- // 缩放值过渡计算
187
- x = this.computerGradient([(preX + x) / 2, x, (x + nextX) / 2], ii / segments);
188
- y = this.computerGradient([(preY + y) / 2, y, (y + nextY) / 2], ii / segments);
189
- // 添加一个带矩阵的点
190
- this.pointerWidthRadius.push({
191
- vector3: ridiusP.toArray(),
192
- matrix: this.computerMatrix((v2Inv1Left ? circleCenter.toArray() : ridiusP.toArray()), (v2Inv1Left ? ridiusP.toArray() : circleCenter.toArray()), [1, y, x])
193
- });
194
- }
195
- }
196
- }
197
- else {
198
- // 最后一个位置
199
- if (i === paths.length - 1) {
200
- // 如果是闭合的话 就把第一个点的位置加入最后后面
201
- if (closed) {
202
- this.pointerWidthRadius.push(first(this.pointerWidthRadius));
203
- }
204
- else {
205
- // 如果路径未闭合并且遍历到最后一个时特殊处理
206
- let lastPoint = last(this.pointerWidthRadius);
207
- // 在没有闭合的情况下添加第一个点的位置
208
- let v = ExtrudeMesh.temp1Vector3
209
- .set(...vector3)
210
- .sub(ExtrudeMesh.temp3Vector3.set(...lastPoint.vector3))
211
- .normalize();
212
- let lookAtPoint = ExtrudeMesh.temp2Vector3
213
- .set(v.z, 0, -v.x)
214
- .add(ExtrudeMesh.temp3Vector3.set(...vector3))
215
- .toArray();
216
- let { x = 1, y = 1 } = scale;
217
- this.pointerWidthRadius.push({
218
- vector3,
219
- matrix: this.computerMatrix(vector3, lookAtPoint, [1, y, x])
220
- });
221
- }
222
- }
223
- else {
224
- let { x = 1, y = 1 } = scale;
225
- // 在没有闭合的情况下添加第一个点的位置
226
- let v2 = ExtrudeMesh.temp1Vector3
227
- .set(...paths[i + 1].vector3)
228
- .sub(ExtrudeMesh.temp2Vector3.set(...vector3))
229
- .normalize();
230
- let lookAtPoint = ExtrudeMesh.temp1Vector3
231
- .set(v2.z, 0, -v2.x)
232
- .add(ExtrudeMesh.temp2Vector3.set(...vector3))
233
- .toArray();
234
- this.pointerWidthRadius.push({
235
- vector3,
236
- matrix: this.computerMatrix(vector3, lookAtPoint, [1, y, x])
237
- });
238
- }
239
- } // else
240
- });
241
- // 清理圆角路径, 如果2个点相距0.001以内就删除一个点
242
- // for (let i = 0; i < this.pointerWidthRadius.length; i++) {
243
- // while (
244
- // this.pointerWidthRadius[i] &&
245
- // this.pointerWidthRadius[i + 1] &&
246
- // this.computerPathLength(
247
- // this.pointerWidthRadius.slice(i, i + 2).map(p => p.vector3)
248
- // ) < 0.001
249
- // ) {
250
- // this.pointerWidthRadius.splice(i, 1);
251
- // }
252
- // }
253
- }
254
- // 计算三维路径的总长度
255
- computerPathLength(points) {
256
- let length = 0;
257
- points.map((p, index) => {
258
- if (index < points.length - 1) {
259
- ExtrudeMesh.temp1Vector3.set(...p);
260
- ExtrudeMesh.temp2Vector3.set(...points[index + 1]);
261
- length += ExtrudeMesh.temp1Vector3.distanceTo(ExtrudeMesh.temp2Vector3);
262
- }
263
- });
264
- return length;
265
- }
266
- // 通过一条线段计算挤出模型的各个顶点的位置和 uv坐标
267
- setExtrudeMesh(startP, // 起点位置
268
- startMatrix4, // 起点位置的矩阵
269
- endP, // 终点位置
270
- endMatrix4, // 终点位置的矩阵
271
- uvx, // uv.x 的起始位置
272
- shapePath) {
273
- // // 反转路径, 这样uv 坐标看起来习惯一些
274
- // shapePath = cloneDeep(shapePath).reverse();
275
- // 计算二维路径的总长度 作为uv坐标的边长
276
- let shapePathLength = this.computerPathLength(shapePath.map(p => [...p, 0]));
277
- // 挤出长度
278
- let extrudeLength = this.computerPathLength([startP, endP]);
279
- // 网格模型每个点
280
- let geometryPoint = [];
281
- // 网格模型每个点对应的uv坐标
282
- let uvPoint = [];
283
- shapePath.map((P, index) => {
284
- if (index < shapePath.length - 1) {
285
- // 计算出矩形面的4个顶点
286
- //[pa....Pd]
287
- //[. .]
288
- //[. .]
289
- //[. .]
290
- //[Pb....Pc]
291
- let Pa = ExtrudeMesh.tempVector3
292
- .set(0, P[1], -P[0])
293
- .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...startMatrix4))
294
- .add(ExtrudeMesh.temp1Vector3.set(...startP))
295
- .toArray();
296
- let Pb = ExtrudeMesh.tempVector3
297
- .set(0, shapePath[index + 1][1], -shapePath[index + 1][0])
298
- .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...startMatrix4))
299
- .add(ExtrudeMesh.temp1Vector3.set(...startP))
300
- .toArray();
301
- let Pc = ExtrudeMesh.tempVector3
302
- .set(0, shapePath[index + 1][1], -shapePath[index + 1][0])
303
- .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...endMatrix4))
304
- .add(ExtrudeMesh.temp1Vector3.set(...endP))
305
- .toArray();
306
- let Pd = ExtrudeMesh.tempVector3
307
- .set(0, P[1], -P[0])
308
- .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...endMatrix4))
309
- .add(ExtrudeMesh.temp1Vector3.set(...endP))
310
- .toArray();
311
- // 组成矩形的2个三角面
312
- geometryPoint.push(...Pa, ...Pb, ...Pc, ...Pa, ...Pc, ...Pd);
313
- // 计算出矩形面的4个顶点的uv坐标
314
- //[Pauv..Pcuv]
315
- //[. .]
316
- //[. .]
317
- //[. .]
318
- //[Pbuv..Pduv]
319
- // 当前位置的 uv.y 位置
320
- let currentUvy = this.computerPathLength([first(shapePath), ...shapePath.slice(0, index + 1)].map(sP => [...sP, 0])) / shapePathLength;
321
- // 下一个坐标的 uv.y 位置
322
- let nextUvy = this.computerPathLength([first(shapePath), ...shapePath.slice(0, index + 2)].map(sP => [...sP, 0])) / shapePathLength;
323
- let Pauv = [uvx / shapePathLength, currentUvy];
324
- let Pbuv = [uvx / shapePathLength, nextUvy];
325
- let Pcuv = [(uvx + extrudeLength) / shapePathLength, nextUvy];
326
- let Pduv = [(uvx + extrudeLength) / shapePathLength, currentUvy];
327
- uvPoint.push(...Pauv, ...Pbuv, ...Pcuv, ...Pauv, ...Pcuv, ...Pduv);
328
- }
329
- });
330
- return { geometryPoint, uvPoint };
331
- }
332
- //设置模型
333
- updateGeometry(props) {
334
- const { paths, shapePath, closed } = props;
335
- this.shapePathWidth = Math.max(...shapePath.map(path => path[0])) - Math.min(...shapePath.map(path => path[0]));
336
- // 计算圆角路径
337
- this.computerPointerWidthRadius(paths, closed);
338
- //网格模型的每个顶点位置
339
- let geometryPoint = [];
340
- // 网格模型每个点对应的uv坐标
341
- let uvPoint = [];
342
- for (let i = 0; i < this.pointerWidthRadius.length - 1; i++) {
343
- let Point1 = this.pointerWidthRadius[i];
344
- let Point2 = this.pointerWidthRadius[i + 1];
345
- let uvx = this.computerPathLength(this.pointerWidthRadius.slice(0, i + 1).map(p => p.vector3));
346
- const { geometryPoint: geo, uvPoint: uv } = this.setExtrudeMesh(Point1.vector3, Point1.matrix, Point2.vector3, Point2.matrix, uvx, shapePath);
347
- // 把挤出的网格模型的顶点收集起来
348
- geometryPoint.push(...geo);
349
- // 把挤出的网格模型顶点的uv坐标收集起来
350
- uvPoint.push(...uv);
351
- }
352
- let geometry = new BufferGeometry();
353
- // 修改模型
354
- let positionArray = new Float32Array(geometryPoint);
355
- let position = new BufferAttribute(positionArray, 3, false);
356
- geometry.setAttribute("position", position);
357
- let uvArray = new Float32Array(uvPoint);
358
- let uv = new BufferAttribute(uvArray, 2, false);
359
- geometry.setAttribute("uv", uv);
360
- // 计算顶点法线
361
- geometry.computeVertexNormals();
362
- this.geometry.dispose();
363
- this.geometry = geometry;
364
- this.dispatchEvent({ type: "loaded" });
365
- }
366
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2022-06-08 14:20:52
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2025-01-17 20:42:22
6
+ */
7
+ import { clamp, cloneDeep, filter, first, get, isNumber, last } from "lodash";
8
+ import { BufferAttribute, BufferGeometry, Matrix4, Mesh, Vector2, Vector3 } from "three";
9
+ /**
10
+ * 挤出模型
11
+ * 此组件适合自动生成 公路 河流山川等 2d 平面上的模型
12
+ *
13
+ * @export
14
+ * @class ExtrudeMesh
15
+ * @extends {Mesh<BufferGeometry, Material>}
16
+ */
17
+ export default class ExtrudeMesh extends Mesh {
18
+ // 临时使用的二维顶点对象
19
+ static temp1Vector2 = new Vector2();
20
+ static temp2Vector2 = new Vector2();
21
+ static temp3Vector2 = new Vector2();
22
+ // 临时使用的3维顶点对象
23
+ static tempVector3 = new Vector3();
24
+ static temp1Vector3 = new Vector3();
25
+ static temp2Vector3 = new Vector3();
26
+ static temp3Vector3 = new Vector3();
27
+ // 临时使用的矩阵
28
+ static temp1Matrix4 = new Matrix4();
29
+ static temp2Matrix4 = new Matrix4();
30
+ static temp3Matrix4 = new Matrix4();
31
+ constructor(props, material) {
32
+ super(undefined, material);
33
+ this.updateGeometry(props);
34
+ }
35
+ //宽度
36
+ shapePathWidth = 1;
37
+ // 通过 props.paths 和圆角属性 计算出来的 最终路径
38
+ pointerWidthRadius = [];
39
+ // 通过2个顶点和缩放值计算矩阵
40
+ computerMatrix(vector1, vector2, scale) {
41
+ ExtrudeMesh.temp1Matrix4.identity();
42
+ ExtrudeMesh.temp1Matrix4.multiply(ExtrudeMesh.temp2Matrix4.lookAt(ExtrudeMesh.temp1Vector3.set(vector1[0], vector1[1], vector1[2]), ExtrudeMesh.temp2Vector3.set(vector2[0], vector1[1], vector2[2]), ExtrudeMesh.temp3Vector3.set(0, 1, 0)));
43
+ ExtrudeMesh.temp1Matrix4.scale(ExtrudeMesh.temp1Vector3.set(...scale));
44
+ return ExtrudeMesh.temp1Matrix4.transpose().toArray();
45
+ }
46
+ /**
47
+ * @计算渐变值 computerGradient([3,2,4], 0.3);
48
+ * @渐变区间 values:number[]
49
+ * @百分比 percentage:number
50
+ */
51
+ computerGradient(values, percentage) {
52
+ // 过滤有效数字类型
53
+ values = filter(values, v => isNumber(v));
54
+ // 限定取值范围
55
+ let P = clamp(percentage, 0, 1);
56
+ let VL = values.length;
57
+ for (let i = (VL - 1); i >= 0; i--) {
58
+ if (P === (i / (VL - 1))) {
59
+ return values[i];
60
+ }
61
+ else if (P > (i / (VL - 1))) {
62
+ P -= (i / (VL - 1));
63
+ P *= 1 / ((i + 1) / (VL - 1) - (i / (VL - 1)));
64
+ return (values[i + 1] - values[i]) * P + values[i];
65
+ }
66
+ }
67
+ return 0;
68
+ }
69
+ // 计算圆角路径 并存到 this.pointerWidthRadius 上
70
+ computerPointerWidthRadius(paths, closed) {
71
+ // 路径小于2个就不执行计算
72
+ if (paths.length < 2) {
73
+ return;
74
+ }
75
+ // 路径少于3个闭合路径参数不起作用
76
+ if (paths.length < 3) {
77
+ closed = false;
78
+ }
79
+ // 清空圆角路径
80
+ this.pointerWidthRadius = [];
81
+ paths = cloneDeep(paths);
82
+ // 如果是闭合路径的话就头尾相连
83
+ if (closed) {
84
+ Object.defineProperties(paths, {
85
+ [-1]: {
86
+ value: last(paths),
87
+ configurable: false, // 此属性能否删除和修改
88
+ enumerable: false, // 此属性能否被遍历
89
+ writable: false // 此属性的值能否修改
90
+ },
91
+ [paths.length]: {
92
+ value: first(paths),
93
+ configurable: false, // 此属性能否删除和修改
94
+ enumerable: false, // 此属性能否被遍历
95
+ writable: false // 此属性的值能否修改
96
+ }
97
+ });
98
+ }
99
+ paths.map((P, i) => {
100
+ const { vector3, radius = 0.1, segments = 0, scale = { x: 1, y: 1 } } = P;
101
+ //当前点位置前后都有点时就可以做圆角计算
102
+ if (paths[i - 1] && paths[i + 1]) {
103
+ // 第一个向量
104
+ let v1 = ExtrudeMesh.temp1Vector3
105
+ .set(...vector3)
106
+ .sub(ExtrudeMesh.tempVector3.set(...paths[i - 1].vector3))
107
+ .normalize()
108
+ .clone();
109
+ // 第二个向量
110
+ let v2 = ExtrudeMesh.temp2Vector3
111
+ .set(...paths[i + 1].vector3)
112
+ .sub(ExtrudeMesh.tempVector3.set(...vector3))
113
+ .normalize()
114
+ .clone();
115
+ // 夹角
116
+ let innerAngle = v1.clone().multiplyScalar(-1).angleTo(v2);
117
+ // 边长
118
+ let sideLength = radius / Math.tan(innerAngle / 2);
119
+ // 圆角的起点
120
+ let startP = new Vector3(...vector3).sub(v1.clone().multiplyScalar(sideLength));
121
+ // 圆角的终点
122
+ let endP = new Vector3(...vector3).add(v2.clone().multiplyScalar(sideLength));
123
+ // 圆角起点到终点的向量
124
+ let StoE = endP.clone().sub(startP);
125
+ // 弦长
126
+ let chordLength = radius / Math.sin(innerAngle / 2);
127
+ // 圆心位置
128
+ let circleCenter = v2
129
+ .clone()
130
+ .add(v1)
131
+ .multiplyScalar(0.5)
132
+ .sub(v1)
133
+ .setLength(chordLength)
134
+ .add(ExtrudeMesh.tempVector3.set(...vector3));
135
+ //向量2是否在 向量1的左边
136
+ let v2Inv1Left = ExtrudeMesh.temp1Vector3
137
+ .set(-v1.z, 0, v1.x)
138
+ .angleTo(ExtrudeMesh.temp2Vector3.set(v2.x, 0, v2.z))
139
+ < (Math.PI / 2);
140
+ // XZ平面上的夹角
141
+ let innerAngleXZ = ExtrudeMesh.temp1Vector3
142
+ .set(v1.x, 0, v1.z)
143
+ .angleTo(ExtrudeMesh.temp2Vector3.set(v2.x, 0, v2.z));
144
+ // 如果当前点的位置在前后2点的连线上,就不进行分段计算 并且应用上一个点的矩阵
145
+ if (innerAngleXZ === 0) {
146
+ let { x = 1, y = 1 } = scale;
147
+ let lastPointMatrix = last(this.pointerWidthRadius).matrix;
148
+ this.pointerWidthRadius.push({
149
+ vector3,
150
+ matrix: ExtrudeMesh.temp1Matrix4
151
+ .identity()
152
+ .extractRotation(ExtrudeMesh.temp2Matrix4.set(...lastPointMatrix))
153
+ .multiply(ExtrudeMesh.temp3Matrix4
154
+ .identity()
155
+ .scale(ExtrudeMesh.temp1Vector3.set(1, y, x)))
156
+ .transpose()
157
+ .toArray()
158
+ });
159
+ }
160
+ else if (segments === 0) { // 分段数为0时不分段 并在前后各加一个点
161
+ let { x = 1, y = 1 } = scale;
162
+ this.pointerWidthRadius.push({
163
+ vector3,
164
+ matrix: this.computerMatrix((v2Inv1Left ? circleCenter.toArray() : vector3), (v2Inv1Left ? vector3 : circleCenter.toArray()), [1, y, x / Math.sin(innerAngle / 2)])
165
+ });
166
+ }
167
+ else {
168
+ for (let ii = 0; ii <= segments; ii++) {
169
+ // 圆角计算的第n个点的角度
170
+ let Aa = ii * ((Math.PI - innerAngle) / segments);
171
+ //半径对角
172
+ let Cc = Math.PI - Aa - (innerAngle / 2);
173
+ //圆角分段的弦长
174
+ let a = radius * Math.sin(Aa) / Math.sin(Cc);
175
+ // 每段圆角的弦长
176
+ let StoEvector = StoE.clone().setLength(a).add(startP);
177
+ // 当前点的位置
178
+ let ridiusP = StoEvector
179
+ .clone()
180
+ .sub(circleCenter)
181
+ .setLength(radius)
182
+ .add(circleCenter);
183
+ let { x: preX = 1, y: preY = 1 } = get(paths[i - 1], ["scale"]) || {};
184
+ let { x: nextX = 1, y: nextY = 1 } = get(paths[i + 1], ["scale"]) || {};
185
+ let { x = 1, y = 1 } = scale;
186
+ // 缩放值过渡计算
187
+ x = this.computerGradient([(preX + x) / 2, x, (x + nextX) / 2], ii / segments);
188
+ y = this.computerGradient([(preY + y) / 2, y, (y + nextY) / 2], ii / segments);
189
+ // 添加一个带矩阵的点
190
+ this.pointerWidthRadius.push({
191
+ vector3: ridiusP.toArray(),
192
+ matrix: this.computerMatrix((v2Inv1Left ? circleCenter.toArray() : ridiusP.toArray()), (v2Inv1Left ? ridiusP.toArray() : circleCenter.toArray()), [1, y, x])
193
+ });
194
+ }
195
+ }
196
+ }
197
+ else {
198
+ // 最后一个位置
199
+ if (i === paths.length - 1) {
200
+ // 如果是闭合的话 就把第一个点的位置加入最后后面
201
+ if (closed) {
202
+ this.pointerWidthRadius.push(first(this.pointerWidthRadius));
203
+ }
204
+ else {
205
+ // 如果路径未闭合并且遍历到最后一个时特殊处理
206
+ let lastPoint = last(this.pointerWidthRadius);
207
+ // 在没有闭合的情况下添加第一个点的位置
208
+ let v = ExtrudeMesh.temp1Vector3
209
+ .set(...vector3)
210
+ .sub(ExtrudeMesh.temp3Vector3.set(...lastPoint.vector3))
211
+ .normalize();
212
+ let lookAtPoint = ExtrudeMesh.temp2Vector3
213
+ .set(v.z, 0, -v.x)
214
+ .add(ExtrudeMesh.temp3Vector3.set(...vector3))
215
+ .toArray();
216
+ let { x = 1, y = 1 } = scale;
217
+ this.pointerWidthRadius.push({
218
+ vector3,
219
+ matrix: this.computerMatrix(vector3, lookAtPoint, [1, y, x])
220
+ });
221
+ }
222
+ }
223
+ else {
224
+ let { x = 1, y = 1 } = scale;
225
+ // 在没有闭合的情况下添加第一个点的位置
226
+ let v2 = ExtrudeMesh.temp1Vector3
227
+ .set(...paths[i + 1].vector3)
228
+ .sub(ExtrudeMesh.temp2Vector3.set(...vector3))
229
+ .normalize();
230
+ let lookAtPoint = ExtrudeMesh.temp1Vector3
231
+ .set(v2.z, 0, -v2.x)
232
+ .add(ExtrudeMesh.temp2Vector3.set(...vector3))
233
+ .toArray();
234
+ this.pointerWidthRadius.push({
235
+ vector3,
236
+ matrix: this.computerMatrix(vector3, lookAtPoint, [1, y, x])
237
+ });
238
+ }
239
+ } // else
240
+ });
241
+ // 清理圆角路径, 如果2个点相距0.001以内就删除一个点
242
+ // for (let i = 0; i < this.pointerWidthRadius.length; i++) {
243
+ // while (
244
+ // this.pointerWidthRadius[i] &&
245
+ // this.pointerWidthRadius[i + 1] &&
246
+ // this.computerPathLength(
247
+ // this.pointerWidthRadius.slice(i, i + 2).map(p => p.vector3)
248
+ // ) < 0.001
249
+ // ) {
250
+ // this.pointerWidthRadius.splice(i, 1);
251
+ // }
252
+ // }
253
+ }
254
+ // 计算三维路径的总长度
255
+ computerPathLength(points) {
256
+ let length = 0;
257
+ points.map((p, index) => {
258
+ if (index < points.length - 1) {
259
+ ExtrudeMesh.temp1Vector3.set(...p);
260
+ ExtrudeMesh.temp2Vector3.set(...points[index + 1]);
261
+ length += ExtrudeMesh.temp1Vector3.distanceTo(ExtrudeMesh.temp2Vector3);
262
+ }
263
+ });
264
+ return length;
265
+ }
266
+ // 通过一条线段计算挤出模型的各个顶点的位置和 uv坐标
267
+ setExtrudeMesh(startP, // 起点位置
268
+ startMatrix4, // 起点位置的矩阵
269
+ endP, // 终点位置
270
+ endMatrix4, // 终点位置的矩阵
271
+ uvx, // uv.x 的起始位置
272
+ shapePath) {
273
+ // // 反转路径, 这样uv 坐标看起来习惯一些
274
+ // shapePath = cloneDeep(shapePath).reverse();
275
+ // 计算二维路径的总长度 作为uv坐标的边长
276
+ let shapePathLength = this.computerPathLength(shapePath.map(p => [...p, 0]));
277
+ // 挤出长度
278
+ let extrudeLength = this.computerPathLength([startP, endP]);
279
+ // 网格模型每个点
280
+ let geometryPoint = [];
281
+ // 网格模型每个点对应的uv坐标
282
+ let uvPoint = [];
283
+ shapePath.map((P, index) => {
284
+ if (index < shapePath.length - 1) {
285
+ // 计算出矩形面的4个顶点
286
+ //[pa....Pd]
287
+ //[. .]
288
+ //[. .]
289
+ //[. .]
290
+ //[Pb....Pc]
291
+ let Pa = ExtrudeMesh.tempVector3
292
+ .set(0, P[1], -P[0])
293
+ .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...startMatrix4))
294
+ .add(ExtrudeMesh.temp1Vector3.set(...startP))
295
+ .toArray();
296
+ let Pb = ExtrudeMesh.tempVector3
297
+ .set(0, shapePath[index + 1][1], -shapePath[index + 1][0])
298
+ .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...startMatrix4))
299
+ .add(ExtrudeMesh.temp1Vector3.set(...startP))
300
+ .toArray();
301
+ let Pc = ExtrudeMesh.tempVector3
302
+ .set(0, shapePath[index + 1][1], -shapePath[index + 1][0])
303
+ .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...endMatrix4))
304
+ .add(ExtrudeMesh.temp1Vector3.set(...endP))
305
+ .toArray();
306
+ let Pd = ExtrudeMesh.tempVector3
307
+ .set(0, P[1], -P[0])
308
+ .applyMatrix4(ExtrudeMesh.temp1Matrix4.set(...endMatrix4))
309
+ .add(ExtrudeMesh.temp1Vector3.set(...endP))
310
+ .toArray();
311
+ // 组成矩形的2个三角面
312
+ geometryPoint.push(...Pa, ...Pb, ...Pc, ...Pa, ...Pc, ...Pd);
313
+ // 计算出矩形面的4个顶点的uv坐标
314
+ //[Pauv..Pcuv]
315
+ //[. .]
316
+ //[. .]
317
+ //[. .]
318
+ //[Pbuv..Pduv]
319
+ // 当前位置的 uv.y 位置
320
+ let currentUvy = this.computerPathLength([first(shapePath), ...shapePath.slice(0, index + 1)].map(sP => [...sP, 0])) / shapePathLength;
321
+ // 下一个坐标的 uv.y 位置
322
+ let nextUvy = this.computerPathLength([first(shapePath), ...shapePath.slice(0, index + 2)].map(sP => [...sP, 0])) / shapePathLength;
323
+ let Pauv = [uvx / shapePathLength, currentUvy];
324
+ let Pbuv = [uvx / shapePathLength, nextUvy];
325
+ let Pcuv = [(uvx + extrudeLength) / shapePathLength, nextUvy];
326
+ let Pduv = [(uvx + extrudeLength) / shapePathLength, currentUvy];
327
+ uvPoint.push(...Pauv, ...Pbuv, ...Pcuv, ...Pauv, ...Pcuv, ...Pduv);
328
+ }
329
+ });
330
+ return { geometryPoint, uvPoint };
331
+ }
332
+ //设置模型
333
+ updateGeometry(props) {
334
+ const { paths, shapePath, closed } = props;
335
+ this.shapePathWidth = Math.max(...shapePath.map(path => path[0])) - Math.min(...shapePath.map(path => path[0]));
336
+ // 计算圆角路径
337
+ this.computerPointerWidthRadius(paths, closed);
338
+ //网格模型的每个顶点位置
339
+ let geometryPoint = [];
340
+ // 网格模型每个点对应的uv坐标
341
+ let uvPoint = [];
342
+ for (let i = 0; i < this.pointerWidthRadius.length - 1; i++) {
343
+ let Point1 = this.pointerWidthRadius[i];
344
+ let Point2 = this.pointerWidthRadius[i + 1];
345
+ let uvx = this.computerPathLength(this.pointerWidthRadius.slice(0, i + 1).map(p => p.vector3));
346
+ const { geometryPoint: geo, uvPoint: uv } = this.setExtrudeMesh(Point1.vector3, Point1.matrix, Point2.vector3, Point2.matrix, uvx, shapePath);
347
+ // 把挤出的网格模型的顶点收集起来
348
+ geometryPoint.push(...geo);
349
+ // 把挤出的网格模型顶点的uv坐标收集起来
350
+ uvPoint.push(...uv);
351
+ }
352
+ let geometry = new BufferGeometry();
353
+ // 修改模型
354
+ let positionArray = new Float32Array(geometryPoint);
355
+ let position = new BufferAttribute(positionArray, 3, false);
356
+ geometry.setAttribute("position", position);
357
+ let uvArray = new Float32Array(uvPoint);
358
+ let uv = new BufferAttribute(uvArray, 2, false);
359
+ geometry.setAttribute("uv", uv);
360
+ // 计算顶点法线
361
+ geometry.computeVertexNormals();
362
+ this.geometry.dispose();
363
+ this.geometry = geometry;
364
+ this.dispatchEvent({ type: "loaded" });
365
+ }
366
+ }