three实现三维宇宙可调参数星辰星空宇宙粒子效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维宇宙可调参数星辰星空宇宙粒子效果代码

代码标签: three 三维 宇宙 参数 星辰 星空 宇宙 粒子

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">

<style>
    * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  width: 100vw;
}

canvas {
  height: 100%;
  width: 100%;
}
</style>
</head>
<body>
<!-- partial:index.partial.html -->
<script type="x-shader/x-vertex" id="vertex">
    uniform float u_time;
attribute float a_random;

varying float v_random;
varying vec2 vUv;
varying vec3 v_normal;


void main() {
    vUv = uv;
    
    vec3 newPos = position.xyz;
    float wave = sin(newPos.x / 8.0 - u_time) * 1.;
    wave += sin(newPos.y / 13.0 - u_time) * 1.;
    
    newPos.z += wave;
    
    
    gl_Position = projectionMatrix * modelViewMatrix * vec4(newPos, 1.0);


    v_random = wave;
}
</script>

<script type="x-shader/x-fragment" id="fragment">
    varying float v_random;
varying vec2 vUv;
varying vec3 v_normal;
    
void main() {
    gl_FragColor = vec4(0.886, clamp(v_random, 0.70, 0.78), 0.984, 1.0);
}
</script>
<!-- partial -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.125.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.125.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.9.1.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
<script >
    

class THREEScene {
  constructor(container = document.body) {
    this.container = container;

    this.init();
  }

  init() {
    this.setup();
    this.camera();
    this.lights();
    this.addToScene();
    this.eventListeners();
    this.render();
  }

  setup() {
    this.gui = new dat.GUI();
    this.group = new THREE.Group();
    this.textureLoader = new THREE.TextureLoader();
    this.clock = new THREE.Clock();
    this.scene = new THREE.Scene();
    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    this.renderer.setSize(this.viewport.width, this.viewport.height);
    this.renderer.setPixelRatio = window.devicePixelRatio;
    this.renderer.setClearColor(0x151617);
    this.container.appendChild(this.renderer.domElement);
  }

  camera() {
    const fov = 75;
    const near = 0.1;
    const far = 400;
    const aspectRatio = this.viewport.aspectRatio;
    this.camera = new THREE.PerspectiveCamera(fov, aspectRatio, near, far);
    this.camera.position.set(10, 10, 10);
    gsap.to(this.camera.position, {
      x: 0,
      y: 2,
      z: 5,
      duration: 5,
      ease: "power4.out" });

    this.controls = new THREE.OrbitControls(
    this.camera,
    this.renderer.domElement);

  }

  lights() {}

  addToScene() {
    const parameters = {
      count: 100000,
      size: 0.01,
      radius: 5,
      branches: 3,
      spin: 1,
      randomness: 0.2,
      randomnessPower: 3,
      insideColor: "#ff6030",
      outsideColor: "#1b3984" };


    let geometry = null;
    let matetrial = null;
    let points = null;

    const generateGalaxy = () => {
      if (points !== null) {
        geometry.dispose();
        matetrial.dispose();
        this.scene.remove(points);
      }

      geometry = new THREE.BufferGeometry();
      matetrial = new THREE.PointsMaterial({
        size: parameters.size,
        sizeAttenuation: true,
        depthWrite: true,
        blending: THREE.AdditiveBlending,
        vertexColors: true });


      const positions = new Float32Array(paramete.........完整代码请登录后点击上方下载按钮下载查看

网友评论0