输出效果
代码
import { PlaneGeometry, Scene, PerspectiveCamera, WebGLRenderer, Object3D, Clock, AnimationMixer, Color, AmbientLight, DirectionalLight, GridHelper, AxesHelper, Material, Vector3, BoxGeometry, MeshBasicMaterial, CylinderGeometry, MeshLambertMaterial, Mesh, Vector2, TextureLoader, CircleGeometry, cloneUniformsGroups, ArrowHelper, CurvePath, BufferGeometry, LineBasicMaterial, Line, SplineCurve, PointLightShadow, Matrix4, Matrix3, } from "three"; import * as THREE from "three"; import * as TWEEN from "@tweenjs/tween.js"; type IPoint = { x: number; y: number; }; export class Car { public car: Mesh; private carArrow: Vector2; constructor() { this.car = this.createCar(); this.carArrow = new Vector2(1, 0); } private createCar() { const carParam = { width: 0.1, height: 0.05, depth: 0.05, }; const geometry = new BoxGeometry( carParam.width, carParam.height, carParam.depth ); const car = new Mesh( geometry, new MeshLambertMaterial({ color: 0xffffff, }) ); car.position.z = carParam.depth * 0.5; const arrow = new THREE.Vector3(1, 0, 0); arrow.normalize(); const origin = new Vector3(0, 0, 0); const arrowHelper = new ArrowHelper(arrow, origin, 0.3, 0xffff00); car.add(arrowHelper); return car; } public forward() { const carArrowNormal = this.carArrow.normalize(); const matrix = new Matrix4(); // 1 0 0 carArrowNormal.x*0.01 // 0 1 0 carArrowNormal.y * 0.01 // 0 0 1 0 // 0 0 0 1 matrix.set( 1, 0, 0, carArrowNormal.x * 0.01, 0, 1, 0, carArrowNormal.y * 0.01, 0, 0, 1, 0, 0, 0, 0, 1 ); this.car.applyMatrix4(matrix); } public backward() { const carArrowNormal = this.carArrow.normalize(); const matrix = new Matrix4(); matrix.set( 1, 0, 0, -carArrowNormal.x * 0.01, 0, 1, 0, -carArrowNormal.y * 0.01, 0, 0, 1, 0, 0, 0, 0, 1 ); this.car.applyMatrix4(matrix); } public turnleft() { const matrix = new Matrix3(); const angle = Math.PI * 0.01; const sinValue = Math.sin(angle); const cosValue = Math.cos(angle); matrix.set( cosValue, -sinValue, 0, sinValue, cosValue, 0, 0, 0, 1 ); this.carArrow.applyMatrix3(matrix); this.car.rotateZ(angle); } public turnright() { const matrix = new Matrix3(); const angle = Math.PI * 0.01; const sinValue = Math.sin(angle); const cosValue = Math.cos(angle); matrix.set( cosValue, sinValue, 0, -sinValue, cosValue, 0, 0, 0, 1 ); this.carArrow.applyMatrix3(matrix); this.car.rotateZ(-angle); } }
标签:
threejs
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验