threejs-shadertoy-as-texture.html 3.9 KB

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