2
0

render-on-demand.html 2.8 KB

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