Skip to content

Instantly share code, notes, and snippets.

@vorg
Created March 18, 2018 02:18
Show Gist options
  • Save vorg/49cabcd41118a2e8766adf8655a77e98 to your computer and use it in GitHub Desktop.
Save vorg/49cabcd41118a2e8766adf8655a77e98 to your computer and use it in GitHub Desktop.
Minimal pex-v2 example
const createContext = require('pex-context')
const createCube = require('primitive-cube')
const createCamera = require('pex-cam/perspective')
const createOrbiter = require('pex-cam/orbiter')
const ctx = createContext()
const cube = createCube()
const camera = createCamera({
fov: Math.PI / 3,
aspect: window.innerWidth / window.innerHeight
})
const orbiter = createOrbiter({ camera: camera })
const clearCmd = {
pass: ctx.pass({
clearColor: [0, 0, 0, 1],
clearDepth: 1
})
}
const drawCmd = {
pass: ctx.pass({
clearColor: [0.2, 0.2, 0.2, 1],
clearDepth: 1
}),
pipeline: ctx.pipeline({
depthTest: true,
vert: `
attribute vec3 aPosition;
attribute vec3 aNormal;
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
varying vec3 vNormal;
void main () {
gl_Position = uProjectionMatrix * uViewMatrix * vec4(aPosition, 1.0);
vNormal = aNormal;
}
`,
frag: `
precision mediump float;
varying vec3 vNormal;
void main () {
gl_FragColor.rgb = vNormal * 0.5 + 0.5;
gl_FragColor.a = 1.0;
}
`
}),
attributes: {
aPosition: ctx.vertexBuffer(cube.positions),
aNormal: ctx.vertexBuffer(cube.normals)
},
indices: ctx.indexBuffer(cube.cells),
uniforms: {
uProjectionMatrix: camera.projectionMatrix,
uViewMatrix: camera.viewMatrix
}
}
ctx.frame(() => {
ctx.submit(clearCmd)
ctx.submit(drawCmd)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment