code.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. var pause = false;
  2. document.getElementById('pause').onclick = function(){
  3. pause = !pause;
  4. };
  5. const canvas = document.querySelector("#canvas");
  6. //var context = canvas.getContext('2d');
  7. function resize() {
  8. canvas.width = window.innerWidth;
  9. canvas.height = window.innerHeight;
  10. }
  11. window.addEventListener('resize', resize);
  12. resize();
  13. let startGL = function() {
  14. // Initialize the GL context
  15. const gl = canvas.getContext("webgl");
  16. // Only continue if WebGL is available and working
  17. if (!gl) {
  18. alert("Unable to initialize WebGL.");
  19. return;
  20. }
  21. const program = initShader(gl, vsSource, fsSource);
  22. const data = {
  23. program: program,
  24. attribs: {
  25. position: gl.getAttribLocation(program, 'position'),
  26. texcoord: gl.getAttribLocation(program, 'texcoord'),
  27. },
  28. uniforms: {
  29. projection: gl.getUniformLocation(program, 'projection'),
  30. dim: gl.getUniformLocation(program, 'dim'),
  31. enabled: gl.getUniformLocation(program, 'enabled'),
  32. rseed: gl.getUniformLocation(program, 'rseed'),
  33. sampler: gl.getUniformLocation(program, 'sampler'),
  34. },
  35. };
  36. const identity =
  37. [1, 0, 0, 0,
  38. 0, 1, 0, 0,
  39. 0, 0, 1, 0,
  40. 0, 0, 0, 1]
  41. const positionBuffer = gl.createBuffer();
  42. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  43. const positions = [
  44. -1.0, 1.0,
  45. -1.0, -1.0,
  46. 1.0, -1.0,
  47. -1.0, 1.0,
  48. 1.0, -1.0,
  49. 1.0, 1.0
  50. ];
  51. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  52. const textureBuffer = gl.createBuffer();
  53. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  54. const tex = [
  55. 0.0, 1.0,
  56. 0.0, 0.0,
  57. 1.0, 0.0,
  58. 0.0, 1.0,
  59. 1.0, 0.0,
  60. 1.0, 1.0
  61. ];
  62. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tex), gl.STATIC_DRAW);
  63. const dim = 1024;
  64. const tex0 = createTexture(gl, dim);
  65. const tex1 = createTexture(gl, dim);
  66. const fb0 = gl.createFramebuffer();
  67. gl.bindFramebuffer(gl.FRAMEBUFFER, fb0);
  68. const attachmentPoint = gl.COLOR_ATTACHMENT0;
  69. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex0, 0);
  70. const fb1 = gl.createFramebuffer();
  71. gl.bindFramebuffer(gl.FRAMEBUFFER, fb1);
  72. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex1, 0);
  73. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  74. var flipTex = false;
  75. window.requestAnimationFrame(render);
  76. function render(t) {
  77. gl.viewport(0, 0, dim, dim);
  78. // swap which texture is being rendered to each frame
  79. texture = flipTex ? tex0 : tex1;
  80. fb = flipTex ? fb1 : fb0;
  81. flipTex = !flipTex;
  82. gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  83. // Set clear color to black, fully opaque
  84. gl.clearColor(0.2, 0.2, 0.2, 1.0);
  85. // Clear the color buffer with specified clear color
  86. gl.clear(gl.COLOR_BUFFER_BIT);
  87. {
  88. const numComponents = 2; // pull out 2 values per iteration
  89. const type = gl.FLOAT; // the data in the buffer is 32bit floats
  90. const normalize = false; // don't normalize
  91. const stride = 0; // how many bytes to get from one set of values to the next
  92. // 0 = use type and numComponents above
  93. const offset = 0; // how many bytes inside the buffer to start from
  94. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  95. gl.vertexAttribPointer(
  96. data.attribs.position,
  97. numComponents,
  98. type,
  99. normalize,
  100. stride,
  101. offset);
  102. gl.enableVertexAttribArray(data.attribs.position);
  103. }
  104. {
  105. const num = 2; // every coordinate composed of 2 values
  106. const type = gl.FLOAT; // the data in the buffer is 32 bit float
  107. const normalize = false; // don't normalize
  108. const stride = 0; // how many bytes to get from one set to the next
  109. const offset = 0; // how many bytes inside the buffer to start from
  110. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  111. gl.vertexAttribPointer(data.attribs.texcoord, num, type, normalize, stride, offset);
  112. gl.enableVertexAttribArray(data.attribs.texcoord);
  113. }
  114. gl.useProgram(data.program);
  115. gl.uniform1f(data.uniforms.enabled, true);
  116. gl.uniform1f(data.uniforms.dim, dim);
  117. gl.uniform2f(data.uniforms.rseed, Math.random(), Math.random());
  118. gl.uniformMatrix4fv(
  119. data.uniforms.projection,
  120. false,
  121. identity);
  122. {
  123. // Tell WebGL we want to affect texture unit 0
  124. gl.activeTexture(gl.TEXTURE0);
  125. // Bind the texture to texture unit 0
  126. gl.bindTexture(gl.TEXTURE_2D, texture);
  127. // Tell the shader we bound the texture to texture unit 0
  128. gl.uniform1i(data.uniforms.sampler, 0);
  129. const offset = 0;
  130. const vertexCount = 6;
  131. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  132. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  133. gl.uniform1f(data.uniforms.enabled, false);
  134. gl.viewport(0, 0, canvas.width, canvas.height);
  135. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  136. }
  137. window.requestAnimationFrame(render);
  138. }
  139. }
  140. startGL();
  141. /*
  142. var dim = 128
  143. var sq = []
  144. for(var i = 0; i < dim*dim; i++) {
  145. sq.push((i % 128)/128 * 360)
  146. }
  147. var t0 = performance.now();
  148. function loop(t)
  149. {
  150. var dt = (t - t0) / 1000; // delta t, in seconds
  151. t0 = t;
  152. var fy = 0;
  153. var size = Math.min(canvas.height, canvas.width) / dim;
  154. let newArray = []; newArray.length = dim*dim
  155. if (!pause) {
  156. for(var i = 0; i < sq.length; i++) {
  157. newArray[i] = decideOutcome(i)
  158. context.fillStyle = 'hsl(' + newArray[i] + ', 90%, 60%)'
  159. context.fillRect((i % dim)*size, Math.floor(i/dim) * size, size, size);
  160. }
  161. sq = newArray;
  162. }
  163. window.requestAnimationFrame(loop);
  164. }
  165. function decideOutcome(i) {
  166. opts = []
  167. function op(b) {
  168. opts.push(b)
  169. }
  170. op(sq[i])
  171. if(i % dim == 0) {
  172. // first column
  173. op(sq[i+1]) // search right
  174. }
  175. else if(i % dim == dim-1) {
  176. //last column
  177. op(sq[i-1]) // search left
  178. }
  179. else {
  180. op(sq[i+1]) // search right
  181. op(sq[i-1]) // search left
  182. }
  183. if(i < dim) {
  184. // first row
  185. op(sq[i+dim]) // search down
  186. }
  187. else if(i >= sq.length - dim) {
  188. // last row
  189. op(sq[i-dim]) // search up
  190. }
  191. else {
  192. op(sq[i+dim]) // search down
  193. op(sq[i-dim]) // search up
  194. }
  195. end = opts[Math.floor(Math.random() * opts.length)]
  196. return (end - sq[i]) * 0.99 + sq[i];
  197. }
  198. window.requestAnimationFrame(loop);
  199. */