three.js 按键W前进、S退后、A左转、D右转运动

2024-03-11 18:28

本文主要是介绍three.js 按键W前进、S退后、A左转、D右转运动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果:W 键 前进;S 键后退;A 键左转;D 键右转;使用了 tween.js 动画库; 

代码:

<template><div><el-container><el-main><div class="box-card-left"><div id="threejs"></div></div></el-main></el-container></div>
</template>s
<script>
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import TWEEN from '@tweenjs/tween.js';
export default {data() {return {scene: null,camera: null,renderer: null,res1: null,res2: null,clock: null,keyState: {W: false,S: false,A: false,D: false,},left_rotation: true, // 向左旋转的标志right_rotation: true, // 向右旋转的标志VW: new this.$three.Vector3(0, 0, 0),VS: new this.$three.Vector3(0, 0, 0),curr_v: new this.$three.Vector3(0, 0, 0),person: null,deltaTime: 0,a: 30, // 加速度damping: -0.04,};},created() {},mounted() {this.name = this.$route.query.name;this.init();},methods: {goBack() {this.$router.go(-1);},init() {this.clock = new this.$three.Clock();// 创建场景this.scene = new this.$three.Scene();// 创建辅助坐标轴对象const axesHelper = new this.$three.AxesHelper(100);this.scene.add(axesHelper);// 创建环境光const ambientLight = new this.$three.AmbientLight(0xffffff, 10);this.scene.add(ambientLight);// 创建相机对象this.camera = new this.$three.PerspectiveCamera(60,1,0.01,2000);this.camera.position.set(10,10,10);this.camera.lookAt(0,0,0);// 创建渲染器对象this.renderer = new this.$three.WebGLRenderer();this.renderer.setSize(1500,1200);// 创建GLTFLoader对象;加载人物模型const gltfLoader = new GLTFLoader();gltfLoader.load("models/gltf/person2/scene.gltf", gltf => {gltf.scene.position.set(0,0,-10);gltf.scene.scale.set(2,2,2);this.person = gltf.scene;this.scene.add(gltf.scene);this.renderer.render(this.scene, this.camera);window.document.getElementById("threejs").appendChild(this.renderer.domElement);})const controls = new OrbitControls(this.camera, this.renderer.domElement);controls.addEventListener("change", () => {this.renderer.render(this.scene, this.camera);})this.addEventListenerFn();this.renderLoop();},renderLoop() {const deltaTime = this.clock.getDelta();if(this.keyState.W) {if(this.VW.length() < 5) {this.VW.add(new this.$three.Vector3(0,0,1).multiplyScalar(this.a * deltaTime));this.curr_v = this.VW.clone();}let pos = this.VW.clone().multiplyScalar(deltaTime);this.person.position.add(pos);}if(this.keyState.S) {if(this.VS.length() < 5) {this.VS.add(new this.$three.Vector3(0,0,-1).multiplyScalar(this.a * deltaTime));this.curr_v = this.VS.clone();}let pos = this.VS.clone().multiplyScalar(deltaTime);this.person.position.add(pos);}if(this.keyState.A) {}if(this.person) {// .addScaledVector ( v : Vector3, s : Float ) : 将所传入的v与s相乘所得的乘积和这个向量相加。this.curr_v.addScaledVector(this.curr_v, this.damping);let pos = this.curr_v.clone().multiplyScalar(deltaTime);this.person.position.add(pos);}this.renderer.render(this.scene, this.camera);TWEEN.update();requestAnimationFrame(this.renderLoop);},addEventListenerFn() {// 监听按下的 W 键document.addEventListener("keydown", e => {if(e.code == "KeyW") {this.keyState.W = true;}if(e.code == "KeyS") {this.keyState.S = true;}if(e.code == "KeyA") {this.keyState.A = true;if(!this.left_rotation)return false;const tween0 = new TWEEN.Tween(this.person.rotation);let deg = this.$three.MathUtils.radToDeg(this.person.rotation.y);let rad = this.$three.MathUtils.degToRad(deg + 90);if(rad != null) {tween0.to({x:0, y: rad, z:0}, 1000);tween0.start();tween0.onStart(() => {this.left_rotation = false;});tween0.onComplete(() => {this.left_rotation = true;});}}if(e.code == "KeyD") {this.keyState.D = true;if(!this.right_rotation)return false;const tween0 = new TWEEN.Tween(this.person.rotation);let deg = this.$three.MathUtils.radToDeg(this.person.rotation.y);let rad = this.$three.MathUtils.degToRad(deg - 90);if(rad != null) {tween0.to({x:0, y: rad, z:0}, 1000);tween0.start();tween0.onStart(() => {this.right_rotation = false;});tween0.onComplete(() => {this.right_rotation = true;});}}})document.addEventListener("keyup", e => {if(e.code == "KeyW") {this.keyState.W = false;this.VW = new this.$three.Vector3(0, 0, 0);}if(e.code == "KeyS") {this.keyState.S = false;this.VS = new this.$three.Vector3(0, 0, 0);}if(e.code == "KeyA") {this.keyState.A = false;}if(e.code == "KeyD") {this.keyState.D = false;}})}},
};
</script>
<style lang="less" scoped>
.box-card-left {display: flex;align-items: flex-start;flex-direction: row;width: 100%;.box-right {img {width: 500px;user-select: none;}}
}
</style>

这篇关于three.js 按键W前进、S退后、A左转、D右转运动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/798667

相关文章

JS常用组件收集

收集了一些平时遇到的前端比较优秀的组件,方便以后开发的时候查找!!! 函数工具: Lodash 页面固定: stickUp、jQuery.Pin 轮播: unslider、swiper 开关: switch 复选框: icheck 气泡: grumble 隐藏元素: Headroom

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

EasyPlayer.js网页H5 Web js播放器能力合集

最近遇到一个需求,要求做一款播放器,发现能力上跟EasyPlayer.js基本一致,满足要求: 需求 功性能 分类 需求描述 功能 预览 分屏模式 单分屏(单屏/全屏) 多分屏(2*2) 多分屏(3*3) 多分屏(4*4) 播放控制 播放(单个或全部) 暂停(暂停时展示最后一帧画面) 停止(单个或全部) 声音控制(开关/音量调节) 主辅码流切换 辅助功能 屏

使用JS/Jquery获得父窗口的几个方法(笔记)

<pre name="code" class="javascript">取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);如题: $(selector, window.top.document);//获得顶级窗口里面的元素 $(

js异步提交form表单的解决方案

1.定义异步提交表单的方法 (通用方法) /*** 异步提交form表单* @param options {form:form表单元素,success:执行成功后处理函数}* <span style="color:#ff0000;"><strong>@注意 后台接收参数要解码否则中文会导致乱码 如:URLDecoder.decode(param,"UTF-8")</strong></span>

js react 笔记 2

起因, 目的: 记录一些 js, react, css 1. 生成一个随机的 uuid // 需要先安装 crypto 模块const { randomUUID } = require('crypto');const uuid = randomUUID();console.log(uuid); // 输出类似 '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

学习记录:js算法(二十八):删除排序链表中的重复元素、删除排序链表中的重复元素II

文章目录 删除排序链表中的重复元素我的思路解法一:循环解法二:递归 网上思路 删除排序链表中的重复元素 II我的思路网上思路 总结 删除排序链表中的重复元素 给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。 图一 图二 示例 1:(图一)输入:head = [1,1,2]输出:[1,2]示例 2:(图

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to

uuid.js 使用

相关代码 import { NIL } from "uuid";/** 验证UUID* 为空 则返回 false* @param uuid* @returns {boolean}*/export function MyUUIDValidate(uuid: any): boolean {if (typeof uuid === "string" && uuid !== NIL) { //uuid