shadertoy-bleepy-blocks.html 3.4 KB

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