three-base 2.0.87 → 2.0.89

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/Text3D/Text3D.ts DELETED
@@ -1,93 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-06-14 09:48:00
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-04-07 21:52:03
6
- */
7
-
8
- import { TextGeometry, TextGeometryParameters } from 'three/examples/jsm/geometries/TextGeometry.js';
9
- import { Mesh, MeshStandardMaterial } from "three";
10
- import fontContorller from "./fontController";
11
-
12
-
13
-
14
- export type Text3DProps = {
15
- text?: string,
16
- fontType?: string,
17
- size?: number | undefined;
18
- depth?: number | undefined;
19
- curveSegments?: number | undefined;
20
- bevelEnabled?: boolean | undefined;
21
- bevelThickness?: number | undefined;
22
- bevelSize?: number | undefined;
23
- bevelOffset?: number | undefined;
24
- bevelSegments?: number | undefined;
25
- }
26
-
27
-
28
- // 3D文字模型
29
- export default class Text3D extends Mesh {
30
-
31
- constructor(text: string = "new Text3D()") {
32
- super();
33
- this.setText({ text });
34
- }
35
-
36
- // 字体的存储路径 例如 : "http://drm.gitee.io/three-base-font/simhei/字.js"
37
- static path = "//www.3dkeshihua.com/three-base-font/";
38
-
39
-
40
- material = new MeshStandardMaterial({
41
- metalness: 1, // 金属度,值为0到1
42
- roughness: 0.5, // 粗糙度,值为0到1
43
- });
44
-
45
- // 配置
46
- _config: Text3DProps = {
47
- text: "",
48
- fontType: "simhei",
49
- size: 1,
50
- curveSegments: 10,
51
- depth: 0.1, // 文字的厚度
52
- bevelEnabled: true, // 斜角开启
53
- bevelThickness: 0.02, // 斜角厚度
54
- bevelSize: 0.015, // 斜角大小
55
- bevelOffset: 0,// 斜角偏移
56
- bevelSegments: 3,//斜角分段
57
- }
58
-
59
- // 设置文字
60
- setText(text3DProps: Text3DProps) {
61
-
62
- if (Object.keys(text3DProps).some(key => text3DProps[key as keyof typeof text3DProps] !== this._config[key as keyof typeof this._config])) {
63
-
64
- Object.assign(this._config, text3DProps);
65
-
66
- fontContorller.loadText({
67
- fontBasePath: Text3D.path,
68
- text: this._config.text,
69
- fontType: this._config.fontType,
70
- callback: THREEFont => {
71
- this.geometry.dispose();
72
- this.geometry = new TextGeometry(this._config.text || "", {
73
- font: THREEFont,
74
- size: this._config.size,
75
- curveSegments: this._config.curveSegments,
76
- depth: this._config.depth, // 文字的厚度
77
- bevelEnabled: this._config.bevelEnabled, // 斜角开启
78
- bevelThickness: this._config.bevelThickness, // 斜角厚度
79
- bevelSize: this._config.bevelSize, // 斜角大小
80
- bevelOffset: this._config.bevelOffset,// 斜角偏移
81
- bevelSegments: this._config.bevelSegments,//斜角分段
82
- });
83
- this.geometry.center();
84
- this.dispatchEvent({ type: "loaded" } as never);
85
- }
86
- })
87
-
88
- }
89
-
90
- }
91
-
92
-
93
- }
@@ -1,284 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2021-04-15 00:34:44
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2022-06-08 14:33:45
6
- */
7
-
8
- import Dexie, { Table } from "dexie";
9
- import { filter, first, isEmpty } from "lodash";
10
- import { Font } from "three/examples/jsm/loaders/FontLoader.js";
11
- import threeUtils from "../threeUtils";
12
- import gentilisJSON from "./gentilis.json";
13
-
14
- export type saveFontParam = {
15
- fontType?: string,
16
- fontName: string,
17
- fontJson: any
18
- }
19
-
20
- export type addFontFamilyParam = {
21
- fontType?: string,
22
- fontFamilyJson: any
23
- }
24
-
25
- export type getFontParam = {
26
- fontType?: string,
27
- fontName: string
28
- }
29
-
30
- export type loadTextParam = {
31
- fontBasePath: string,
32
- fontType?: string,
33
- text?: string,
34
- callback?: (font: Font) => void
35
- }
36
-
37
- class indexBD extends Dexie {
38
- constructor(dbName: string) {
39
- super(dbName)
40
- }
41
- threeFont!: Table;
42
- }
43
-
44
- // 字体加载器
45
- class fontContorl {
46
-
47
- constructor() { }
48
-
49
- THREEFontMap: Map<string, Font> = new Map();
50
-
51
- // 专门存储字体的数据库
52
- fontBD: indexBD = (() => {
53
-
54
- let fontDB = new indexBD("three-base-font");
55
-
56
- fontDB.version(1).stores({ threeFont: "++KeyPath,fontType,fontName,fontJson" });
57
- return fontDB;
58
- })();
59
-
60
-
61
- /**
62
- * 保存 字体json到本地数据库中
63
- * @param {saveFontParam} { fontType = "simhei", fontName, fontJson }
64
- * @memberof fontContorl
65
- */
66
- async saveFont({ fontType = "simhei", fontName, fontJson = "" }: saveFontParam) {
67
-
68
- let findResult = await this.getFont({ fontType, fontName });
69
-
70
- if (isEmpty(findResult)) {
71
- await this.fontBD.threeFont.add({ fontType, fontName, fontJson });
72
- }
73
- }
74
-
75
- /**
76
- * 通过 fontType 和 fontName 查询 fontJson
77
- * @param {getFontParam} { fontType = "simhei", fontName }
78
- * @returns
79
- * @memberof fontContorl
80
- */
81
- async getFont({ fontType = "simhei", fontName }: getFontParam) {
82
-
83
- let fondResult = await this.fontBD.threeFont
84
- .filter(d => (d.fontType === fontType && d.fontName === fontName))
85
- .toArray();
86
-
87
- if (isEmpty(fondResult)) {
88
- return undefined;
89
- } else {
90
- return first(fondResult)!.fontJson;
91
- }
92
-
93
- }
94
-
95
- // 加载字体的回调函数们
96
- _loadTextCall: Map<string, Array<() => void>> = new Map();
97
-
98
- // 加载文字 提供给需要创建字体模型的方法使用 以节流的形式调用 回调函数
99
- async loadText({ fontType = "simhei", text = "", fontBasePath, callback }: loadTextParam) {
100
-
101
- // 加载字体容器
102
- await this._loadFontIndexJson({ fontType, fontBasePath });
103
-
104
- let newCallback = () => {
105
- let fontFamily = this.THREEFontMap.get(fontType);
106
- if (callback && fontFamily) {
107
- callback(fontFamily);
108
- }
109
- }
110
-
111
- // 遍历文本
112
- filter(text).map(fontName => {
113
-
114
- // 没有的就去加载
115
- if (!this.hasFont({ fontType, fontName })) {
116
-
117
- // 添加文字的默认模型 避免报错
118
- let fontFamily = this.THREEFontMap.get(fontType);
119
-
120
- // 创建 three 字体对象
121
- if (fontFamily) {
122
- //@ts-ignore
123
- fontFamily.data.glyphs[fontName] = { "ha": 1389, "x_min": 477, "x_max": 949, "o": "m 700 901 q 906 770 830 906 q 770 477 949 618 q 711 288 711 412 l 613 288 q 781 608 613 494 q 819 722 825 651 q 700 808 808 803 l 684 808 q 581 673 575 803 l 477 673 q 613 884 477 836 q 700 901 629 901 m 732 184 l 732 71 l 591 71 l 591 184 l 732 184 z " }
124
- }
125
-
126
- this._loadFontJson({ fontType, fontName, fontBasePath });
127
- }
128
-
129
- // 加入待执行的回调
130
- let origin_callbacks = this._loadTextCall.get(`${fontType}_${fontName}`) || [];
131
-
132
- if (newCallback) {
133
- this._loadTextCall.set(`${fontType}_${fontName}`, [...origin_callbacks, newCallback]);
134
- }
135
- });
136
-
137
- // 执行任务队列
138
- threeUtils.addWork(newCallback);
139
- }
140
-
141
- // 加载字体
142
- async _loadFontIndexJson({ fontType = "simhei", fontBasePath }: loadTextParam) {
143
-
144
- let fontFamily = this.THREEFontMap.get(fontType);
145
-
146
- // 创建 three 字体对象
147
- if (!fontFamily) {
148
-
149
- // 初始字体结构
150
- fontFamily = new Font({
151
- "underlineThickness": 69,
152
- "underlinePosition": -174,
153
- "resolution": 1000,
154
- "original_font_information": {},
155
- "familyName": "Source Han Sans CN Normal",
156
- "descender": -167,
157
- "ascender": 0,
158
- "boundingBox": {
159
- "yMin": -700,
160
- "xMin": -1389,
161
- "yMax": 700,
162
- "xMax": 4067
163
- },
164
- "glyphs": {}
165
- });
166
-
167
- this.THREEFontMap.set(fontType, fontFamily);
168
-
169
- // 读取本地数据库中的字体初始结构文件
170
- let fontFamilyIndexJson = await this.getFont({ fontType, fontName: "index" });
171
-
172
- // 加载外部字体初始结构文件
173
- if (!fontFamilyIndexJson) {
174
- this._loadFontJson({ fontType, fontName: "index", fontBasePath });
175
- }
176
- }
177
- }
178
-
179
- // 加载外部字体.js
180
- async _loadFontJson({ fontType = "simhei", fontName, fontBasePath }: { fontType: string, fontName: string, fontBasePath: string }) {
181
-
182
- let fontJson = await this.getFont({ fontType, fontName });
183
-
184
- if (!fontJson) {
185
- fontJson = (gentilisJSON as any).glyphs[fontName];
186
- }
187
-
188
- // 数据库中没有 就去加载字体js
189
- if (!fontJson) {
190
-
191
- // 加载字体js
192
- await new Promise<void>(resolve => {
193
-
194
- let script = document.createElement("script");
195
-
196
- Object.assign(script, {
197
- type: "text/javascript",
198
- src: `${fontBasePath}${fontType}/${fontName}.js`,
199
- async: true,
200
- onload: () => {
201
- script.remove();
202
- resolve();
203
- },
204
- onerror: () => {
205
- }
206
- });
207
- document.head.appendChild(script);
208
- });
209
-
210
- } else {
211
- // 数据库或者默认字体中 有就加入字体文件中
212
- let fontFamily = this.THREEFontMap.get(fontType);
213
-
214
- // 创建 three 字体对象
215
- if (fontFamily) {
216
- //@ts-ignore
217
- fontFamily.data.glyphs[fontName] = fontJson;
218
- }
219
- }
220
-
221
- // 执行相应的回调函数
222
- let origin_callbacks = this._loadTextCall.get(`${fontType}_${fontName}`) || [];
223
-
224
- // // 删除待执行的回调
225
- this._loadTextCall.delete(`${fontType}_${fontName}`);
226
-
227
- origin_callbacks.map(callback => {
228
-
229
- // 执行任务队列
230
- threeUtils.addWork(callback);
231
- });
232
- }
233
-
234
- // 判断是否有这个文字
235
- hasFont({ fontType, fontName }: { fontType: string, fontName: string }) {
236
- return !isEmpty(
237
- this.THREEFontMap.get(fontType) &&
238
- //@ts-ignore
239
- this.THREEFontMap.get(fontType).data.glyphs[fontName]
240
- );
241
- }
242
-
243
- // 这个方法是提供给外部字体文件调用的
244
- addFont({ fontType = "simhei", fontName, fontJson }: saveFontParam) {
245
-
246
- // 保存字体json到本地数据中
247
- this.saveFont({ fontType, fontName, fontJson });
248
-
249
- let fontFamily = this.THREEFontMap.get(fontType);
250
-
251
- // 创建 three 字体对象
252
- if (fontFamily) {
253
- //@ts-ignore
254
- fontFamily.data.glyphs[fontName] = fontJson;
255
- }
256
- }
257
-
258
- // 这个方法是提供给外部字体文件调用的
259
- addFontFamily({ fontType = "simhei", fontFamilyJson }: addFontFamilyParam) {
260
-
261
- // 保存字体初始结构到数据库中
262
- this.saveFont({ fontType, fontName: "index", fontJson: fontFamilyJson });
263
-
264
- let fontFamily = this.THREEFontMap.get(fontType);
265
-
266
- // 创建 three 字体对象
267
- if (fontFamily) {
268
- //@ts-ignore
269
- fontFamily.data = {
270
- ...fontFamilyJson,
271
- //@ts-ignore
272
- glyphs: fontFamily.data.glyphs
273
- }
274
- }
275
-
276
- }
277
-
278
- }
279
-
280
- const fontContorller = new fontContorl();
281
-
282
- Object.assign(window, { fontContorller });
283
-
284
- export default fontContorller;
package/Text3D/index.ts DELETED
@@ -1 +0,0 @@
1
- export { default } from "./Text3D";