-
Notifications
You must be signed in to change notification settings - Fork 172
/
test-msdf.js
88 lines (77 loc) · 2.3 KB
/
test-msdf.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
/*
This is an example of 2D rendering, simply
using bitmap fonts in orthographic space.
var geom = createText({
multipage: true,
... other options
})
*/
global.THREE = require('three')
var createOrbitViewer = require('three-orbit-viewer')(THREE)
var shuffle = require('array-shuffle')
var quotes = shuffle(require('sun-tzu-quotes/quotes.json').join(' ').split('.'))
var createText = require('../')
var MSDFShader = require('../shaders/msdf')
var palettes = require('nice-color-palettes')
var palette = palettes[5]
var background = palette.shift()
require('./load')({
font: 'fnt/Roboto-msdf.json',
image: 'fnt/Roboto-msdf.png'
}, start)
function start (font, texture) {
var app = createOrbitViewer({
clearColor: background,
clearAlpha: 1.0,
fov: 65,
position: new THREE.Vector3()
})
app.camera = new THREE.OrthographicCamera()
app.camera.left = 0
app.camera.top = 0
app.camera.near = -1
app.camera.far = 1000
var container = new THREE.Object3D()
app.scene.add(container)
var count = 25
for (var i = 0; i < count; i++) {
createGlyph()
}
var time = 0
// update orthographic
app.on('tick', function (dt) {
time += dt / 1000
var s = (Math.sin(time * 0.5) * 0.5 + 0.5) * 2.0 + 0.5
container.scale.set(s, s, s)
// update camera
var width = app.engine.width
var height = app.engine.height
app.camera.left = -width / 2
app.camera.right = width / 2
app.camera.top = -height / 2
app.camera.bottom = height / 2
app.camera.updateProjectionMatrix()
})
function createGlyph () {
var angle = (Math.random() * 2 - 1) * Math.PI
var geom = createText({
text: quotes[Math.floor(Math.random() * quotes.length)].split(/\s+/g).slice(0, 6).join(' '),
font: font,
align: 'left',
flipY: texture.flipY
})
var material = new THREE.RawShaderMaterial(MSDFShader({
map: texture,
transparent: true,
color: palette[Math.floor(Math.random() * palette.length)]
}))
var layout = geom.layout
var text = new THREE.Mesh(geom, material)
text.position.set(0, -layout.descender + layout.height, 0)
text.scale.multiplyScalar(Math.random() * 0.5 + 0.5)
var textAnchor = new THREE.Object3D()
textAnchor.add(text)
textAnchor.rotation.z = angle
container.add(textAnchor)
}
}