debugging-mcve.html 3.9 KB

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