three实现可配置参数的炫酷三维可交互发光粒子线条代码

代码语言:html

所属分类:三维

代码描述:three实现可配置参数的炫酷三维可交互发光粒子线条代码

代码标签: three 配置 参数 炫酷 三维 交互 发光 粒子 线条 代码

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

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

    <style>
      body, html {
        margin: 0;
        overflow: hidden;
        font-family: Arial, sans-serif;
        background-color: #fff;
        user-select: none;
      }
      canvas { display: block; }
    </style>
  </head>
  <body>
      
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/es-module-shims.1.6.3.js"></script>
      <script type="importmap">
  {
    "imports": {      
      "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/164/three.module.js",
		     
      "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/164/jsm/"
    }
  }
</script>
    <script type="module">
  
      import * as THREE from 'three';
      import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
      import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
      import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
      import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';      
      import { GUI } from '//repo.bfw.wiki/bfwrepo/js/module/dat.gui.js';

      let scene, camera, renderer, particles, lines, mouse, controls, gui, composer, material, lineMaterial;
      const params = {
        particleCount: 1000,
        lineDistance: 1000,
        repulsionStrength: 5,
        bloomStrength: 1.5,
        bloomThreshold: 0,
        bloomRadius: 0
      };

      init();
      animate();

      function init() {
        scene = new THREE.Scene();
        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 1200;
        scene.fog = new THREE.Fog( 0x000000, 10, 2000 );
        renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

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

        const geometry = new THREE.BufferGeometry();
        const positions = new Float32Array(params.particleCount * 3);
        for (let i = 0; i < positions.length; i++) {
          positions[i] = (Math.random() * 2 - 1) * 500;
        }
        geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));

        material = new THREE.PointsMaterial({ color: getRandomColor(), size: 10, fog:true });
        particles = new THREE.Points(geometry, material);
        scene.add(particles);

        lineMaterial = new THREE.LineBasicMaterial({ color: getRandomColor(), linewidth: .25, fog:true });

        lines = new THREE.LineSegments(new THREE.BufferGeometry(), lineMaterial);
        scene.add(lines);

        mouse = new THREE.Vector2();

        gui = new GUI({width: '100%',closeOnTop:true, closed:true});
        gui.domElement.querySelector('.close-button').innerHTML = 'Open Controls';

        gui.add(params, 'lineDistance', 500, 1000);
        gui.add(params, 'repulsionStrength', 1, 10);
        gui.add(params, 'bloomStrength', 0, 3).onChange(updateBloom);
        gui.add(params, 'bloomThreshold', 0, 1).onChange(updateBloom);
        gui.add(params, 'bloomRadius', 0, 1).onChange(updateBloom);

        window.addEventListener('resize', onWindowResize, false);
        document.addEventListener('mousemove', onMouseMove, false);

        const renderScene = new RenderPass(scene, camera);
        const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), params.bloomStrength, params.bloomRadius, params.bloomThreshold);
        composer = new EffectComposer(renderer);
        composer.addPass(renderScene);
        composer.addPass(bloomPass);
      }
      function getRandomColor() {
        return `hsl(${Math.random() * 360}, 100%, 80%)`;
      }
      function changeColor() {
        mate.........完整代码请登录后点击上方下载按钮下载查看

网友评论0