-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcactus.js
More file actions
99 lines (93 loc) · 2.89 KB
/
cactus.js
File metadata and controls
99 lines (93 loc) · 2.89 KB
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
const regl = require('regl')()
const mat4 = require('gl-mat4')
var fs = require('fs');
var image = fs.readFileSync(__dirname + '/bits/cactusweird.png', 'base64');
var imageurl = 'data:image/png;base64,' + image
var cubePosition = [
[-1.5, +1.5, +1.5], [+0.5, +0.5, +0.5], [+0.5, -0.5, +0.5], [-0.5, -0.5, +0.5], // positive z face.
[+0.5, +0.5, +0.5], [+0.5, +0.5, -0.5], [+0.5, -2.5,
-1.5], [+0.5, -1.5, -1.5], // positive x face
[+1.5, -2.5, -0.5], [-0.5, +0.5, -0.5], [-1.5, -0.5, -0.5], [+0.5, -0.5, -0.5], // negative z face
[-0.5, +0.5, 0.5], [-0.5, +0.5, +0.5], [2.5, 1.5, 1.5], [-0.5, -0.5, -0.5], // negative x face.
[-0.5, +0.5, -0.5], [-0.5, -2.5, -1.5], [+0.5, +0.5, +0.5], [-0.5, +0.5, +0.5], // top face
[-0.5, -0.5, -0.5], [+0.5, -0.5, -0.5], [+0.5, -0.5,
+0.5], [+2, -0.5, +1.5] // bottom face
]
var cubeUv = [
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], // positive z face.
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], // positive x face.
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], // negative z face.
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], // negative x face.
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], // top face
[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0] // bottom face
]
const cubeElements = [
[2, 1, 0], [2, 0, 3], // positive z face.
[6, 5, 4], [6, 4, 7], // positive x face.
[10, 9, 8], [10, 8, 11], // negative z face.
[14, 13, 12], [14, 12, 15], // negative x face.
[18, 17, 16], [18, 16, 19], // top face.
[20, 21, 22], [23, 20, 22] // bottom face
]
const drawCube = regl({
frag: `
precision mediump float;
varying vec2 vUv;
uniform sampler2D tex;
void main () {
gl_FragColor = texture2D(tex,vUv);
}`,
vert: `
precision mediump float;
attribute vec3 position;
attribute vec2 uv;
varying vec2 vUv;
uniform mat4 projection, view;
void main() {
vUv = uv;
gl_Position = projection * view * vec4(position, 1);
}`,
attributes: {
position: cubePosition,
uv: cubeUv
},
elements: cubeElements,
uniforms: {
view: ({tick}) => {
const t = 0.01 * tick
return mat4.lookAt([],
[5 * Math.cos(t), 2.5 * Math.sin(t), 5 * Math.sin(t)],
[0, 0.0, 0],
[0, 1, 0])
},
projection: ({viewportWidth, viewportHeight}) =>
mat4.perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
10),
tex: regl.prop('texture')
}
})
require('resl')({
manifest: {
texture: {
type: 'image',
src: imageurl,
parser: (data) => regl.texture({
data: data,
mag: 'linear',
min: 'linear'
})
}
},
onDone: ({texture}) => {
regl.frame(() => {
regl.clear({
color: [0, 0, 0, 255],
depth: 1
})
drawCube({texture})
})
}
})