Skip to content

Commit a4cac02

Browse files
author
takuma-hmng8
committed
add useFxBlending
1 parent c7b902d commit a4cac02

File tree

20 files changed

+633
-272
lines changed

20 files changed

+633
-272
lines changed

.storybook/stories/UseBlending.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const setConfig = () => {
3131
};
3232

3333
/**
34-
* Blending the texture passed as map
34+
* Blend map to texture. You can set the threshold for blending with brightness. You can set the dodge color by setting color. If you don't want to reflect the map's color, you can use useFxBlending instead.
3535
*/
3636
export const UseBlending = (args: BlendingParams) => {
3737
const updateGUI = useGUI(setGUI);
@@ -51,9 +51,9 @@ export const UseBlending = (args: BlendingParams) => {
5151
});
5252
const noise = updateNoise(props);
5353
const fx = updateBlending(props, {
54+
...setConfig(),
5455
texture: bgTexture,
5556
map: noise,
56-
...setConfig(),
5757
});
5858
fxRef.current!.u_fx = fx;
5959
updateGUI();

.storybook/stories/UseFxBlending.tsx

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as React from "react";
2+
import * as THREE from "three";
3+
import { useFrame, useLoader, extend, useThree } from "@react-three/fiber";
4+
import { FxMaterial, FxMaterialProps } from "../../utils/fxMaterial";
5+
import { CONSTANT } from "../constant";
6+
import GUI from "lil-gui";
7+
import { useGUI } from "../../utils/useGUI";
8+
import {
9+
useFxBlending,
10+
useFluid,
11+
useNoise,
12+
} from "../../packages/use-shader-fx/src";
13+
import {
14+
FxBlendingParams,
15+
FXBLENDING_PARAMS,
16+
} from "../../packages/use-shader-fx/src/hooks/useFxBlending";
17+
18+
extend({ FxMaterial });
19+
20+
const CONFIG: FxBlendingParams = structuredClone(FXBLENDING_PARAMS);
21+
const setGUI = (gui: GUI) => {
22+
gui.add(CONFIG, "mapIntensity", 0, 1, 0.01);
23+
};
24+
const setConfig = () => {
25+
return {
26+
...CONFIG,
27+
} as FxBlendingParams;
28+
};
29+
30+
/**
31+
* Blend map to texture. You can change the intensity of fx applied by the rg value of map. Unlike "useBlending", the map color is not reflected.
32+
*/
33+
export const UseFxBlending = (args: FxBlendingParams) => {
34+
const updateGUI = useGUI(setGUI);
35+
const fxRef = React.useRef<FxMaterialProps>();
36+
const { size, dpr } = useThree((state) => {
37+
return { size: state.size, dpr: state.viewport.dpr };
38+
});
39+
const [updateFluid] = useFluid({ size, dpr });
40+
const [updateNoise] = useNoise({ size, dpr });
41+
const [updateFxBlending] = useFxBlending({ size, dpr });
42+
43+
useFrame((props) => {
44+
const noise = updateNoise(props);
45+
const fluid = updateFluid(props);
46+
const blending = updateFxBlending(props, {
47+
...setConfig(),
48+
texture: fluid,
49+
map: noise,
50+
});
51+
fxRef.current!.u_fx = blending;
52+
updateGUI();
53+
});
54+
55+
return (
56+
<mesh>
57+
<planeGeometry args={[2, 2]} />
58+
<fxMaterial key={FxMaterial.key} ref={fxRef} />
59+
</mesh>
60+
);
61+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from "react";
2+
import type { StoryObj } from "@storybook/react";
3+
import { setArgTypes } from "../utils/setArgTypes";
4+
import { Setup } from "../utils/Setup";
5+
import type { Meta } from "@storybook/react";
6+
import { UseFxBlending } from "./UseFxBlending";
7+
import {
8+
FxBlendingParams,
9+
FXBLENDING_PARAMS,
10+
} from "../../packages/use-shader-fx/src/hooks/useFxBlending";
11+
12+
const meta = {
13+
title: "useFxBlending",
14+
component: UseFxBlending,
15+
tags: ["autodocs"],
16+
decorators: [(storyFn: any) => <Setup>{storyFn()}</Setup>],
17+
} satisfies Meta<typeof UseFxBlending>;
18+
export default meta;
19+
type Story = StoryObj<typeof meta>;
20+
21+
export const Default: Story = {
22+
args: FXBLENDING_PARAMS,
23+
argTypes: setArgTypes<FxBlendingParams>(FXBLENDING_PARAMS),
24+
};

app/_home/index.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useFrame, useThree } from "@react-three/fiber";
44
import {
55
useNoise,
66
useFluid,
7-
useBlending,
7+
useFxBlending,
88
useColorStrata,
99
useBrightnessPicker,
1010
} from "@/packages/use-shader-fx/src";
@@ -16,13 +16,12 @@ export const Home = () => {
1616
});
1717
const [updateNoise, setNoise] = useNoise({ size, dpr });
1818
const [updateFluid, setFluid] = useFluid({ size, dpr });
19-
const [updateBlending, setBlending] = useBlending({ size, dpr });
19+
const [updateFxBlending, setFxBlending] = useFxBlending({ size, dpr });
2020
const [updateColorStrata, setColorStrata] = useColorStrata({ size, dpr });
2121
const [updateBrightnessPicker] = useBrightnessPicker({ size, dpr });
2222

23-
setBlending({
23+
setFxBlending({
2424
mapIntensity: 0.45,
25-
brightness: new THREE.Vector3(0.2, 0.2, 0.2),
2625
});
2726

2827
setNoise({
@@ -53,7 +52,7 @@ export const Home = () => {
5352
useFrame((props) => {
5453
const noise = updateNoise(props);
5554
const fluid = updateFluid(props);
56-
const blending = updateBlending(props, {
55+
const blending = updateFxBlending(props, {
5756
texture: fluid,
5857
map: noise,
5958
});

0 commit comments

Comments
 (0)