shadertoy-as-texture.html 4.2 KB

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