Spaces:
Running
Running
File size: 1,903 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 | import { Effect, EffectAttribute } from "postprocessing";
import { Texture, Uniform, Vector3 } from "three";
import compositeFrag from "../glsl/composite.frag?raw";
import { hexToRgb } from "../utils";
import { type EffectProps } from "../types";
type Uniforms = {
tFluid: Texture;
uColor: Vector3;
uBackgroundColor: Vector3;
uRainbow: boolean;
uShowBackground: boolean;
uDistort: number;
uBlend: number;
uIntensity: number;
};
export class FluidEffect extends Effect {
state: EffectProps;
constructor(props: EffectProps) {
const uniforms: Record<keyof Uniforms, Uniform> = {
tFluid: new Uniform(props.tFluid),
uDistort: new Uniform(props.distortion),
uRainbow: new Uniform(props.rainbow),
uIntensity: new Uniform(props.intensity),
uBlend: new Uniform(props.blend),
uShowBackground: new Uniform(props.showBackground),
uColor: new Uniform(hexToRgb(props.fluidColor!)),
uBackgroundColor: new Uniform(hexToRgb(props.backgroundColor!)),
};
super("FluidEffect", compositeFrag, {
blendFunction: props.blendFunction,
attributes: EffectAttribute.CONVOLUTION,
uniforms: new Map(Object.entries(uniforms)),
});
this.state = props;
}
private updateUniform<K extends keyof Uniforms>(key: K, value: Uniforms[K]) {
const uniform = this.uniforms.get(key);
if (uniform) {
uniform.value = value;
}
}
update() {
this.updateUniform("uIntensity", this.state.intensity);
this.updateUniform("uDistort", this.state.distortion);
this.updateUniform("uRainbow", this.state.rainbow);
this.updateUniform("uBlend", this.state.blend);
this.updateUniform("uShowBackground", this.state.showBackground);
this.updateUniform("uColor", hexToRgb(this.state.fluidColor));
this.updateUniform(
"uBackgroundColor",
hexToRgb(this.state.backgroundColor),
);
}
}
|