load-gltf-car-path.html 7.2 KB

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