threejs-shadertoy-as-texture.html 3.9 KB

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