Skip to content

Commit 6db8124

Browse files
committed
easier webgl and shaders
1 parent f6186b5 commit 6db8124

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Easier WebGL and Shaders with stack.gl
2+
3+
> [https://www.youtube.com/watch?v=Qsku8RfB-pM](https://www.youtube.com/watch?v=Qsku8RfB-pM)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `npm start` to run the app viewable on `http://localhost:9966`.

easier-webgl-shaders-stackgl/index.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const shell = require('gl-now')()
2+
const createShader = require('gl-shader')
3+
const createBuffer = require('gl-buffer')
4+
const mat4 = require('gl-mat4')
5+
6+
let shader, buffer
7+
shell.on('gl-init', function () {
8+
const gl = shell.gl
9+
shader = createShader(gl, `
10+
uniform mat4 model;
11+
uniform mat4 camera;
12+
attribute vec3 position;
13+
void main() {
14+
gl_Position = camera * model * vec4(position, 1.0);
15+
}
16+
`, `
17+
void main() {
18+
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
19+
}
20+
`)
21+
buffer = createBuffer(gl, [
22+
0.0, 0.5, 0.0,
23+
-0.5, -0.5, 0.0,
24+
0.5, -0.5, 0.0,
25+
])
26+
})
27+
28+
shell.on('gl-render', function () {
29+
const gl = shell.gl
30+
shader.bind()
31+
buffer.bind()
32+
33+
const camera = mat4.create()
34+
mat4.perspective(camera, 45 * Math.PI / 180, shell.width / shell.height, 0.1, 1000.0)
35+
mat4.translate(camera, camera, [0, 0, -2])
36+
shader.uniforms.camera = camera
37+
38+
const model = mat4.create()
39+
mat4.translate(model, model, [-.3, 0, 0])
40+
//mat4.rotate(model, model, 45, [0, 0, 1])
41+
mat4.scale(model, model, [.5, .5, 1])
42+
shader.uniforms.model = model
43+
shader.attributes.position.pointer()
44+
gl.drawArrays(gl.TRIANGLES, 0, 3)
45+
})
46+
47+
48+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "easier-webgl-shaders-stackgl",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js --live"
8+
},
9+
"author": "Kyle Robinson Young <[email protected]> (http://dontkry.com)",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"budo": "^11.2.0"
13+
},
14+
"dependencies": {
15+
"gl-buffer": "^2.1.2",
16+
"gl-mat4": "^1.2.0",
17+
"gl-now": "^1.4.0",
18+
"gl-shader": "^4.2.1"
19+
}
20+
}

0 commit comments

Comments
 (0)