Created
January 12, 2018 12:26
-
-
Save vorg/3a18c86c706232e4a3a46bf2306eea80 to your computer and use it in GitHub Desktop.
pex-context example
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 createContext = require('pex-context') | |
| const createCube = require('primitive-cube') | |
| const mat4 = require('pex-math/mat4') | |
| const ctx = createContext({ width: 640, height: 480 }) | |
| const cube = createCube() | |
| 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; | |
| gl_FragColor.a = 1.0; | |
| } | |
| ` | |
| }), | |
| attributes: { | |
| aPosition: ctx.vertexBuffer(cube.positions), | |
| aNormal: ctx.vertexBuffer(cube.normals) | |
| }, | |
| indices: ctx.indexBuffer(cube.cells), | |
| uniforms: { | |
| uProjectionMatrix: mat4.perspective(mat4.create(), Math.PI / 4, 640 / 480, 0.1, 100), | |
| uViewMatrix: mat4.lookAt(mat4.create(), [2, 2, 5], [0, 0, 0], [0, 1, 0]) | |
| } | |
| } | |
| 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