three-base 1.0.82 → 1.0.83
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.
- package/index.js +1 -1
- package/index.ts +1 -1
- package/lib/ApngTexture.js +40 -107
- package/lib/ApngTextureManage.js +51 -100
- package/lib/EventMesh.js +22 -42
- package/lib/ExtrudeMesh.js +147 -178
- package/lib/FPScontrol.js +116 -133
- package/lib/InstancedGroupMesh.js +25 -45
- package/lib/KeyFrameAnimation.js +37 -54
- package/lib/PlaneText.d.ts +2 -2
- package/lib/PlaneText.js +16 -36
- package/lib/PlaneText.ts +2 -3
- package/lib/PlaneWithHtml.js +48 -126
- package/lib/PointLight/PointLight.d.ts +2 -2
- package/lib/PointLight/PointLight.js +47 -67
- package/lib/PointLight/PointLight.ts +3 -3
- package/lib/Sky/Sky.js +11 -30
- package/lib/SpriteText.d.ts +45 -0
- package/lib/SpriteText.js +162 -0
- package/lib/SpriteText.ts +189 -0
- package/lib/SpriteText2.d.ts +4 -4
- package/lib/SpriteText2.js +90 -166
- package/lib/Text3D/Text3D.js +40 -61
- package/lib/Text3D/fontController.js +149 -284
- package/lib/ThreeBase.js +204 -341
- package/lib/ThreeMouse.js +244 -203
- package/lib/Transition.js +95 -161
- package/lib/keyContorls.js +74 -102
- package/lib/planeModal1/PlaneModal1.js +90 -162
- package/lib/shaders.js +178 -36
- package/lib/spriteModal1/SpriteModal1.js +93 -165
- package/lib/threeLoader.js +66 -89
- package/lib/threeUtils.js +61 -85
- package/lib/threeUtils.ts +2 -1
- package/package.json +2 -4
|
@@ -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).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
|
+
}
|
package/lib/SpriteText2.d.ts
CHANGED
|
@@ -19,10 +19,10 @@ export default class SpriteText2 extends Sprite {
|
|
|
19
19
|
set text(text: string);
|
|
20
20
|
get textHeight(): number;
|
|
21
21
|
set textHeight(textHeight: number);
|
|
22
|
-
get color():
|
|
23
|
-
set color(color:
|
|
24
|
-
get backgroundColor(): false |
|
|
25
|
-
set backgroundColor(color: false |
|
|
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
26
|
get padding(): number;
|
|
27
27
|
set padding(padding: number);
|
|
28
28
|
get borderWidth(): number;
|
package/lib/SpriteText2.js
CHANGED
|
@@ -4,160 +4,87 @@
|
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
5
|
* @Last Modified time: 2023-04-24 16:18:47
|
|
6
6
|
*/
|
|
7
|
-
var __extends = (this && this.__extends) || (function () {
|
|
8
|
-
var extendStatics = function (d, b) {
|
|
9
|
-
extendStatics = Object.setPrototypeOf ||
|
|
10
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
11
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
12
|
-
return extendStatics(d, b);
|
|
13
|
-
};
|
|
14
|
-
return function (d, b) {
|
|
15
|
-
if (typeof b !== "function" && b !== null)
|
|
16
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
17
|
-
extendStatics(d, b);
|
|
18
|
-
function __() { this.constructor = d; }
|
|
19
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
20
|
-
};
|
|
21
|
-
})();
|
|
22
7
|
import { LinearFilter, Sprite, Texture } from 'three';
|
|
23
8
|
// 带箭头的精灵文字
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
_this._fontFace = 'Arial';
|
|
42
|
-
_this._fontSize = 90; // defines text resolution
|
|
43
|
-
_this._fontWeight = 'normal';
|
|
44
|
-
_this._canvas = document.createElement('canvas');
|
|
45
|
-
_this._text = "".concat(text);
|
|
46
|
-
_this._textHeight = textHeight;
|
|
47
|
-
_this._color = color;
|
|
48
|
-
_this._backgroundColor = false; // no background color
|
|
49
|
-
_this._padding = 0;
|
|
50
|
-
_this._borderWidth = 0;
|
|
51
|
-
_this._borderRadius = 0;
|
|
52
|
-
_this._borderColor = 'white';
|
|
53
|
-
_this._strokeWidth = 0;
|
|
54
|
-
_this._strokeColor = 'white';
|
|
55
|
-
_this._fontFace = 'Arial';
|
|
56
|
-
_this._fontSize = 90; // defines text resolution
|
|
57
|
-
_this._fontWeight = 'normal';
|
|
58
|
-
_this._genCanvas();
|
|
59
|
-
return _this;
|
|
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();
|
|
60
26
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
enumerable: false,
|
|
113
|
-
configurable: true
|
|
114
|
-
});
|
|
115
|
-
Object.defineProperty(SpriteText2.prototype, "fontSize", {
|
|
116
|
-
get: function () { return this._fontSize; },
|
|
117
|
-
set: function (fontSize) { this._fontSize = fontSize; this._genCanvas(); },
|
|
118
|
-
enumerable: false,
|
|
119
|
-
configurable: true
|
|
120
|
-
});
|
|
121
|
-
Object.defineProperty(SpriteText2.prototype, "fontWeight", {
|
|
122
|
-
get: function () { return this._fontWeight; },
|
|
123
|
-
set: function (fontWeight) { this._fontWeight = fontWeight; this._genCanvas(); },
|
|
124
|
-
enumerable: false,
|
|
125
|
-
configurable: true
|
|
126
|
-
});
|
|
127
|
-
Object.defineProperty(SpriteText2.prototype, "strokeWidth", {
|
|
128
|
-
get: function () { return this._strokeWidth; },
|
|
129
|
-
set: function (strokeWidth) { this._strokeWidth = strokeWidth; this._genCanvas(); },
|
|
130
|
-
enumerable: false,
|
|
131
|
-
configurable: true
|
|
132
|
-
});
|
|
133
|
-
Object.defineProperty(SpriteText2.prototype, "strokeColor", {
|
|
134
|
-
get: function () { return this._strokeColor; },
|
|
135
|
-
set: function (strokeColor) { this._strokeColor = strokeColor; this._genCanvas(); },
|
|
136
|
-
enumerable: false,
|
|
137
|
-
configurable: true
|
|
138
|
-
});
|
|
139
|
-
SpriteText2.prototype._genCanvas = function () {
|
|
140
|
-
var _this = this;
|
|
141
|
-
var canvas = this._canvas;
|
|
142
|
-
var ctx = canvas.getContext('2d');
|
|
143
|
-
var border = Array.isArray(this.borderWidth) ? this.borderWidth : [this.borderWidth, this.borderWidth]; // x,y border
|
|
144
|
-
var relBorder = border.map(function (b) { return b * _this.fontSize * 0.1; }); // border in canvas units
|
|
145
|
-
var borderRadius = Array.isArray(this.borderRadius) ? this.borderRadius : [this.borderRadius, this.borderRadius, this.borderRadius, this.borderRadius]; // tl tr br bl corners
|
|
146
|
-
var relBorderRadius = borderRadius.map(function (b) { return b * _this.fontSize * 0.1; }); // border radius in canvas units
|
|
147
|
-
var padding = Array.isArray(this.padding) ? this.padding : [this.padding, this.padding]; // x,y padding
|
|
148
|
-
var relPadding = padding.map(function (p) { return p * _this.fontSize * 0.1; }); // padding in canvas units
|
|
149
|
-
var lines = this.text.split('\n');
|
|
150
|
-
var font = "".concat(this.fontWeight, " ").concat(this.fontSize, "px ").concat(this.fontFace);
|
|
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}`;
|
|
151
78
|
ctx.font = font; // measure canvas with appropriate font
|
|
152
|
-
|
|
153
|
-
|
|
79
|
+
const innerWidth = Math.max(...lines.map(line => ctx.measureText(line).width));
|
|
80
|
+
const innerHeight = this.fontSize * lines.length;
|
|
154
81
|
canvas.width = innerWidth + relBorder[0] * 2 + relPadding[0] * 2;
|
|
155
82
|
canvas.height = (innerHeight + relBorder[1] * 2 + relPadding[1] * 2) * 2 + 40;
|
|
156
83
|
// paint border
|
|
157
84
|
if (this.borderWidth) {
|
|
158
85
|
ctx.strokeStyle = this.borderColor;
|
|
159
86
|
if (relBorder[0]) { // left + right borders
|
|
160
|
-
|
|
87
|
+
const hb = relBorder[0] / 2;
|
|
161
88
|
ctx.lineWidth = relBorder[0];
|
|
162
89
|
ctx.beginPath();
|
|
163
90
|
ctx.moveTo(hb, relBorderRadius[0]);
|
|
@@ -167,7 +94,7 @@ var SpriteText2 = /** @class */ (function (_super) {
|
|
|
167
94
|
ctx.stroke();
|
|
168
95
|
}
|
|
169
96
|
if (relBorder[1]) { // top + bottom borders
|
|
170
|
-
|
|
97
|
+
const hb = relBorder[1] / 2;
|
|
171
98
|
ctx.lineWidth = relBorder[1];
|
|
172
99
|
ctx.beginPath();
|
|
173
100
|
ctx.moveTo(Math.max(relBorder[0], relBorderRadius[0]), hb);
|
|
@@ -180,8 +107,8 @@ var SpriteText2 = /** @class */ (function (_super) {
|
|
|
180
107
|
ctx.stroke();
|
|
181
108
|
}
|
|
182
109
|
if (this.borderRadius) { // strike rounded corners
|
|
183
|
-
|
|
184
|
-
|
|
110
|
+
const cornerWidth = Math.max(...relBorder);
|
|
111
|
+
const hb = cornerWidth / 2;
|
|
185
112
|
ctx.lineWidth = cornerWidth;
|
|
186
113
|
ctx.beginPath();
|
|
187
114
|
[
|
|
@@ -189,8 +116,7 @@ var SpriteText2 = /** @class */ (function (_super) {
|
|
|
189
116
|
!!relBorderRadius[1] && [canvas.width - relBorderRadius[1], canvas.width - hb, hb, relBorderRadius[1]],
|
|
190
117
|
!!relBorderRadius[2] && [canvas.width - relBorderRadius[2], canvas.width - hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[2]],
|
|
191
118
|
!!relBorderRadius[3] && [relBorderRadius[3], hb, ((canvas.height - 40) / 2) - hb, ((canvas.height - 40) / 2) - relBorderRadius[3]]
|
|
192
|
-
].filter(
|
|
193
|
-
var x0 = _a[0], x1 = _a[1], y0 = _a[2], y1 = _a[3];
|
|
119
|
+
].filter(d => d).forEach(([x0, x1, y0, y1]) => {
|
|
194
120
|
ctx.moveTo(x0, y0);
|
|
195
121
|
ctx.quadraticCurveTo(x1, y0, x1, y1);
|
|
196
122
|
});
|
|
@@ -226,36 +152,36 @@ var SpriteText2 = /** @class */ (function (_super) {
|
|
|
226
152
|
ctx.font = font; // Set font again after canvas is resized, as context properties are reset
|
|
227
153
|
ctx.fillStyle = this.color;
|
|
228
154
|
ctx.textBaseline = 'bottom';
|
|
229
|
-
|
|
155
|
+
const drawTextStroke = this.strokeWidth > 0;
|
|
230
156
|
if (drawTextStroke) {
|
|
231
157
|
ctx.lineWidth = this.strokeWidth * this.fontSize / 10;
|
|
232
158
|
ctx.strokeStyle = this.strokeColor;
|
|
233
159
|
}
|
|
234
|
-
lines.forEach(
|
|
235
|
-
|
|
236
|
-
|
|
160
|
+
lines.forEach((line, index) => {
|
|
161
|
+
const lineX = (innerWidth - ctx.measureText(line).width) / 2;
|
|
162
|
+
const lineY = (index + 1) * this.fontSize;
|
|
237
163
|
drawTextStroke && ctx.strokeText(line, lineX, lineY);
|
|
238
164
|
ctx.fillText(line, lineX, lineY);
|
|
239
165
|
});
|
|
240
166
|
// Inject canvas into sprite
|
|
241
167
|
if (this.material.map)
|
|
242
168
|
this.material.map.dispose(); // gc previous texture
|
|
243
|
-
|
|
169
|
+
const texture = this.material.map = new Texture(canvas);
|
|
244
170
|
texture.minFilter = LinearFilter;
|
|
245
171
|
texture.needsUpdate = true;
|
|
246
|
-
|
|
172
|
+
const yScale = this.textHeight * lines.length + border[1] * 2 + padding[1] * 2;
|
|
247
173
|
this.scale.set(yScale * canvas.width / canvas.height, yScale, 0);
|
|
248
|
-
}
|
|
174
|
+
}
|
|
249
175
|
//鼠标拾取计算
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
176
|
+
raycast(raycaster, intersects) {
|
|
177
|
+
let intersect = [];
|
|
178
|
+
super.raycast(raycaster, intersect);
|
|
179
|
+
let inte = intersect[0];
|
|
254
180
|
if (inte && (inte.uv.y > 0.55)) {
|
|
255
181
|
intersects.push(inte);
|
|
256
182
|
}
|
|
257
|
-
}
|
|
258
|
-
|
|
183
|
+
}
|
|
184
|
+
copy(source) {
|
|
259
185
|
this.color = source.color;
|
|
260
186
|
this.backgroundColor = source.backgroundColor;
|
|
261
187
|
this.padding = source.padding;
|
|
@@ -267,7 +193,5 @@ var SpriteText2 = /** @class */ (function (_super) {
|
|
|
267
193
|
this.strokeWidth = source.strokeWidth;
|
|
268
194
|
this.strokeColor = source.strokeColor;
|
|
269
195
|
return this;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
}(Sprite));
|
|
273
|
-
export default SpriteText2;
|
|
196
|
+
}
|
|
197
|
+
}
|
package/lib/Text3D/Text3D.js
CHANGED
|
@@ -4,80 +4,59 @@
|
|
|
4
4
|
* @Last Modified by: xiaosihan
|
|
5
5
|
* @Last Modified time: 2023-04-07 21:52:03
|
|
6
6
|
*/
|
|
7
|
-
var __extends = (this && this.__extends) || (function () {
|
|
8
|
-
var extendStatics = function (d, b) {
|
|
9
|
-
extendStatics = Object.setPrototypeOf ||
|
|
10
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
11
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
12
|
-
return extendStatics(d, b);
|
|
13
|
-
};
|
|
14
|
-
return function (d, b) {
|
|
15
|
-
if (typeof b !== "function" && b !== null)
|
|
16
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
17
|
-
extendStatics(d, b);
|
|
18
|
-
function __() { this.constructor = d; }
|
|
19
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
20
|
-
};
|
|
21
|
-
})();
|
|
22
7
|
import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry.js';
|
|
23
8
|
import { Mesh, MeshStandardMaterial } from "three";
|
|
24
9
|
import fontContorller from "./fontController";
|
|
25
10
|
// 3D文字模型
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var _this = _super.call(this) || this;
|
|
31
|
-
_this.material = new MeshStandardMaterial({
|
|
32
|
-
metalness: 0.8,
|
|
33
|
-
roughness: 0.5, // 粗糙度,值为0到1
|
|
34
|
-
});
|
|
35
|
-
// 配置
|
|
36
|
-
_this._config = {
|
|
37
|
-
text: "",
|
|
38
|
-
fontType: "simhei",
|
|
39
|
-
size: 1,
|
|
40
|
-
curveSegments: 10,
|
|
41
|
-
height: 0.1,
|
|
42
|
-
bevelEnabled: true,
|
|
43
|
-
bevelThickness: 0.02,
|
|
44
|
-
bevelSize: 0.015,
|
|
45
|
-
bevelOffset: 0,
|
|
46
|
-
bevelSegments: 3, //斜角分段
|
|
47
|
-
};
|
|
48
|
-
_this.setText({ text: text });
|
|
49
|
-
return _this;
|
|
11
|
+
export default class Text3D extends Mesh {
|
|
12
|
+
constructor(text = "new Text3D()") {
|
|
13
|
+
super();
|
|
14
|
+
this.setText({ text });
|
|
50
15
|
}
|
|
16
|
+
// 字体的存储路径 例如 : "http://drm.gitee.io/three-base-font/simhei/字.js"
|
|
17
|
+
static path = "http://drm.gitee.io/three-base-font/";
|
|
18
|
+
material = new MeshStandardMaterial({
|
|
19
|
+
metalness: 0.8,
|
|
20
|
+
roughness: 0.5, // 粗糙度,值为0到1
|
|
21
|
+
});
|
|
22
|
+
// 配置
|
|
23
|
+
_config = {
|
|
24
|
+
text: "",
|
|
25
|
+
fontType: "simhei",
|
|
26
|
+
size: 1,
|
|
27
|
+
curveSegments: 10,
|
|
28
|
+
height: 0.1,
|
|
29
|
+
bevelEnabled: true,
|
|
30
|
+
bevelThickness: 0.02,
|
|
31
|
+
bevelSize: 0.015,
|
|
32
|
+
bevelOffset: 0,
|
|
33
|
+
bevelSegments: 3, //斜角分段
|
|
34
|
+
};
|
|
51
35
|
// 设置文字
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (Object.keys(text3DProps).some(function (key) { return text3DProps[key] !== _this._config[key]; })) {
|
|
36
|
+
setText(text3DProps) {
|
|
37
|
+
if (Object.keys(text3DProps).some(key => text3DProps[key] !== this._config[key])) {
|
|
55
38
|
Object.assign(this._config, text3DProps);
|
|
56
39
|
fontContorller.loadText({
|
|
57
40
|
fontBasePath: Text3D.path,
|
|
58
41
|
text: this._config.text,
|
|
59
42
|
fontType: this._config.fontType,
|
|
60
|
-
callback:
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
callback: THREEFont => {
|
|
44
|
+
this.geometry.dispose();
|
|
45
|
+
this.geometry = new TextGeometry(this._config.text || "", {
|
|
63
46
|
font: THREEFont,
|
|
64
|
-
size:
|
|
65
|
-
curveSegments:
|
|
66
|
-
height:
|
|
67
|
-
bevelEnabled:
|
|
68
|
-
bevelThickness:
|
|
69
|
-
bevelSize:
|
|
70
|
-
bevelOffset:
|
|
71
|
-
bevelSegments:
|
|
47
|
+
size: this._config.size,
|
|
48
|
+
curveSegments: this._config.curveSegments,
|
|
49
|
+
height: this._config.height,
|
|
50
|
+
bevelEnabled: this._config.bevelEnabled,
|
|
51
|
+
bevelThickness: this._config.bevelThickness,
|
|
52
|
+
bevelSize: this._config.bevelSize,
|
|
53
|
+
bevelOffset: this._config.bevelOffset,
|
|
54
|
+
bevelSegments: this._config.bevelSegments, //斜角分段
|
|
72
55
|
});
|
|
73
|
-
|
|
74
|
-
|
|
56
|
+
this.geometry.center();
|
|
57
|
+
this.dispatchEvent({ type: "loaded" });
|
|
75
58
|
}
|
|
76
59
|
});
|
|
77
60
|
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
Text3D.path = "http://drm.gitee.io/three-base-font/";
|
|
81
|
-
return Text3D;
|
|
82
|
-
}(Mesh));
|
|
83
|
-
export default Text3D;
|
|
61
|
+
}
|
|
62
|
+
}
|