This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
137 lines (121 loc) · 3.31 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* global AFRAME, THREE */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
var createText = require('three-bmfont-text');
var loadFont = require('load-bmfont');
var SDFShader = require('./lib/shaders/sdf');
require('./extras/text-primitive.js'); // Register experimental text primitive
/**
* bmfont text component for A-Frame.
*/
AFRAME.registerComponent('bmfont-text', {
schema: {
text: {
type: 'string'
},
width: {
type: 'number',
default: 1000
},
align: {
type: 'string',
default: 'left'
},
letterSpacing: {
type: 'number',
default: 0
},
lineHeight: {
type: 'number',
default: 38
},
fnt: {
type: 'string',
default: 'https://cdn.jsdelivr.net/gh/bryik/aframe-bmfont-text-component@aa0655cf90f646e12c40ab4708ea90b4686cfb45/assets/DejaVu-sdf.fnt'
},
fntImage: {
type: 'string',
default: 'https://cdn.jsdelivr.net/gh/bryik/aframe-bmfont-text-component@aa0655cf90f646e12c40ab4708ea90b4686cfb45/assets/DejaVu-sdf.png'
},
mode: {
type: 'string',
default: 'normal'
},
color: {
type: 'color',
default: '#000'
},
opacity: {
type: 'number',
default: '1.0'
}
},
/**
* Called when component is attached and when component data changes.
* Generally modifies the entity based on the data.
*/
update: function (oldData) {
// Entity data
var el = this.el;
var data = this.data;
// Use fontLoader utility to load 'fnt' and texture
fontLoader({
font: data.fnt,
image: data.fntImage
}, start);
function start (font, texture) {
// Setup texture, should set anisotropy to user maximum...
texture.needsUpdate = true;
texture.anisotropy = 16;
var options = {
font: font, // the bitmap font definition
text: data.text, // the string to render
width: data.width,
align: data.align,
letterSpacing: data.letterSpacing,
lineHeight: data.lineHeight,
mode: data.mode
};
// Create text geometry
var geometry = createText(options);
// Use './lib/shaders/sdf' to help build a shader material
var material = new THREE.RawShaderMaterial(SDFShader({
map: texture,
side: THREE.DoubleSide,
transparent: true,
color: data.color,
opacity: data.opacity
}));
var text = new THREE.Mesh(geometry, material);
// Rotate so text faces the camera
text.rotation.y = Math.PI;
// Scale text down
text.scale.multiplyScalar(-0.005);
// Register text mesh under entity's object3DMap
el.setObject3D('bmfont-text', text);
}
},
/**
* Called when a component is removed (e.g., via removeAttribute).
* Generally undoes all modifications to the entity.
*/
remove: function () {
this.el.removeObject3D('bmfont-text');
}
});
/**
* A utility to load a font with bmfont-load
* and a texture with Three.TextureLoader()
*/
function fontLoader (opt, cb) {
loadFont(opt.font, function (err, font) {
if (err) {
throw err;
}
var textureLoader = new THREE.TextureLoader();
textureLoader.load(opt.image, function (texture) {
cb(font, texture);
});
});
}