forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
114 lines (94 loc) · 2.1 KB
/
text.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
/*
tags: basic
<p>This example shows how you can draw vectorized text in regl.</p>
*/
const regl = require('../regl')()
const vectorizeText = require('vectorize-text')
const perspective = require('gl-mat4/perspective')
const lookAt = require('gl-mat4/lookAt')
const textMesh = vectorizeText('hello regl!', {
textAlign: 'center',
textBaseline: 'middle'
})
const feedBackTexture = regl.texture({
copy: true,
min: 'linear',
mag: 'linear'
})
const drawFeedback = regl({
frag: `
precision mediump float;
uniform sampler2D texture;
uniform float t;
varying vec2 uv;
void main () {
vec2 warp = uv + 0.01 * sin(t) * vec2(0.5 - uv.y, uv.x - 0.5)
- 0.01 * (uv - 0.5);
gl_FragColor = vec4(0.98 * texture2D(texture, warp).rgb, 1);
}`,
vert: `
precision mediump float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(2.0 * position - 1.0, 0, 1);
}`,
attributes: {
position: [-2, 0, 0, -2, 2, 2]
},
uniforms: {
texture: feedBackTexture,
t: ({tick}) => 0.001 * tick
},
depth: {enable: false},
count: 3
})
const drawText = regl({
frag: `
precision mediump float;
uniform float t;
void main () {
gl_FragColor = vec4(
1.0 + cos(2.0 * t),
1.0 + cos(2.1 * t + 1.0),
1.0 + cos(2.2 * t + 2.0),
1);
}`,
vert: `
attribute vec2 position;
uniform mat4 projection, view;
void main () {
gl_Position = projection * view * vec4(position, 0, 1);
}`,
attributes: {
position: textMesh.positions
},
elements: textMesh.edges,
uniforms: {
t: ({tick}) => 0.01 * tick,
view: ({tick}) => {
const t = 0.01 * tick
return lookAt([],
[5 * Math.sin(t), 0, -5 * Math.cos(t)],
[0, 0, 0],
[0, -1, 0])
},
projection: ({viewportWidth, viewportHeight}) =>
perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
1000)
},
depth: {enable: false}
})
regl.frame(() => {
drawFeedback()
drawText()
feedBackTexture({
copy: true,
min: 'linear',
mag: 'linear'
})
})