当前位置: 首页 > news >正文

仿黑神话悟空跑动-脚下波纹特效(键盘wasd控制走动)

vue使用three.js实现仿黑神话悟空跑动-脚下波纹特效

<template><div ref="container" class="container"></div>
</template><script>
import * as THREE from "three";
export default {name: "WaterRipple",data() {return {scene: null,camera: null,renderer: null,player: null,clock: new THREE.Clock(),keyboard: {},rippleMaterial: null,ripples: []};},mounted() {this.init();this.animate();document.addEventListener("keydown", this.onDocumentKeyDown, false);document.addEventListener("keyup", this.onDocumentKeyUp, false);},beforeDestroy() {document.removeEventListener("keydown", this.onDocumentKeyDown, false);document.removeEventListener("keyup", this.onDocumentKeyUp, false);},methods: {init() {// 创建场景this.scene = new THREE.Scene();// 创建相机this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);this.camera.position.set(0, 50, 100);this.camera.lookAt(0, 0, 0);// 创建渲染器this.renderer = new THREE.WebGLRenderer();this.renderer.setSize(window.innerWidth, window.innerHeight);this.$refs.container.appendChild(this.renderer.domElement);// 创建平面const geometry = new THREE.PlaneGeometry(200, 200, 32, 32);const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: true });const plane = new THREE.Mesh(geometry, material);plane.rotation.x = -Math.PI / 2;this.scene.add(plane);// 创建玩家const playerGeometry = new THREE.SphereGeometry(1, 32, 32);const playerMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff });this.player = new THREE.Mesh(playerGeometry, playerMaterial);this.player.position.y = 1;this.scene.add(this.player);// 窗口调整window.addEventListener('resize', this.onWindowResize, false);},onWindowResize() {this.camera.aspect = window.innerWidth / window.innerHeight;this.camera.updateProjectionMatrix();this.renderer.setSize(window.innerWidth, window.innerHeight);},createRipple(x, z) {const rippleGeometry = new THREE.RingGeometry(0.1, 0.5, 32);const rippleMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff, transparent: true, opacity: 1, wireframe: true });const ripple = new THREE.Mesh(rippleGeometry, rippleMaterial);ripple.position.set(x, 0.1, z);ripple.rotation.x = -Math.PI / 2;ripple.scale.set(1, 1, 1);this.scene.add(ripple);this.ripples.push({ mesh: ripple, startTime: this.clock.getElapsedTime() });},onDocumentKeyDown(event) {this.keyboard[event.key] = true;},onDocumentKeyUp(event) {this.keyboard[event.key] = false;},animate() {requestAnimationFrame(this.animate);const delta = this.clock.getDelta();const elapsedTime = this.clock.getElapsedTime();const step = 10 * delta;if (this.keyboard['w']) {this.player.position.z -= step;this.createRipple(this.player.position.x, this.player.position.z);}if (this.keyboard['a']) {this.player.position.x -= step;this.createRipple(this.player.position.x, this.player.position.z);}if (this.keyboard['s']) {this.player.position.z += step;this.createRipple(this.player.position.x, this.player.position.z);}if (this.keyboard['d']) {this.player.position.x += step;this.createRipple(this.player.position.x, this.player.position.z);}// 更新水波纹this.ripples.forEach(ripple => {const age = elapsedTime - ripple.startTime;ripple.mesh.scale.set(1 + age * 10, 1 + age * 10, 1 + age * 10);ripple.mesh.material.opacity = Math.max(0, 1 - age / 0.5); // 水波纹消散更快ripple.mesh.material.color.setHSL(0.6, 1, 0.5 * (1 - age / 0.5)); // 颜色随着扩散变淡});this.ripples = this.ripples.filter(ripple => ripple.mesh.material.opacity > 0);this.renderer.render(this.scene, this.camera);}}
};
</script><style scoped>
.container {width: 100vw;height: 100vh;overflow: hidden;
}
</style>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • V3s pinctrl与gpio的耦合问题
  • 大数据新视界 --大数据大厂之 Reactjs 在大数据应用开发中的优势与实践
  • 我们一般使用的家庭宽带支持udp吗
  • sv标准解读第九章-进程
  • Python和C++及R相关系数数学统计学可视化和神经模型及评估指标
  • ubuntu22 解决docker无法下载镜像问题
  • 【题解】CF2013A
  • Linux运维常用指令讲解(持续更新)
  • Nginx从入门到入土(四):负载均衡策略
  • Go语言流程控制
  • “跨链桥“的危害
  • docker zookeeper集群启动报错:Cannot open channel to * at election address /ip:3888
  • 数据驱动农业——农业中的大数据
  • 【已解决】使用JAVA语言实现递归调用-本关任务:用循环和递归算法求 n(小于 10 的正整数) 的阶乘 n!。
  • 通信系统中频偏估计补偿的流程
  • Angular 响应式表单之下拉框
  • canvas 五子棋游戏
  • Druid 在有赞的实践
  • laravel 用artisan创建自己的模板
  • Linux gpio口使用方法
  • Linux中的硬链接与软链接
  • maven工程打包jar以及java jar命令的classpath使用
  • mysql 数据库四种事务隔离级别
  • Mysql优化
  • php的插入排序,通过双层for循环
  • php中curl和soap方式请求服务超时问题
  • V4L2视频输入框架概述
  • 关于 Cirru Editor 存储格式
  • 基于HAProxy的高性能缓存服务器nuster
  • 基于web的全景—— Pannellum小试
  • 记录:CentOS7.2配置LNMP环境记录
  • 将 Measurements 和 Units 应用到物理学
  • 聚类分析——Kmeans
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 追踪解析 FutureTask 源码
  • Hibernate主键生成策略及选择
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #传输# #传输数据判断#
  • (13)DroneCAN 适配器节点(一)
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (3)STL算法之搜索
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (el-Date-Picker)操作(不使用 ts):Element-plus 中 DatePicker 组件的使用及输出想要日期格式需求的解决过程
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (一一四)第九章编程练习
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • ***原理与防范
  • .net 调用海康SDK以及常见的坑解释
  • .NET 中 GetProcess 相关方法的性能
  • .net反编译工具
  • .NET正则基础之——正则委托
  • .NET之C#编程:懒汉模式的终结,单例模式的正确打开方式
  • [ CTF ]【天格】战队WriteUp- 2022年第三届“网鼎杯”网络安全大赛(青龙组)