threejs-shadertoy-basic.html 2.5 KB

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