three-base 1.0.44 → 1.0.46
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/README.md +3 -2
- package/index.ts +5 -2
- package/lib/PlaneText.ts +59 -0
- package/lib/spriteModal1/SpriteModal1.ts +156 -0
- package/lib/spriteModal1/spriteModal1.png +0 -0
- package/lib/text/Text2D.ts +1 -23
- package/lib/text/Text3D.ts +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -88,7 +88,8 @@ homeRenderer.setContainer( document.getElementById("div") );
|
|
|
88
88
|
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
演示地址: https://drm.gitee.io/vite_react_mobx_threebase/
|
|
92
92
|
|
|
93
|
-
|
|
93
|
+
代码: https://gitee.com/drm/vite_react_mobx_threebase
|
|
94
94
|
|
|
95
|
+

|
package/index.ts
CHANGED
|
@@ -36,5 +36,8 @@ export { default as threeLoader } from "./lib/threeLoader";
|
|
|
36
36
|
// 第一人称控制器
|
|
37
37
|
export { default as FPScontrol } from "./lib/FPScontrol";
|
|
38
38
|
|
|
39
|
-
//
|
|
40
|
-
export { default as
|
|
39
|
+
// 平面文字
|
|
40
|
+
export { default as PlaneText } from "./lib/PlaneText";
|
|
41
|
+
|
|
42
|
+
// 精灵文字
|
|
43
|
+
export { default as SpriteText } from "three-spritetext";
|
package/lib/PlaneText.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2022-05-23 13:53:06
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2022-05-23 19:38:24
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { DoubleSide, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
|
|
9
|
+
import SpriteText from "three-spritetext";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
type Props = {
|
|
14
|
+
text?: string;
|
|
15
|
+
textHeight?: number;
|
|
16
|
+
color?: string;
|
|
17
|
+
backgroundColor?: string;
|
|
18
|
+
fontFace?: string;
|
|
19
|
+
fontSize?: number;
|
|
20
|
+
fontWeight?: string;
|
|
21
|
+
padding?: number | [number, number];
|
|
22
|
+
borderWidth?: number;
|
|
23
|
+
borderRadius?: number;
|
|
24
|
+
borderColor?: string;
|
|
25
|
+
strokeWidth?: number;
|
|
26
|
+
strokeColor?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default class PlaneText extends Mesh {
|
|
30
|
+
|
|
31
|
+
constructor(text: string = "planeText") {
|
|
32
|
+
super(PlaneText.geometry);
|
|
33
|
+
this.set({ text });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type = "PlaneText";
|
|
37
|
+
|
|
38
|
+
static geometry = new PlaneGeometry(1, 1, 1, 1);
|
|
39
|
+
|
|
40
|
+
spriteText = new SpriteText("");
|
|
41
|
+
|
|
42
|
+
material = new MeshBasicMaterial({ transparent: true, side: DoubleSide, map: this.spriteText.material.map });
|
|
43
|
+
|
|
44
|
+
set(parame: Props) {
|
|
45
|
+
for (let key in parame) {
|
|
46
|
+
//@ts-ignore
|
|
47
|
+
this.spriteText[key] = parame[key];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.scale.copy(this.spriteText.scale);
|
|
51
|
+
this.scale.max(new Vector3(1, 1, 1));
|
|
52
|
+
this.material.map = this.spriteText.material.map;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get(prop: keyof SpriteText) {
|
|
56
|
+
return this.spriteText[prop];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2023-03-08 00:01:34
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2023-03-08 02:05:25
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import { Sprite, CanvasTexture, Vector2, Raycaster, Intersection } from "three";
|
|
13
|
+
import spriteModal1PNG from "./spriteModal1.png";
|
|
14
|
+
|
|
15
|
+
export type SpritModal1Config = {
|
|
16
|
+
size?: number,
|
|
17
|
+
title?: string,
|
|
18
|
+
titleColor?: string,
|
|
19
|
+
texts?: Array<{
|
|
20
|
+
text?: string,
|
|
21
|
+
color?: string,
|
|
22
|
+
size?: number
|
|
23
|
+
}>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// sprit弹窗1
|
|
27
|
+
export default class SpritModal1 extends Sprite {
|
|
28
|
+
|
|
29
|
+
constructor(config: SpritModal1Config = {}) {
|
|
30
|
+
super();
|
|
31
|
+
this.updata(config);
|
|
32
|
+
this.material.map = this.texture;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 背景图
|
|
36
|
+
static img = (() => {
|
|
37
|
+
const img = new Image();
|
|
38
|
+
img.src = spriteModal1PNG;
|
|
39
|
+
return img;
|
|
40
|
+
})();
|
|
41
|
+
|
|
42
|
+
canvas = (() => {
|
|
43
|
+
const canvas = document.createElement("canvas");
|
|
44
|
+
canvas.width = 1024;
|
|
45
|
+
canvas.height = 1024;
|
|
46
|
+
return canvas;
|
|
47
|
+
})();
|
|
48
|
+
|
|
49
|
+
context = this.canvas.getContext("2d")!;
|
|
50
|
+
|
|
51
|
+
texture = new CanvasTexture(this.canvas);
|
|
52
|
+
|
|
53
|
+
// 配置
|
|
54
|
+
_config: {
|
|
55
|
+
size: number,
|
|
56
|
+
title: string,
|
|
57
|
+
titleColor: string,
|
|
58
|
+
texts: Array<{
|
|
59
|
+
text: string,
|
|
60
|
+
color: string,
|
|
61
|
+
size: number
|
|
62
|
+
}>
|
|
63
|
+
} = {
|
|
64
|
+
size: 10,
|
|
65
|
+
title: "SpritModal1",
|
|
66
|
+
titleColor: "#ffffff",
|
|
67
|
+
texts: []
|
|
68
|
+
// [
|
|
69
|
+
// {
|
|
70
|
+
// text: "1",
|
|
71
|
+
// color: "#ffff00",
|
|
72
|
+
// size: 40
|
|
73
|
+
// },
|
|
74
|
+
// {
|
|
75
|
+
// text: "2",
|
|
76
|
+
// color: "#ff00ff",
|
|
77
|
+
// size: 40
|
|
78
|
+
// },
|
|
79
|
+
// ]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async updata(config: SpritModal1Config) {
|
|
83
|
+
|
|
84
|
+
Object.assign(this._config, config)
|
|
85
|
+
|
|
86
|
+
//绘制背景
|
|
87
|
+
this.context.clearRect(0, 0, 1024, 1024);
|
|
88
|
+
|
|
89
|
+
while (!SpritModal1.img.complete) {
|
|
90
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 设置大小
|
|
94
|
+
this.scale.set(this._config.size, this._config.size, this._config.size);
|
|
95
|
+
|
|
96
|
+
// this.context.fillStyle = "#00000088";
|
|
97
|
+
// this.context.fillRect(0, 0, 1024, 1024);
|
|
98
|
+
|
|
99
|
+
//绘制背景
|
|
100
|
+
this.context.drawImage(SpritModal1.img, 186, 0);
|
|
101
|
+
|
|
102
|
+
// 绘制标题
|
|
103
|
+
this.context.fillStyle = this._config.titleColor;
|
|
104
|
+
this.context.font = '40px serif';// 设置字体大小
|
|
105
|
+
this.context.textAlign = "left";
|
|
106
|
+
this.context.textBaseline = "top";
|
|
107
|
+
this.context.fillText(this._config.title, 250, 72);
|
|
108
|
+
|
|
109
|
+
let textPos = new Vector2(250, 150);
|
|
110
|
+
let maxWidth = 530;
|
|
111
|
+
// 绘制内容
|
|
112
|
+
this._config.texts.map(({ text = "text", color = "#ffffff", size = 40 }) => {
|
|
113
|
+
this.context.fillStyle = color;
|
|
114
|
+
this.context.font = `${size}px serif`;// 设置字体大小
|
|
115
|
+
this.context.textAlign = "left";
|
|
116
|
+
this.context.textBaseline = "top";
|
|
117
|
+
|
|
118
|
+
// 字符分隔为数组
|
|
119
|
+
var arrText = text.split('');
|
|
120
|
+
var line = '';
|
|
121
|
+
|
|
122
|
+
// 文字自动换行计算
|
|
123
|
+
for (var n = 0; n < arrText.length; n++) {
|
|
124
|
+
var metrics = this.context.measureText(line + arrText[n]);
|
|
125
|
+
var testWidth = metrics.width;
|
|
126
|
+
if ((testWidth > maxWidth && n > 0) || arrText[n] === "\n") {
|
|
127
|
+
this.context.fillText(line, textPos.x, textPos.y);
|
|
128
|
+
line = "";
|
|
129
|
+
textPos.y += size;
|
|
130
|
+
} else {
|
|
131
|
+
line += arrText[n];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this.context.fillText(line, textPos.x, textPos.y);
|
|
136
|
+
textPos.y += size;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
this.texture.needsUpdate = true;
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//鼠标拾取计算
|
|
144
|
+
raycast(raycaster: Raycaster, intersects: Intersection[]) {
|
|
145
|
+
|
|
146
|
+
let intersect: Intersection[] = [];
|
|
147
|
+
super.raycast(raycaster, intersect);
|
|
148
|
+
let inte = intersect[0];
|
|
149
|
+
if (inte && (inte.uv!.x > 0.22 && inte.uv!.x < 0.78) && (inte.uv!.y > 0.54 && inte.uv!.y < 0.94)) {
|
|
150
|
+
intersects.push(inte);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
|
|
Binary file
|
package/lib/text/Text2D.ts
CHANGED
|
@@ -35,29 +35,7 @@ type Props = {
|
|
|
35
35
|
export default class Text2D extends Mesh {
|
|
36
36
|
|
|
37
37
|
constructor(text: string = "Text2D") {
|
|
38
|
-
super(
|
|
39
|
-
this.set({ text });
|
|
40
|
-
this.material = new MeshBasicMaterial({ transparent: true, side: DoubleSide, map: this.spriteText.material.map });
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type = "Text2D";
|
|
44
|
-
|
|
45
|
-
static geometry = new PlaneGeometry(1, 1, 1, 1);
|
|
46
|
-
|
|
47
|
-
spriteText = new SpriteText("");
|
|
48
|
-
|
|
49
|
-
material = new MeshBasicMaterial({ transparent: true, side: DoubleSide, map: this.spriteText.material.map });
|
|
50
|
-
|
|
51
|
-
minScale = new Vector3(0.1, 0.1, 0.1);
|
|
52
|
-
|
|
53
|
-
set(parame: Props) {
|
|
54
|
-
for (let key in parame) {
|
|
55
|
-
//@ts-ignore
|
|
56
|
-
this.spriteText[key] = parame[key];
|
|
57
|
-
}
|
|
58
|
-
this.scale.copy(this.spriteText.scale);
|
|
59
|
-
this.scale.multiplyScalar(0.1);
|
|
60
|
-
this.scale.max(this.minScale);
|
|
38
|
+
super();
|
|
61
39
|
}
|
|
62
40
|
|
|
63
41
|
}
|
package/lib/text/Text3D.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "three-base",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.46",
|
|
4
4
|
"description": "three 3d three-base",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"keywords": [
|
|
10
10
|
"three",
|
|
11
11
|
"3d",
|
|
12
|
-
"three-base"
|
|
12
|
+
"three-base",
|
|
13
|
+
"three-spritetext"
|
|
13
14
|
],
|
|
14
15
|
"main": "index.ts",
|
|
15
16
|
"module": "index.ts",
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
"element-resize-event": "^3",
|
|
42
43
|
"lodash": "^4",
|
|
43
44
|
"three": ">=0.150.0",
|
|
45
|
+
"vite": "^4",
|
|
44
46
|
"three-spritetext": "^1",
|
|
45
47
|
"@types/three": ">=0.149.0",
|
|
46
48
|
"@types/element-resize-event": "^3",
|