当前位置:首页 > VUE

vue实现3d宇宙

2026-01-07 05:30:58VUE

Vue实现3D宇宙效果

使用Three.js库

Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖:

npm install three

创建一个Vue组件,初始化Three.js场景:

<template>
  <div ref="container" class="three-container"></div>
</template>

<script>
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

export default {
  mounted() {
    this.initThree();
  },
  methods: {
    initThree() {
      const container = this.$refs.container;
      const scene = new THREE.Scene();
      const camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000);
      const renderer = new THREE.WebGLRenderer({ antialias: true });

      renderer.setSize(container.clientWidth, container.clientHeight);
      container.appendChild(renderer.domElement);

      // 添加星空背景
      const starGeometry = new THREE.BufferGeometry();
      const starMaterial = new THREE.PointsMaterial({ color: 0xffffff });
      const starVertices = [];

      for (let i = 0; i < 10000; i++) {
        const x = (Math.random() - 0.5) * 2000;
        const y = (Math.random() - 0.5) * 2000;
        const z = (Math.random() - 0.5) * 2000;
        starVertices.push(x, y, z);
      }

      starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
      const stars = new THREE.Points(starGeometry, starMaterial);
      scene.add(stars);

      // 添加行星
      const planetGeometry = new THREE.SphereGeometry(5, 32, 32);
      const planetMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
      const planet = new THREE.Mesh(planetGeometry, planetMaterial);
      scene.add(planet);

      camera.position.z = 15;
      new OrbitControls(camera, renderer.domElement);

      const animate = () => {
        requestAnimationFrame(animate);
        planet.rotation.x += 0.01;
        planet.rotation.y += 0.01;
        renderer.render(scene, camera);
      };
      animate();
    }
  }
};
</script>

<style>
.three-container {
  width: 100%;
  height: 100vh;
}
</style>

使用A-Frame框架

A-Frame是专门为WebVR设计的框架,但也可以用于普通3D场景。安装A-Frame Vue插件:

vue实现3d宇宙

npm install aframe vue-aframe

在Vue项目中使用:

<template>
  <a-scene>
    <a-sky color="#000000"></a-sky>
    <a-entity
      v-for="(star, index) in stars"
      :key="index"
      :position="star.position"
      geometry="primitive: sphere; radius: 0.05"
      material="color: white"
    ></a-entity>
    <a-sphere
      position="0 0 -5"
      radius="1"
      color="blue"
      animation="property: rotation; to: 0 360 0; loop: true; dur: 10000"
    ></a-sphere>
  </a-scene>
</template>

<script>
export default {
  data() {
    return {
      stars: []
    };
  },
  created() {
    for (let i = 0; i < 500; i++) {
      this.stars.push({
        position: {
          x: (Math.random() - 0.5) * 10,
          y: (Math.random() - 0.5) * 10,
          z: (Math.random() - 0.5) * 10 - 5
        }
      });
    }
  }
};
</script>

性能优化技巧

对于大规模3D场景,考虑使用实例化渲染提高性能。Three.js中可以使用InstancedMesh

vue实现3d宇宙

const geometry = new THREE.SphereGeometry(0.05, 8, 8);
const material = new THREE.MeshBasicMaterial({ color: 0xffffff });
const mesh = new THREE.InstancedMesh(geometry, material, 10000);

const matrix = new THREE.Matrix4();
for (let i = 0; i < 10000; i++) {
  matrix.setPosition(
    (Math.random() - 0.5) * 2000,
    (Math.random() - 0.5) * 2000,
    (Math.random() - 0.5) * 2000
  );
  mesh.setMatrixAt(i, matrix);
}
scene.add(mesh);

添加交互功能

实现鼠标点击选择行星的功能:

const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();

function onMouseClick(event) {
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  raycaster.setFromCamera(mouse, camera);
  const intersects = raycaster.intersectObjects(scene.children);

  if (intersects.length > 0) {
    console.log('选中了物体', intersects[0].object);
  }
}

window.addEventListener('click', onMouseClick, false);

响应式设计

确保3D场景适应不同屏幕尺寸:

function onWindowResize() {
  camera.aspect = container.clientWidth / container.clientHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(container.clientWidth, container.clientHeight);
}

window.addEventListener('resize', onWindowResize);

标签: 宇宙vue
分享给朋友:

相关文章

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <tem…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…