Created
June 4, 2017 16:34
-
-
Save akilism/55fdc1401fa1ae6f32393c46fb749036 to your computer and use it in GitHub Desktop.
jam-3-exploding-bunny-regl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const regl = require('regl')(); | |
const mat4 = require('gl-mat4'); | |
const fNormals = require('face-normals'); | |
const unindex = require('unindex-mesh'); | |
const bunny = require('bunny'); | |
const pos = unindex(bunny); | |
const draw = regl({ | |
frag: ` | |
precision mediump float; | |
varying vec3 vNormal; | |
void main() { | |
gl_FragColor = vec4(vNormal, 1.0); | |
} | |
`, | |
vert: ` | |
precision mediump float; | |
attribute vec3 position; | |
attribute vec3 normal; | |
uniform mat4 model, view, projection; | |
uniform float t; | |
varying vec3 vNormal; | |
void main() { | |
vNormal = normal; | |
vec4 offset = vec4(position, 1); | |
float dist = sin(t) * 1.0 + 1.0; | |
offset.xyz += normal * dist; | |
gl_Position = projection * view * model * offset; | |
} | |
`, | |
attributes: { | |
position: pos, | |
normal: fNormals(pos) | |
}, | |
count: pos.length / 3, | |
uniforms: { | |
model: mat4.identity([]), | |
view: mat4.lookAt([], | |
[-10, 2.5, 20], | |
[0, 4, 0], | |
[0, 1, 0] | |
), | |
projection: ({ viewportWidth, viewportHeight }) => { | |
return mat4.perspective([], | |
Math.PI / 4, | |
viewportWidth / viewportHeight, | |
0.01, | |
1000) | |
}, | |
t: ({ time }) => time | |
} | |
}); | |
regl.frame((context) => { | |
regl.clear({ | |
depth: 1, | |
color: [0, 0, 0, 1] | |
}) | |
draw(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/Jam3/jam3-lesson-webgl-shader-threejs