Guest User

Untitled

a guest
Mar 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. precision mediump float;
  2.  
  3. attribute vec2 a_texcoord;
  4. varying vec2 v_texcoord;
  5.  
  6. void main() {
  7. // Convert texture coordinate to render coordinate
  8. gl_Position = vec4(2.0 * a_texcoord.x - 1.0, 1.0 - 2.0 * a_texcoord.y, 0, 1);
  9. gl_PointSize = 1.0;
  10. v_texcoord = a_texcoord;
  11. }
  12.  
  13. precision mediump float;
  14.  
  15. uniform sampler2D u_texture;
  16. varying vec2 v_texcoord;
  17.  
  18. void main() {
  19. vec4 data = texture2D(u_texture, v_texcoord);
  20. gl_FragColor = data;
  21. }
  22.  
  23. var canvas = document.getElementById("canvas");
  24.  
  25. // WebGL context
  26. var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
  27. if (!gl) {
  28. console.log("WebGL not supported");
  29. }
  30.  
  31. // Set canvas dimensions
  32. canvas.width = window.innerWidth;
  33. canvas.height = window.innerHeight;
  34.  
  35. // Create program with shaders
  36. var program = glUtils.createProgram(gl, 'vshader', 'fshader');
  37.  
  38. // Texture dimensions
  39. var textureWidth = 2,
  40. textureHeight = 2;
  41.  
  42. // Texture coordinates
  43. var coords = [];
  44. for (var i = 0; i < textureWidth; ++i) {
  45. for (var j = 0; j < textureHeight; ++j) {
  46. coords.push(i / textureWidth, j / textureHeight);
  47. }
  48. }
  49.  
  50. // Random colors for texture
  51. var d = [];
  52. for (var i = 0; i < textureWidth * textureHeight; ++i) {
  53. d.push(
  54. Math.floor(Math.random() * 256),
  55. Math.floor(Math.random() * 256),
  56. Math.floor(Math.random() * 256),
  57. Math.floor(Math.random() * 256)
  58. );
  59. }
  60.  
  61. // Texture with random colors
  62. var data = new Uint8Array(d);
  63. var texture0 = gl.createTexture();
  64. gl.bindTexture(gl.TEXTURE_2D, texture0);
  65. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
  66. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  67. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  68. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  69. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  70. gl.bindTexture(gl.TEXTURE_2D, null);
  71.  
  72. // Texture to render to
  73. var texture1 = gl.createTexture();
  74. gl.bindTexture(gl.TEXTURE_2D, texture1);
  75. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureWidth, textureHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  76. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  77. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  78. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  79. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  80. gl.bindTexture(gl.TEXTURE_2D, null);
  81.  
  82. // Framebuffer
  83. var fb = gl.createFramebuffer();
  84. gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  85. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture1, 0);
  86.  
  87. // Program
  88. gl.useProgram(program);
  89.  
  90. // Bind texture0
  91. gl.activeTexture(gl.TEXTURE0);
  92. gl.bindTexture(gl.TEXTURE_2D, texture0);
  93. gl.uniform1i(program.uniforms.u_texture, 0);
  94.  
  95. // Bind framebuffer
  96. gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  97. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture1, 0);
  98.  
  99. // Set coordinate array
  100. sendCoords(program);
  101.  
  102. // Set WebGL viewport
  103. setupViewport(gl, textureWidth, textureHeight);
  104.  
  105. // Clear
  106. gl.clearColor(0, 0, 0, 1);
  107. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  108.  
  109. // Draw
  110. gl.drawArrays(gl.POINTS, 0, textureWidth * textureHeight);
  111.  
  112. // Read pixels
  113. var pixels = new Uint8Array(textureWidth * textureHeight * 4);
  114. gl.readPixels(0, 0, textureWidth, textureHeight, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
  115. gl.deleteFramebuffer(fb);
  116. console.log(pixels);
  117.  
  118. // Enable and bind data to attribute for the texture coordinates
  119. function sendCoords(program) {
  120. var coordBuffer = gl.createBuffer();
  121. gl.bindBuffer(gl.ARRAY_BUFFER, coordBuffer);
  122. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(coords), gl.STATIC_DRAW);
  123.  
  124. gl.vertexAttribPointer(
  125. program.attribs.a_texcoord,
  126. 2,
  127. gl.FLOAT,
  128. gl.FALSE,
  129. 0,
  130. 0
  131. );
  132. gl.enableVertexAttribArray(program.attribs.a_texcoord);
  133. }
  134.  
  135. // Viewport setup
  136. function setupViewport(gl, width, height) {
  137. gl.viewport(0, 0, width, height);
  138. gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
  139. gl.enable(gl.BLEND);
  140. gl.disable(gl.DEPTH_TEST);
  141. }
Add Comment
Please, Sign In to add comment