three+gsap三维几何体随机组合空间旋转动画效果代码

代码语言:html

所属分类:三维

代码描述:three+gsap三维几何体随机组合空间旋转动画效果代码,鼠标双击可实现更换颜色和几何体。

代码标签: three gsap 三维 几何体 随机 组合 空间 旋转 动画

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

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


    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
      body, html {
        margin: 0;
        overflow: hidden;
        font-family: Arial, sans-serif;
        background-color: #000;
        user-select: none;
      }
    </style>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/gsap.3.10.4.js"></script>
</head>

  <body>
      
          <script type="importmap">
  {
    "imports": {      
      "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/136/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 { BokehPass } from 'three/addons/postprocessing/BokehPass.js';
      import { Reflector } from 'three/addons/objects/Reflector.js';

    

      let scene, camera, renderer, controls, composer, bloomPass, shaderPass, group, spotLight, foregroundShader, bokehPass;
      let objects = [];

      init();

      function init() {

        scene = new THREE.Scene();
        scene.background = new THREE.Color(0xfed7c3); 

        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
        camera.position.set(0, 3, 5);

        renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.setPixelRatio(window.devicePixelRatio * 1.5);
        renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        renderer.shadowMap.enabled = true;
        document.body.appendChild(renderer.domElement);
        /*
        const planeReflector = new THREE.PlaneGeometry(10, 10);
        const planeReflectorBack = new THREE.PlaneGeometry(10, 10);
        const frontReflector = new Reflector(planeReflector, {
          clipBias: 0.003,
          textureWidth: window.innerWidth * window.devicePixelRatio,
          textureHeight: window.innerHeight * window.devicePixelRatio,
          color: 0x889999,
          recursion:.21
        });
        frontReflector.position.y = 1;
        frontReflector.rotateX(-Math.PI / 2);
        scene.add(frontReflector);

        const backReflector = new Reflector(planeReflectorBack, {
          clipBias: 0.003,
          textureWidth: window.innerWidth * window.devicePixelRatio,
          textureHeight: window.innerHeight * window.devicePixelRatio,
          color: 0x889999
        });
        backReflector.position.y = 1;
        backReflector.rotateX(Math.PI / 2);
        //scene.add(backReflector);
        */

        foregroundShader = {
          uniforms: {
            tDiffuse: { value: null },
            u_time: { value: 0.0 },
            u_resolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) },
            blendMode: { value: 1.0 } 
          },
          vertexShader: `
            varying vec2 vUv;
            void main() {
              vUv = uv;
              gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
            }
          `,
          fragmentShader: `
            precision mediump float;
            uniform sampler2D tDiffuse;
            uniform float u_time;
            uniform vec2 u_resolution;
            uniform float blendMode; 
            varying vec2 vUv;

            float random(vec2 st) {
              return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123);
            }

            float noise(vec2 st) {
              vec2 i = floor(st);
              vec2 f = fract(st);
              vec2 u = f * f * (3.0 - 2.0 * f);
              return mix(mix(random(i + vec2(0.0, 0.0)), random(i + vec2(1.0, 0.0)), u.x),
                         mix(random(i + vec2(0.0, 1.0)), random(i + vec2(1.0, 1.0)), u.x), u.y);
            }

            void main() {
              vec2 st = gl_FragCoord.xy / u_resolution;
              st.x *= u_resolution.x / u_resolution.y;

              float t = u_time * 0.1;
              float n = noise(st + t);

              vec4 color = texture2D(tDiffuse, vUv);
              vec4 noiseColor = vec4(vec3(n), 1.0);

              if (blendMode == 1.0) {
                gl_FragColor = mix(color, noiseColor, 0.5); 
              } else {
                gl_FragColor = color + noiseColor * 0.5; 
              }
            }
          `
        };

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

        shaderPass = new ShaderPass(foregroundShader);
        shaderPass.renderToScreen = true;
        composer.addPass(shaderPass);


        bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight));
        bloomPass.threshold = 0.1;
        bloomPass.strength = 0.08;
        bloomPass.radius = 2;
        composer.addPass(bloomPass);

        // DOF
        /*
        bokehPass = new BokehPass(scene, camera, {
          focus: 10.0,
          aperture: 0.025,
          maxblur: .25,
        });
        composer.addPass(bokehPass);
        */
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
        scene.add(ambientLight);

        spotLight = new THREE.SpotLight(0xffffff, 1);
        spotLight.position.set(5, 10, 10);
        spotLight.castShadow = true;
        scene.add(spotLight);

        group = new THREE.Group();
        scene.add(group);

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

        creat.........完整代码请登录后点击上方下载按钮下载查看

网友评论0