使用three.js+vue3完成无人机上下运动

news/2024/9/28 10:31:03/

效果图如上

代码:

javascript"><template><div class="drones"><div ref="dronesContainer" class="drones-container"></div></div></template><script setup>import { ref, onMounted, onUnmounted, render } from 'vue';import * as THREE from 'three';// 导入无人机材质import droneTexture from '../../../assets/images/大疆无人机.png';const dronesContainer = ref(null);let scene, camera, renderer, droneMesh, droneGeometry, droneMaterial, animationId;onMounted(async () => {try {await init();animate();} catch (error) {console.error('Initialization error:', error);}});onUnmounted(() => {cancelAnimationFrame(animationId);});async function init() {const width = window.innerWidth;const height = window.innerHeight;// 创建场景scene = new THREE.Scene();scene.background = null;// 创建相机camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);camera.position.z = 5;// 创建渲染器,antialias: true 表示开启抗锯齿renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });// 利用渲染器将场景的透明度设置为0renderer.setClearAlpha(0);renderer.setSize(width, height);dronesContainer.value.appendChild(renderer.domElement);// 添加光源, AmbientLight 表示环境光,DirectionalLight 表示平行光const light = new THREE.AmbientLight(0x404040);scene.add(light);const directionalLight = new THREE.DirectionalLight(0xffffff, 1);directionalLight.position.set(-1, 2, 4).normalize();scene.add(directionalLight);const texture = new THREE.TextureLoader().load(droneTexture);// 创建无人机面容器droneGeometry = new THREE.PlaneGeometry(2, 2);// 创建无人机材质droneMaterial = new THREE.MeshBasicMaterial({ map: texture, transparent: true});// texture.transparent = true;droneMesh = new THREE.Mesh(droneGeometry, droneMaterial);droneMesh.position.set(0.5, 1, 0);scene.add(droneMesh);renderer.render(scene, camera);}const animate = () => {animationId = requestAnimationFrame(animate);const positionY = Math.abs(Math.sin(Date.now() * 0.001)) * 1.5;const rotationX = Math.cos(Date.now() * 0.001) * Math.PI / 3;droneMesh.rotation.x = rotationX;droneMesh.position.y = positionY;renderer.render(scene, camera);};window.addEventListener('resize', () => {const width = window.innerWidth;const height = window.innerHeight;renderer.setSize(width, height);camera.aspect = width / height;camera.updateProjectionMatrix();});</script><style scoped>.drones-container {width: 100%;height: 100vh;position: relative;overflow: hidden;z-index: 1000;}</style>

说明:

由于材质是二维图片挂载到二维平面,所以无人机的所谓上下翻转运动,是面的绕轴运动和上下运动的复合运动(绕x轴),其次注意png图片作为材质,让透明地方不为黑色,一定要设置MeshBasicMaterial的transparent为true,即透明度使用。


http://www.ppmy.cn/news/1531022.html

相关文章

【开源免费】基于SpringBoot+Vue.JS教师工作量管理系统(JAVA毕业设计)

本文项目编号 T 043 &#xff0c;文末自助获取源码 \color{red}{T043&#xff0c;文末自助获取源码} T043&#xff0c;文末自助获取源码 目录 一、系统介绍二、演示录屏三、启动教程四、功能截图五、文案资料5.1 选题背景5.2 国内外研究现状5.3 可行性分析 六、核心代码6.1 查…

大数据平台符合信创(CDH国产化代替)详细方案(企业内部不外传方案)

目录 第一章 介绍信创 1.1 信创背景与要求 1.2 介绍符合信创要求的产品 第二章 CDH不符合信创要求的原因 2.1 国外厂商背景 2.2 技术自主性不足 2.3 安全和合规问题 第三章 国产整体方案代替方案 3.1 华为 FusionInsight 3.2 阿里云MaxCompute 3.3 星环科技TDH 第四…

Linux云计算 |【第四阶段】NOSQL-DAY1

主要内容&#xff1a; NoSQL概述&#xff08;RDBMS、NoSQL&#xff09;、部署Redis服务、Redis数据类型&#xff08;字符串、散列类型、列表类型、集合类型、有序集合类型&#xff09;、Redis其它操作命令、修改Redis服务运行参数、部署支持PHP和Redis的Nginx服务器 一、NoSQL…

tomcat 配置jenkins_home 目录

要在 Tomcat 中为 Jenkins 配置自定义的 JENKINS_HOME 目录&#xff08;用于存储 Jenkins 的工作目录和配置&#xff09;&#xff0c;你需要在 Tomcat 的环境变量中进行配置。下面是详细步骤&#xff1a; 1. 创建 Jenkins Home 目录 首先&#xff0c;选择一个你想要作为 JENK…

C语言中的一些小知识(三)

一、你了解printf()吗&#xff1f; 你知道下面代码的输出结果吗&#xff1f; int a123; printf("%2d \n",a); printf() 函数是 C 语言中用于格式化输出的标准函数&#xff0c;它允许你将数据以特定的格式输出到标准输出设备&#xff08;通常是屏幕&#xff09;。p…

微信小程序03-页面交互

零、文章目录 微信小程序03-页面交互 1、案例&#xff1a;比较数字大小 &#xff08;1&#xff09;案例分析 需求&#xff1a;本案例将实现“比较数字大小”微信小程序&#xff0c;它的功能是当用户输入两个数字后&#xff0c;点击“比较”按钮可以自动比较这两个数字的大小…

【MySQL】字符集与Collation

今天做项目&#xff0c;突然发现&#xff0c;项目中使用的MySQL的库排序规则是 utf8mb4_general_ci&#xff0c;而我自己用的MySQL8默认库规则是utf8mb4_0900_ai_ci&#xff0c;于是想要弄清楚 出处&#xff08;写的非常详细&#xff09;&#xff1a;mysql设置了utf8mb4&#x…

Centos7.9在K8s安装生产级别的分布式存储Rook+Ceph

1.介绍 在k8s云原生平台中&#xff0c;存储是除了网络之外的另一个核心&#xff0c;因为他涉及到了数据的保存&#xff0c;以及容灾等一系列的问题&#xff0c;做生产级别的应用&#xff0c;一定要具有多节点分布式&#xff0c;灾备及时恢复&#xff0c;数据平滑迁移等多种特性…