debugging-mcve.html 4.2 KB

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