scenegraph-car.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Scenegraph - Car</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script src="resources/threejs-lessons-helper.js"></script> <!-- you can and should delete this script. it is only used on the site to help with errors -->
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../../build/three.module.js"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. function main() {
  34. const canvas = document.querySelector( '#c' );
  35. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  36. renderer.setClearColor( 0xAAAAAA );
  37. const fov = 40;
  38. const aspect = 2; // the canvas default
  39. const near = 0.1;
  40. const far = 1000;
  41. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  42. camera.position.set( 8, 4, 10 );
  43. camera.lookAt( 0, 0, 0 );
  44. const scene = new THREE.Scene();
  45. {
  46. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  47. light.position.set( - 1, 2, 4 );
  48. scene.add( light );
  49. }
  50. {
  51. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  52. light.position.set( 1, - 2, - 4 );
  53. scene.add( light );
  54. }
  55. const carWidth = 4;
  56. const carHeight = 1;
  57. const carLength = 8;
  58. const bodyGeometry = new THREE.BoxGeometry( carWidth, carHeight, carLength );
  59. const bodyMaterial = new THREE.MeshPhongMaterial( { color: 0x6688AA } );
  60. const bodyMesh = new THREE.Mesh( bodyGeometry, bodyMaterial );
  61. scene.add( bodyMesh );
  62. const wheelRadius = 1;
  63. const wheelThickness = .5;
  64. const wheelSegments = 6;
  65. const wheelGeometry = new THREE.CylinderGeometry(
  66. wheelRadius, // top radius
  67. wheelRadius, // bottom radius
  68. wheelThickness, // height of cylinder
  69. wheelSegments );
  70. const wheelMaterial = new THREE.MeshPhongMaterial( { color: 0x888888 } );
  71. const wheelPositions = [
  72. [ - carWidth / 2 + wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  73. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  74. [ - carWidth / 2 + wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  75. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  76. ];
  77. const wheelMeshes = wheelPositions.map( ( position ) => {
  78. const mesh = new THREE.Mesh( wheelGeometry, wheelMaterial );
  79. mesh.position.set( ...position );
  80. mesh.rotation.z = Math.PI * .5;
  81. scene.add( mesh );
  82. return mesh;
  83. } );
  84. function resizeRendererToDisplaySize( renderer ) {
  85. const canvas = renderer.domElement;
  86. const width = canvas.clientWidth;
  87. const height = canvas.clientHeight;
  88. const needResize = canvas.width !== width || canvas.height !== height;
  89. if ( needResize ) {
  90. renderer.setSize( width, height, false );
  91. }
  92. return needResize;
  93. }
  94. // const carPosition = [0, 0, 0];
  95. function render( time ) {
  96. time *= 0.001;
  97. if ( resizeRendererToDisplaySize( renderer ) ) {
  98. const canvas = renderer.domElement;
  99. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  100. camera.updateProjectionMatrix();
  101. }
  102. wheelMeshes.forEach( ( obj ) => {
  103. obj.rotation.x = time;
  104. } );
  105. renderer.render( scene, camera );
  106. requestAnimationFrame( render );
  107. }
  108. requestAnimationFrame( render );
  109. }
  110. main();
  111. </script>
  112. </html>