three-base 2.0.66 → 2.0.67

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 (82) 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/EventMesh.d.ts +18 -18
  6. package/EventMesh.js +59 -59
  7. package/ExtrudeMesh.d.ts +111 -111
  8. package/ExtrudeMesh.js +363 -363
  9. package/FPScontrol.d.ts +45 -45
  10. package/FPScontrol.js +171 -171
  11. package/Fire/Fire.d.ts +28 -28
  12. package/Fire/Fire.js +79 -79
  13. package/Fire/FireGromtry.d.ts +9 -9
  14. package/Fire/FireGromtry.js +40 -40
  15. package/Fire/FireMaterial.d.ts +28 -28
  16. package/Fire/FireMaterial.js +32 -32
  17. package/InstancedMeshBase.d.ts +24 -24
  18. package/InstancedMeshBase.js +190 -190
  19. package/KeyFrameAnimation.d.ts +25 -25
  20. package/KeyFrameAnimation.js +78 -78
  21. package/MeshBase.d.ts +32 -32
  22. package/MeshBase.js +172 -172
  23. package/Mirror.d.ts +25 -25
  24. package/Mirror.js +139 -139
  25. package/PlaneModal1/PlaneModal1.d.ts +31 -31
  26. package/PlaneModal1/PlaneModal1.js +105 -105
  27. package/PlaneModal1/index.d.ts +1 -1
  28. package/PlaneModal1/index.js +1 -1
  29. package/PlaneText.d.ts +44 -44
  30. package/PlaneText.js +30 -30
  31. package/PlaneWithHtml.d.ts +11 -11
  32. package/PlaneWithHtml.js +68 -68
  33. package/PointLight/PointLight.d.ts +13 -13
  34. package/PointLight/PointLight.js +64 -64
  35. package/PointLight/index.d.ts +1 -1
  36. package/PointLight/index.js +1 -1
  37. package/Sky/Sky.d.ts +13 -13
  38. package/Sky/Sky.js +28 -28
  39. package/Sky/index.d.ts +1 -1
  40. package/Sky/index.js +1 -1
  41. package/SpriteModal1/SpriteModal1.d.ts +30 -30
  42. package/SpriteModal1/SpriteModal1.js +110 -110
  43. package/SpriteModal1/index.d.ts +1 -1
  44. package/SpriteModal1/index.js +1 -1
  45. package/SpriteText.d.ts +45 -45
  46. package/SpriteText.js +163 -163
  47. package/SpriteText2.d.ts +47 -47
  48. package/SpriteText2.js +198 -198
  49. package/SpriteWithHtml.d.ts +7 -7
  50. package/SpriteWithHtml.js +49 -49
  51. package/Text3D/Text3D.d.ts +20 -20
  52. package/Text3D/Text3D.js +62 -62
  53. package/Text3D/fontController.d.ts +59 -59
  54. package/Text3D/fontController.js +200 -200
  55. package/Text3D/index.d.ts +1 -1
  56. package/Text3D/index.js +1 -1
  57. package/ThreeBase.d.ts +91 -91
  58. package/ThreeBase.js +611 -611
  59. package/ThreeMouse.d.ts +39 -39
  60. package/ThreeMouse.js +359 -359
  61. package/Transition.d.ts +40 -40
  62. package/Transition.js +207 -207
  63. package/index.d.ts +1 -1
  64. package/index.js +2 -2
  65. package/keyContorls.d.ts +25 -25
  66. package/keyContorls.js +98 -98
  67. package/package.json +1 -1
  68. package/pass/FullScreenQuad.d.ts +7 -7
  69. package/pass/FullScreenQuad.js +25 -25
  70. package/pass/glow_pass.d.ts +2 -2
  71. package/pass/glow_pass.js +236 -236
  72. package/pass/stroke_pass.d.ts +2 -2
  73. package/pass/stroke_pass.js +202 -202
  74. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  75. package/shaders/FXAAShaderMaterial.js +21 -21
  76. package/shaders.d.ts +17 -17
  77. package/shaders.js +99 -99
  78. package/threeLoader.d.ts +26 -26
  79. package/threeLoader.js +189 -174
  80. package/threeLoader.ts +17 -1
  81. package/threeUtils.d.ts +47 -47
  82. package/threeUtils.js +266 -266
package/SpriteText2.d.ts CHANGED
@@ -1,47 +1,47 @@
1
- import { Intersection, Raycaster, Sprite } from 'three';
2
- export default class SpriteText2 extends Sprite {
3
- constructor(text?: string, textHeight?: number, color?: `rgb(${string})` | `rgba(${string})` | `#${string}`);
4
- _text: string;
5
- _textHeight: number;
6
- _color: `rgb(${string})` | `rgba(${string})` | `#${string}`;
7
- _backgroundColor: `rgb(${string})` | `rgba(${string})` | `#${string}` | false;
8
- _padding: number;
9
- _borderWidth: number;
10
- _borderRadius: number;
11
- _borderColor: string;
12
- _strokeWidth: number;
13
- _strokeColor: string;
14
- _fontFace: string;
15
- _fontSize: number;
16
- _fontWeight: string;
17
- _canvas: HTMLCanvasElement;
18
- get text(): string;
19
- set text(text: string);
20
- get textHeight(): number;
21
- set textHeight(textHeight: number);
22
- get color(): `#${string}` | `rgba(${string})` | `rgb(${string})`;
23
- set color(color: `#${string}` | `rgba(${string})` | `rgb(${string})`);
24
- get backgroundColor(): false | `#${string}` | `rgba(${string})` | `rgb(${string})`;
25
- set backgroundColor(color: false | `#${string}` | `rgba(${string})` | `rgb(${string})`);
26
- get padding(): number;
27
- set padding(padding: number);
28
- get borderWidth(): number;
29
- set borderWidth(borderWidth: number);
30
- get borderRadius(): number;
31
- set borderRadius(borderRadius: number);
32
- get borderColor(): string;
33
- set borderColor(borderColor: string);
34
- get fontFace(): string;
35
- set fontFace(fontFace: string);
36
- get fontSize(): number;
37
- set fontSize(fontSize: number);
38
- get fontWeight(): string;
39
- set fontWeight(fontWeight: string);
40
- get strokeWidth(): number;
41
- set strokeWidth(strokeWidth: number);
42
- get strokeColor(): string;
43
- set strokeColor(strokeColor: string);
44
- _genCanvas(): void;
45
- raycast(raycaster: Raycaster, intersects: Intersection[]): void;
46
- copy(source: SpriteText2): this;
47
- }
1
+ import { Intersection, Raycaster, Sprite } from 'three';
2
+ export default class SpriteText2 extends Sprite {
3
+ constructor(text?: string, textHeight?: number, color?: `rgb(${string})` | `rgba(${string})` | `#${string}`);
4
+ _text: string;
5
+ _textHeight: number;
6
+ _color: `rgb(${string})` | `rgba(${string})` | `#${string}`;
7
+ _backgroundColor: `rgb(${string})` | `rgba(${string})` | `#${string}` | false;
8
+ _padding: number;
9
+ _borderWidth: number;
10
+ _borderRadius: number;
11
+ _borderColor: string;
12
+ _strokeWidth: number;
13
+ _strokeColor: string;
14
+ _fontFace: string;
15
+ _fontSize: number;
16
+ _fontWeight: string;
17
+ _canvas: HTMLCanvasElement;
18
+ get text(): string;
19
+ set text(text: string);
20
+ get textHeight(): number;
21
+ set textHeight(textHeight: number);
22
+ get color(): `#${string}` | `rgba(${string})` | `rgb(${string})`;
23
+ set color(color: `#${string}` | `rgba(${string})` | `rgb(${string})`);
24
+ get backgroundColor(): false | `#${string}` | `rgba(${string})` | `rgb(${string})`;
25
+ set backgroundColor(color: false | `#${string}` | `rgba(${string})` | `rgb(${string})`);
26
+ get padding(): number;
27
+ set padding(padding: number);
28
+ get borderWidth(): number;
29
+ set borderWidth(borderWidth: number);
30
+ get borderRadius(): number;
31
+ set borderRadius(borderRadius: number);
32
+ get borderColor(): string;
33
+ set borderColor(borderColor: string);
34
+ get fontFace(): string;
35
+ set fontFace(fontFace: string);
36
+ get fontSize(): number;
37
+ set fontSize(fontSize: number);
38
+ get fontWeight(): string;
39
+ set fontWeight(fontWeight: string);
40
+ get strokeWidth(): number;
41
+ set strokeWidth(strokeWidth: number);
42
+ get strokeColor(): string;
43
+ set strokeColor(strokeColor: string);
44
+ _genCanvas(): void;
45
+ raycast(raycaster: Raycaster, intersects: Intersection[]): void;
46
+ copy(source: SpriteText2): this;
47
+ }
package/SpriteText2.js CHANGED
@@ -1,198 +1,198 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-04-24 15:31:02
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-24 16:18:47
6
- */
7
- import { LinearFilter, Sprite, Texture } from 'three';
8
- // 带箭头的精灵文字
9
- export default class SpriteText2 extends Sprite {
10
- constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
11
- super();
12
- this.material.alphaTest = 0.1;
13
- this._text = `${text}`;
14
- this._textHeight = textHeight;
15
- this._color = color;
16
- this._backgroundColor = false; // no background color
17
- this._padding = 0;
18
- this._borderWidth = 0;
19
- this._borderRadius = 0;
20
- this._borderColor = 'white';
21
- this._strokeWidth = 0;
22
- this._strokeColor = 'white';
23
- this._fontFace = 'Arial';
24
- this._fontSize = 90; // defines text resolution
25
- this._fontWeight = 'normal';
26
- this._genCanvas();
27
- }
28
- _text = "";
29
- _textHeight = 0;
30
- _color = "#ffffff";
31
- _backgroundColor = false; // no background color
32
- _padding = 0;
33
- _borderWidth = 0;
34
- _borderRadius = 0;
35
- _borderColor = 'white';
36
- _strokeWidth = 0;
37
- _strokeColor = 'white';
38
- _fontFace = 'Arial';
39
- _fontSize = 90; // defines text resolution
40
- _fontWeight = 'normal';
41
- _canvas = document.createElement('canvas');
42
- get text() { return this._text; }
43
- set text(text) { this._text = text; this._genCanvas(); }
44
- get textHeight() { return this._textHeight; }
45
- set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
46
- get color() { return this._color; }
47
- set color(color) { this._color = color; this._genCanvas(); }
48
- get backgroundColor() { return this._backgroundColor; }
49
- set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
50
- get padding() { return this._padding; }
51
- set padding(padding) { this._padding = padding; this._genCanvas(); }
52
- get borderWidth() { return this._borderWidth; }
53
- set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
54
- get borderRadius() { return this._borderRadius; }
55
- set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
56
- get borderColor() { return this._borderColor; }
57
- set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
58
- get fontFace() { return this._fontFace; }
59
- set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
60
- get fontSize() { return this._fontSize; }
61
- set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
62
- get fontWeight() { return this._fontWeight; }
63
- set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
64
- get strokeWidth() { return this._strokeWidth; }
65
- set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
66
- get strokeColor() { return this._strokeColor; }
67
- set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
68
- _genCanvas() {
69
- const canvas = this._canvas;
70
- const ctx = canvas.getContext('2d');
71
- const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
72
- const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
73
- const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
74
- const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
75
- const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
76
- const relPadding = padding.map(p => p * this.fontSize * 0.1); // padding in canvas units
77
- const lines = this.text.split('\n');
78
- const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
79
- ctx.font = font; // measure canvas with appropriate font
80
- const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
81
- const innerHeight = this.fontSize * lines.length;
82
- canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
83
- canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
84
- // paint border
85
- if (this.borderWidth) {
86
- ctx.strokeStyle = this.borderColor;
87
- if (relBorder[0]) { // left + right borders
88
- const hb = relBorder[0] / 2;
89
- ctx.lineWidth = relBorder[0];
90
- ctx.beginPath();
91
- ctx.moveTo(hb, relBorderRadius[0]);
92
- ctx.lineTo(hb, ((canvas.height - 40) / 2) - relBorderRadius[3]);
93
- ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
94
- ctx.lineTo(canvas.width - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]);
95
- ctx.stroke();
96
- }
97
- if (relBorder[1]) { // top + bottom borders
98
- const hb = relBorder[1] / 2;
99
- ctx.lineWidth = relBorder[1];
100
- ctx.beginPath();
101
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
102
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
103
- ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), ((canvas.height - 40) / 2) - hb);
104
- ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - hb);
105
- ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - hb);
106
- ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - hb);
107
- ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), ((canvas.height - 40) / 2) - hb);
108
- ctx.stroke();
109
- }
110
- if (this.borderRadius) { // strike rounded corners
111
- const cornerWidth = Math.max(...relBorder);
112
- const hb = cornerWidth / 2;
113
- ctx.lineWidth = cornerWidth;
114
- ctx.beginPath();
115
- [
116
- !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
117
- !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
118
- !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
119
- !!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
120
- ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
121
- ctx.moveTo(x0, y0);
122
- ctx.quadraticCurveTo(x1, y0, x1, y1);
123
- });
124
- ctx.stroke();
125
- }
126
- }
127
- // paint background
128
- if (this.backgroundColor) {
129
- ctx.fillStyle = this.backgroundColor;
130
- if (!this.borderRadius) {
131
- ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, ((canvas.height - 40) / 2) - relBorder[1] * 2);
132
- }
133
- else { // fill with rounded corners
134
- ctx.beginPath();
135
- ctx.moveTo(relBorder[0], relBorderRadius[0]);
136
- ctx.quadraticCurveTo(relBorder[0], relBorder[1], relBorderRadius[0], relBorder[1]);
137
- ctx.lineTo(canvas.width - relBorderRadius[1], relBorder[1]);
138
- ctx.quadraticCurveTo(canvas.width - relBorder[0], relBorder[1], canvas.width - relBorder[0], relBorderRadius[1]);
139
- ctx.lineTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[2]);
140
- ctx.quadraticCurveTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], canvas.width - relBorderRadius[2], ((canvas.height - 40) / 2) - relBorder[1]);
141
- ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - relBorder[1]);
142
- ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - relBorder[1]);
143
- ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - relBorder[1]);
144
- ctx.lineTo(relBorderRadius[3], ((canvas.height - 40) / 2) - relBorder[1]);
145
- ctx.quadraticCurveTo(relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[3]);
146
- ctx.closePath();
147
- ctx.fill();
148
- }
149
- }
150
- ctx.translate(relBorder[0], relBorder[1]);
151
- ctx.translate(relPadding[0], relPadding[0]);
152
- // paint text
153
- ctx.font = font; // Set font again after canvas is resized, as context properties are reset
154
- ctx.fillStyle = this.color;
155
- ctx.textBaseline = 'bottom';
156
- const drawTextStroke = this.strokeWidth > 0;
157
- if (drawTextStroke) {
158
- ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
159
- ctx.strokeStyle = this.strokeColor;
160
- }
161
- lines.forEach((line, index) => {
162
- const lineX = (innerWidth - ctx.measureText(line).width) / 2;
163
- const lineY = (index + 1) * this.fontSize;
164
- drawTextStroke && ctx.strokeText(line, lineX, lineY);
165
- ctx.fillText(line, lineX, lineY);
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
- //鼠标拾取计算
177
- raycast(raycaster, intersects) {
178
- let intersect = [];
179
- super.raycast(raycaster, intersect);
180
- let inte = intersect[0];
181
- if (inte && (inte.uv.y > 0.55)) {
182
- intersects.push(inte);
183
- }
184
- }
185
- copy(source) {
186
- this.color = source.color;
187
- this.backgroundColor = source.backgroundColor;
188
- this.padding = source.padding;
189
- this.borderWidth = source.borderWidth;
190
- this.borderColor = source.borderColor;
191
- this.fontFace = source.fontFace;
192
- this.fontSize = source.fontSize;
193
- this.fontWeight = source.fontWeight;
194
- this.strokeWidth = source.strokeWidth;
195
- this.strokeColor = source.strokeColor;
196
- return this;
197
- }
198
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-04-24 15:31:02
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-04-24 16:18:47
6
+ */
7
+ import { LinearFilter, Sprite, Texture } from 'three';
8
+ // 带箭头的精灵文字
9
+ export default class SpriteText2 extends Sprite {
10
+ constructor(text = '', textHeight = 10, color = 'rgba(255, 255, 255, 1)') {
11
+ super();
12
+ this.material.alphaTest = 0.1;
13
+ this._text = `${text}`;
14
+ this._textHeight = textHeight;
15
+ this._color = color;
16
+ this._backgroundColor = false; // no background color
17
+ this._padding = 0;
18
+ this._borderWidth = 0;
19
+ this._borderRadius = 0;
20
+ this._borderColor = 'white';
21
+ this._strokeWidth = 0;
22
+ this._strokeColor = 'white';
23
+ this._fontFace = 'Arial';
24
+ this._fontSize = 90; // defines text resolution
25
+ this._fontWeight = 'normal';
26
+ this._genCanvas();
27
+ }
28
+ _text = "";
29
+ _textHeight = 0;
30
+ _color = "#ffffff";
31
+ _backgroundColor = false; // no background color
32
+ _padding = 0;
33
+ _borderWidth = 0;
34
+ _borderRadius = 0;
35
+ _borderColor = 'white';
36
+ _strokeWidth = 0;
37
+ _strokeColor = 'white';
38
+ _fontFace = 'Arial';
39
+ _fontSize = 90; // defines text resolution
40
+ _fontWeight = 'normal';
41
+ _canvas = document.createElement('canvas');
42
+ get text() { return this._text; }
43
+ set text(text) { this._text = text; this._genCanvas(); }
44
+ get textHeight() { return this._textHeight; }
45
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
46
+ get color() { return this._color; }
47
+ set color(color) { this._color = color; this._genCanvas(); }
48
+ get backgroundColor() { return this._backgroundColor; }
49
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
50
+ get padding() { return this._padding; }
51
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
52
+ get borderWidth() { return this._borderWidth; }
53
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
54
+ get borderRadius() { return this._borderRadius; }
55
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
56
+ get borderColor() { return this._borderColor; }
57
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
58
+ get fontFace() { return this._fontFace; }
59
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
60
+ get fontSize() { return this._fontSize; }
61
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
62
+ get fontWeight() { return this._fontWeight; }
63
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
64
+ get strokeWidth() { return this._strokeWidth; }
65
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
66
+ get strokeColor() { return this._strokeColor; }
67
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
68
+ _genCanvas() {
69
+ const canvas = this._canvas;
70
+ const ctx = canvas.getContext('2d');
71
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
72
+ const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
73
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
74
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
75
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
76
+ const relPadding = padding.map(p => p * this.fontSize * 0.1); // padding in canvas units
77
+ const lines = this.text.split('\n');
78
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
79
+ ctx.font = font; // measure canvas with appropriate font
80
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
81
+ const innerHeight = this.fontSize * lines.length;
82
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
83
+ canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
84
+ // paint border
85
+ if (this.borderWidth) {
86
+ ctx.strokeStyle = this.borderColor;
87
+ if (relBorder[0]) { // left + right borders
88
+ const hb = relBorder[0] / 2;
89
+ ctx.lineWidth = relBorder[0];
90
+ ctx.beginPath();
91
+ ctx.moveTo(hb, relBorderRadius[0]);
92
+ ctx.lineTo(hb, ((canvas.height - 40) / 2) - relBorderRadius[3]);
93
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
94
+ ctx.lineTo(canvas.width - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]);
95
+ ctx.stroke();
96
+ }
97
+ if (relBorder[1]) { // top + bottom borders
98
+ const hb = relBorder[1] / 2;
99
+ ctx.lineWidth = relBorder[1];
100
+ ctx.beginPath();
101
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
102
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
103
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), ((canvas.height - 40) / 2) - hb);
104
+ ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - hb);
105
+ ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - hb);
106
+ ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - hb);
107
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), ((canvas.height - 40) / 2) - hb);
108
+ ctx.stroke();
109
+ }
110
+ if (this.borderRadius) { // strike rounded corners
111
+ const cornerWidth = Math.max(...relBorder);
112
+ const hb = cornerWidth / 2;
113
+ ctx.lineWidth = cornerWidth;
114
+ ctx.beginPath();
115
+ [
116
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
117
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
118
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
119
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
120
+ ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
121
+ ctx.moveTo(x0, y0);
122
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
123
+ });
124
+ ctx.stroke();
125
+ }
126
+ }
127
+ // paint background
128
+ if (this.backgroundColor) {
129
+ ctx.fillStyle = this.backgroundColor;
130
+ if (!this.borderRadius) {
131
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, ((canvas.height - 40) / 2) - relBorder[1] * 2);
132
+ }
133
+ else { // fill with rounded corners
134
+ ctx.beginPath();
135
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
136
+ ctx.quadraticCurveTo(relBorder[0], relBorder[1], relBorderRadius[0], relBorder[1]);
137
+ ctx.lineTo(canvas.width - relBorderRadius[1], relBorder[1]);
138
+ ctx.quadraticCurveTo(canvas.width - relBorder[0], relBorder[1], canvas.width - relBorder[0], relBorderRadius[1]);
139
+ ctx.lineTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[2]);
140
+ ctx.quadraticCurveTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], canvas.width - relBorderRadius[2], ((canvas.height - 40) / 2) - relBorder[1]);
141
+ ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - relBorder[1]);
142
+ ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - relBorder[1]);
143
+ ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - relBorder[1]);
144
+ ctx.lineTo(relBorderRadius[3], ((canvas.height - 40) / 2) - relBorder[1]);
145
+ ctx.quadraticCurveTo(relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[3]);
146
+ ctx.closePath();
147
+ ctx.fill();
148
+ }
149
+ }
150
+ ctx.translate(relBorder[0], relBorder[1]);
151
+ ctx.translate(relPadding[0], relPadding[0]);
152
+ // paint text
153
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
154
+ ctx.fillStyle = this.color;
155
+ ctx.textBaseline = 'bottom';
156
+ const drawTextStroke = this.strokeWidth > 0;
157
+ if (drawTextStroke) {
158
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
159
+ ctx.strokeStyle = this.strokeColor;
160
+ }
161
+ lines.forEach((line, index) => {
162
+ const lineX = (innerWidth - ctx.measureText(line).width) / 2;
163
+ const lineY = (index + 1) * this.fontSize;
164
+ drawTextStroke && ctx.strokeText(line, lineX, lineY);
165
+ ctx.fillText(line, lineX, lineY);
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
+ //鼠标拾取计算
177
+ raycast(raycaster, intersects) {
178
+ let intersect = [];
179
+ super.raycast(raycaster, intersect);
180
+ let inte = intersect[0];
181
+ if (inte && (inte.uv.y > 0.55)) {
182
+ intersects.push(inte);
183
+ }
184
+ }
185
+ copy(source) {
186
+ this.color = source.color;
187
+ this.backgroundColor = source.backgroundColor;
188
+ this.padding = source.padding;
189
+ this.borderWidth = source.borderWidth;
190
+ this.borderColor = source.borderColor;
191
+ this.fontFace = source.fontFace;
192
+ this.fontSize = source.fontSize;
193
+ this.fontWeight = source.fontWeight;
194
+ this.strokeWidth = source.strokeWidth;
195
+ this.strokeColor = source.strokeColor;
196
+ return this;
197
+ }
198
+ }
@@ -1,7 +1,7 @@
1
- import { Sprite } from "three";
2
- export default class SpriteWithHtml extends Sprite {
3
- constructor(html?: string, backgroundColor?: `#${string}`);
4
- visible: boolean;
5
- div: HTMLDivElement;
6
- setHtml(html?: string, backgroundColor?: `#${string}`): Promise<void>;
7
- }
1
+ import { Sprite } from "three";
2
+ export default class SpriteWithHtml extends Sprite {
3
+ constructor(html?: string, backgroundColor?: `#${string}`);
4
+ visible: boolean;
5
+ div: HTMLDivElement;
6
+ setHtml(html?: string, backgroundColor?: `#${string}`): Promise<void>;
7
+ }
package/SpriteWithHtml.js CHANGED
@@ -1,49 +1,49 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2023-04-15 11:26:32
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-15 12:38:09
6
- */
7
- import html2canvas from "html2canvas";
8
- import { CanvasTexture, Sprite } from "three";
9
- import threeLoader from "./threeLoader";
10
- // html 文本面板
11
- export default class SpriteWithHtml extends Sprite {
12
- constructor(html = "", backgroundColor = "#00000000") {
13
- super();
14
- this.setHtml(html, backgroundColor);
15
- }
16
- visible = false;
17
- div = (() => {
18
- const div = document.createElement("div");
19
- div.style.position = "fixed";
20
- div.style.top = "100%";
21
- div.style.left = "100%";
22
- div.style.pointerEvents = "none";
23
- div.style.userSelect = "none";
24
- div.style.lineHeight = "1";
25
- div.style.whiteSpace = "nowrap";
26
- return div;
27
- })();
28
- async setHtml(html = "", backgroundColor = "#00000000") {
29
- this.div.innerHTML = html;
30
- document.body.appendChild(this.div);
31
- await new Promise((resolve, reject) => {
32
- html2canvas(this.div, {
33
- backgroundColor,
34
- removeContainer: true
35
- }).then(async (canvas) => {
36
- this.scale.set(canvas.width, canvas.height, 1);
37
- this.material.map = new CanvasTexture(canvas);
38
- this.material.map.anisotropy = threeLoader.anisotropy;
39
- this.material.alphaTest = 0.1;
40
- this.material.needsUpdate = true;
41
- await new Promise(resolve => requestAnimationFrame(resolve));
42
- this.visible = true;
43
- this.div.remove();
44
- resolve();
45
- });
46
- });
47
- this.dispatchEvent({ type: "loaded" });
48
- }
49
- }
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-04-15 11:26:32
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-04-15 12:38:09
6
+ */
7
+ import html2canvas from "html2canvas";
8
+ import { CanvasTexture, Sprite } from "three";
9
+ import threeLoader from "./threeLoader";
10
+ // html 文本面板
11
+ export default class SpriteWithHtml extends Sprite {
12
+ constructor(html = "", backgroundColor = "#00000000") {
13
+ super();
14
+ this.setHtml(html, backgroundColor);
15
+ }
16
+ visible = false;
17
+ div = (() => {
18
+ const div = document.createElement("div");
19
+ div.style.position = "fixed";
20
+ div.style.top = "100%";
21
+ div.style.left = "100%";
22
+ div.style.pointerEvents = "none";
23
+ div.style.userSelect = "none";
24
+ div.style.lineHeight = "1";
25
+ div.style.whiteSpace = "nowrap";
26
+ return div;
27
+ })();
28
+ async setHtml(html = "", backgroundColor = "#00000000") {
29
+ this.div.innerHTML = html;
30
+ document.body.appendChild(this.div);
31
+ await new Promise((resolve, reject) => {
32
+ html2canvas(this.div, {
33
+ backgroundColor,
34
+ removeContainer: true
35
+ }).then(async (canvas) => {
36
+ this.scale.set(canvas.width, canvas.height, 1);
37
+ this.material.map = new CanvasTexture(canvas);
38
+ this.material.map.anisotropy = threeLoader.anisotropy;
39
+ this.material.alphaTest = 0.1;
40
+ this.material.needsUpdate = true;
41
+ await new Promise(resolve => requestAnimationFrame(resolve));
42
+ this.visible = true;
43
+ this.div.remove();
44
+ resolve();
45
+ });
46
+ });
47
+ this.dispatchEvent({ type: "loaded" });
48
+ }
49
+ }
@@ -1,20 +1,20 @@
1
- import { Mesh, MeshStandardMaterial } from "three";
2
- export type Text3DProps = {
3
- text?: string;
4
- fontType?: string;
5
- size?: number | undefined;
6
- height?: number | undefined;
7
- curveSegments?: number | undefined;
8
- bevelEnabled?: boolean | undefined;
9
- bevelThickness?: number | undefined;
10
- bevelSize?: number | undefined;
11
- bevelOffset?: number | undefined;
12
- bevelSegments?: number | undefined;
13
- };
14
- export default class Text3D extends Mesh {
15
- constructor(text?: string);
16
- static path: string;
17
- material: MeshStandardMaterial;
18
- _config: Text3DProps;
19
- setText(text3DProps: Text3DProps): void;
20
- }
1
+ import { Mesh, MeshStandardMaterial } from "three";
2
+ export type Text3DProps = {
3
+ text?: string;
4
+ fontType?: string;
5
+ size?: number | undefined;
6
+ height?: number | undefined;
7
+ curveSegments?: number | undefined;
8
+ bevelEnabled?: boolean | undefined;
9
+ bevelThickness?: number | undefined;
10
+ bevelSize?: number | undefined;
11
+ bevelOffset?: number | undefined;
12
+ bevelSegments?: number | undefined;
13
+ };
14
+ export default class Text3D extends Mesh {
15
+ constructor(text?: string);
16
+ static path: string;
17
+ material: MeshStandardMaterial;
18
+ _config: Text3DProps;
19
+ setText(text3DProps: Text3DProps): void;
20
+ }