shadertoy-as-texture.html 3.9 KB

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