-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (68 loc) · 1.78 KB
/
main.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
import { Vec2, Renderer, Camera, Transform, Box, Program, Mesh } from 'ogl';
const vertex = `
attribute vec3 position;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`
const fragment = `
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform float u_time;
#include "lygia/space/ratio.glsl"
#include "lygia/math/decimate.glsl"
void main() {
vec3 color = vec3(0.0);
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st = ratio(st, u_resolution);
color = vec3(st.x,st.y,abs(sin(u_time)));
color = decimate(color, 20.);
gl_FragColor = vec4(color, 1.0);
}
`
{
const renderer = new Renderer();
const gl = renderer.gl;
document.body.appendChild(gl.canvas);
const camera = new Camera(gl);
camera.position.z = 4;
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.perspective({
aspect: gl.canvas.width / gl.canvas.height,
});
}
window.addEventListener('resize', resize, false);
resize();
const scene = new Transform();
const geometry = new Box(gl);
const program = new Program(gl,
{
vertex: resolveLygia(vertex),
fragment: resolveLygia(fragment),
uniforms: {
u_time: { value: 0 },
u_resolution: {
value: new Vec2(
window.innerWidth,
window.innerHeight
)
}
}
}
);
const mesh = new Mesh(gl, { geometry, program });
mesh.setParent(scene);
requestAnimationFrame(update);
function update(t) {
requestAnimationFrame(update);
program.uniforms.u_time.value = t * 0.001;
mesh.rotation.y -= 0.01;
mesh.rotation.x += 0.02;
renderer.render({ scene, camera });
}
}