Skip to content

Commit a6d907d

Browse files
committed
Move useTweaks and rename shader
1 parent 9d997a2 commit a6d907d

File tree

4 files changed

+225
-7
lines changed

4 files changed

+225
-7
lines changed

app/page.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
'use client';
22

3-
import { useRef } from 'react';
3+
import { useEffect, useRef } from 'react';
44
import { Canvas, useFrame, useThree } from '@react-three/fiber';
55
import fragmentShader from './shaders/fragment.glsl';
66
import vertexShader from './shaders/vertex.glsl';
77
import * as THREE from 'three';
8+
import { useTweaks } from 'use-tweaks';
89

9-
function Box(props: any) {
10+
function Shader(props: any) {
1011
const shaderRef = useRef<THREE.ShaderMaterial | null>(null);
1112
const { viewport } = useThree();
13+
const { opacity } = useTweaks({
14+
opacity: { value: 1, min: 0, max: 1, step: 0.05 },
15+
});
1216
useFrame((state) => {
1317
if (shaderRef.current) {
1418
shaderRef.current.uniforms.uTime.value += 0.01;
19+
shaderRef.current.uniforms.uOpacity.value = opacity;
1520
shaderRef.current.uniforms.uPointer.value = state.pointer;
21+
// console.log(opacity);
1622
}
1723
});
1824

@@ -26,6 +32,7 @@ function Box(props: any) {
2632
uniforms={{
2733
uTime: { value: 0.0 },
2834
uPointer: { value: new THREE.Vector2(0.5, 0.5) },
35+
uOpacity: { value: 0.0 },
2936
}}
3037
/>
3138
</mesh>
@@ -38,7 +45,7 @@ export default function Home() {
3845
<Canvas orthographic>
3946
<ambientLight intensity={Math.PI / 2} />
4047
<pointLight position={[-10, -10, -10]} decay={0} intensity={Math.PI} />
41-
<Box props={{ position: [0, 0, 0] }} />
48+
<Shader props={{ position: [0, 0, 0] }} />
4249
</Canvas>
4350
</main>
4451
);

app/shaders/fragment.glsl

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
uniform float uTime;
21
varying vec2 vUv;
2+
3+
uniform float uTime;
34
uniform vec2 uPointer;
5+
uniform float uOpacity;
46

57
void main(void)
68
{
@@ -15,6 +17,6 @@ void main(void)
1517
color.g+=mix(.25,1.,vUv.y*pointer.y);
1618
color.b+=1.;
1719

18-
vec4 outputColor=vec4(color,1.);
20+
vec4 outputColor=vec4(color,uOpacity);
1921
gl_FragColor=outputColor;
2022
}

0 commit comments

Comments
 (0)