scenegraph-car.html 3.4 KB

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