shadertoy-as-texture.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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({canvas});
  38. const fov = 75;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 5;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. camera.position.z = 2;
  44. const scene = new THREE.Scene();
  45. const boxWidth = 1;
  46. const boxHeight = 1;
  47. const boxDepth = 1;
  48. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  49. const fragmentShader = `
  50. #include <common>
  51. uniform vec3 iResolution;
  52. uniform float iTime;
  53. uniform sampler2D iChannel0;
  54. // By Daedelus: https://www.shadertoy.com/user/Daedelus
  55. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  56. #define TIMESCALE 0.25
  57. #define TILES 8
  58. #define COLOR 0.7, 1.6, 2.8
  59. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  60. {
  61. vec2 uv = fragCoord.xy / iResolution.xy;
  62. uv.x *= iResolution.x / iResolution.y;
  63. vec4 noise = texture2D(iChannel0, floor(uv * float(TILES)) / float(TILES));
  64. float p = 1.0 - mod(noise.r + noise.g + noise.b + iTime * float(TIMESCALE), 1.0);
  65. p = min(max(p * 3.0 - 1.8, 0.1), 2.0);
  66. vec2 r = mod(uv * float(TILES), 1.0);
  67. r = vec2(pow(r.x - 0.5, 2.0), pow(r.y - 0.5, 2.0));
  68. p *= 1.0 - pow(min(1.0, 12.0 * dot(r, r)), 2.0);
  69. fragColor = vec4(COLOR, 1.0) * p;
  70. }
  71. varying vec2 vUv;
  72. void main() {
  73. mainImage(gl_FragColor, vUv * iResolution.xy);
  74. }
  75. `;
  76. const vertexShader = `
  77. varying vec2 vUv;
  78. void main() {
  79. vUv = uv;
  80. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  81. }
  82. `;
  83. const loader = new THREE.TextureLoader();
  84. const texture = loader.load('resources/images/bayer.png');
  85. texture.minFilter = THREE.NearestFilter;
  86. texture.magFilter = THREE.NearestFilter;
  87. texture.wrapS = THREE.RepeatWrapping;
  88. texture.wrapT = THREE.RepeatWrapping;
  89. const uniforms = {
  90. iTime: { value: 0 },
  91. iResolution: { value: new THREE.Vector3(1, 1, 1) },
  92. iChannel0: { value: texture },
  93. };
  94. const material = new THREE.ShaderMaterial({
  95. vertexShader,
  96. fragmentShader,
  97. uniforms,
  98. });
  99. function makeInstance(geometry, x) {
  100. const cube = new THREE.Mesh(geometry, material);
  101. scene.add(cube);
  102. cube.position.x = x;
  103. return cube;
  104. }
  105. const cubes = [
  106. makeInstance(geometry, 0),
  107. makeInstance(geometry, -2),
  108. makeInstance(geometry, 2),
  109. ];
  110. function resizeRendererToDisplaySize(renderer) {
  111. const canvas = renderer.domElement;
  112. const width = canvas.clientWidth;
  113. const height = canvas.clientHeight;
  114. const needResize = canvas.width !== width || canvas.height !== height;
  115. if (needResize) {
  116. renderer.setSize(width, height, false);
  117. }
  118. return needResize;
  119. }
  120. function render(time) {
  121. time *= 0.001; // convert to seconds
  122. if (resizeRendererToDisplaySize(renderer)) {
  123. const canvas = renderer.domElement;
  124. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  125. camera.updateProjectionMatrix();
  126. }
  127. cubes.forEach((cube, ndx) => {
  128. const speed = 1 + ndx * .1;
  129. const rot = time * speed;
  130. cube.rotation.x = rot;
  131. cube.rotation.y = rot;
  132. });
  133. uniforms.iTime.value = time;
  134. renderer.render(scene, camera);
  135. requestAnimationFrame(render);
  136. }
  137. requestAnimationFrame(render);
  138. }
  139. main();
  140. </script>
  141. </html>