threejs-load-gltf-car-path-fixed.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 - Car Path 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 type="module">
  24. import * as THREE from './resources/threejs/r108/build/three.module.js';
  25. import {OrbitControls} from './resources/threejs/r108/examples/jsm/controls/OrbitControls.js';
  26. import {GLTFLoader} from './resources/threejs/r108/examples/jsm/loaders/GLTFLoader.js';
  27. import { GUI } from '../3rdparty/dat.gui.module.js';
  28. function main() {
  29. const canvas = document.querySelector('#c');
  30. const renderer = new THREE.WebGLRenderer({canvas});
  31. const fov = 45;
  32. const aspect = 2; // the canvas default
  33. const near = 0.1;
  34. const far = 100;
  35. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  36. camera.position.set(0, 10, 20);
  37. const controls = new OrbitControls(camera, canvas);
  38. controls.target.set(0, 5, 0);
  39. controls.update();
  40. const scene = new THREE.Scene();
  41. scene.background = new THREE.Color('black');
  42. {
  43. const planeSize = 40;
  44. const loader = new THREE.TextureLoader();
  45. const texture = loader.load('resources/images/checker.png');
  46. texture.wrapS = THREE.RepeatWrapping;
  47. texture.wrapT = THREE.RepeatWrapping;
  48. texture.magFilter = THREE.NearestFilter;
  49. const repeats = planeSize / 2;
  50. texture.repeat.set(repeats, repeats);
  51. const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
  52. const planeMat = new THREE.MeshPhongMaterial({
  53. map: texture,
  54. side: THREE.DoubleSide,
  55. });
  56. const mesh = new THREE.Mesh(planeGeo, planeMat);
  57. mesh.rotation.x = Math.PI * -.5;
  58. scene.add(mesh);
  59. }
  60. {
  61. const skyColor = 0xB1E1FF; // light blue
  62. const groundColor = 0xB97A20; // brownish orange
  63. const intensity = 1;
  64. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  65. scene.add(light);
  66. }
  67. {
  68. const color = 0xFFFFFF;
  69. const intensity = 1;
  70. const light = new THREE.DirectionalLight(color, intensity);
  71. light.position.set(5, 10, 2);
  72. scene.add(light);
  73. scene.add(light.target);
  74. }
  75. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  76. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  77. const halfFovY = THREE.Math.degToRad(camera.fov * .5);
  78. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  79. // compute a unit vector that points in the direction the camera is now
  80. // in the xz plane from the center of the box
  81. const direction = (new THREE.Vector3())
  82. .subVectors(camera.position, boxCenter)
  83. .multiply(new THREE.Vector3(1, 0, 1))
  84. .normalize();
  85. // move the camera to a position distance units way from the center
  86. // in whatever direction the camera was from the center already
  87. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  88. // pick some near and far values for the frustum that
  89. // will contain the box.
  90. camera.near = boxSize / 100;
  91. camera.far = boxSize * 100;
  92. camera.updateProjectionMatrix();
  93. // point the camera to look at the center of the box
  94. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  95. }
  96. let curve;
  97. let curveObject;
  98. {
  99. const controlPoints = [
  100. [1.118281, 5.115846, -3.681386],
  101. [3.948875, 5.115846, -3.641834],
  102. [3.960072, 5.115846, -0.240352],
  103. [3.985447, 5.115846, 4.585005],
  104. [-3.793631, 5.115846, 4.585006],
  105. [-3.826839, 5.115846, -14.736200],
  106. [-14.542292, 5.115846, -14.765865],
  107. [-14.520929, 5.115846, -3.627002],
  108. [-5.452815, 5.115846, -3.634418],
  109. [-5.467251, 5.115846, 4.549161],
  110. [-13.266233, 5.115846, 4.567083],
  111. [-13.250067, 5.115846, -13.499271],
  112. [4.081842, 5.115846, -13.435463],
  113. [4.125436, 5.115846, -5.334928],
  114. [-14.521364, 5.115846, -5.239871],
  115. [-14.510466, 5.115846, 5.486727],
  116. [5.745666, 5.115846, 5.510492],
  117. [5.787942, 5.115846, -14.728308],
  118. [-5.423720, 5.115846, -14.761919],
  119. [-5.373599, 5.115846, -3.704133],
  120. [1.004861, 5.115846, -3.641834],
  121. ];
  122. const p0 = new THREE.Vector3();
  123. const p1 = new THREE.Vector3();
  124. curve = new THREE.CatmullRomCurve3(
  125. controlPoints.map((p, ndx) => {
  126. p0.set(...p);
  127. p1.set(...controlPoints[(ndx + 1) % controlPoints.length]);
  128. return [
  129. (new THREE.Vector3()).copy(p0),
  130. (new THREE.Vector3()).lerpVectors(p0, p1, 0.1),
  131. (new THREE.Vector3()).lerpVectors(p0, p1, 0.9),
  132. ];
  133. }).flat(),
  134. true,
  135. );
  136. {
  137. const points = curve.getPoints(250);
  138. const geometry = new THREE.BufferGeometry().setFromPoints(points);
  139. const material = new THREE.LineBasicMaterial({color: 0xff0000});
  140. curveObject = new THREE.Line(geometry, material);
  141. curveObject.scale.set(100, 100, 100);
  142. curveObject.position.y = -621;
  143. //curveObject.visible = false;
  144. material.depthTest = false;
  145. curveObject.renderOrder = 1;
  146. scene.add(curveObject);
  147. }
  148. }
  149. const cars = [];
  150. {
  151. const gltfLoader = new GLTFLoader();
  152. gltfLoader.load('resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf', (gltf) => {
  153. const root = gltf.scene;
  154. scene.add(root);
  155. const loadedCars = root.getObjectByName('Cars');
  156. const fixes = [
  157. { prefix: 'Car_08', rot: [Math.PI * .5, 0, Math.PI * .5], },
  158. { prefix: 'CAR_03', rot: [0, Math.PI, 0], },
  159. { prefix: 'Car_04', rot: [0, Math.PI, 0], },
  160. ];
  161. root.updateMatrixWorld();
  162. for (const car of loadedCars.children.slice()) {
  163. const fix = fixes.find(fix => car.name.startsWith(fix.prefix));
  164. const obj = new THREE.Object3D();
  165. car.getWorldPosition(obj.position);
  166. car.position.set(0, 0, 0);
  167. car.rotation.set(...fix.rot);
  168. obj.add(car);
  169. scene.add(obj);
  170. cars.push(obj);
  171. }
  172. // compute the box that contains all the stuff
  173. // from root and below
  174. const box = new THREE.Box3().setFromObject(root);
  175. const boxSize = box.getSize(new THREE.Vector3()).length();
  176. const boxCenter = box.getCenter(new THREE.Vector3());
  177. // set the camera to frame the box
  178. frameArea(boxSize * 0.5, boxSize, boxCenter, camera);
  179. // update the Trackball controls to handle the new size
  180. controls.maxDistance = boxSize * 10;
  181. controls.target.copy(boxCenter);
  182. controls.update();
  183. });
  184. }
  185. function resizeRendererToDisplaySize(renderer) {
  186. const canvas = renderer.domElement;
  187. const width = canvas.clientWidth;
  188. const height = canvas.clientHeight;
  189. const needResize = canvas.width !== width || canvas.height !== height;
  190. if (needResize) {
  191. renderer.setSize(width, height, false);
  192. }
  193. return needResize;
  194. }
  195. function render(time) {
  196. time *= 0.001; // convert to seconds
  197. if (resizeRendererToDisplaySize(renderer)) {
  198. const canvas = renderer.domElement;
  199. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  200. camera.updateProjectionMatrix();
  201. }
  202. for (const car of cars) {
  203. car.rotation.y = time;
  204. }
  205. renderer.render(scene, camera);
  206. requestAnimationFrame(render);
  207. }
  208. requestAnimationFrame(render);
  209. }
  210. main();
  211. </script>
  212. </html>