threejs-load-gltf-rotate-cars-fixed.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 .GLTF - Rotate Cars Fixed</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. <script src="resources/threejs/r103/three.js"></script>
  24. <script src="resources/threejs/r103/js/controls/OrbitControls.js"></script>
  25. <script src="resources/threejs/r103/js/loaders/GLTFLoader.js"></script>
  26. <script src="../3rdparty/dat.gui.min.js"></script>
  27. <script>
  28. 'use strict';
  29. /* global THREE */
  30. function main() {
  31. const canvas = document.querySelector('#c');
  32. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  33. const fov = 45;
  34. const aspect = 2; // the canvas default
  35. const near = 0.1;
  36. const far = 100;
  37. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  38. camera.position.set(0, 10, 20);
  39. const controls = new THREE.OrbitControls(camera, canvas);
  40. controls.target.set(0, 5, 0);
  41. controls.update();
  42. const scene = new THREE.Scene();
  43. scene.background = new THREE.Color('black');
  44. {
  45. const planeSize = 40;
  46. const loader = new THREE.TextureLoader();
  47. const texture = loader.load('resources/images/checker.png');
  48. texture.wrapS = THREE.RepeatWrapping;
  49. texture.wrapT = THREE.RepeatWrapping;
  50. texture.magFilter = THREE.NearestFilter;
  51. const repeats = planeSize / 2;
  52. texture.repeat.set(repeats, repeats);
  53. const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
  54. const planeMat = new THREE.MeshPhongMaterial({
  55. map: texture,
  56. side: THREE.DoubleSide,
  57. });
  58. const mesh = new THREE.Mesh(planeGeo, planeMat);
  59. mesh.rotation.x = Math.PI * -.5;
  60. scene.add(mesh);
  61. }
  62. {
  63. const skyColor = 0xB1E1FF; // light blue
  64. const groundColor = 0xB97A20; // brownish orange
  65. const intensity = 1;
  66. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  67. scene.add(light);
  68. }
  69. {
  70. const color = 0xFFFFFF;
  71. const intensity = 1;
  72. const light = new THREE.DirectionalLight(color, intensity);
  73. light.position.set(5, 10, 2);
  74. scene.add(light);
  75. scene.add(light.target);
  76. }
  77. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  78. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  79. const halfFovY = THREE.Math.degToRad(camera.fov * .5);
  80. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  81. // compute a unit vector that points in the direction the camera is now
  82. // in the xz plane from the center of the box
  83. const direction = (new THREE.Vector3())
  84. .subVectors(camera.position, boxCenter)
  85. .multiply(new THREE.Vector3(1, 0, 1))
  86. .normalize();
  87. // move the camera to a position distance units way from the center
  88. // in whatever direction the camera was from the center already
  89. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  90. // pick some near and far values for the frustum that
  91. // will contain the box.
  92. camera.near = boxSize / 100;
  93. camera.far = boxSize * 100;
  94. camera.updateProjectionMatrix();
  95. // point the camera to look at the center of the box
  96. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  97. }
  98. const cars = [];
  99. {
  100. const gltfLoader = new THREE.GLTFLoader();
  101. gltfLoader.load('resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', (gltf) => {
  102. const root = gltf.scene;
  103. scene.add(root);
  104. const loadedCars = root.getObjectByName('Cars');
  105. const fixes = [
  106. { prefix: 'Car_08', rot: [Math.PI * .5, 0, Math.PI * .5], },
  107. { prefix: 'CAR_03', rot: [0, Math.PI, 0], },
  108. { prefix: 'Car_04', rot: [0, Math.PI, 0], },
  109. ];
  110. root.updateMatrixWorld();
  111. for (const car of loadedCars.children.slice()) {
  112. const fix = fixes.find(fix => car.name.startsWith(fix.prefix));
  113. const obj = new THREE.Object3D();
  114. car.getWorldPosition(obj.position);
  115. car.position.set(0, 0, 0);
  116. car.rotation.set(...fix.rot);
  117. obj.add(car);
  118. scene.add(obj);
  119. cars.push(obj);
  120. }
  121. // compute the box that contains all the stuff
  122. // from root and below
  123. const box = new THREE.Box3().setFromObject(root);
  124. const boxSize = box.getSize(new THREE.Vector3()).length();
  125. const boxCenter = box.getCenter(new THREE.Vector3());
  126. // set the camera to frame the box
  127. frameArea(boxSize * 0.5, boxSize, boxCenter, camera);
  128. // update the Trackball controls to handle the new size
  129. controls.maxDistance = boxSize * 10;
  130. controls.target.copy(boxCenter);
  131. controls.update();
  132. });
  133. }
  134. function resizeRendererToDisplaySize(renderer) {
  135. const canvas = renderer.domElement;
  136. const width = canvas.clientWidth;
  137. const height = canvas.clientHeight;
  138. const needResize = canvas.width !== width || canvas.height !== height;
  139. if (needResize) {
  140. renderer.setSize(width, height, false);
  141. }
  142. return needResize;
  143. }
  144. function render(time) {
  145. time *= 0.001; // convert to seconds
  146. if (resizeRendererToDisplaySize(renderer)) {
  147. const canvas = renderer.domElement;
  148. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  149. camera.updateProjectionMatrix();
  150. }
  151. for (const car of cars) {
  152. car.rotation.y = time;
  153. }
  154. renderer.render(scene, camera);
  155. requestAnimationFrame(render);
  156. }
  157. requestAnimationFrame(render);
  158. }
  159. main();
  160. </script>
  161. </html>