shadertoy-prep.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. <!-- 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.useLegacyLights = false;
  39. renderer.autoClearColor = false;
  40. const camera = new THREE.OrthographicCamera(
  41. - 1, // left
  42. 1, // right
  43. 1, // top
  44. - 1, // bottom
  45. - 1, // near,
  46. 1, // far
  47. );
  48. const scene = new THREE.Scene();
  49. const plane = new THREE.PlaneGeometry( 2, 2 );
  50. const material = new THREE.MeshBasicMaterial( {
  51. color: 'red',
  52. } );
  53. scene.add( new THREE.Mesh( plane, material ) );
  54. function resizeRendererToDisplaySize( renderer ) {
  55. const canvas = renderer.domElement;
  56. const width = canvas.clientWidth;
  57. const height = canvas.clientHeight;
  58. const needResize = canvas.width !== width || canvas.height !== height;
  59. if ( needResize ) {
  60. renderer.setSize( width, height, false );
  61. }
  62. return needResize;
  63. }
  64. function render() {
  65. resizeRendererToDisplaySize( renderer );
  66. renderer.render( scene, camera );
  67. requestAnimationFrame( render );
  68. }
  69. requestAnimationFrame( render );
  70. }
  71. main();
  72. </script>
  73. </html>