debugging-mcve.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <body>
  2. <canvas id="c"></canvas>
  3. </body>
  4. <!-- Import maps polyfill -->
  5. <!-- Remove this when import maps will be widely supported -->
  6. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  7. <script type="importmap">
  8. {
  9. "imports": {
  10. "three": "../../build/three.module.js",
  11. "three/addons/": "../../examples/jsm/"
  12. }
  13. }
  14. </script>
  15. <script type="module">
  16. import * as THREE from 'three';
  17. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  18. function main() {
  19. const canvas = document.querySelector('#c');
  20. const renderer = new THREE.WebGLRenderer({canvas});
  21. const fov = 45;
  22. const aspect = 2; // the canvas default
  23. const near = 0.1;
  24. const far = 10000;
  25. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  26. camera.position.set(0, 1000, 2000);
  27. const controls = new OrbitControls(camera, canvas);
  28. controls.target.set(0, 5, 0);
  29. controls.update();
  30. const scene = new THREE.Scene();
  31. scene.background = new THREE.Color('black');
  32. scene.add(new THREE.GridHelper(5000, 10));
  33. let curve;
  34. let curveObject;
  35. {
  36. const controlPoints = [
  37. [1.118281, 5.115846, -3.681386],
  38. [3.948875, 5.115846, -3.641834],
  39. [3.960072, 5.115846, -0.240352],
  40. [3.985447, 5.115846, 4.585005],
  41. [-3.793631, 5.115846, 4.585006],
  42. [-3.826839, 5.115846, -14.736200],
  43. [-14.542292, 5.115846, -14.765865],
  44. [-14.520929, 5.115846, -3.627002],
  45. [-5.452815, 5.115846, -3.634418],
  46. [-5.467251, 5.115846, 4.549161],
  47. [-13.266233, 5.115846, 4.567083],
  48. [-13.250067, 5.115846, -13.499271],
  49. [4.081842, 5.115846, -13.435463],
  50. [4.125436, 5.115846, -5.334928],
  51. [-14.521364, 5.115846, -5.239871],
  52. [-14.510466, 5.115846, 5.486727],
  53. [5.745666, 5.115846, 5.510492],
  54. [5.787942, 5.115846, -14.728308],
  55. [-5.423720, 5.115846, -14.761919],
  56. [-5.373599, 5.115846, -3.704133],
  57. [1.004861, 5.115846, -3.641834],
  58. ];
  59. const p0 = new THREE.Vector3();
  60. const p1 = new THREE.Vector3();
  61. curve = new THREE.CatmullRomCurve3(
  62. controlPoints.map((p, ndx) => {
  63. p0.set(...p);
  64. p1.set(...controlPoints[(ndx + 1) % controlPoints.length]);
  65. return [
  66. (new THREE.Vector3()).copy(p0),
  67. (new THREE.Vector3()).lerpVectors(p0, p1, 0.1),
  68. (new THREE.Vector3()).lerpVectors(p0, p1, 0.9),
  69. ];
  70. }).flat(),
  71. true,
  72. );
  73. {
  74. const points = curve.getPoints(250);
  75. const geometry = new THREE.BufferGeometry().setFromPoints(points);
  76. const material = new THREE.LineBasicMaterial({color: 0xff0000});
  77. curveObject = new THREE.Line(geometry, material);
  78. curveObject.scale.set(100, 100, 100);
  79. curveObject.position.y = -621;
  80. material.depthTest = false;
  81. curveObject.renderOrder = 1;
  82. scene.add(curveObject);
  83. }
  84. }
  85. const geometry = new THREE.BoxGeometry(100, 100, 300);
  86. const material = new THREE.MeshBasicMaterial({color: 'cyan'});
  87. const cars = [];
  88. for (let i = 0; i < 10; ++i) {
  89. const mesh = new THREE.Mesh(geometry, material);
  90. scene.add(mesh);
  91. cars.push(mesh);
  92. }
  93. // create 2 Vector3s we can use for path calculations
  94. const carPosition = new THREE.Vector3();
  95. const carTarget = new THREE.Vector3();
  96. function render(time) {
  97. time *= 0.001; // convert to seconds
  98. {
  99. const pathTime = time * .01;
  100. const targetOffset = 0.01;
  101. cars.forEach((car, ndx) => {
  102. // a number between 0 and 1 to evenly space the cars
  103. const u = pathTime + ndx / cars.length;
  104. // get the first point
  105. curve.getPointAt(u % 1, carPosition);
  106. carPosition.applyMatrix4(curveObject.matrixWorld);
  107. // get a second point slightly further down the curve
  108. curve.getPointAt((u + targetOffset) % 1, carTarget);
  109. carTarget.applyMatrix4(curveObject.matrixWorld);
  110. // put the car at the first point (temporarily)
  111. car.position.copy(carPosition);
  112. // point the car the second point
  113. car.lookAt(carTarget);
  114. // put the car between the 2 points
  115. car.position.lerpVectors(carPosition, carTarget, 0.5);
  116. });
  117. }
  118. renderer.render(scene, camera);
  119. requestAnimationFrame(render);
  120. }
  121. requestAnimationFrame(render);
  122. }
  123. main();
  124. </script>