three-base 1.129.14 → 1.129.17

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 (46) hide show
  1. package/index.js +6 -2
  2. package/index.ts +9 -3
  3. package/lib/ExtrudeMesh.js +1 -0
  4. package/lib/ExtrudeMesh.ts +2 -0
  5. package/lib/PlaneText.d.ts +7 -7
  6. package/lib/PlaneText.js +2 -2
  7. package/lib/PlaneText.ts +2 -3
  8. package/lib/PlaneWithHtml.d.ts +11 -0
  9. package/lib/PlaneWithHtml.js +67 -0
  10. package/lib/PlaneWithHtml.ts +84 -0
  11. package/lib/{PointLight.d.ts → PointLight/PointLight.d.ts} +4 -4
  12. package/lib/PointLight/PointLight.js +63 -0
  13. package/lib/PointLight/PointLight.ts +74 -0
  14. package/lib/PointLight/pointLight_map.png +0 -0
  15. package/lib/SpriteText.d.ts +45 -0
  16. package/lib/SpriteText.js +162 -0
  17. package/lib/SpriteText.ts +189 -0
  18. package/lib/SpriteText2.d.ts +47 -0
  19. package/lib/SpriteText2.js +197 -0
  20. package/lib/SpriteText2.ts +236 -0
  21. package/lib/Text3D/Text3D.js +2 -1
  22. package/lib/Text3D/Text3D.ts +2 -1
  23. package/lib/Text3D/fontController.ts +1 -1
  24. package/lib/ThreeBase.d.ts +3 -3
  25. package/lib/ThreeBase.js +14 -7
  26. package/lib/ThreeBase.ts +20 -9
  27. package/lib/Transition.d.ts +1 -3
  28. package/lib/Transition.js +8 -5
  29. package/lib/Transition.ts +10 -6
  30. package/lib/planeModal1/PlaneModal1.d.ts +0 -1
  31. package/lib/planeModal1/PlaneModal1.js +1 -1
  32. package/lib/planeModal1/PlaneModal1.ts +1 -2
  33. package/lib/spriteModal1/SpriteModal1.d.ts +0 -1
  34. package/lib/spriteModal1/SpriteModal1.js +1 -1
  35. package/lib/spriteModal1/SpriteModal1.ts +1 -2
  36. package/lib/stroke_pass/FullScreenQuad.d.ts +7 -0
  37. package/lib/stroke_pass/FullScreenQuad.js +25 -0
  38. package/lib/stroke_pass/FullScreenQuad.ts +33 -0
  39. package/lib/stroke_pass/stroke_pass.d.ts +2 -0
  40. package/lib/stroke_pass/stroke_pass.js +190 -0
  41. package/lib/stroke_pass/stroke_pass.ts +211 -0
  42. package/lib/threeUtils.js +5 -5
  43. package/lib/threeUtils.ts +6 -5
  44. package/package.json +5 -8
  45. package/lib/PointLight.js +0 -64
  46. package/lib/PointLight.ts +0 -78
@@ -0,0 +1,162 @@
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._text = `${text}`;
12
+ this._textHeight = textHeight;
13
+ this._color = color;
14
+ this._genCanvas();
15
+ }
16
+ _text = "";
17
+ _textHeight = 10;
18
+ _color = "#fffff";
19
+ _backgroundColor = false; // no background color
20
+ _padding = 0;
21
+ _borderWidth = 0;
22
+ _borderRadius = 0;
23
+ _borderColor = "#fffff";
24
+ _strokeWidth = 0;
25
+ _strokeColor = "#fffff";
26
+ _fontFace = 'Arial';
27
+ _fontSize = 90; // defines text resolution
28
+ _fontWeight = 'normal';
29
+ _canvas = document.createElement('canvas');
30
+ get text() { return this._text; }
31
+ set text(text) { this._text = text; this._genCanvas(); }
32
+ get textHeight() { return this._textHeight; }
33
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
34
+ get color() { return this._color; }
35
+ set color(color) { this._color = color; this._genCanvas(); }
36
+ get backgroundColor() { return this._backgroundColor; }
37
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
38
+ get padding() { return this._padding; }
39
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
40
+ get borderWidth() { return this._borderWidth; }
41
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
42
+ get borderRadius() { return this._borderRadius; }
43
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
44
+ get borderColor() { return this._borderColor; }
45
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
46
+ get fontFace() { return this._fontFace; }
47
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
48
+ get fontSize() { return this._fontSize; }
49
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
50
+ get fontWeight() { return this._fontWeight; }
51
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
52
+ get strokeWidth() { return this._strokeWidth; }
53
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
54
+ get strokeColor() { return this._strokeColor; }
55
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
56
+ _genCanvas() {
57
+ const canvas = this._canvas;
58
+ const ctx = canvas.getContext('2d');
59
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
60
+ const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
61
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
62
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
63
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
64
+ const relPadding = padding.map(p => p * this.fontSize * 0.1);
65
+ ; // padding in canvas units
66
+ const lines = this.text.split('\n');
67
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
68
+ ctx.font = font; // measure canvas with appropriate font
69
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
70
+ const innerHeight = this.fontSize * lines.length;
71
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
72
+ canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
73
+ // paint border
74
+ if (this.borderWidth) {
75
+ ctx.strokeStyle = this.borderColor;
76
+ if (relBorder[0]) { // left + right borders
77
+ const hb = relBorder[0] / 2;
78
+ ctx.lineWidth = relBorder[0];
79
+ ctx.beginPath();
80
+ ctx.moveTo(hb, relBorderRadius[0]);
81
+ ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
82
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
83
+ ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
84
+ ctx.stroke();
85
+ }
86
+ if (relBorder[1]) { // top + bottom borders
87
+ const hb = relBorder[1] / 2;
88
+ ctx.lineWidth = relBorder[1];
89
+ ctx.beginPath();
90
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
91
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
92
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
93
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
94
+ ctx.stroke();
95
+ }
96
+ if (this.borderRadius) { // strike rounded corners
97
+ const cornerWidth = Math.max(...relBorder);
98
+ const hb = cornerWidth / 2;
99
+ ctx.lineWidth = cornerWidth;
100
+ ctx.beginPath();
101
+ [
102
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
103
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
104
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
105
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
106
+ ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
107
+ ctx.moveTo(x0, y0);
108
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
109
+ });
110
+ ctx.stroke();
111
+ }
112
+ }
113
+ // paint background
114
+ if (this.backgroundColor) {
115
+ //@ts-ignore
116
+ ctx.fillStyle = this.backgroundColor;
117
+ if (!this.borderRadius) {
118
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
119
+ }
120
+ else { // fill with rounded corners
121
+ ctx.beginPath();
122
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
123
+ [
124
+ [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]],
125
+ [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]],
126
+ [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]],
127
+ [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
128
+ ].forEach(([x0, x1, x2, y0, y1, y2]) => {
129
+ ctx.quadraticCurveTo(x0, y0, x1, y1);
130
+ ctx.lineTo(x2, y2);
131
+ });
132
+ ctx.closePath();
133
+ ctx.fill();
134
+ }
135
+ }
136
+ ctx.translate(...relBorder);
137
+ ctx.translate(...relPadding);
138
+ // paint text
139
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
140
+ ctx.fillStyle = this.color;
141
+ ctx.textBaseline = 'bottom';
142
+ const drawTextStroke = this.strokeWidth > 0;
143
+ if (drawTextStroke) {
144
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
145
+ ctx.strokeStyle = this.strokeColor;
146
+ }
147
+ lines.forEach((line, index) => {
148
+ const lineX = (innerWidth - ctx.measureText(line).width) / 2;
149
+ const lineY = (index + 1) * this.fontSize;
150
+ drawTextStroke && ctx.strokeText(line, lineX, lineY);
151
+ ctx.fillText(line, lineX, lineY);
152
+ });
153
+ // Inject canvas into sprite
154
+ if (this.material.map)
155
+ this.material.map.dispose(); // gc previous texture
156
+ const texture = this.material.map = new Texture(canvas);
157
+ texture.minFilter = LinearFilter;
158
+ texture.needsUpdate = true;
159
+ const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
160
+ this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
161
+ }
162
+ }
@@ -0,0 +1,189 @@
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
+
8
+ import { Sprite, SpriteMaterial, Texture, LinearFilter } from "three";
9
+
10
+ export default class SpriteText extends Sprite {
11
+
12
+ constructor(text = '', textHeight = 10, color: `#${string}` | `rgba(${string})` = 'rgba(255, 255, 255, 1)') {
13
+ super(new SpriteMaterial());
14
+
15
+ this._text = `${text}`;
16
+ this._textHeight = textHeight;
17
+ this._color = color;
18
+
19
+ this._genCanvas();
20
+ }
21
+
22
+ _text: string = "";
23
+ _textHeight: number = 10;
24
+ _color: `#${string}` | `rgba(${string})` = "#fffff";
25
+ _backgroundColor = false; // no background color
26
+
27
+ _padding: number = 0;
28
+ _borderWidth: number = 0;
29
+ _borderRadius: number = 0;
30
+ _borderColor: `#${string}` | `rgba(${string})` = "#fffff";
31
+
32
+ _strokeWidth: number = 0;
33
+ _strokeColor: `#${string}` | `rgba(${string})` = "#fffff";
34
+
35
+ _fontFace: string = 'Arial';
36
+ _fontSize: number = 90; // defines text resolution
37
+ _fontWeight: string = 'normal';
38
+
39
+ _canvas = document.createElement('canvas');
40
+
41
+ get text() { return this._text; }
42
+ set text(text) { this._text = text; this._genCanvas(); }
43
+ get textHeight() { return this._textHeight; }
44
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
45
+ get color() { return this._color; }
46
+ set color(color) { this._color = color; this._genCanvas(); }
47
+ get backgroundColor() { return this._backgroundColor; }
48
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
49
+ get padding() { return this._padding; }
50
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
51
+ get borderWidth() { return this._borderWidth; }
52
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
53
+ get borderRadius() { return this._borderRadius; }
54
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
55
+ get borderColor() { return this._borderColor; }
56
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
57
+ get fontFace() { return this._fontFace; }
58
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
59
+ get fontSize() { return this._fontSize; }
60
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
61
+ get fontWeight() { return this._fontWeight; }
62
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
63
+ get strokeWidth() { return this._strokeWidth; }
64
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
65
+ get strokeColor() { return this._strokeColor; }
66
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
67
+
68
+ _genCanvas() {
69
+ const canvas = this._canvas;
70
+ const ctx = canvas.getContext('2d')!;
71
+
72
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
73
+ const relBorder = border.map(b => b * this.fontSize * 0.1) as [number, number]; // border in canvas units
74
+
75
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
76
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
77
+
78
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
79
+ const relPadding = padding.map(p => p * this.fontSize * 0.1) as [number, number];; // padding in canvas units
80
+
81
+ const lines = this.text.split('\n');
82
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
83
+
84
+ ctx.font = font; // measure canvas with appropriate font
85
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
86
+ const innerHeight = this.fontSize * lines.length;
87
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
88
+ canvas.height = innerHeight + relBorder[1] * 2 + relPadding[1] * 2;
89
+
90
+ // paint border
91
+ if (this.borderWidth) {
92
+ ctx.strokeStyle = this.borderColor;
93
+
94
+ if (relBorder[0]) { // left + right borders
95
+ const hb = relBorder[0] / 2;
96
+ ctx.lineWidth = relBorder[0];
97
+ ctx.beginPath();
98
+ ctx.moveTo(hb, relBorderRadius[0]);
99
+ ctx.lineTo(hb, canvas.height - relBorderRadius[3]);
100
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
101
+ ctx.lineTo(canvas.width - hb, canvas.height - relBorderRadius[2]);
102
+ ctx.stroke();
103
+ }
104
+
105
+ if (relBorder[1]) { // top + bottom borders
106
+ const hb = relBorder[1] / 2;
107
+ ctx.lineWidth = relBorder[1];
108
+ ctx.beginPath();
109
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
110
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
111
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), canvas.height - hb);
112
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), canvas.height - hb);
113
+ ctx.stroke();
114
+ }
115
+
116
+ if (this.borderRadius) { // strike rounded corners
117
+ const cornerWidth = Math.max(...relBorder);
118
+ const hb = cornerWidth / 2;
119
+ ctx.lineWidth = cornerWidth;
120
+ ctx.beginPath();
121
+ ([
122
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
123
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
124
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, canvas.height - hb, canvas.height - relBorderRadius[2]],
125
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, canvas.height - hb, canvas.height - relBorderRadius[3]]
126
+ ].filter(d => d) as [number, number, number, number][]).forEach(([x0, x1, y0, y1]) => {
127
+ ctx.moveTo(x0, y0);
128
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
129
+ });
130
+ ctx.stroke();
131
+ }
132
+ }
133
+
134
+ // paint background
135
+ if (this.backgroundColor) {
136
+ //@ts-ignore
137
+ ctx.fillStyle = this.backgroundColor;
138
+ if (!this.borderRadius) {
139
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, canvas.height - relBorder[1] * 2);
140
+ } else { // fill with rounded corners
141
+ ctx.beginPath();
142
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
143
+ [
144
+ [relBorder[0], relBorderRadius[0], canvas.width - relBorderRadius[1], relBorder[1], relBorder[1], relBorder[1]], // t
145
+ [canvas.width - relBorder[0], canvas.width - relBorder[0], canvas.width - relBorder[0], relBorder[1], relBorderRadius[1], canvas.height - relBorderRadius[2]], // r
146
+ [canvas.width - relBorder[0], canvas.width - relBorderRadius[2], relBorderRadius[3], canvas.height - relBorder[1], canvas.height - relBorder[1], canvas.height - relBorder[1]], // b
147
+ [relBorder[0], relBorder[0], relBorder[0], canvas.height - relBorder[1], canvas.height - relBorderRadius[3], relBorderRadius[0]], // t
148
+ ].forEach(([x0, x1, x2, y0, y1, y2]) => {
149
+ ctx.quadraticCurveTo(x0, y0, x1, y1);
150
+ ctx.lineTo(x2, y2);
151
+ });
152
+ ctx.closePath();
153
+ ctx.fill();
154
+ }
155
+ }
156
+
157
+ ctx.translate(...relBorder);
158
+ ctx.translate(...relPadding);
159
+
160
+ // paint text
161
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
162
+ ctx.fillStyle = this.color;
163
+ ctx.textBaseline = 'bottom';
164
+
165
+ const drawTextStroke = this.strokeWidth > 0;
166
+ if (drawTextStroke) {
167
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
168
+ ctx.strokeStyle = this.strokeColor;
169
+ }
170
+
171
+ lines.forEach((line, index) => {
172
+ const lineX = (innerWidth - ctx.measureText(line).width) / 2;
173
+ const lineY = (index + 1) * this.fontSize;
174
+
175
+ drawTextStroke && ctx.strokeText(line, lineX, lineY);
176
+ ctx.fillText(line, lineX, lineY);
177
+ });
178
+
179
+ // Inject canvas into sprite
180
+ if (this.material.map) this.material.map.dispose(); // gc previous texture
181
+ const texture = this.material.map = new Texture(canvas);
182
+ texture.minFilter = LinearFilter;
183
+ texture.needsUpdate = true;
184
+
185
+ const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
186
+ this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
187
+ }
188
+
189
+ }
@@ -0,0 +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
+ }
@@ -0,0 +1,197 @@
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._text = `${text}`;
13
+ this._textHeight = textHeight;
14
+ this._color = color;
15
+ this._backgroundColor = false; // no background color
16
+ this._padding = 0;
17
+ this._borderWidth = 0;
18
+ this._borderRadius = 0;
19
+ this._borderColor = 'white';
20
+ this._strokeWidth = 0;
21
+ this._strokeColor = 'white';
22
+ this._fontFace = 'Arial';
23
+ this._fontSize = 90; // defines text resolution
24
+ this._fontWeight = 'normal';
25
+ this._genCanvas();
26
+ }
27
+ _text = "";
28
+ _textHeight = 0;
29
+ _color = "#ffffff";
30
+ _backgroundColor = false; // no background color
31
+ _padding = 0;
32
+ _borderWidth = 0;
33
+ _borderRadius = 0;
34
+ _borderColor = 'white';
35
+ _strokeWidth = 0;
36
+ _strokeColor = 'white';
37
+ _fontFace = 'Arial';
38
+ _fontSize = 90; // defines text resolution
39
+ _fontWeight = 'normal';
40
+ _canvas = document.createElement('canvas');
41
+ get text() { return this._text; }
42
+ set text(text) { this._text = text; this._genCanvas(); }
43
+ get textHeight() { return this._textHeight; }
44
+ set textHeight(textHeight) { this._textHeight = textHeight; this._genCanvas(); }
45
+ get color() { return this._color; }
46
+ set color(color) { this._color = color; this._genCanvas(); }
47
+ get backgroundColor() { return this._backgroundColor; }
48
+ set backgroundColor(color) { this._backgroundColor = color; this._genCanvas(); }
49
+ get padding() { return this._padding; }
50
+ set padding(padding) { this._padding = padding; this._genCanvas(); }
51
+ get borderWidth() { return this._borderWidth; }
52
+ set borderWidth(borderWidth) { this._borderWidth = borderWidth; this._genCanvas(); }
53
+ get borderRadius() { return this._borderRadius; }
54
+ set borderRadius(borderRadius) { this._borderRadius = borderRadius; this._genCanvas(); }
55
+ get borderColor() { return this._borderColor; }
56
+ set borderColor(borderColor) { this._borderColor = borderColor; this._genCanvas(); }
57
+ get fontFace() { return this._fontFace; }
58
+ set fontFace(fontFace) { this._fontFace = fontFace; this._genCanvas(); }
59
+ get fontSize() { return this._fontSize; }
60
+ set fontSize(fontSize) { this._fontSize = fontSize; this._genCanvas(); }
61
+ get fontWeight() { return this._fontWeight; }
62
+ set fontWeight(fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); }
63
+ get strokeWidth() { return this._strokeWidth; }
64
+ set strokeWidth(strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); }
65
+ get strokeColor() { return this._strokeColor; }
66
+ set strokeColor(strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); }
67
+ _genCanvas() {
68
+ const canvas = this._canvas;
69
+ const ctx = canvas.getContext('2d');
70
+ const border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
71
+ const relBorder = border.map(b => b * this.fontSize * 0.1); // border in canvas units
72
+ const borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
73
+ const relBorderRadius = borderRadius.map(b => b * this.fontSize * 0.1); // border radius in canvas units
74
+ const padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
75
+ const relPadding = padding.map(p => p * this.fontSize * 0.1); // padding in canvas units
76
+ const lines = this.text.split('\n');
77
+ const font = `${this.fontWeight} ${this.fontSize}px ${this.fontFace}`;
78
+ ctx.font = font; // measure canvas with appropriate font
79
+ const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
80
+ const innerHeight = this.fontSize * lines.length;
81
+ canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
82
+ canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
83
+ // paint border
84
+ if (this.borderWidth) {
85
+ ctx.strokeStyle = this.borderColor;
86
+ if (relBorder[0]) { // left + right borders
87
+ const hb = relBorder[0] / 2;
88
+ ctx.lineWidth = relBorder[0];
89
+ ctx.beginPath();
90
+ ctx.moveTo(hb, relBorderRadius[0]);
91
+ ctx.lineTo(hb, ((canvas.height - 40) / 2) - relBorderRadius[3]);
92
+ ctx.moveTo(canvas.width - hb, relBorderRadius[1]);
93
+ ctx.lineTo(canvas.width - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]);
94
+ ctx.stroke();
95
+ }
96
+ if (relBorder[1]) { // top + bottom borders
97
+ const hb = relBorder[1] / 2;
98
+ ctx.lineWidth = relBorder[1];
99
+ ctx.beginPath();
100
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
101
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[1]), hb);
102
+ ctx.moveTo(Math.max(relBorder[0], relBorderRadius[3]), ((canvas.height - 40) / 2) - hb);
103
+ ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - hb);
104
+ ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - hb);
105
+ ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - hb);
106
+ ctx.lineTo(canvas.width - Math.max(relBorder[0], relBorderRadius[2]), ((canvas.height - 40) / 2) - hb);
107
+ ctx.stroke();
108
+ }
109
+ if (this.borderRadius) { // strike rounded corners
110
+ const cornerWidth = Math.max(...relBorder);
111
+ const hb = cornerWidth / 2;
112
+ ctx.lineWidth = cornerWidth;
113
+ ctx.beginPath();
114
+ [
115
+ !!relBorderRadius[0] && [relBorderRadius[0], hb, hb, relBorderRadius[0]],
116
+ !!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
117
+ !!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
118
+ !!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
119
+ ].filter(d => d).forEach(([x0, x1, y0, y1]) => {
120
+ ctx.moveTo(x0, y0);
121
+ ctx.quadraticCurveTo(x1, y0, x1, y1);
122
+ });
123
+ ctx.stroke();
124
+ }
125
+ }
126
+ // paint background
127
+ if (this.backgroundColor) {
128
+ ctx.fillStyle = this.backgroundColor;
129
+ if (!this.borderRadius) {
130
+ ctx.fillRect(relBorder[0], relBorder[1], canvas.width - relBorder[0] * 2, ((canvas.height - 40) / 2) - relBorder[1] * 2);
131
+ }
132
+ else { // fill with rounded corners
133
+ ctx.beginPath();
134
+ ctx.moveTo(relBorder[0], relBorderRadius[0]);
135
+ ctx.quadraticCurveTo(relBorder[0], relBorder[1], relBorderRadius[0], relBorder[1]);
136
+ ctx.lineTo(canvas.width - relBorderRadius[1], relBorder[1]);
137
+ ctx.quadraticCurveTo(canvas.width - relBorder[0], relBorder[1], canvas.width - relBorder[0], relBorderRadius[1]);
138
+ ctx.lineTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[2]);
139
+ ctx.quadraticCurveTo(canvas.width - relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], canvas.width - relBorderRadius[2], ((canvas.height - 40) / 2) - relBorder[1]);
140
+ ctx.lineTo((canvas.width / 2 + 20), ((canvas.height - 40) / 2) - relBorder[1]);
141
+ ctx.lineTo((canvas.width / 2), ((canvas.height - 40) / 2) + 20 - relBorder[1]);
142
+ ctx.lineTo((canvas.width / 2 - 20), ((canvas.height - 40) / 2) - relBorder[1]);
143
+ ctx.lineTo(relBorderRadius[3], ((canvas.height - 40) / 2) - relBorder[1]);
144
+ ctx.quadraticCurveTo(relBorder[0], ((canvas.height - 40) / 2) - relBorder[1], relBorder[0], ((canvas.height - 40) / 2) - relBorderRadius[3]);
145
+ ctx.closePath();
146
+ ctx.fill();
147
+ }
148
+ }
149
+ ctx.translate(relBorder[0], relBorder[1]);
150
+ ctx.translate(relPadding[0], relPadding[0]);
151
+ // paint text
152
+ ctx.font = font; // Set font again after canvas is resized, as context properties are reset
153
+ ctx.fillStyle = this.color;
154
+ ctx.textBaseline = 'bottom';
155
+ const drawTextStroke = this.strokeWidth > 0;
156
+ if (drawTextStroke) {
157
+ ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
158
+ ctx.strokeStyle = this.strokeColor;
159
+ }
160
+ lines.forEach((line, index) => {
161
+ const lineX = (innerWidth - ctx.measureText(line).width) / 2;
162
+ const lineY = (index + 1) * this.fontSize;
163
+ drawTextStroke && ctx.strokeText(line, lineX, lineY);
164
+ ctx.fillText(line, lineX, lineY);
165
+ });
166
+ // Inject canvas into sprite
167
+ if (this.material.map)
168
+ this.material.map.dispose(); // gc previous texture
169
+ const texture = this.material.map = new Texture(canvas);
170
+ texture.minFilter = LinearFilter;
171
+ texture.needsUpdate = true;
172
+ const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
173
+ this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
174
+ }
175
+ //鼠标拾取计算
176
+ raycast(raycaster, intersects) {
177
+ let intersect = [];
178
+ super.raycast(raycaster, intersect);
179
+ let inte = intersect[0];
180
+ if (inte && (inte.uv.y > 0.55)) {
181
+ intersects.push(inte);
182
+ }
183
+ }
184
+ copy(source) {
185
+ this.color = source.color;
186
+ this.backgroundColor = source.backgroundColor;
187
+ this.padding = source.padding;
188
+ this.borderWidth = source.borderWidth;
189
+ this.borderColor = source.borderColor;
190
+ this.fontFace = source.fontFace;
191
+ this.fontSize = source.fontSize;
192
+ this.fontWeight = source.fontWeight;
193
+ this.strokeWidth = source.strokeWidth;
194
+ this.strokeColor = source.strokeColor;
195
+ return this;
196
+ }
197
+ }