feat: add effects, move canvas style to css
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
.canvas {
|
||||||
|
width: 100vw !important;
|
||||||
|
height: 100vh !important;
|
||||||
|
}
|
||||||
+73
-32
@@ -1,45 +1,86 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { OrbitControls } from "@react-three/drei";
|
import { OrbitControls } from '@react-three/drei';
|
||||||
import { Canvas, useFrame, useLoader } from "@react-three/fiber";
|
import { Canvas, useFrame, useLoader } from '@react-three/fiber';
|
||||||
import { useRef } from "react";
|
import {
|
||||||
|
Bloom,
|
||||||
|
EffectComposer,
|
||||||
|
GodRays,
|
||||||
|
Noise
|
||||||
|
} from '@react-three/postprocessing';
|
||||||
|
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
|
||||||
|
|
||||||
import * as THREE from 'three';
|
import { Mesh, TextureLoader } from 'three';
|
||||||
|
|
||||||
function SealCube() {
|
import './page.css';
|
||||||
const meshRef = useRef<THREE.Mesh>(null);
|
|
||||||
const texture = useLoader(THREE.TextureLoader, '/img/niko.jpg');
|
|
||||||
|
|
||||||
useFrame((state, delta) => {
|
const SealCube = forwardRef<Mesh>((props, ref) => {
|
||||||
if (meshRef.current) {
|
const texture = useLoader(TextureLoader, '/img/niko.jpg');
|
||||||
meshRef.current.rotation.x += delta * 0.5;
|
const meshRef = useRef<Mesh>(null);
|
||||||
meshRef.current.rotation.y += delta * 0.5;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
useImperativeHandle(ref, () => meshRef.current!, []);
|
||||||
<mesh ref={meshRef} castShadow receiveShadow>
|
|
||||||
<boxGeometry args={[2.5, 2.5, 2.5]} />
|
|
||||||
<meshBasicMaterial map={texture} />
|
|
||||||
</mesh>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
useFrame((state, delta) => {
|
||||||
|
if (meshRef.current) {
|
||||||
|
meshRef.current.rotation.x += delta * 0.5;
|
||||||
|
meshRef.current.rotation.y += delta * 0.5;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<mesh ref={meshRef} castShadow receiveShadow {...props}>
|
||||||
|
<boxGeometry args={[2.5, 2.5, 2.5]} />
|
||||||
|
<meshBasicMaterial map={texture} />
|
||||||
|
</mesh>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
SealCube.displayName = 'SealCube';
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
return (
|
const [sunMesh, setSunMesh] = useState<Mesh | null>(null);
|
||||||
<>
|
|
||||||
<Canvas
|
|
||||||
shadows
|
|
||||||
camera={{ position: [5, 5, 5], fov: 50 }}
|
|
||||||
gl={{ antialias: true }}
|
|
||||||
style={{ width: "100vw", height: "100vh" }}>
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Canvas
|
||||||
|
shadows
|
||||||
|
camera={{ position: [5, 5, 5], fov: 50 }}
|
||||||
|
gl={{ antialias: true }}
|
||||||
|
className='canvas'
|
||||||
|
>
|
||||||
|
<SealCube
|
||||||
|
ref={(mesh) => {
|
||||||
|
if (mesh && !sunMesh) {
|
||||||
|
setSunMesh(mesh);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<EffectComposer>
|
||||||
|
<Noise opacity={0.1} />
|
||||||
|
<Bloom
|
||||||
|
intensity={2}
|
||||||
|
luminanceThreshold={0.5}
|
||||||
|
luminanceSmoothing={0.1}
|
||||||
|
/>
|
||||||
|
{
|
||||||
|
sunMesh ? (
|
||||||
|
<GodRays
|
||||||
|
sun={sunMesh}
|
||||||
|
samples={30}
|
||||||
|
density={1.5}
|
||||||
|
decay={0.8}
|
||||||
|
weight={0.2}
|
||||||
|
exposure={0.5}
|
||||||
|
clampMax={1}
|
||||||
|
blur={true}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<></>
|
||||||
|
)
|
||||||
|
// it feels genuinely so cursed to have to do : <></> just because EffectComposer can't handle null
|
||||||
|
}
|
||||||
|
</EffectComposer>
|
||||||
|
|
||||||
<SealCube />
|
<OrbitControls />
|
||||||
<OrbitControls />
|
</Canvas>
|
||||||
</Canvas>
|
);
|
||||||
</>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user