shadertoy-bleepy-blocks.html 3.2 KB

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