Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ From each `fxHooks`, you can receive [`updateFx`, `setParams`, `fxObject`] in ar

1. `updateFx` - A function to be invoked inside `useFrame`, returning a `THREE.Texture`.
2. `setParams` - A function to refresh the parameters, beneficial for performance tweaking, etc.
3. `fxObject` - An object that holds various FX components, such as scene, camera, material, and renderTarget.
3. `fxObject` - An object that holds various FX components, such as scene, camera, material,renderTarget, and `output`(final rendered texture).

```js
const [updateFx, setParams, fxObject] = useSomeFx(config);
Expand Down Expand Up @@ -43,13 +43,11 @@ import { useRef } from "react";
import { useFrame, useThree } from "@react-three/fiber";
import { useFluid } from "@hmng8/use-shader-fx";

export const Demo = () => {
export const Home = () => {
const ref = useRef<THREE.ShaderMaterial>(null);
const { size, viewport } = useThree();
const [updateFluid] = useFluid({ size, dpr: viewport.dpr });
useFrame((props) => {
ref.current!.uniforms.u_fx.value = updateFluid(props);
});
const [updateFluid, , { output }] = useFluid({ size, dpr: viewport.dpr });
useFrame((props) => updateFluid(props));

return (
<mesh>
Expand All @@ -74,7 +72,7 @@ export const Demo = () => {
}
`}
uniforms={{
u_fx: { value: null },
u_fx: { value: output },
}}
/>
</mesh>
Expand Down Expand Up @@ -121,22 +119,16 @@ function Box(props: any) {
export const Home = () => {
const ref = useRef<THREE.ShaderMaterial>(null);
const { size, viewport, camera } = useThree();
const [updateNoise, setNoise] = useNoise({ size, dpr: viewport.dpr });

setNoise({
scale: 0.01,
warpOctaves: 1,
noiseOctaves: 1,
fbmOctaves: 1,
timeStrength: 1.2,
warpStrength: 20.0,
const [updateNoise, , { output }] = useNoise({
size,
dpr: viewport.dpr,
});

// This scene is rendered offscreen
const offscreenScene = useMemo(() => new THREE.Scene(), []);

// create FBO for offscreen rendering
const [_, updateRenderTarget] = useSingleFBO({
const [boxView, updateRenderTarget] = useSingleFBO({
scene: offscreenScene,
camera,
size,
Expand All @@ -145,9 +137,8 @@ export const Home = () => {
});

useFrame((props) => {
const noise = updateNoise(props);
ref.current!.uniforms.u_fx.value = noise;
ref.current!.uniforms.u_texture.value = updateRenderTarget(props.gl);
updateNoise(props);
updateRenderTarget(props.gl);
});

return (
Expand Down Expand Up @@ -201,8 +192,8 @@ export const Home = () => {
}
`}
uniforms={{
u_texture: { value: null },
u_fx: { value: null },
u_texture: { value: boxView.texture },
u_fx: { value: output },
}}
/>
</mesh>
Expand Down
9 changes: 4 additions & 5 deletions app/_home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const Home = () => {
const offscreenScene = useMemo(() => new THREE.Scene(), []);

// create FBO for offscreen rendering
const [_, updateRenderTarget] = useSingleFBO({
const [boxView, updateRenderTarget] = useSingleFBO({
scene: offscreenScene,
camera,
size,
Expand All @@ -95,9 +95,8 @@ export const Home = () => {

useFrame((props) => {
const noise = updateNoise(props);
const fluid = updateFluid(props);
const blending = updateFxBlending(props, {
texture: fluid,
texture: updateFluid(props),
map: noise,
});
const picked = updateBrightnessPicker(props, {
Expand All @@ -107,8 +106,8 @@ export const Home = () => {
texture: picked,
noise: noise,
});
updateRenderTarget(props.gl);
ref.current!.uniforms.u_fx.value = colorStrata;
ref.current!.uniforms.u_texture.value = updateRenderTarget(props.gl);
});

return (
Expand Down Expand Up @@ -173,7 +172,7 @@ export const Home = () => {
}
`}
uniforms={{
u_texture: { value: null },
u_texture: { value: boxView.texture },
u_fx: { value: null },
}}
/>
Expand Down
Loading