基本结构

代码
| import * as THREE from 'three' |
| import Stat from 'three/examples/jsm/libs/stats.module' |
| import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' |
| |
| const w = window.innerWidth |
| const h = window.innerHeight |
| const stat = new Stat() |
| |
| |
| const scene = new THREE.Scene() |
| |
| |
| const material = new THREE.MeshNormalMaterial() |
| |
| |
| |
| const car = new THREE.Group() |
| |
| |
| const body = new THREE.Group() |
| |
| |
| const bodyCube1 = new THREE.Mesh( |
| new THREE.BoxGeometry(1, 2, 0.5), |
| material |
| ) |
| |
| const bodyCube2 = new THREE.Mesh( |
| new THREE.BoxGeometry(0.5, 0.5, 0.5), |
| new THREE.MeshBasicMaterial({ color: 0xff0000 }) |
| ) |
| bodyCube2.position.z = 0.5 |
| |
| body.add(bodyCube1) |
| body.add(bodyCube2) |
| |
| car.add(body) |
| |
| |
| const wheelGroup1 = new THREE.Group() |
| const wheel1 = new THREE.Mesh( |
| new THREE.BoxGeometry(0.1, 0.7, 0.7), |
| material |
| ) |
| wheelGroup1.position.set(-0.7, 0.6, 0) |
| wheelGroup1.add(wheel1) |
| car.add(wheelGroup1) |
| |
| const wheelGroup2 = new THREE.Group() |
| const wheel2 = new THREE.Mesh( |
| new THREE.BoxGeometry(0.1, 0.7, 0.7), |
| material |
| ) |
| wheelGroup2.position.set(0.7, 0.6, 0) |
| wheelGroup2.add(wheel2) |
| car.add(wheelGroup2) |
| |
| const wheelGroup3 = wheelGroup1.clone() |
| wheelGroup3.position.y = -0.6 |
| car.add(wheelGroup3) |
| |
| const wheelGroup4 = wheelGroup2.clone() |
| wheelGroup4.position.y = -0.6 |
| car.add(wheelGroup4) |
| |
| |
| const circle = new THREE.Group() |
| |
| let n = 20 |
| for (let i = 0; i < n; i++) { |
| let r = 0.5 |
| const geometry = new THREE.BoxGeometry(0.1, 0.1, 0.2) |
| const mesh = new THREE.Mesh(geometry, material) |
| mesh.position.x = r * Math.cos(Math.PI * 2 / n * i) |
| mesh.position.y = r * Math.sin(Math.PI * 2 / n * i) |
| circle.add(mesh) |
| } |
| circle.rotation.y = -0.5 * Math.PI |
| |
| wheelGroup1.add(circle) |
| wheelGroup2.add(circle.clone()) |
| wheelGroup3.add(circle.clone()) |
| wheelGroup4.add(circle.clone()) |
| |
| scene.add(car) |
| |
| |
| const camera = new THREE.PerspectiveCamera(75, w / h, 0.1, 100) |
| camera.position.set(0, 0, 5) |
| camera.lookAt(0, 0, 0) |
| |
| |
| const renderer = new THREE.WebGLRenderer() |
| renderer.setSize(w, h) |
| document.body.append(renderer.domElement) |
| document.body.append(stat.dom) |
| |
| const orbitControls = new OrbitControls(camera, renderer.domElement) |
| |
| const clock = new THREE.Clock() |
| tick() |
| function tick() { |
| const time = clock.getElapsedTime() |
| |
| car.position.y = time % 4 - 2 |
| wheelGroup1.rotation.x = -time |
| wheelGroup2.rotation.x = -time |
| wheelGroup3.rotation.x = -time |
| wheelGroup4.rotation.x = -time |
| |
| requestAnimationFrame(tick) |
| renderer.render(scene, camera) |
| stat.update() |
| orbitControls.update() |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战