Skip to content

Instantly share code, notes, and snippets.

@vorg
Created September 12, 2016 18:57
Show Gist options
  • Save vorg/32b573b9e4d3f88218c7b4115aa2641e to your computer and use it in GitHub Desktop.
Save vorg/32b573b9e4d3f88218c7b4115aa2641e to your computer and use it in GitHub Desktop.
/*
<p> This example shows how to draw a mesh with regl </p>
*/
const regl = require('regl')()
const mat4 = require('gl-mat4')
const bunny = require('bunny')
var vert = `
precision mediump float;
attribute vec3 position;
uniform mat4 model, view, projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1);
}
`
var frag = `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 1, 1, 1);
}`
const drawBunny = regl({
vert: regl.prop('vert'),
frag: regl.prop('frag'),
// this converts the vertices of the mesh into the position attribute
attributes: {
position: bunny.positions
},
// and this converts the faces fo the mesh into elements
elements: bunny.cells,
uniforms: {
model: mat4.identity([]),
view: ({tick}) => {
const t = 0.01 * tick
return mat4.lookAt([],
[30 * Math.cos(t), 2.5, 30 * Math.sin(t)],
[0, 2.5, 0],
[0, 1, 0])
},
projection: ({viewportWidth, viewportHeight}) =>
mat4.perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
1000)
}
})
var frame = 0
regl.frame(() => {
regl.clear({
depth: 1,
color: [0, 0, 0, 1]
})
drawBunny({
vert: vert,
frag: frag
})
if ((frame++ % 30) == 0) {
console.log(regl.stats)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment