render-on-demand.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 - Rendering on Demand</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" tabindex="1"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. function main() {
  35. const canvas = document.querySelector( '#c' );
  36. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  37. const fov = 75;
  38. const aspect = 2; // the canvas default
  39. const near = 0.1;
  40. const far = 5;
  41. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  42. camera.position.z = 2;
  43. const controls = new OrbitControls( camera, canvas );
  44. controls.target.set( 0, 0, 0 );
  45. controls.update();
  46. const scene = new THREE.Scene();
  47. {
  48. const color = 0xFFFFFF;
  49. const intensity = 3;
  50. const light = new THREE.DirectionalLight( color, intensity );
  51. light.position.set( - 1, 2, 4 );
  52. scene.add( light );
  53. }
  54. const boxWidth = 1;
  55. const boxHeight = 1;
  56. const boxDepth = 1;
  57. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  58. function makeInstance( geometry, color, x ) {
  59. const material = new THREE.MeshPhongMaterial( { color } );
  60. const cube = new THREE.Mesh( geometry, material );
  61. scene.add( cube );
  62. cube.position.x = x;
  63. return cube;
  64. }
  65. makeInstance( geometry, 0x44aa88, 0 );
  66. makeInstance( geometry, 0x8844aa, - 2 );
  67. makeInstance( geometry, 0xaa8844, 2 );
  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() {
  79. if ( resizeRendererToDisplaySize( renderer ) ) {
  80. const canvas = renderer.domElement;
  81. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  82. camera.updateProjectionMatrix();
  83. }
  84. renderer.render( scene, camera );
  85. }
  86. render();
  87. controls.addEventListener( 'change', render );
  88. window.addEventListener( 'resize', render );
  89. // note: this is a workaround for an OrbitControls issue
  90. // in an iframe. Will remove once the issue is fixed in
  91. // three.js
  92. window.addEventListener( 'mousedown', ( e ) => {
  93. e.preventDefault();
  94. window.focus();
  95. } );
  96. window.addEventListener( 'keydown', ( e ) => {
  97. e.preventDefault();
  98. } );
  99. }
  100. main();
  101. </script>
  102. </html>