three实现三维空间液态彩色液体融合交互动画效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维空间液态彩色液体融合交互动画效果代码

代码标签: three 三维 空间 液态 彩色 液体 融合 交互 动画

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

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.84.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/postprocessing.2.3.1.js"></script>
    </head>
    <body>

        <!-- partial -->
        <script >
            const {
  WebGLRenderer, Scene, PerspectiveCamera,
  MeshBasicMaterial, Mesh, Color, TextureLoader, Clock,
  Vector3, PlaneBufferGeometry, ShaderMaterial } =
THREE;

const {
  EffectComposer, RenderPass, BloomPass, Pass } =
POSTPROCESSING;

const getRandomFloat = (min, max) => Math.random() * (max - min) + min;

const clock = new Clock();

const COLORS = [
'#336699',
'#7AAABB',
'#86BBD8',
'#9EE493'];


/* ---- METABALL CUSTOM PASS ---- */
const metaballShader = {
  uniforms: {
    tDiffuse: { value: null } },

  vertexShader: [
  "varying vec2 vUv;",
  "void main() {",
  "vUv = uv;",
  "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  "}"].
  join("\n"),
  fragmentShader: [
  "uniform sampler2D tDiffuse;",
  "varying vec2 vUv;",
  "void main() {",
  "vec4 currentScreen = texture2D(tDiffuse, vUv);",
  "gl_FragColor = vec4(currentScreen.xyz * 1.5, currentScreen.w) * max(sign(currentScreen.w - 0.8), 0.0);",
  "}"].
  join("\n") };


class MetaballPass extends Pass {
  constructor() {
    super();
    this.name = 'MetaballPass';
    this.needsSwap = true;
    this.material = new ShaderMaterial(metaballShader);
    this.quad.material = this.material;
  }
  render(renderer, readBuffer, writeBuffer) {
    this.material.uniforms.tDiffuse.value = readBuffer.texture;
    renderer.render(this.scene, this.camera, this.renderToScreen ? null : writeBuffer);
  }}


/* ---- CORE ---- */
let windowWidth = window.innerWidth;
let windowHeight = window.innerHeight;
class Webgl {
  constructor(w, h) {
    this.meshCount = 0;
    this.meshListeners = [];
    this.renderer = new WebGLRenderer({ antialias: true, alpha: true });
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.scene = new Scene();
    this.camera = new PerspectiveCamera(50, w / h, 1, 1000);
    this.camera.position.set(0, 0, 10);
    this.dom = this.renderer.domElement;
    this._composer = false;
    this._passes = {};
    this.initPostprocessing();
    this.update = this.update.bind(this);
    this.resize = this.resize.bind(this);
    this.resize(w, h); // set render size
  }
  add(mesh) {
    this.scene.add(mesh);
    if (!mesh.update) return;
    this.meshListeners.push(mesh.update);
    this.meshCount++;
  }
  update() {
    let i = this.meshCount;
    while (--i >= 0) {
      this.meshListeners[i].apply(this, null);
    }
    this._composer.render(clock.getDelta());
  }
  resize(w, h) {
    this.camera.aspect = w / h;
    this.camera.updateProjectionMatrix();
    this.renderer.setSize(w, h);
    this._composer.setSize(w, h);
  }

  initPostprocessing() {
    this._composer = new EffectComposer(this.renderer, {
      // stencilBuffer: true,
      // depthTexture: true,
    });
    // PASSES
    const renderPass = new RenderPass(this.scene, this.camera);
 .........完整代码请登录后点击上方下载按钮下载查看

网友评论0