当前位置:首页 > VUE

vue实现3d宇宙

2026-03-06 19:24:43VUE

实现3D宇宙的基础方法

使用Vue结合Three.js库可以高效创建3D宇宙场景。Three.js是基于WebGL的JavaScript 3D库,能简化3D渲染的复杂度。

安装依赖:

npm install three @types/three

基础场景搭建

创建Vue组件并初始化Three.js基础结构:

vue实现3d宇宙

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

<script>
import * as THREE from 'three';

export default {
  mounted() {
    this.initScene();
  },
  methods: {
    initScene() {
      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 stars = new THREE.Points(starGeometry, starMaterial);
      scene.add(stars);

      // 动画循环
      const animate = () => {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
      };
      animate();
    }
  }
};
</script>

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

生成星空效果

创建随机分布的星星:

createStars() {
  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);
  }

  const geometry = new THREE.BufferGeometry();
  geometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));

  const material = new THREE.PointsMaterial({
    size: 2,
    color: 0xFFFFFF,
    transparent: true,
    opacity: 0.8
  });

  return new THREE.Points(geometry, material);
}

添加行星系统

创建带纹理的行星和轨道:

vue实现3d宇宙

createPlanet() {
  const geometry = new THREE.SphereGeometry(5, 32, 32);
  const textureLoader = new THREE.TextureLoader();
  const material = new THREE.MeshStandardMaterial({
    map: textureLoader.load('/path/to/texture.jpg'),
    roughness: 1,
    metalness: 0
  });

  const planet = new THREE.Mesh(geometry, material);
  planet.position.set(20, 0, 0);

  // 添加轨道
  const orbitGeometry = new THREE.RingGeometry(20, 20.1, 64);
  const orbitMaterial = new THREE.MeshBasicMaterial({ 
    color: 0x444444, 
    side: THREE.DoubleSide,
    transparent: true,
    opacity: 0.5
  });
  const orbit = new THREE.Mesh(orbitGeometry, orbitMaterial);
  orbit.rotation.x = Math.PI / 2;

  scene.add(planet, orbit);
  return planet;
}

实现动态效果

添加自动旋转和交互控制:

animate() {
  requestAnimationFrame(this.animate);

  // 行星自转
  if (this.planet) {
    this.planet.rotation.y += 0.01;
  }

  // 相机环绕
  if (this.controls) {
    this.controls.update();
  }

  this.renderer.render(this.scene, this.camera);
}

addControls() {
  const OrbitControls = require('three/examples/jsm/controls/OrbitControls').default;
  this.controls = new OrbitControls(this.camera, this.renderer.domElement);
  this.controls.enableDamping = true;
  this.controls.dampingFactor = 0.05;
}

性能优化技巧

使用实例化渲染处理大量星体:

createStarCluster() {
  const geometry = new THREE.InstancedBufferGeometry();
  const baseGeometry = new THREE.SphereGeometry(0.5, 8, 8);

  geometry.index = baseGeometry.index;
  geometry.attributes = baseGeometry.attributes;

  const count = 1000;
  const positions = new THREE.InstancedBufferAttribute(new Float32Array(count * 3), 3);
  const colors = new THREE.InstancedBufferAttribute(new Float32Array(count * 3), 3);

  for (let i = 0; i < count; i++) {
    positions.setXYZ(i, 
      Math.random() * 100 - 50,
      Math.random() * 100 - 50,
      Math.random() * 100 - 50
    );
    colors.setXYZ(i,
      Math.random(),
      Math.random(),
      Math.random()
    );
  }

  geometry.setAttribute('instancePosition', positions);
  geometry.setAttribute('instanceColor', colors);

  const material = new THREE.ShaderMaterial({
    vertexShader: `
      attribute vec3 instancePosition;
      attribute vec3 instanceColor;
      varying vec3 vColor;
      void main() {
        vColor = instanceColor;
        vec3 pos = position * 0.2 + instancePosition;
        gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
      }
    `,
    fragmentShader: `
      varying vec3 vColor;
      void main() {
        gl_FragColor = vec4(vColor, 1.0);
      }
    `
  });

  return new THREE.Mesh(geometry, material);
}

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

相关文章

vue实现轮播图

vue实现轮播图

Vue 实现轮播图的方法 使用第三方库(推荐) Vue 生态中有许多成熟的轮播图组件库,例如 Swiper、Vue-Awesome-Swiper 等,它们提供了丰富的配置和响应式支持。 安装…

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <di…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的 :…

vue实现列表

vue实现列表

Vue 实现列表的方法 在 Vue 中实现列表渲染通常使用 v-for 指令,可以动态生成多个元素。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 遍历数组,生成列表项。v-for 需要…

vue实现追加

vue实现追加

追加数据到数组或列表 在Vue中追加数据到数组或列表,可以通过push方法或concat方法实现。以下是几种常见的实现方式: 方法一:使用push方法 this.items.push(newIte…