scenegraph-car.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. <!-- Import maps polyfill -->
  25. <!-- Remove this when import maps will be widely supported -->
  26. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  27. <script type="importmap">
  28. {
  29. "imports": {
  30. "three": "../../build/three.module.js"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. function main() {
  37. const canvas = document.querySelector( '#c' );
  38. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  39. renderer.useLegacyLights = false;
  40. renderer.setClearColor( 0xAAAAAA );
  41. const fov = 40;
  42. const aspect = 2; // the canvas default
  43. const near = 0.1;
  44. const far = 1000;
  45. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  46. camera.position.set( 8, 4, 10 );
  47. camera.lookAt( 0, 0, 0 );
  48. const scene = new THREE.Scene();
  49. {
  50. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  51. light.position.set( - 1, 2, 4 );
  52. scene.add( light );
  53. }
  54. {
  55. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  56. light.position.set( 1, - 2, - 4 );
  57. scene.add( light );
  58. }
  59. const carWidth = 4;
  60. const carHeight = 1;
  61. const carLength = 8;
  62. const bodyGeometry = new THREE.BoxGeometry( carWidth, carHeight, carLength );
  63. const bodyMaterial = new THREE.MeshPhongMaterial( { color: 0x6688AA } );
  64. const bodyMesh = new THREE.Mesh( bodyGeometry, bodyMaterial );
  65. scene.add( bodyMesh );
  66. const wheelRadius = 1;
  67. const wheelThickness = .5;
  68. const wheelSegments = 6;
  69. const wheelGeometry = new THREE.CylinderGeometry(
  70. wheelRadius, // top radius
  71. wheelRadius, // bottom radius
  72. wheelThickness, // height of cylinder
  73. wheelSegments );
  74. const wheelMaterial = new THREE.MeshPhongMaterial( { color: 0x888888 } );
  75. const wheelPositions = [
  76. [ - carWidth / 2 + wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  77. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, carLength / 3 ],
  78. [ - carWidth / 2 + wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  79. [ carWidth / 2 + wheelThickness / 2, - carHeight / 2, - carLength / 3 ],
  80. ];
  81. const wheelMeshes = wheelPositions.map( ( position ) => {
  82. const mesh = new THREE.Mesh( wheelGeometry, wheelMaterial );
  83. mesh.position.set( ...position );
  84. mesh.rotation.z = Math.PI * .5;
  85. scene.add( mesh );
  86. return mesh;
  87. } );
  88. function resizeRendererToDisplaySize( renderer ) {
  89. const canvas = renderer.domElement;
  90. const width = canvas.clientWidth;
  91. const height = canvas.clientHeight;
  92. const needResize = canvas.width !== width || canvas.height !== height;
  93. if ( needResize ) {
  94. renderer.setSize( width, height, false );
  95. }
  96. return needResize;
  97. }
  98. // const carPosition = [0, 0, 0];
  99. function render( time ) {
  100. time *= 0.001;
  101. if ( resizeRendererToDisplaySize( renderer ) ) {
  102. const canvas = renderer.domElement;
  103. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  104. camera.updateProjectionMatrix();
  105. }
  106. wheelMeshes.forEach( ( obj ) => {
  107. obj.rotation.x = time;
  108. } );
  109. renderer.render( scene, camera );
  110. requestAnimationFrame( render );
  111. }
  112. requestAnimationFrame( render );
  113. }
  114. main();
  115. </script>
  116. </html>