threejs-shadertoy-basic.html 2.5 KB

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