forked from Experience-Monks/three-bmfont-text
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·122 lines (100 loc) · 3.21 KB
/
index.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
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
113
114
115
116
117
118
119
120
121
122
import createLayout from 'layout-bmfont-text';
import createIndices from 'quad-indices';
import {pages,positions,uvs} from './lib/vertices';
import {computeBox, computeSphere} from './lib/utils';
import {BufferGeometry, BufferAttribute, Sphere, Box3} from 'three';
export function createGeometry (opt) {
return new TextGeometry(opt)
}
class TextGeometry extends BufferGeometry {
constructor (opt) {
super()
this.type = 'TextGeometry'
if (typeof opt === 'string') {
opt = { text: opt }
}
// use these as default values for any subsequent
// calls to update()
this._opt = Object.assign({}, opt)
// also do an initial setup...
if (opt) this.update(opt)
}
update (opt) {
if (typeof opt === 'string') {
opt = { text: opt }
}
// use constructor defaults
opt = Object.assign({}, this._opt, opt)
if (!opt.font) {
throw new TypeError('must specify a { font } in options')
}
this.layout = createLayout(opt)
// get vec2 texcoords
const flipY = opt.flipY !== false
// the desired BMFont data
const font = opt.font
// determine texture size from font file
const texWidth = font.common.scaleW
const texHeight = font.common.scaleH
// get visible glyphs
const glyphs = this.layout.glyphs.filter(function (glyph) {
const bitmap = glyph.data
return bitmap.width * bitmap.height > 0
})
// provide visible glyphs for convenience
this.visibleGlyphs = glyphs
// get common vertex data
const glyphPositions = positions(glyphs)
const glyphUvs = uvs(glyphs, texWidth, texHeight, flipY)
const indices = createIndices([], {
clockwise: true,
type: 'uint16',
count: glyphs.length
})
// update vertex data
this.setIndex(indices)
this.setAttribute('position', new BufferAttribute(glyphPositions, 2))
this.setAttribute('uv', new BufferAttribute(glyphUvs, 2))
// update multipage data
if (!opt.multipage && 'page' in this.attributes) {
// disable multipage rendering
this.deleteAttribute('page')
} else if (opt.multipage) {
// enable multipage rendering
this.setAttribute('page', new BufferAttribute(pages(glyphs), 1))
}
}
computeBoundingSphere () {
if (this.boundingSphere === null) {
this.boundingSphere = new Sphere()
}
const positions = this.attributes.position.array
const itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
this.boundingSphere.radius = 0
this.boundingSphere.center.set(0, 0, 0)
return
}
computeSphere(positions, this.boundingSphere)
if (isNaN(this.boundingSphere.radius)) {
console.error(
`BufferGeometry.computeBoundingSphere():
Computed radius is NaN. The
"position" attribute is likely to have NaN values.`
)
}
}
computeBoundingBox () {
if (this.boundingBox === null) {
this.boundingBox = new Box3()
}
const bbox = this.boundingBox
const positions = this.attributes.position.array
const itemSize = this.attributes.position.itemSize
if (!positions || !itemSize || positions.length < 2) {
bbox.makeEmpty()
return
}
computeBox(positions, bbox)
}
}