threejs-shadertoy-bleepy-blocks.html 3.2 KB

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