Skip to content

Commit 94f24f0

Browse files
author
takuma-hmng8
committed
2.19
1 parent a5c5f87 commit 94f24f0

File tree

6 files changed

+158
-6
lines changed

6 files changed

+158
-6
lines changed

app/useBrush/FxMaterial.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as THREE from "three";
2+
import { shaderMaterial } from "@react-three/drei";
3+
import fragment from "./main.frag";
4+
5+
declare global {
6+
namespace JSX {
7+
interface IntrinsicElements {
8+
fxMaterial: any;
9+
}
10+
}
11+
}
12+
13+
export type FxMaterialProps = {
14+
u_fx: THREE.Texture;
15+
};
16+
17+
export const FxMaterial = shaderMaterial(
18+
{
19+
u_fx: new THREE.Texture(),
20+
},
21+
22+
`
23+
varying vec2 vUv;
24+
void main() {
25+
vUv = uv;
26+
gl_Position = vec4(position, 1.0);
27+
}
28+
`,
29+
fragment
30+
);

app/useBrush/Playground.tsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"use client";
2+
3+
import * as THREE from "three";
4+
import { useEffect, useRef } from "react";
5+
import { useFrame, useThree, extend } from "@react-three/fiber";
6+
import {
7+
useNoise,
8+
useFluid,
9+
useFxBlending,
10+
useColorStrata,
11+
useBrightnessPicker,
12+
useChromaKey,
13+
useBeat,
14+
useRipple,
15+
useMarble,
16+
useFxTexture,
17+
useBrush,
18+
} from "@/packages/use-shader-fx/src";
19+
import {
20+
NoiseParams,
21+
NOISE_PARAMS,
22+
} from "@/packages/use-shader-fx/src/hooks/useNoise";
23+
import {
24+
ColorStrataParams,
25+
COLORSTRATA_PARAMS,
26+
} from "@/packages/use-shader-fx/src/hooks/useColorStrata";
27+
import GUI from "lil-gui";
28+
import { useGUI } from "@/utils/useGUI";
29+
import { FxMaterial, FxMaterialProps } from "./FxMaterial";
30+
import { useTexture, useVideoTexture } from "@react-three/drei";
31+
import { Console } from "console";
32+
33+
extend({ FxMaterial });
34+
35+
/*===============================================
36+
TODO:
37+
~~ 初期状態で真ん中に来ないようにする ~~
38+
39+
- falloffつける:速度に応じて小さくなって消滅する係数
40+
- disipaccionと関係sる値
41+
- uniform名、変数名を満足いくものにする
42+
43+
- colorをfluidみたく関数しましょっか
44+
45+
- パフォーマンス
46+
47+
- あんまうまくいかなかったら、useFlowmapを別途つくる?
48+
- パクリすぎるかも?
49+
===============================================*/
50+
51+
export const Playground = () => {
52+
const ref = useRef<FxMaterialProps>();
53+
54+
const { size, viewport } = useThree();
55+
// const bbbb = useVideoTexture("/bbbb.mov");
56+
// const glitch = useVideoTexture("/glitch.mov");
57+
const [updateBrush, setBrush, { output }] = useBrush({
58+
size,
59+
dpr: viewport.dpr,
60+
});
61+
62+
setBrush({
63+
radius: 0.4,
64+
smudge: 4,
65+
motionBlur: 3,
66+
motionSample: 8,
67+
dissipation: 0.93,
68+
});
69+
70+
// const updateBeat = useBeat(157);
71+
72+
useFrame((props) => {
73+
updateBrush(props);
74+
});
75+
76+
return (
77+
<mesh>
78+
<planeGeometry args={[2, 2]} />
79+
<fxMaterial key={FxMaterial.key} u_fx={output} ref={ref} />
80+
</mesh>
81+
);
82+
};

app/useBrush/main.frag

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
precision highp float;
2+
varying vec2 vUv;
3+
uniform sampler2D u_fx;
4+
5+
void main() {
6+
vec2 uv = vUv;
7+
8+
vec4 fx = texture2D(u_fx, uv);
9+
10+
gl_FragColor = fx;
11+
}

app/useBrush/page.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ShaderFx } from "../ShaderFx";
2+
import { Playground } from "./Playground";
3+
4+
export default function Page() {
5+
return (
6+
<div style={{ width: "100%" }}>
7+
<div
8+
style={{
9+
position: "fixed",
10+
width: "100%",
11+
height: "100svh",
12+
pointerEvents: "none",
13+
}}>
14+
<ShaderFx>
15+
<Playground />
16+
</ShaderFx>
17+
</div>
18+
</div>
19+
);
20+
}

packages/use-shader-fx/src/hooks/useBrush/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const BRUSH_PARAMS: BrushParams = {
4141
dissipation: 1.0,
4242
motionBlur: 0.0,
4343
motionSample: 5,
44-
color: new THREE.Color(0xffffff),
44+
color: new THREE.Color(0xff0000),
4545
};
4646

4747
/**

packages/use-shader-fx/src/hooks/useBrush/shader/main.frag

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ float isOnLine(vec2 point, vec2 start, vec2 end, float width, float aspect) {
4444
return float(withinLine);
4545
}
4646

47+
// IDEA : マルチサンプリングしてるんだけど、もっといい方法ありそうだけどな〜
48+
// IDEA ; 普通にマウスからの距離でいけないかな〜
4749
vec4 createSmudge(){
4850
vec2 offsets[9];
4951
offsets[0] = vec2(-1, -1); offsets[1] = vec2( 0, -1); offsets[2] = vec2( 1, -1);
@@ -60,6 +62,7 @@ vec4 createSmudge(){
6062
return smudgedColor / 9.0;
6163
}
6264

65+
//IDEA : 速度からサンプリングして擬似的なモーションブラーしてるんだけど、ここで速度による拡散作れないかな〜
6366
vec4 createMotionBlur(vec4 baseColor, vec2 velocity, float motion, int samples) {
6467
vec4 motionBlurredColor = baseColor;
6568
vec2 scaledVelocity = velocity * motion;
@@ -71,9 +74,7 @@ vec4 createMotionBlur(vec4 baseColor, vec2 velocity, float motion, int samples)
7174
return motionBlurredColor / float(samples);
7275
}
7376

74-
7577
void main() {
76-
// Convert UV coordinates to range [-1, 1]
7778
vec2 st = vUv * 2.0 - 1.0;
7879

7980
// velocity vector
@@ -94,11 +95,19 @@ void main() {
9495
vec3 color = uColor;
9596

9697
// map texture to color
98+
// TODO:ここのミックスもよう使い勝手わるい
9799
vec4 textureColor = texture2D(uTexture, vUv);
98100
vec3 finalColor = mix(color, textureColor.rgb, textureColor.a);
99101

100-
float onLine = isOnLine(st, uPrevMouse, uMouse, modifiedRadius, uAspect);
101-
bufferColor.rgb = mix(bufferColor.rgb, finalColor, onLine);
102+
// ここ0 || 1で かえって来る
103+
float onLine = length(uVelocity) > 0. ? isOnLine(st, uPrevMouse, uMouse, modifiedRadius, uAspect) : .0;
102104

103-
gl_FragColor = vec4(bufferColor.rgb,1.0);
105+
// ここで、onlineの値に乗算するのが良さそう falloff
106+
// float falloff = smoothstep(.9, 0.0, onLine);
107+
108+
bufferColor.rgb = mix(bufferColor.rgb, finalColor, onLine);
109+
110+
gl_FragColor = vec4(bufferColor.rgb,1.);
111+
112+
104113
}

0 commit comments

Comments
 (0)