-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (65 loc) · 1.62 KB
/
index.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
var triangle = require('a-big-triangle');
var createShader = require('gl-shader');
var glslify = require('glslify');
var renderAnimationFrameLoop = require('raf-loop');
var createTexture = require('gl-texture2d');
var webGLContext = require('webgl-context');
var ndpack = require('ndpack-image');
var img = require('./assets/image.png');
/*
* Initialize WebGL context as canvas
*/
var gl = webGLContext({
width: window.innerWidth,
height: window.innerHeight
});
/*
* Append canvas to the DOM
*/
document.body.appendChild(gl.canvas);
/*
* Track the time in JS and in GL
*/
var time = 0
/*
* Create the global shader with uniforms
*/
var shader = createShader(
gl,
glslify('./demo.vert'),
glslify('./demo.frag')
);
shader.bind();
shader.uniforms.iResolution = [
gl.drawingBufferWidth,
gl.drawingBufferHeight,
0 // @NOTE: z doesn't matter so why this is a vec3 on ShaderToy?
];
shader.uniforms.iGlobalTime = 0;
shader.uniforms.iChannel0 = 0;
/*
* Create any global textures you may need access to
*/
const base64ImgData = img.replace('data:image/png;base64,', '');
const imageData = ndpack(512, 512, 4, base64ImgData);
var texture = createTexture(gl, imageData);
texture.wrapS = texture.wrapT = gl.REPEAT;
/*
* Let's go!
*/
start();
function start() {
renderAnimationFrameLoop(render).start();
function render(dt) {
var width = gl.drawingBufferWidth;
var height = gl.drawingBufferHeight;
gl.viewport(0, 0, width, height);
// bind textures and shader
texture.bind();
shader.bind();
// update uniforms
shader.uniforms.iGlobalTime = (time += dt) / 1000;
// render
triangle(gl);
}
}