threejs-scenegraph-car.html 3.4 KB

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