threejs-scenegraph-car.html 3.4 KB

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