threejs-shadertoy-bleepy-blocks.html 3.2 KB

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