threejs-debugging-mcve.html 4.0 KB

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