debugging-mcve.html 4.2 KB

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