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

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

效果图如上

代码:

<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 });
    // 利用渲染器将场景的透明度设置为0
    renderer.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.kler.cn/news/317859.html

相关文章:

  • 汽车售后诊断ECU参数分析
  • 寄宿制学校自闭症教育:为每个孩子创造奇迹
  • spring boot项目对接人大金仓
  • 线性代数学习笔记~
  • 初识JavaScript
  • 【图像压缩与重构】基于BP神经网络
  • 新版torch_geometric不存在uniform、maybe_num_nodes函数问题(Prune4ED论文报错解决)
  • python request库的使用
  • 深度学习领域相关的专业术语(附带音标解释)
  • EtherCAT转Profient协议网关简述
  • MySQL函数及存储过程
  • 视频制作软件哪个好?前十名推荐!
  • 云手机的便捷性和安全性体现在哪?
  • redisson 延迟队列实现任务过期监听
  • Hbase操作手册
  • git笔记之重置本地仓库所有分支和远程保持一致、工作区恢复干净,像刚clone下来一样
  • 阅读记录:Gradient Episodic Memory for Continual Learning
  • 十三 系统架构设计(考点篇)
  • 【python】数据类型
  • react hooks--useCallback
  • 误删系统引导如何恢复?如何创建系统引导?
  • Vue 内存泄漏分析:如何避免开发过程中导致的内存泄漏问题
  • Appium高级话题:混合应用与原生应用测试策略
  • Mysql 常用方法和函数(查询)
  • 数据结构应试-树和二叉树
  • 这个浏览器插件:提高测试效率且好用!
  • Haskell网络编程:代理服务器的高级使用技巧
  • mac安装JetBtains全家桶新版本时报错:Cannot start the IDE
  • GitLab将会持续支持FluxCD
  • Vulkan 学习(9)---- vkSuraceKHR 创建