load-obj-auto-camera.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 - Load .OBJ - Auto Framing Camera</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  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. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. import {OBJLoader} from '../../examples/jsm/loaders/OBJLoader.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. const fov = 45;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 100;
  44. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  45. camera.position.set(0, 10, 20);
  46. const controls = new OrbitControls(camera, canvas);
  47. controls.target.set(0, 5, 0);
  48. controls.update();
  49. const scene = new THREE.Scene();
  50. scene.background = new THREE.Color('black');
  51. {
  52. const planeSize = 40;
  53. const loader = new THREE.TextureLoader();
  54. const texture = loader.load('resources/images/checker.png');
  55. texture.wrapS = THREE.RepeatWrapping;
  56. texture.wrapT = THREE.RepeatWrapping;
  57. texture.magFilter = THREE.NearestFilter;
  58. const repeats = planeSize / 2;
  59. texture.repeat.set(repeats, repeats);
  60. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  61. const planeMat = new THREE.MeshPhongMaterial({
  62. map: texture,
  63. side: THREE.DoubleSide,
  64. });
  65. const mesh = new THREE.Mesh(planeGeo, planeMat);
  66. mesh.rotation.x = Math.PI * -.5;
  67. scene.add(mesh);
  68. }
  69. {
  70. const skyColor = 0xB1E1FF; // light blue
  71. const groundColor = 0xB97A20; // brownish orange
  72. const intensity = 1;
  73. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  74. scene.add(light);
  75. }
  76. {
  77. const color = 0xFFFFFF;
  78. const intensity = 1;
  79. const light = new THREE.DirectionalLight(color, intensity);
  80. light.position.set(5, 10, 2);
  81. scene.add(light);
  82. scene.add(light.target);
  83. }
  84. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  85. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  86. const halfFovY = THREE.MathUtils.degToRad(camera.fov * .5);
  87. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  88. // compute a unit vector that points in the direction the camera is now
  89. // from the center of the box
  90. const direction = (new THREE.Vector3()).subVectors(camera.position, boxCenter).normalize();
  91. // move the camera to a position distance units way from the center
  92. // in whatever direction the camera was from the center already
  93. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  94. // pick some near and far values for the frustum that
  95. // will contain the box.
  96. camera.near = boxSize / 100;
  97. camera.far = boxSize * 100;
  98. camera.updateProjectionMatrix();
  99. // point the camera to look at the center of the box
  100. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  101. }
  102. {
  103. const objLoader = new OBJLoader();
  104. objLoader.load('resources/models/windmill_2/windmill.obj', (root) => {
  105. scene.add(root);
  106. // compute the box that contains all the stuff
  107. // from root and below
  108. const box = new THREE.Box3().setFromObject(root);
  109. const boxSize = box.getSize(new THREE.Vector3()).length();
  110. const boxCenter = box.getCenter(new THREE.Vector3());
  111. // set the camera to frame the box
  112. frameArea(boxSize * 1.2, boxSize, boxCenter, camera);
  113. // update the Trackball controls to handle the new size
  114. controls.maxDistance = boxSize * 10;
  115. controls.target.copy(boxCenter);
  116. controls.update();
  117. });
  118. }
  119. function resizeRendererToDisplaySize(renderer) {
  120. const canvas = renderer.domElement;
  121. const width = canvas.clientWidth;
  122. const height = canvas.clientHeight;
  123. const needResize = canvas.width !== width || canvas.height !== height;
  124. if (needResize) {
  125. renderer.setSize(width, height, false);
  126. }
  127. return needResize;
  128. }
  129. function render() {
  130. if (resizeRendererToDisplaySize(renderer)) {
  131. const canvas = renderer.domElement;
  132. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  133. camera.updateProjectionMatrix();
  134. }
  135. renderer.render(scene, camera);
  136. requestAnimationFrame(render);
  137. }
  138. requestAnimationFrame(render);
  139. }
  140. main();
  141. </script>
  142. </html>