three实现三维三角形碎片拼筹的小球旋转代码

代码语言:html

所属分类:三维

代码描述:three实现三维三角形碎片拼筹的小球旋转代码,可通过gui来调整参数。

代码标签: three 三维 三角形 碎片 拼筹 小球 旋转 代码

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

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

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <style>
      body {
        margin: 0;
        overflow: hidden;
        user-select: none;
        background: #000000;        
      } 
      canvas {
        display: block;
      }
      .title {
        position: absolute;
        font-family: Arial;
        color: #fff;
        font-size: .8rem;
        mix-blend-mode:normal;
      }
      .title._1 { top: 20px; left: 30px; }
      .title._2 { bottom: 20px; left: 30px; }
      .title._3 { bottom: 20px; right: 30px; cursor: pointer; }
    </style>
  </head>
  <body>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.12.2.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 { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
      import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
      import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
      import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
      import { GUI } from '//repo.bfw.wiki/bfwrepo/js/module/dat.gui.js';


      let scene, camera, renderer, composer, bloomPass, noisePass, noiseShader, controls, sphere, particleSystem, lineMesh, group;
      let guiParams = {
        deformAmount: 0.1,
        particleSize: .01,
        transparency: 0.5,
        iridescenceIntensity: 1.0,
        activateNoise:false,
        noiseStrength: 0.2,
        color1: [0, 0, 0],  
        color2: [255, 255, 255]
      };

      function init() {
        scene = new THREE.Scene();
        camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 30;

        renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setPixelRatio(window.devicePixelRatio * 1.5);
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

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

        const ambientLight = new THREE.AmbientLight(0x404040);
        const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
        directionalLight.position.set(5, 5, 5);
        scene.add(ambientLight, directionalLight);
        group = new THREE.Group();
        scene.add(group);
        createSphere();

        // Noise Shader
        noiseShader = {
          uniforms: {
            "tDiffuse": { value: null },
            "uTime": { value: 0.0 },
            "uNoiseStrength": { value: 0.5 },
            "uColor1": { value: new THREE.Color(0x000000) },
            "uColor2": { value: new THREE.Color(0xffffff) }
          },
          vertexShader: `
              varying vec2 vUv;
              void main() {
                  vUv = uv;
                  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
              }
          `,
          fragmentShader: `
              varying vec2 vUv;
              uniform sampler2D tDiffuse;
              uniform float uTime;
              uniform float uNoiseStrength;
              uniform vec3 uColor1;  // Uniform pour la couleur 1
              uniform vec3 uColor2;  // Uniform pour la couleur 2

              float rand(vec2 co) {
                  return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
              }

              void main() {
                  vec4 color = texture2D(tDiffuse, vUv);

                  // Générer du bruit
                  float noise = rand(vUv * uTime) * uNoiseStrength;

                  // Interpolation linéaire entre les deux couleurs en fonction du bruit
                  vec3 noiseColor = mix(uColor1, uColor2, noise);

                  // Mélanger la couleur du texture avec les couleurs bruitées
                  vec3 finalColor = 1.0 - (1.0 - color.rgb) * (1.0 - noiseColor);

                  gl_FragColor = vec4(finalColor, color.a);
              }
          `
        };

        composer = new EffectComposer(renderer);
        composer.addPass(new RenderPass(scene, camera));

        bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
        bloomPass.threshold = 0.21;
        bloomPass.strength = 0.6;
        bloomPass.radius = 0.35;
        composer.addPass(bloomPass);

        noisePass = new ShaderPass(noiseShader);
        noisePass.renderToScreen = true;
        noisePass.enabled = false;
        composer.addPass(noisePass);

        const gui = new GUI({width: '100%',closeOnTop:true, closed:true});
        gui.domElement.querySelector('.close-button').innerHTML = 'Open Controls';
        gui.add(guiParams, 'deformAmount', 0.1, 1).name('Deform Intensity');
        gui.add(guiParams, 'particleSize', .01, .1).name('Particle Size').onChange(updateParticleSize);
        gui.add(guiParams, 'transparency', 0, 1).name('Transparency').onChange(updateMaterial);
        gui.add(guiParams, 'iridescenceIntensity'.........完整代码请登录后点击上方下载按钮下载查看

网友评论0