scenegraph-car.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.setClearColor(0xAAAAAA);
  40. const fov = 40;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 1000;
  44. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  45. camera.position.set(8, 4, 10);
  46. camera.lookAt(0, 0, 0);
  47. const scene = new THREE.Scene();
  48. {
  49. const light = new THREE.DirectionalLight(0xffffff, 1);
  50. light.position.set(-1, 2, 4);
  51. scene.add(light);
  52. }
  53. {
  54. const light = new THREE.DirectionalLight(0xffffff, 1);
  55. light.position.set(1, -2, -4);
  56. scene.add(light);
  57. }
  58. const carWidth = 4;
  59. const carHeight = 1;
  60. const carLength = 8;
  61. const bodyGeometry = new THREE.BoxGeometry(carWidth, carHeight, carLength);
  62. const bodyMaterial = new THREE.MeshPhongMaterial({color: 0x6688AA});
  63. const bodyMesh = new THREE.Mesh(bodyGeometry, bodyMaterial);
  64. scene.add(bodyMesh);
  65. const wheelRadius = 1;
  66. const wheelThickness = .5;
  67. const wheelSegments = 6;
  68. const wheelGeometry = new THREE.CylinderGeometry(
  69. wheelRadius, // top radius
  70. wheelRadius, // bottom radius
  71. wheelThickness, // height of cylinder
  72. wheelSegments);
  73. const wheelMaterial = new THREE.MeshPhongMaterial({color: 0x888888});
  74. const wheelPositions = [
  75. [-carWidth / 2 + wheelThickness / 2, -carHeight / 2, carLength / 3],
  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. ];
  80. const wheelMeshes = wheelPositions.map((position) => {
  81. const mesh = new THREE.Mesh(wheelGeometry, wheelMaterial);
  82. mesh.position.set(...position);
  83. mesh.rotation.z = Math.PI * .5;
  84. scene.add(mesh);
  85. return mesh;
  86. });
  87. function resizeRendererToDisplaySize(renderer) {
  88. const canvas = renderer.domElement;
  89. const width = canvas.clientWidth;
  90. const height = canvas.clientHeight;
  91. const needResize = canvas.width !== width || canvas.height !== height;
  92. if (needResize) {
  93. renderer.setSize(width, height, false);
  94. }
  95. return needResize;
  96. }
  97. // const carPosition = [0, 0, 0];
  98. function render(time) {
  99. time *= 0.001;
  100. if (resizeRendererToDisplaySize(renderer)) {
  101. const canvas = renderer.domElement;
  102. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  103. camera.updateProjectionMatrix();
  104. }
  105. wheelMeshes.forEach((obj) => {
  106. obj.rotation.x = time;
  107. });
  108. renderer.render(scene, camera);
  109. requestAnimationFrame(render);
  110. }
  111. requestAnimationFrame(render);
  112. }
  113. main();
  114. </script>
  115. </html>