shadertoy-basic.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Basic</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.autoClearColor = false;
  39. const camera = new THREE.OrthographicCamera(
  40. - 1, // left
  41. 1, // right
  42. 1, // top
  43. - 1, // bottom
  44. - 1, // near,
  45. 1, // far
  46. );
  47. const scene = new THREE.Scene();
  48. const plane = new THREE.PlaneGeometry( 2, 2 );
  49. const fragmentShader = `
  50. #include <common>
  51. uniform vec3 iResolution;
  52. uniform float iTime;
  53. // By iq: https://www.shadertoy.com/user/iq
  54. // license: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  55. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  56. {
  57. // Normalized pixel coordinates (from 0 to 1)
  58. vec2 uv = fragCoord/iResolution.xy;
  59. // Time varying pixel color
  60. vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));
  61. // Output to screen
  62. fragColor = vec4(col,1.0);
  63. }
  64. void main() {
  65. mainImage(gl_FragColor, gl_FragCoord.xy);
  66. }
  67. `;
  68. const uniforms = {
  69. iTime: { value: 0 },
  70. iResolution: { value: new THREE.Vector3() },
  71. };
  72. const material = new THREE.ShaderMaterial( {
  73. fragmentShader,
  74. uniforms,
  75. } );
  76. scene.add( new THREE.Mesh( plane, material ) );
  77. function resizeRendererToDisplaySize( renderer ) {
  78. const canvas = renderer.domElement;
  79. const width = canvas.clientWidth;
  80. const height = canvas.clientHeight;
  81. const needResize = canvas.width !== width || canvas.height !== height;
  82. if ( needResize ) {
  83. renderer.setSize( width, height, false );
  84. }
  85. return needResize;
  86. }
  87. function render( time ) {
  88. time *= 0.001; // convert to seconds
  89. resizeRendererToDisplaySize( renderer );
  90. const canvas = renderer.domElement;
  91. uniforms.iResolution.value.set( canvas.width, canvas.height, 1 );
  92. uniforms.iTime.value = time;
  93. renderer.render( scene, camera );
  94. requestAnimationFrame( render );
  95. }
  96. requestAnimationFrame( render );
  97. }
  98. main();
  99. </script>
  100. </html>