shadertoy-bleepy-blocks.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector('#c');
  37. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  38. renderer.autoClearColor = false;
  39. const camera = new THREE.OrthographicCamera(
  40. -1, // left
  41. 1, // right
  42. 1, // top
  43. -1, // bottom
  44. -1, // near,
  45. 1, // far
  46. );
  47. const scene = new THREE.Scene();
  48. const plane = new THREE.PlaneGeometry(2, 2);
  49. const fragmentShader = `
  50. #include <common>
  51. uniform vec3 iResolution;
  52. uniform float iTime;
  53. uniform sampler2D iChannel0;
  54. // By Daedelus: https://www.shadertoy.com/user/Daedelus
  55. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  56. #define TIMESCALE 0.25
  57. #define TILES 8
  58. #define COLOR 0.7, 1.6, 2.8
  59. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  60. {
  61. vec2 uv = fragCoord.xy / iResolution.xy;
  62. uv.x *= iResolution.x / iResolution.y;
  63. vec4 noise = texture2D(iChannel0, floor(uv * float(TILES)) / float(TILES));
  64. float p = 1.0 - mod(noise.r + noise.g + noise.b + iTime * float(TIMESCALE), 1.0);
  65. p = min(max(p * 3.0 - 1.8, 0.1), 2.0);
  66. vec2 r = mod(uv * float(TILES), 1.0);
  67. r = vec2(pow(r.x - 0.5, 2.0), pow(r.y - 0.5, 2.0));
  68. p *= 1.0 - pow(min(1.0, 12.0 * dot(r, r)), 2.0);
  69. fragColor = vec4(COLOR, 1.0) * p;
  70. }
  71. void main() {
  72. mainImage(gl_FragColor, gl_FragCoord.xy);
  73. }
  74. `;
  75. const loader = new THREE.TextureLoader();
  76. const texture = loader.load('resources/images/bayer.png');
  77. texture.minFilter = THREE.NearestFilter;
  78. texture.magFilter = THREE.NearestFilter;
  79. texture.wrapS = THREE.RepeatWrapping;
  80. texture.wrapT = THREE.RepeatWrapping;
  81. const uniforms = {
  82. iTime: { value: 0 },
  83. iResolution: { value: new THREE.Vector3() },
  84. iChannel0: { value: texture },
  85. };
  86. const material = new THREE.ShaderMaterial({
  87. fragmentShader,
  88. uniforms,
  89. });
  90. scene.add(new THREE.Mesh(plane, material));
  91. function resizeRendererToDisplaySize(renderer) {
  92. const canvas = renderer.domElement;
  93. const width = canvas.clientWidth;
  94. const height = canvas.clientHeight;
  95. const needResize = canvas.width !== width || canvas.height !== height;
  96. if (needResize) {
  97. renderer.setSize(width, height, false);
  98. }
  99. return needResize;
  100. }
  101. function render(time) {
  102. time *= 0.001; // convert to seconds
  103. resizeRendererToDisplaySize(renderer);
  104. const canvas = renderer.domElement;
  105. uniforms.iResolution.value.set(canvas.width, canvas.height, 1);
  106. uniforms.iTime.value = time;
  107. renderer.render(scene, camera);
  108. requestAnimationFrame(render);
  109. }
  110. requestAnimationFrame(render);
  111. }
  112. main();
  113. </script>
  114. </html>