1
0

debugging-mcve.html 4.0 KB

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