shadertoy-prep.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Prep</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 material = new THREE.MeshBasicMaterial( {
  47. color: 'red',
  48. } );
  49. scene.add( new THREE.Mesh( plane, material ) );
  50. function resizeRendererToDisplaySize( renderer ) {
  51. const canvas = renderer.domElement;
  52. const width = canvas.clientWidth;
  53. const height = canvas.clientHeight;
  54. const needResize = canvas.width !== width || canvas.height !== height;
  55. if ( needResize ) {
  56. renderer.setSize( width, height, false );
  57. }
  58. return needResize;
  59. }
  60. function render() {
  61. resizeRendererToDisplaySize( renderer );
  62. renderer.render( scene, camera );
  63. requestAnimationFrame( render );
  64. }
  65. requestAnimationFrame( render );
  66. }
  67. main();
  68. </script>
  69. </html>