shadertoy-basic.html 2.6 KB

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