three+webgl三维模拟下雨屋顶遮挡雨水动画效果代码

代码语言:html

所属分类:三维

代码描述:three+webgl三维模拟下雨屋顶遮挡雨水动画效果代码

代码标签: three webgl 三维 模拟 下雨 屋顶 遮挡 雨水 动画

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

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

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

  
  
  
<style>
body {
  overflow: hidden;
  margin: 0;
}
</style>

  
  
  
</head>

<body translate="no">
  <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";

console.clear();

class DepthData extends THREE.WebGLRenderTarget {
  constructor(size, camParams) {
    super(size, size);
    this.texture.minFilter = THREE.NearestFilter;
    this.texture.magFilter = THREE.NearestFilter;
    this.stencilBuffer = false;
    this.depthTexture = new THREE.DepthTexture();
    this.depthTexture.format = THREE.DepthFormat;
    this.depthTexture.type = THREE.UnsignedIntType;

    let hw = camParams.width * 0.5;
    let hh = camParams.height * 0.5;
    let d = camParams.depth;
    this.depthCam = new THREE.OrthographicCamera(-hw, hw, hh, -hh, 0, d);
    this.depthCam.layers.set(1);
    this.depthCam.position.set(0, d, 0);
    this.depthCam.lookAt(0, 0, 0);
  }

  update() {
    renderer.setRenderTarget(this);
    renderer.render(scene, this.depthCam);
    renderer.setRenderTarget(null);
  }}


class Rain extends THREE.Line {
  constructor(size, amount) {
    let v = new THREE.Vector3();
    let gBase = new THREE.BufferGeometry().setFromPoints([new THREE.Vector2(0, 0), new THREE.Vector2(0, 1)]);
    let g = new THREE.InstancedBufferGeometry().copy(gBase);
    g.setAttribute("instPos", new THREE.InstancedBufferAttribute(
    new Float32Array(
    Array.from({ length: amount }, () => {
      v.random().subScalar(0.5);
      v.y += 0.5;
      v.multiply(size);
      return [...v];
    }).flat()),
    3));

    g.instanceCount = amount;

    let m = new THREE.LineBasicMaterial({
      color: 0x4488ff,
      transparent: true,
      onBeforeCompile: shader => {
        shader.uniforms.depthData = gu.depthData;
        shader.uniforms.time = gu.time;
        shader.vertexShader = `
          uniform float time;
          
          attribute vec3 instPos;
          
          varying float colorTransition;
          varying vec3 vPos;
          ${shader.vertexShader}
        `.replace(
        `#include <begin_vertex>`,
        `#include <begin_vertex>
          
          float t = time;
          vec3 iPos = instPos;
          iPos.y = mod(20. - instPos.y - t * 5., 20.);
          
          transformed.y *= 0.5;
          transformed += iPos;
          
          vPos = transformed;
          
          colorTransition = position.y;
          `);

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

网友评论0