code.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. m00 = 0, m10 = 4, m20 = 8, m30 = 12, m01 = 1, m11 = 5, m21 = 9, m31 = 13, m02 = 2, m12 = 6,
  14. m22 = 10, m32 = 14, m03 = 3, m13 = 7, m23 = 11, m33 = 15;
  15. function ortho(left, right, bottom, top, z_near, z_far) {
  16. a = 2 / (right - left);
  17. b = 2 / (top - bottom);
  18. c = -2 / (z_far - z_near);
  19. r = -(right + left) / (right - left);
  20. s = -(top + bottom) / (top - bottom);
  21. t = -(z_far + z_near) / (z_far - z_near);
  22. m =
  23. [1, 0, 0, 0,
  24. 0, 1, 0, 0,
  25. 0, 0, 1, 0,
  26. 0, 0, 0, 1];
  27. m[m00] = a;
  28. m[m11] = b;
  29. m[m22] = c;
  30. m[m30] = r;
  31. m[m31] = s;
  32. m[m32] = t;
  33. m[m33] = 1;
  34. return m;
  35. }
  36. function isPowerOf2(value) {
  37. return (value & (value - 1)) == 0;
  38. }
  39. let startGL = function() {
  40. // Initialize the GL context
  41. const gl = canvas.getContext("webgl");
  42. // Only continue if WebGL is available and working
  43. if (!gl) {
  44. alert("Unable to initialize WebGL.");
  45. return;
  46. }
  47. const program = initShader(gl, vsSource, fsSource);
  48. const data = {
  49. program: program,
  50. attribs: {
  51. position: gl.getAttribLocation(program, 'position'),
  52. texcoord: gl.getAttribLocation(program, 'texcoord'),
  53. },
  54. uniforms: {
  55. projection: gl.getUniformLocation(program, 'projection'),
  56. dim: gl.getUniformLocation(program, 'dim'),
  57. enabled: gl.getUniformLocation(program, 'enabled'),
  58. rseed: gl.getUniformLocation(program, 'rseed'),
  59. sampler: gl.getUniformLocation(program, 'sampler'),
  60. },
  61. };
  62. const identity =
  63. [1, 0, 0, 0,
  64. 0, 1, 0, 0,
  65. 0, 0, 1, 0,
  66. 0, 0, 0, 1]
  67. const mat = ortho(-1, 1, 1, -1, 1, -1);
  68. const positionBuffer = gl.createBuffer();
  69. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  70. const positions = [
  71. -1.0, 1.0,
  72. -1.0, -1.0,
  73. 1.0, -1.0,
  74. -1.0, 1.0,
  75. 1.0, -1.0,
  76. 1.0, 1.0
  77. ];
  78. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
  79. const textureBuffer = gl.createBuffer();
  80. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  81. const tex = [
  82. 0.0, 1.0,
  83. 0.0, 0.0,
  84. 1.0, 0.0,
  85. 0.0, 1.0,
  86. 1.0, 0.0,
  87. 1.0, 1.0
  88. ];
  89. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(tex), gl.STATIC_DRAW);
  90. const dim = 2048;
  91. const tex0 = createTexture(gl, dim);
  92. const tex1 = createTextureF(gl, dim);
  93. const fb0 = gl.createFramebuffer();
  94. gl.bindFramebuffer(gl.FRAMEBUFFER, fb0);
  95. const attachmentPoint = gl.COLOR_ATTACHMENT0;
  96. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex0, 0);
  97. const fb1 = gl.createFramebuffer();
  98. gl.bindFramebuffer(gl.FRAMEBUFFER, fb1);
  99. gl.framebufferTexture2D(gl.FRAMEBUFFER, attachmentPoint, gl.TEXTURE_2D, tex1, 0);
  100. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  101. var flipTex = false;
  102. function render(t) {
  103. gl.viewport(0, 0, dim, dim);
  104. // swap which texture is being rendered to each frame
  105. texture = flipTex ? tex0 : tex1;
  106. fb = flipTex ? fb1 : fb0;
  107. flipTex = !flipTex;
  108. gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  109. // Set clear color to black, fully opaque
  110. gl.clearColor(0.2, 0.2, 0.2, 1.0);
  111. // Clear the color buffer with specified clear color
  112. gl.clear(gl.COLOR_BUFFER_BIT);
  113. {
  114. const numComponents = 2; // pull out 2 values per iteration
  115. const type = gl.FLOAT; // the data in the buffer is 32bit floats
  116. const normalize = false; // don't normalize
  117. const stride = 0; // how many bytes to get from one set of values to the next
  118. // 0 = use type and numComponents above
  119. const offset = 0; // how many bytes inside the buffer to start from
  120. gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
  121. gl.vertexAttribPointer(
  122. data.attribs.position,
  123. numComponents,
  124. type,
  125. normalize,
  126. stride,
  127. offset);
  128. gl.enableVertexAttribArray(data.attribs.position);
  129. }
  130. {
  131. const num = 2; // every coordinate composed of 2 values
  132. const type = gl.FLOAT; // the data in the buffer is 32 bit float
  133. const normalize = false; // don't normalize
  134. const stride = 0; // how many bytes to get from one set to the next
  135. const offset = 0; // how many bytes inside the buffer to start from
  136. gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
  137. gl.vertexAttribPointer(data.attribs.texcoord, num, type, normalize, stride, offset);
  138. gl.enableVertexAttribArray(data.attribs.texcoord);
  139. }
  140. gl.useProgram(data.program);
  141. gl.uniform1f(data.uniforms.enabled, true);
  142. gl.uniform1f(data.uniforms.dim, dim);
  143. gl.uniform2f(data.uniforms.rseed, Math.random(), Math.random());
  144. {
  145. // Tell WebGL we want to affect texture unit 0
  146. gl.activeTexture(gl.TEXTURE0);
  147. // Bind the texture to texture unit 0
  148. gl.bindTexture(gl.TEXTURE_2D, texture);
  149. // Tell the shader we bound the texture to texture unit 0
  150. gl.uniform1i(data.uniforms.sampler, 0);
  151. const offset = 0;
  152. const vertexCount = 6;
  153. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  154. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  155. gl.uniform1f(data.uniforms.enabled, false);
  156. gl.uniformMatrix4fv(
  157. data.uniforms.projection,
  158. false,
  159. ortho(-1, 1, -1, 1, 1, -1) );
  160. gl.viewport(0, 0, canvas.width, canvas.height);
  161. gl.drawArrays(gl.TRIANGLES, offset, vertexCount);
  162. }
  163. window.requestAnimationFrame(render);
  164. }
  165. function createTexture(gl, dim) {
  166. const texture = gl.createTexture();
  167. gl.bindTexture(gl.TEXTURE_2D, texture);
  168. const blue = [20, 20, 199, 255];
  169. const red = [199, 20, 20, 255]
  170. var sq = []
  171. for(var i = 0; i < dim*dim; i++) {
  172. if(i % dim < dim/2) {
  173. sq.push.apply(sq, red)
  174. } else {
  175. sq.push.apply(sq, blue)
  176. }
  177. }
  178. const data = new Uint8Array(sq); // opaque blue
  179. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, dim, dim, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
  180. const image = new Image();
  181. image.onload = function() {
  182. gl.bindTexture(gl.TEXTURE_2D, texture);
  183. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
  184. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
  185. // WebGL1 has different requirements for power of 2 images
  186. // vs non power of 2 images so check if the image is a
  187. // power of 2 in both dimensions.
  188. if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
  189. // Yes, it's a power of 2. Generate mips.
  190. gl.generateMipmap(gl.TEXTURE_2D);
  191. } else {
  192. // No, it's not a power of 2. Turn of mips and set
  193. // wrapping to clamp to edge
  194. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  195. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  196. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  197. }
  198. };
  199. image.src = "img.jpg";
  200. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  201. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  202. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  203. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  204. return texture;
  205. }
  206. function createTextureF(gl, dim) {
  207. const texture = gl.createTexture();
  208. gl.bindTexture(gl.TEXTURE_2D, texture);
  209. const blue = [20, 20, 199, 255];
  210. const red = [199, 20, 20, 255]
  211. var sq = []
  212. for(var i = 0; i < dim*dim; i++) {
  213. if(i % dim < dim/2) {
  214. sq.push.apply(sq, red)
  215. } else {
  216. sq.push.apply(sq, blue)
  217. }
  218. }
  219. const data = new Uint8Array(sq); // opaque blue
  220. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, dim, dim, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
  221. const image = new Image();
  222. image.onload = function() {
  223. gl.bindTexture(gl.TEXTURE_2D, texture);
  224. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
  225. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
  226. // WebGL1 has different requirements for power of 2 images
  227. // vs non power of 2 images so check if the image is a
  228. // power of 2 in both dimensions.
  229. if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
  230. // Yes, it's a power of 2. Generate mips.
  231. gl.generateMipmap(gl.TEXTURE_2D);
  232. } else {
  233. // No, it's not a power of 2. Turn of mips and set
  234. // wrapping to clamp to edge
  235. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  236. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  237. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  238. }
  239. window.requestAnimationFrame(render);
  240. };
  241. image.src = "img.jpg";
  242. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  243. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  244. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  245. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  246. return texture;
  247. }
  248. }
  249. startGL();
  250. /*
  251. var dim = 128
  252. var sq = []
  253. for(var i = 0; i < dim*dim; i++) {
  254. sq.push((i % 128)/128 * 360)
  255. }
  256. var t0 = performance.now();
  257. function loop(t)
  258. {
  259. var dt = (t - t0) / 1000; // delta t, in seconds
  260. t0 = t;
  261. var fy = 0;
  262. var size = Math.min(canvas.height, canvas.width) / dim;
  263. let newArray = []; newArray.length = dim*dim
  264. if (!pause) {
  265. for(var i = 0; i < sq.length; i++) {
  266. newArray[i] = decideOutcome(i)
  267. context.fillStyle = 'hsl(' + newArray[i] + ', 90%, 60%)'
  268. context.fillRect((i % dim)*size, Math.floor(i/dim) * size, size, size);
  269. }
  270. sq = newArray;
  271. }
  272. window.requestAnimationFrame(loop);
  273. }
  274. function decideOutcome(i) {
  275. opts = []
  276. function op(b) {
  277. opts.push(b)
  278. }
  279. op(sq[i])
  280. if(i % dim == 0) {
  281. // first column
  282. op(sq[i+1]) // search right
  283. }
  284. else if(i % dim == dim-1) {
  285. //last column
  286. op(sq[i-1]) // search left
  287. }
  288. else {
  289. op(sq[i+1]) // search right
  290. op(sq[i-1]) // search left
  291. }
  292. if(i < dim) {
  293. // first row
  294. op(sq[i+dim]) // search down
  295. }
  296. else if(i >= sq.length - dim) {
  297. // last row
  298. op(sq[i-dim]) // search up
  299. }
  300. else {
  301. op(sq[i+dim]) // search down
  302. op(sq[i-dim]) // search up
  303. }
  304. end = opts[Math.floor(Math.random() * opts.length)]
  305. return (end - sq[i]) * 0.99 + sq[i];
  306. }
  307. window.requestAnimationFrame(loop);
  308. */