-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen-tri.js
105 lines (82 loc) · 2.27 KB
/
screen-tri.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* This sample shows a hack which performs a full screen render pass
* by drawing a triangle instead of a quad in NDC space.
*/
var gl = null;
var texLoc,tex;
var images =[];
var canvas = null;
function init()
{
var image = new Image();
image.src = "assets/lena_color_512.png";
image.addEventListener('load',function(){
console.log("loaded");
images.push(image);
run();
});
}
function run()
{
canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
document.body.appendChild(canvas);
gl = canvas.getContext('webgl2', { antialias: false });
var isWebGL2 = !!gl;
if (isWebGL2 === false) {
console.error("WebGL2 context not supported");
return;
}
const vert = [
"#version 300 es",
"out vec2 v_uv;",
"void main()",
"{",
" float x = -1.0 + float((gl_VertexID & 1) << 2);",
" float y = -1.0 + float((gl_VertexID & 2) << 1);",
" v_uv.x = (x + 1.0) * 0.5;",
" v_uv.y = (1.0 - y) * 0.5;",
" gl_Position = vec4(x, y, 0.0, 1.0);",
"}"
].join('\n');
const fragMb = [
"#version 300 es",
"precision highp float;",
"precision highp int;",
"uniform sampler2D tex;",
"in vec2 v_uv;",
"out vec4 oColor;",
"void main(){",
"oColor = texture(tex, v_uv);",
"}"
].join('\n');
var prog = createProgram(gl, vert, fragMb);
if (!prog == null) {
console.error("failed to create shader prog");
return;
}
gl.useProgram(prog);
texLoc = gl.getUniformLocation(prog,"tex");
var img = images[0];
tex = createGLTexture(0,gl.RGB,gl.RGB8,img.width,img.height,gl.CLAMP_TO_EDGE,false,false,img);
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.uniform1i(texLoc, 0);
window.addEventListener('resize', onResize);
requestAnimationFrame(render);
}
function onResize()
{
const w = window.innerWidth;
const h = window.innerHeight;
canvas.width = w;
canvas.height = h;
gl.viewport(0,0,w,h);
}
function render()
{
gl.clearColor(0.2,0.2,0.2,1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES,0,3);
requestAnimationFrame(render);
}