Skip to content

Instantly share code, notes, and snippets.

@ndugger
Last active June 21, 2019 06:20
Show Gist options
  • Save ndugger/6c4214f5bf3545c7e43ff590aae035ca to your computer and use it in GitHub Desktop.
Save ndugger/6c4214f5bf3545c7e43ff590aae035ca to your computer and use it in GitHub Desktop.
import Canvas from '../library/Canvas';
import Program from '../library/Program';
import Shader from '../library/Shader';
const canvas = new Canvas(640, 480);
const image = new Image();
const program = new Program(canvas, [
new Shader.Vertex(`
in vec2 i_Position;
in vec2 i_Sample;
uniform vec2 u_Resolution;
out vec2 o_Sample;
void main () {
vec2 one = i_Position / u_Resolution;
vec2 two = one * 2.0;
vec2 clip = two - 1.0;
vec2 flip = vec2(1.0, -1.0);
gl_Position = vec4(clip * flip, 0.0, 1.0);
o_Sample = i_Sample;
}
`),
new Shader.Fragment(`
precision mediump float;
in vec2 o_Sample;
uniform sampler2D u_Texture;
out vec4 o_Color;
void main () {
o_Color = texture(u_Texture, o_Sample);
}
`)
]);
program.compile();
program.use();
program.setUniform('u_Resolution', new Float32Array([
program.canvas.element.width,
program.canvas.element.height
]));
image.onload = () => {
program.setUniform('u_Texture', image);
program.setInput('i_Position', new Float32Array([
0, 0,
image.width, 0,
0, image.height,
0, image.height,
image.width, 0,
image.width, image.height
]));
program.setInput('i_Sample', new Float32Array([
0, 0,
1, 0,
0, 1,
0, 1,
1, 0,
1, 1
]));
canvas.clear(0, 0, 0, 1);
canvas.drawTriangles(0, 6);
};
image.src = './tests/assets/troll.png';
document.body.append(canvas.element);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment