-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTextMesh.js
executable file
·66 lines (64 loc) · 2.02 KB
/
TextMesh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { THREE } from 'expo-three'; // 2.2.2-alpha.1
import AssetUtils from 'expo-asset-utils'; // 0.0.4-alpha.0
class TextMesh extends THREE.Mesh {
params = {};
get text() {
return this.params.text;
}
set text(text) {
this.update({ text });
}
/*
font — an instance of THREE.Font.
size — Float. Size of the text. Default is 100.
height — Float. Thickness to extrude text. Default is 50.
curveSegments — Integer. Number of points on the curves. Default is 12.
bevelEnabled — Boolean. Turn on bevel. Default is False.
bevelThickness — Float. How deep into text bevel goes. Default is 10.
bevelSize — Float. How far from text outline is bevel. Default is 8.
bevelSegments — Integer. Number of bevel segments. Default is 3.
*/
update = async (props = {}) => {
this.params = { ...this.params, ...props };
if (this.geometry) {
this.geometry.dispose();
}
const { font } = this.params;
if (!font) {
console.warn(
'TextMesh.updateWithParams: font is required to create TextBufferGeometry!',
);
return;
} else if (!(font instanceof THREE.Font)) {
if (typeof font === 'object') {
this.params.font = this.loadFontFromJson(font);
} else if (typeof font === 'string') {
const uri = await AssetUtils.uriAsync(font);
this.params.font = this.loadFontFromUriAsync(uri);
}
}
this.geometry = new THREE.TextBufferGeometry(
this.params.text || this.text,
this.params,
);
this.geometry.computeBoundingBox();
this.geometry.computeVertexNormals();
return this.geometry;
};
loadFontFromJson = json => {
const font = new THREE.FontLoader().parse(json);
this.update({ font });
return font;
};
loadFontFromUriAsync = async uri => {
const font = await this._loadFontFromUri(uri);
this.update({ font });
return font;
};
_loadFontFromUriAsync = async uri => {
new Promise((res, rej) =>
new THREE.FontLoader().load(uri, res, () => {}, rej),
);
};
}
export default TextMesh;