three-base 2.0.142 → 2.0.166

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 (231) 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 +531 -511
  10. package/FPScontrol.js +1866 -1799
  11. package/FPScontrol.ts +1922 -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 +251 -247
  33. package/MeshBase.ts +6 -2
  34. package/Mirror.d.ts +25 -25
  35. package/Mirror.js +143 -143
  36. package/MktMesh/MktMesh.d.ts +301 -291
  37. package/MktMesh/MktMesh.js +1064 -1045
  38. package/MktMesh/MktMesh.ts +32 -11
  39. package/MktMesh/assetUrl.d.ts +59 -35
  40. package/MktMesh/assetUrl.js +165 -69
  41. package/MktMesh/assetUrl.ts +164 -69
  42. package/MktMesh/mktMappingShader.d.ts +12 -12
  43. package/MktMesh/mktMappingShader.js +138 -138
  44. package/MktMesh/mktMappingShader.ts +139 -139
  45. package/MktMesh/mktShader.d.ts +263 -263
  46. package/MktMesh/mktShader.js +1236 -1236
  47. package/MktMesh/mktShader.ts +1344 -1344
  48. package/MktMesh/panoBitmapTexture.d.ts +25 -25
  49. package/MktMesh/panoBitmapTexture.js +52 -52
  50. package/MktMesh/panoBitmapTexture.ts +54 -54
  51. package/MktMesh/panoSlotBind.d.ts +29 -29
  52. package/MktMesh/panoSlotBind.js +106 -106
  53. package/MktMesh/panoSlotBind.ts +153 -153
  54. package/MktMesh/panoSlotRoster.d.ts +22 -22
  55. package/MktMesh/panoSlotRoster.js +169 -169
  56. package/MktMesh/panoSlotRoster.ts +168 -168
  57. package/MktMesh/panoTileStitcher.d.ts +119 -119
  58. package/MktMesh/panoTileStitcher.js +529 -529
  59. package/MktMesh/panoTileStitcher.ts +616 -616
  60. package/MktMesh/types.d.ts +75 -75
  61. package/MktMesh/types.js +1 -1
  62. package/MktMesh/wallCapTessellate.d.ts +33 -33
  63. package/MktMesh/wallCapTessellate.js +97 -97
  64. package/MktMesh/wallCapTessellate.ts +68 -68
  65. package/PipeMesh/PathGeometry.d.ts +44 -44
  66. package/PipeMesh/PathGeometry.js +307 -307
  67. package/PipeMesh/PathPoint.d.ts +17 -17
  68. package/PipeMesh/PathPoint.js +31 -31
  69. package/PipeMesh/PathPointList.d.ts +31 -31
  70. package/PipeMesh/PathPointList.js +209 -209
  71. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  72. package/PipeMesh/PathTubeGeometry.js +130 -130
  73. package/PipeMesh/PipeMesh.d.ts +25 -25
  74. package/PipeMesh/PipeMesh.js +63 -63
  75. package/PipeMesh/index.d.ts +1 -1
  76. package/PipeMesh/index.js +1 -1
  77. package/PlaneModal1/PlaneModal1.d.ts +34 -34
  78. package/PlaneModal1/PlaneModal1.js +109 -109
  79. package/PlaneModal1/index.d.ts +1 -1
  80. package/PlaneModal1/index.js +1 -1
  81. package/PlaneText.d.ts +27 -27
  82. package/PlaneText.js +35 -35
  83. package/PlaneWithHtml.d.ts +13 -13
  84. package/PlaneWithHtml.js +66 -64
  85. package/PlaneWithHtml.ts +3 -1
  86. package/PointLight/PointLight.d.ts +13 -13
  87. package/PointLight/PointLight.js +64 -64
  88. package/PointLight/index.d.ts +1 -1
  89. package/PointLight/index.js +1 -1
  90. package/Sky/Sky.d.ts +9 -9
  91. package/Sky/Sky.js +22 -22
  92. package/Sky/index.d.ts +1 -1
  93. package/Sky/index.js +1 -1
  94. package/SpotLight.d.ts +6 -6
  95. package/SpotLight.js +72 -72
  96. package/SpriteModal1/SpriteModal1.d.ts +33 -33
  97. package/SpriteModal1/SpriteModal1.js +116 -116
  98. package/SpriteModal1/index.d.ts +1 -1
  99. package/SpriteModal1/index.js +1 -1
  100. package/SpriteText.d.ts +46 -46
  101. package/SpriteText.js +176 -176
  102. package/SpriteText2.d.ts +48 -48
  103. package/SpriteText2.js +223 -223
  104. package/SpriteText3.d.ts +86 -86
  105. package/SpriteText3.js +517 -517
  106. package/SpriteText3.ts +599 -599
  107. package/SpriteWithHtml.d.ts +9 -9
  108. package/SpriteWithHtml.js +46 -44
  109. package/SpriteWithHtml.ts +3 -1
  110. package/Text3D/Text3D.d.ts +20 -20
  111. package/Text3D/Text3D.js +58 -58
  112. package/Text3D/fontController.d.ts +60 -60
  113. package/Text3D/fontController.js +166 -166
  114. package/Text3D/index.d.ts +1 -1
  115. package/Text3D/index.js +1 -1
  116. package/ThreeBase.d.ts +248 -248
  117. package/ThreeBase.js +1202 -1202
  118. package/ThreeBase.ts +1314 -1314
  119. package/ThreeMouse.d.ts +53 -54
  120. package/ThreeMouse.js +453 -453
  121. package/Transition.d.ts +45 -45
  122. package/Transition.js +209 -209
  123. package/Water/Water.d.ts +5 -5
  124. package/Water/Water.js +44 -44
  125. package/Water/index.d.ts +2 -2
  126. package/Water/index.js +2 -2
  127. package/WaterFlow/FloatingDuck.d.ts +88 -88
  128. package/WaterFlow/FloatingDuck.js +307 -307
  129. package/WaterFlow/FloatingDuck.ts +445 -445
  130. package/WaterFlow/GPUFluidSimulation.d.ts +56 -56
  131. package/WaterFlow/GPUFluidSimulation.js +781 -781
  132. package/WaterFlow/GPUFluidSimulation.ts +827 -827
  133. package/WaterFlow/ReservoirSystem.d.ts +106 -106
  134. package/WaterFlow/ReservoirSystem.js +611 -611
  135. package/WaterFlow/ReservoirSystem.ts +754 -754
  136. package/WaterFlow/SPHField.d.ts +35 -35
  137. package/WaterFlow/SPHField.js +203 -203
  138. package/WaterFlow/SPHField.ts +228 -228
  139. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -69
  140. package/WaterFlow/ScreenSpaceFluidPass.js +728 -728
  141. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -789
  142. package/WaterFlow/ShallowWaterSurface.d.ts +45 -45
  143. package/WaterFlow/ShallowWaterSurface.js +438 -438
  144. package/WaterFlow/ShallowWaterSurface.ts +510 -510
  145. package/WaterFlow/WaterFlow.d.ts +87 -87
  146. package/WaterFlow/WaterFlow.js +171 -171
  147. package/WaterFlow/WaterFlow.ts +219 -219
  148. package/WaterFlow/types.d.ts +161 -161
  149. package/WaterFlow/types.js +1 -1
  150. package/WaterFlow/types.ts +189 -189
  151. package/WaterFlow/waterFlowPass.d.ts +3 -3
  152. package/WaterFlow/waterFlowPass.js +20 -20
  153. package/WaterFlow/waterFlowPass.ts +40 -40
  154. package/collisionOBB.d.ts +42 -42
  155. package/collisionOBB.js +112 -112
  156. package/collisionOBB.ts +108 -108
  157. package/declare.d.ts +25 -25
  158. package/index.d.ts +1 -1
  159. package/index.js +1 -1
  160. package/instanced/InstancedBatch.d.ts +57 -57
  161. package/instanced/InstancedBatch.js +348 -348
  162. package/instanced/InstancedBatch.ts +406 -406
  163. package/instanced/InstancedChild.d.ts +174 -174
  164. package/instanced/InstancedChild.js +264 -264
  165. package/instanced/InstancedChild.ts +327 -327
  166. package/instanced/InstancedGroup.d.ts +43 -43
  167. package/instanced/InstancedGroup.js +126 -126
  168. package/instanced/InstancedGroup.ts +137 -137
  169. package/instanced/InstancedMeshBase.d.ts +38 -38
  170. package/instanced/InstancedMeshBase.js +282 -282
  171. package/instanced/InstancedTemplate.d.ts +46 -46
  172. package/instanced/InstancedTemplate.js +125 -125
  173. package/keyControls.d.ts +29 -30
  174. package/keyControls.js +101 -101
  175. package/lensflare/LensFlare.d.ts +39 -39
  176. package/lensflare/LensFlare.js +284 -284
  177. package/package.json +6 -1
  178. package/pass/bloom_pass.d.ts +2 -2
  179. package/pass/bloom_pass.js +174 -174
  180. package/pass/cover_pass.d.ts +2 -2
  181. package/pass/cover_pass.js +108 -108
  182. package/pass/debugDepthState.d.ts +20 -20
  183. package/pass/debugDepthState.js +9 -9
  184. package/pass/debugDepthState.ts +22 -22
  185. package/pass/depthTestBias.d.ts +12 -12
  186. package/pass/depthTestBias.js +12 -12
  187. package/pass/depthTestBias.ts +12 -12
  188. package/pass/effectUserData.d.ts +27 -27
  189. package/pass/effectUserData.js +1 -1
  190. package/pass/effectUserData.ts +46 -46
  191. package/pass/frosted_glass_pass.d.ts +2 -2
  192. package/pass/frosted_glass_pass.js +150 -150
  193. package/pass/fsQuad.d.ts +9 -9
  194. package/pass/fsQuad.js +28 -28
  195. package/pass/glow_pass.d.ts +2 -2
  196. package/pass/glow_pass.js +350 -350
  197. package/pass/screen_bloom_pass.d.ts +11 -11
  198. package/pass/screen_bloom_pass.js +136 -136
  199. package/pass/ssao_pass.d.ts +88 -88
  200. package/pass/ssao_pass.js +799 -799
  201. package/pass/ssao_pass.ts +918 -918
  202. package/pass/stroke_pass.d.ts +2 -2
  203. package/pass/stroke_pass.js +267 -267
  204. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  205. package/shaders/FXAAShaderMaterial.js +20 -20
  206. package/shaders/baseDepthVisShader.d.ts +11 -11
  207. package/shaders/baseDepthVisShader.js +36 -36
  208. package/shaders/baseDepthVisShader.ts +36 -36
  209. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  210. package/shaders/baseTextureToRenderShader.js +19 -19
  211. package/shaders/copyShader.d.ts +3 -3
  212. package/shaders/copyShader.js +13 -13
  213. package/shaders/debugDepthCompareShader.d.ts +14 -14
  214. package/shaders/debugDepthCompareShader.js +49 -49
  215. package/shaders/debugDepthCompareShader.ts +50 -50
  216. package/shaders/debugDepthJudgeShader.d.ts +18 -18
  217. package/shaders/debugDepthJudgeShader.js +52 -52
  218. package/shaders/debugDepthJudgeShader.ts +54 -54
  219. package/shaders/debugExcludedShader.d.ts +13 -13
  220. package/shaders/debugExcludedShader.js +46 -46
  221. package/shaders/debugExcludedShader.ts +48 -48
  222. package/spriteTextBg.d.ts +1 -1
  223. package/spriteTextBg.js +20 -20
  224. package/spriteTextBg.ts +20 -20
  225. package/threeLoader.d.ts +35 -35
  226. package/threeLoader.js +518 -518
  227. package/threeUtils.d.ts +71 -53
  228. package/threeUtils.js +363 -329
  229. package/threeUtils.ts +35 -0
  230. package/types.d.ts +2 -2
  231. package/types.js +6 -6
package/SpriteText.js CHANGED
@@ -1,176 +1,176 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-05-03 02:42:07
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 02:53:00
6
- */
7
- import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
8
- export default class SpriteText extends Sprite {
9
- constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
10
- super(new SpriteMaterial());
11
- this.material.alphaTest = 0.1;
12
- this._text = `${text}`;
13
- this._textHeight = textHeight;
14
- this._color = color;
15
- this._genCanvas();
16
- }
17
- _text = "";
18
- _textHeight = 10;
19
- _color = "#ffffff";
20
- _backgroundColor = false; // no background color
21
- _padding = 0;
22
- _borderWidth = 0;
23
- _borderRadius = 0;
24
- _borderColor = "#fffff";
25
- _strokeWidth = 0;
26
- _strokeColor = "#fffff";
27
- _fontFace = 'Arial';
28
- _fontSize = 90; // defines text resolution
29
- _fontWeight = 'normal';
30
- _canvas = document.createElement('canvas');
31
- get text() { return this._text; }
32
- set text(text) { this._text = text; this._genCanvas(); }
33
- get textHeight() { return this._textHeight; }
34
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
35
- get color() { return this._color; }
36
- set color(color) { this._color = color; this._genCanvas(); }
37
- get backgroundColor() { return this._backgroundColor; }
38
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
39
- get padding() { return this._padding; }
40
- set padding(padding) { this._padding = padding; this._genCanvas(); }
41
- get borderWidth() { return this._borderWidth; }
42
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
43
- get borderRadius() { return this._borderRadius; }
44
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
45
- get borderColor() { return this._borderColor; }
46
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
47
- get fontFace() { return this._fontFace; }
48
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
49
- get fontSize() { return this._fontSize; }
50
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
51
- get fontWeight() { return this._fontWeight; }
52
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
53
- get strokeWidth() { return this._strokeWidth; }
54
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
55
- get strokeColor() { return this._strokeColor; }
56
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
57
- _genCanvas() {
58
- const canvas = this._canvas;
59
- const ctx = canvas.getContext('2d');
60
- if (!ctx)
61
- return;
62
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
63
- const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
64
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
65
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
66
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
67
- const relPadding = padding.map(p => p * this.fontSize * 0.1);
68
- ; // padding in canvas units
69
- const lines = this.text.split('\n');
70
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
71
- ctx.font = font; // measure canvas with appropriate font
72
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
73
- const innerHeight = this.fontSize * lines.length;
74
- canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
75
- canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
76
- // paint border
77
- if (this.borderWidth) {
78
- ctx.strokeStyle = this.borderColor;
79
- if (relBorder[0]) { // left + right borders
80
- const hb = relBorder[0] / 2;
81
- ctx.lineWidth = relBorder[0];
82
- ctx.beginPath();
83
- ctx.moveTo(hb, relBorderRadius[0]);
84
- ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
85
- ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
86
- ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
87
- ctx.stroke();
88
- }
89
- if (relBorder[1]) { // top + bottom borders
90
- const hb = relBorder[1] / 2;
91
- ctx.lineWidth = relBorder[1];
92
- ctx.beginPath();
93
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
94
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
95
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
96
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
97
- ctx.stroke();
98
- }
99
- if (this.borderRadius) { // strike rounded corners
100
- const cornerWidth = Math.max(...relBorder);
101
- const hb = cornerWidth / 2;
102
- ctx.lineWidth = cornerWidth;
103
- ctx.beginPath();
104
- [
105
- !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
106
- !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
107
- !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
108
- !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
109
- ].filter((d) => !!d).forEach(([x0, x1, y0, y1]) => {
110
- ctx.moveTo(x0, y0);
111
- ctx.quadraticCurveTo(x1, y0, x1, y1);
112
- });
113
- ctx.stroke();
114
- }
115
- }
116
- // paint background
117
- if (this.backgroundColor && typeof this.backgroundColor === 'string') {
118
- ctx.fillStyle = this.backgroundColor;
119
- if (!this.borderRadius) {
120
- ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
121
- }
122
- else { // fill with rounded corners
123
- ctx.beginPath();
124
- ctx.moveTo(relBorder[0], relBorderRadius[0]);
125
- [
126
- [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]],
127
- [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]],
128
- [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]],
129
- [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
130
- ].forEach(([x0, x1, x2, y0, y1, y2]) => {
131
- ctx.quadraticCurveTo(x0, y0, x1, y1);
132
- ctx.lineTo(x2, y2);
133
- });
134
- ctx.closePath();
135
- ctx.fill();
136
- }
137
- }
138
- ctx.translate(...relBorder);
139
- ctx.translate(...relPadding);
140
- // paint text
141
- ctx.font = font; // Set font again after canvas is resized, as context properties are reset
142
- ctx.fillStyle = this.color;
143
- // 精确垂直居中:用 measureText 的 actualBoundingBox 实测字形上下沿,
144
- // 不再依赖 textBaseline='middle'(middle 是 em 盒中线,与真实字形视觉中线常偏差,
145
- // 导致整行文字偏上/偏下、看着不居中)。改为 alphabetic 基线 + 实测字形高度,
146
- // 把每行字形放进其 fontSize 高的槽中心。
147
- ctx.textAlign = 'center';
148
- ctx.textBaseline = 'alphabetic';
149
- const drawTextStroke = this.strokeWidth > 0;
150
- if (drawTextStroke) {
151
- ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
152
- ctx.strokeStyle = this.strokeColor;
153
- }
154
- const slotH = this.fontSize; // 每行槽高
155
- lines.forEach((line, index) => {
156
- const metrics = ctx.measureText(line);
157
- const ascent = metrics.actualBoundingBoxAscent; // 字形顶部到基线
158
- const descent = metrics.actualBoundingBoxDescent; // 基线到字形底部
159
- const glyphH = (ascent || 0) + (descent || 0);
160
- const slotTop = index * slotH; // 本行槽顶(内容框坐标系,已平移)
161
- // 让字形在槽中垂直居中:先平移到槽中线,再上移 ascent 回到基线
162
- const baselineY = slotTop + (slotH - glyphH) / 2 + (ascent || slotH / 2);
163
- const lineX = innerWidth / 2;
164
- drawTextStroke && ctx.strokeText(line, lineX, baselineY);
165
- ctx.fillText(line, lineX, baselineY);
166
- });
167
- // Inject canvas into sprite
168
- if (this.material.map)
169
- this.material.map.dispose(); // gc previous texture
170
- const texture = this.material.map = new Texture(canvas);
171
- texture.minFilter = LinearFilter;
172
- texture.needsUpdate = true;
173
- const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
174
- this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
175
- }
176
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-05-03 02:42:07
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-05-03 02:53:00
6
+ */
7
+ import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
8
+ export default class SpriteText extends Sprite {
9
+ constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
10
+ super(new SpriteMaterial());
11
+ this.material.alphaTest = 0.1;
12
+ this._text = `${text}`;
13
+ this._textHeight = textHeight;
14
+ this._color = color;
15
+ this._genCanvas();
16
+ }
17
+ _text = "";
18
+ _textHeight = 10;
19
+ _color = "#ffffff";
20
+ _backgroundColor = false; // no background color
21
+ _padding = 0;
22
+ _borderWidth = 0;
23
+ _borderRadius = 0;
24
+ _borderColor = "#fffff";
25
+ _strokeWidth = 0;
26
+ _strokeColor = "#fffff";
27
+ _fontFace = 'Arial';
28
+ _fontSize = 90; // defines text resolution
29
+ _fontWeight = 'normal';
30
+ _canvas = document.createElement('canvas');
31
+ get text() { return this._text; }
32
+ set text(text) { this._text = text; this._genCanvas(); }
33
+ get textHeight() { return this._textHeight; }
34
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
35
+ get color() { return this._color; }
36
+ set color(color) { this._color = color; this._genCanvas(); }
37
+ get backgroundColor() { return this._backgroundColor; }
38
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
39
+ get padding() { return this._padding; }
40
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
41
+ get borderWidth() { return this._borderWidth; }
42
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
43
+ get borderRadius() { return this._borderRadius; }
44
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
45
+ get borderColor() { return this._borderColor; }
46
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
47
+ get fontFace() { return this._fontFace; }
48
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
49
+ get fontSize() { return this._fontSize; }
50
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
51
+ get fontWeight() { return this._fontWeight; }
52
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
53
+ get strokeWidth() { return this._strokeWidth; }
54
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
55
+ get strokeColor() { return this._strokeColor; }
56
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
57
+ _genCanvas() {
58
+ const canvas = this._canvas;
59
+ const ctx = canvas.getContext('2d');
60
+ if (!ctx)
61
+ return;
62
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
63
+ const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
64
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
65
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
66
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
67
+ const relPadding = padding.map(p => p * this.fontSize * 0.1);
68
+ ; // padding in canvas units
69
+ const lines = this.text.split('\n');
70
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
71
+ ctx.font = font; // measure canvas with appropriate font
72
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
73
+ const innerHeight = this.fontSize * lines.length;
74
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
75
+ canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
76
+ // paint border
77
+ if (this.borderWidth) {
78
+ ctx.strokeStyle = this.borderColor;
79
+ if (relBorder[0]) { // left + right borders
80
+ const hb = relBorder[0] / 2;
81
+ ctx.lineWidth = relBorder[0];
82
+ ctx.beginPath();
83
+ ctx.moveTo(hb, relBorderRadius[0]);
84
+ ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
85
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
86
+ ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
87
+ ctx.stroke();
88
+ }
89
+ if (relBorder[1]) { // top + bottom borders
90
+ const hb = relBorder[1] / 2;
91
+ ctx.lineWidth = relBorder[1];
92
+ ctx.beginPath();
93
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
94
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
95
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
96
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
97
+ ctx.stroke();
98
+ }
99
+ if (this.borderRadius) { // strike rounded corners
100
+ const cornerWidth = Math.max(...relBorder);
101
+ const hb = cornerWidth / 2;
102
+ ctx.lineWidth = cornerWidth;
103
+ ctx.beginPath();
104
+ [
105
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
106
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
107
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
108
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
109
+ ].filter((d) => !!d).forEach(([x0, x1, y0, y1]) => {
110
+ ctx.moveTo(x0, y0);
111
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
112
+ });
113
+ ctx.stroke();
114
+ }
115
+ }
116
+ // paint background
117
+ if (this.backgroundColor && typeof this.backgroundColor === 'string') {
118
+ ctx.fillStyle = this.backgroundColor;
119
+ if (!this.borderRadius) {
120
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
121
+ }
122
+ else { // fill with rounded corners
123
+ ctx.beginPath();
124
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
125
+ [
126
+ [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
127
+ [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
128
+ [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
129
+ [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
130
+ ].forEach(([x0, x1, x2, y0, y1, y2]) => {
131
+ ctx.quadraticCurveTo(x0, y0, x1, y1);
132
+ ctx.lineTo(x2, y2);
133
+ });
134
+ ctx.closePath();
135
+ ctx.fill();
136
+ }
137
+ }
138
+ ctx.translate(...relBorder);
139
+ ctx.translate(...relPadding);
140
+ // paint text
141
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
142
+ ctx.fillStyle = this.color;
143
+ // 精确垂直居中:用 measureText 的 actualBoundingBox 实测字形上下沿,
144
+ // 不再依赖 textBaseline='middle'(middle 是 em 盒中线,与真实字形视觉中线常偏差,
145
+ // 导致整行文字偏上/偏下、看着不居中)。改为 alphabetic 基线 + 实测字形高度,
146
+ // 把每行字形放进其 fontSize 高的槽中心。
147
+ ctx.textAlign = 'center';
148
+ ctx.textBaseline = 'alphabetic';
149
+ const drawTextStroke = this.strokeWidth > 0;
150
+ if (drawTextStroke) {
151
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
152
+ ctx.strokeStyle = this.strokeColor;
153
+ }
154
+ const slotH = this.fontSize; // 每行槽高
155
+ lines.forEach((line, index) => {
156
+ const metrics = ctx.measureText(line);
157
+ const ascent = metrics.actualBoundingBoxAscent; // 字形顶部到基线
158
+ const descent = metrics.actualBoundingBoxDescent; // 基线到字形底部
159
+ const glyphH = (ascent || 0) + (descent || 0);
160
+ const slotTop = index * slotH; // 本行槽顶(内容框坐标系,已平移)
161
+ // 让字形在槽中垂直居中:先平移到槽中线,再上移 ascent 回到基线
162
+ const baselineY = slotTop + (slotH - glyphH) / 2 + (ascent || slotH / 2);
163
+ const lineX = innerWidth / 2;
164
+ drawTextStroke && ctx.strokeText(line, lineX, baselineY);
165
+ ctx.fillText(line, lineX, baselineY);
166
+ });
167
+ // Inject canvas into sprite
168
+ if (this.material.map)
169
+ this.material.map.dispose(); // gc previous texture
170
+ const texture = this.material.map = new Texture(canvas);
171
+ texture.minFilter = LinearFilter;
172
+ texture.needsUpdate = true;
173
+ const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
174
+ this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
175
+ }
176
+ }
package/SpriteText2.d.ts CHANGED
@@ -1,48 +1,48 @@
1
- import { Sprite, Raycaster, Intersection } from 'three';
2
- import type { ColorString } from "./types";
3
- export default class SpriteText2 extends Sprite {
4
- constructor(text?: string, textHeight?: number, color?: ColorString);
5
- _text: string;
6
- _textHeight: number;
7
- _color: ColorString;
8
- _backgroundColor: ColorString | false;
9
- _padding: number;
10
- _borderWidth: number;
11
- _borderRadius: number;
12
- _borderColor: string;
13
- _strokeWidth: number;
14
- _strokeColor: string;
15
- _fontFace: string;
16
- _fontSize: number;
17
- _fontWeight: string;
18
- _canvas: HTMLCanvasElement;
19
- get text(): string;
20
- set text(text: string);
21
- get textHeight(): number;
22
- set textHeight(textHeight: number);
23
- get color(): ColorString;
24
- set color(color: ColorString);
25
- get backgroundColor(): ColorString | false;
26
- set backgroundColor(color: ColorString | false);
27
- get padding(): number;
28
- set padding(padding: number);
29
- get borderWidth(): number;
30
- set borderWidth(borderWidth: number);
31
- get borderRadius(): number;
32
- set borderRadius(borderRadius: number);
33
- get borderColor(): string;
34
- set borderColor(borderColor: string);
35
- get fontFace(): string;
36
- set fontFace(fontFace: string);
37
- get fontSize(): number;
38
- set fontSize(fontSize: number);
39
- get fontWeight(): string;
40
- set fontWeight(fontWeight: string);
41
- get strokeWidth(): number;
42
- set strokeWidth(strokeWidth: number);
43
- get strokeColor(): string;
44
- set strokeColor(strokeColor: string);
45
- _genCanvas(): void;
46
- raycast(raycaster: Raycaster, intersects: Intersection[]): void;
47
- copy(source: SpriteText2): this;
48
- }
1
+ import { Sprite, Raycaster, Intersection } from 'three';
2
+ import type { ColorString } from "./types";
3
+ export default class SpriteText2 extends Sprite {
4
+ constructor(text?: string, textHeight?: number, color?: ColorString);
5
+ _text: string;
6
+ _textHeight: number;
7
+ _color: ColorString;
8
+ _backgroundColor: ColorString | false;
9
+ _padding: number;
10
+ _borderWidth: number;
11
+ _borderRadius: number;
12
+ _borderColor: string;
13
+ _strokeWidth: number;
14
+ _strokeColor: string;
15
+ _fontFace: string;
16
+ _fontSize: number;
17
+ _fontWeight: string;
18
+ _canvas: HTMLCanvasElement;
19
+ get text(): string;
20
+ set text(text: string);
21
+ get textHeight(): number;
22
+ set textHeight(textHeight: number);
23
+ get color(): ColorString;
24
+ set color(color: ColorString);
25
+ get backgroundColor(): ColorString | false;
26
+ set backgroundColor(color: ColorString | false);
27
+ get padding(): number;
28
+ set padding(padding: number);
29
+ get borderWidth(): number;
30
+ set borderWidth(borderWidth: number);
31
+ get borderRadius(): number;
32
+ set borderRadius(borderRadius: number);
33
+ get borderColor(): string;
34
+ set borderColor(borderColor: string);
35
+ get fontFace(): string;
36
+ set fontFace(fontFace: string);
37
+ get fontSize(): number;
38
+ set fontSize(fontSize: number);
39
+ get fontWeight(): string;
40
+ set fontWeight(fontWeight: string);
41
+ get strokeWidth(): number;
42
+ set strokeWidth(strokeWidth: number);
43
+ get strokeColor(): string;
44
+ set strokeColor(strokeColor: string);
45
+ _genCanvas(): void;
46
+ raycast(raycaster: Raycaster, intersects: Intersection[]): void;
47
+ copy(source: SpriteText2): this;
48
+ }