shadertoy-as-texture.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 - Procedural Texture</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. const fov = 75;
  36. const aspect = 2; // the canvas default
  37. const near = 0.1;
  38. const far = 5;
  39. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  40. camera.position.z = 2;
  41. const scene = new THREE.Scene();
  42. const boxWidth = 1;
  43. const boxHeight = 1;
  44. const boxDepth = 1;
  45. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  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. varying vec2 vUv;
  69. void main() {
  70. mainImage(gl_FragColor, vUv * iResolution.xy);
  71. }
  72. `;
  73. const vertexShader = `
  74. varying vec2 vUv;
  75. void main() {
  76. vUv = uv;
  77. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  78. }
  79. `;
  80. const loader = new THREE.TextureLoader();
  81. const texture = loader.load( 'resources/images/bayer.png' );
  82. texture.minFilter = THREE.NearestFilter;
  83. texture.magFilter = THREE.NearestFilter;
  84. texture.wrapS = THREE.RepeatWrapping;
  85. texture.wrapT = THREE.RepeatWrapping;
  86. const uniforms = {
  87. iTime: { value: 0 },
  88. iResolution: { value: new THREE.Vector3( 1, 1, 1 ) },
  89. iChannel0: { value: texture },
  90. };
  91. const material = new THREE.ShaderMaterial( {
  92. vertexShader,
  93. fragmentShader,
  94. uniforms,
  95. } );
  96. function makeInstance( geometry, x ) {
  97. const cube = new THREE.Mesh( geometry, material );
  98. scene.add( cube );
  99. cube.position.x = x;
  100. return cube;
  101. }
  102. const cubes = [
  103. makeInstance( geometry, 0 ),
  104. makeInstance( geometry, - 2 ),
  105. makeInstance( geometry, 2 ),
  106. ];
  107. function resizeRendererToDisplaySize( renderer ) {
  108. const canvas = renderer.domElement;
  109. const width = canvas.clientWidth;
  110. const height = canvas.clientHeight;
  111. const needResize = canvas.width !== width || canvas.height !== height;
  112. if ( needResize ) {
  113. renderer.setSize( width, height, false );
  114. }
  115. return needResize;
  116. }
  117. function render( time ) {
  118. time *= 0.001; // convert to seconds
  119. if ( resizeRendererToDisplaySize( renderer ) ) {
  120. const canvas = renderer.domElement;
  121. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  122. camera.updateProjectionMatrix();
  123. }
  124. cubes.forEach( ( cube, ndx ) => {
  125. const speed = 1 + ndx * .1;
  126. const rot = time * speed;
  127. cube.rotation.x = rot;
  128. cube.rotation.y = rot;
  129. } );
  130. uniforms.iTime.value = time;
  131. renderer.render( scene, camera );
  132. requestAnimationFrame( render );
  133. }
  134. requestAnimationFrame( render );
  135. }
  136. main();
  137. </script>
  138. </html>