File size: 6,340 Bytes
01488bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { createPortal, useFrame, useThree } from "@react-three/fiber";
import { useCallback, useMemo, useRef, useState } from "react";
import { Camera, Color, Mesh, Scene, Texture, Vector2, Vector3 } from "three";
import { ShaderPass } from "three/examples/jsm/Addons.js";
import { Effect as FluidEffect } from "./effect/Fluid";
import { useFBOs } from "./hooks/useFBOs";
import { useMaterials } from "./hooks/useMaterials";
import { DEFAULT_CONFIG } from "./constant";
import { usePointer } from "./hooks/usePointer";
import { normalizeScreenHz } from "./utils";
import { type FluidProps } from "./types";

type MaterialName = keyof ReturnType<typeof useMaterials>;
type FBONames = keyof ReturnType<typeof useFBOs>;

type Uniforms = {
  uColor: Vector3 | Color;
  uPointer: Vector2;
  uTarget: Texture | null;
  uVelocity: Texture;
  uCurl: Texture;
  uTexture: Texture;
  uPressure: Texture;
  uDivergence: Texture;
  uSource: Texture;
  uRadius: number;
  uClearValue: number;
  uCurlValue: number;
  uDissipation: number;
};

export const Fluid = ({
  blend = DEFAULT_CONFIG.blend,
  force = DEFAULT_CONFIG.force,
  radius = DEFAULT_CONFIG.radius,
  curl = DEFAULT_CONFIG.curl,
  swirl = DEFAULT_CONFIG.swirl,
  intensity = DEFAULT_CONFIG.intensity,
  distortion = DEFAULT_CONFIG.distortion,
  fluidColor = DEFAULT_CONFIG.fluidColor,
  backgroundColor = DEFAULT_CONFIG.backgroundColor,
  showBackground = DEFAULT_CONFIG.showBackground,
  rainbow = DEFAULT_CONFIG.rainbow,
  pressure = DEFAULT_CONFIG.pressure,
  densityDissipation = DEFAULT_CONFIG.densityDissipation,
  velocityDissipation = DEFAULT_CONFIG.velocityDissipation,
  blendFunction = DEFAULT_CONFIG.blendFunction,
}: FluidProps) => {
  const size = useThree((three) => three.size);
  const gl = useThree((three) => three.gl);

  const [bufferScene] = useState(() => new Scene());
  const bufferCamera = useMemo(() => new Camera(), []);

  const meshRef = useRef<Mesh>(null);
  const postRef = useRef<ShaderPass>(null);
  const pointerRef = useRef(new Vector2());
  const colorRef = useRef(new Vector3());

  const FBOs = useFBOs();
  const materials = useMaterials();
  const splatStack = usePointer({ force });

  const setShaderMaterial = useCallback(
    (name: MaterialName) => {
      if (!meshRef.current) return;

      meshRef.current.material = materials[name];
      meshRef.current.material.needsUpdate = true;
    },
    [materials],
  );

  const setRenderTarget = useCallback(
    (name: FBONames) => {
      const target = FBOs[name];

      if ("write" in target) {
        gl.setRenderTarget(target.write);
        gl.clear();
        gl.render(bufferScene, bufferCamera);
        target.swap();
      } else {
        gl.setRenderTarget(target);
        gl.clear();
        gl.render(bufferScene, bufferCamera);
      }
    },
    [bufferCamera, bufferScene, FBOs, gl],
  );

  const setUniforms = useCallback(
    <K extends keyof Uniforms>(
      material: MaterialName,
      uniform: K,
      value: Uniforms[K],
    ) => {
      const mat = materials[material];

      if (mat && mat.uniforms[uniform]) {
        mat.uniforms[uniform].value = value;
      }
    },
    [materials],
  );

  useFrame((_, delta) => {
    if (!meshRef.current || !postRef.current) return;

    for (let i = splatStack.length - 1; i >= 0; i--) {
      const { mouseX, mouseY, velocityX, velocityY } = splatStack[i];

      pointerRef.current.set(mouseX, mouseY);
      colorRef.current.set(velocityX, velocityY, 10.0);

      setShaderMaterial("splat");
      setUniforms("splat", "uTarget", FBOs.velocity.read.texture);
      setUniforms("splat", "uPointer", pointerRef.current);
      setUniforms("splat", "uColor", colorRef.current);
      setUniforms("splat", "uRadius", radius / 100.0);
      setRenderTarget("velocity");
      setUniforms("splat", "uTarget", FBOs.density.read.texture);
      setRenderTarget("density");

      splatStack.pop();
    }

    setShaderMaterial("curl");
    setUniforms("curl", "uVelocity", FBOs.velocity.read.texture);
    setRenderTarget("curl");

    setShaderMaterial("vorticity");
    setUniforms("vorticity", "uVelocity", FBOs.velocity.read.texture);
    setUniforms("vorticity", "uCurl", FBOs.curl.texture);
    setUniforms("vorticity", "uCurlValue", curl);
    setRenderTarget("velocity");

    setShaderMaterial("divergence");
    setUniforms("divergence", "uVelocity", FBOs.velocity.read.texture);
    setRenderTarget("divergence");

    setShaderMaterial("clear");
    setUniforms("clear", "uTexture", FBOs.pressure.read.texture);
    setUniforms("clear", "uClearValue", normalizeScreenHz(pressure, delta));
    setRenderTarget("pressure");

    setShaderMaterial("pressure");
    setUniforms("pressure", "uDivergence", FBOs.divergence.texture);

    for (let i = 0; i < swirl; i++) {
      setUniforms("pressure", "uPressure", FBOs.pressure.read.texture);
      setRenderTarget("pressure");
    }

    setShaderMaterial("gradientSubstract");
    setUniforms("gradientSubstract", "uPressure", FBOs.pressure.read.texture);
    setUniforms("gradientSubstract", "uVelocity", FBOs.velocity.read.texture);
    setRenderTarget("velocity");

    setShaderMaterial("advection");
    setUniforms("advection", "uVelocity", FBOs.velocity.read.texture);
    setUniforms("advection", "uSource", FBOs.velocity.read.texture);
    setUniforms(
      "advection",
      "uDissipation",
      normalizeScreenHz(velocityDissipation, delta),
    );

    setRenderTarget("velocity");
    setUniforms("advection", "uVelocity", FBOs.velocity.read.texture);
    setUniforms("advection", "uSource", FBOs.density.read.texture);
    setUniforms(
      "advection",
      "uDissipation",
      normalizeScreenHz(densityDissipation, delta),
    );

    setRenderTarget("density");
  });

  return (
    <>
      {createPortal(
        <mesh ref={meshRef} scale={[size.width, size.height, 1]}>
          <planeGeometry args={[2, 2]} />
        </mesh>,
        bufferScene,
      )}

      <FluidEffect
        blendFunction={blendFunction}
        intensity={intensity}
        rainbow={rainbow}
        distortion={distortion}
        backgroundColor={backgroundColor}
        blend={blend}
        fluidColor={fluidColor}
        showBackground={showBackground}
        ref={postRef}
        tFluid={FBOs.density.read.texture}
      />
    </>
  );
};