-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
96 lines (85 loc) · 2.13 KB
/
example.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
/**
* ATTRIBUTES
* SHADERS
* UNIFORM
* VARYING
*/
const regl = require('regl')()
const mat4 = require('gl-mat4')
const glsl = require('glslify')
const positions = generatePlane(100, 100)
const drawPlane = regl({
vert: glsl`
precision mediump float;
attribute vec3 position;
uniform mat4 view, projection;
void main() {
gl_Position = projection * view * vec4(position, 1);
}
`,
frag: glsl`
precision mediump float;
void main() {
// vec4(red, green, blue, alpha)
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
`,
// this converts the vertices of the mesh into the position attribute
attributes: {
position: positions,
},
count: positions.length,
uniforms: {
view: ({time}) => {
const t = time * 0.4
return mat4.lookAt([],
[2 * Math.cos(t), 1.5, 2 * Math.sin(t)],
[0, 0, 0],
[0, 1, 0])
},
time: ({time}) => time,
projection: ({viewportWidth, viewportHeight}) => (
mat4.perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
1000)
)
}
})
function generatePlane (segmentsX, segmentsZ) {
const positions = []
const widthX = 1 / segmentsX
const widthZ = 1 / segmentsZ
for (let x = 0; x < segmentsX; x++) {
for (let z = 0; z < segmentsZ; z++) {
const x0 = x * widthX - 0.5
const x1 = (x + 1) * widthX - 0.5
const z0 = z * widthZ - 0.5
const z1 = (z + 1) * widthZ - 0.5
// Build 2 triangles
//
// (x0, z1) (x1, z1)
// *-------*
// | A / |
// | / |
// | / B |
// *-------*
// (x0, z0) (x1, z0)
// Triangle A
positions.push([x0, 0, z0])
positions.push([x0, 0, z1])
positions.push([x1, 0, z1])
// Triangle B
positions.push([x1, 0, z1])
positions.push([x1, 0, z0])
positions.push([x0, 0, z0])
}
}
return positions
}
// Run the draw code on every frame update at 60fps.
regl.frame(() => {
regl.clear({ depth: 1, color: [0, 0, 0, 1] })
drawPlane()
})