shadertoy-bleepy-blocks.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Shadertoy Bleepy Blocks</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="module">
  24. import * as THREE from '../../build/three.module.js';
  25. function main() {
  26. const canvas = document.querySelector('#c');
  27. const renderer = new THREE.WebGLRenderer({canvas});
  28. renderer.autoClearColor = false;
  29. const camera = new THREE.OrthographicCamera(
  30. -1, // left
  31. 1, // right
  32. 1, // top
  33. -1, // bottom
  34. -1, // near,
  35. 1, // far
  36. );
  37. const scene = new THREE.Scene();
  38. const plane = new THREE.PlaneGeometry(2, 2);
  39. const fragmentShader = `
  40. #include <common>
  41. uniform vec3 iResolution;
  42. uniform float iTime;
  43. uniform sampler2D iChannel0;
  44. // By Daedelus: https://www.shadertoy.com/user/Daedelus
  45. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  46. #define TIMESCALE 0.25
  47. #define TILES 8
  48. #define COLOR 0.7, 1.6, 2.8
  49. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  50. {
  51. vec2 uv = fragCoord.xy / iResolution.xy;
  52. uv.x *= iResolution.x / iResolution.y;
  53. vec4 noise = texture2D(iChannel0, floor(uv * float(TILES)) / float(TILES));
  54. float p = 1.0 - mod(noise.r + noise.g + noise.b + iTime * float(TIMESCALE), 1.0);
  55. p = min(max(p * 3.0 - 1.8, 0.1), 2.0);
  56. vec2 r = mod(uv * float(TILES), 1.0);
  57. r = vec2(pow(r.x - 0.5, 2.0), pow(r.y - 0.5, 2.0));
  58. p *= 1.0 - pow(min(1.0, 12.0 * dot(r, r)), 2.0);
  59. fragColor = vec4(COLOR, 1.0) * p;
  60. }
  61. void main() {
  62. mainImage(gl_FragColor, gl_FragCoord.xy);
  63. }
  64. `;
  65. const loader = new THREE.TextureLoader();
  66. const texture = loader.load('resources/images/bayer.png');
  67. texture.minFilter = THREE.NearestFilter;
  68. texture.magFilter = THREE.NearestFilter;
  69. texture.wrapS = THREE.RepeatWrapping;
  70. texture.wrapT = THREE.RepeatWrapping;
  71. const uniforms = {
  72. iTime: { value: 0 },
  73. iResolution: { value: new THREE.Vector3() },
  74. iChannel0: { value: texture },
  75. };
  76. const material = new THREE.ShaderMaterial({
  77. fragmentShader,
  78. uniforms,
  79. });
  80. scene.add(new THREE.Mesh(plane, material));
  81. function resizeRendererToDisplaySize(renderer) {
  82. const canvas = renderer.domElement;
  83. const width = canvas.clientWidth;
  84. const height = canvas.clientHeight;
  85. const needResize = canvas.width !== width || canvas.height !== height;
  86. if (needResize) {
  87. renderer.setSize(width, height, false);
  88. }
  89. return needResize;
  90. }
  91. function render(time) {
  92. time *= 0.001; // convert to seconds
  93. resizeRendererToDisplaySize(renderer);
  94. const canvas = renderer.domElement;
  95. uniforms.iResolution.value.set(canvas.width, canvas.height, 1);
  96. uniforms.iTime.value = time;
  97. renderer.render(scene, camera);
  98. requestAnimationFrame(render);
  99. }
  100. requestAnimationFrame(render);
  101. }
  102. main();
  103. </script>
  104. </html>