feat: add loading effect
This commit is contained in:
+73
-48
@@ -3,7 +3,8 @@
|
||||
import {
|
||||
Environment,
|
||||
OrbitControls,
|
||||
PerspectiveCamera
|
||||
PerspectiveCamera,
|
||||
useProgress
|
||||
} from '@react-three/drei';
|
||||
import { Canvas, useLoader, useFrame } from '@react-three/fiber';
|
||||
import {
|
||||
@@ -24,7 +25,8 @@ import {
|
||||
DoubleSide,
|
||||
TextureLoader,
|
||||
Color,
|
||||
MeshStandardMaterial
|
||||
MeshStandardMaterial,
|
||||
FogExp2
|
||||
} from 'three';
|
||||
|
||||
import './page.css';
|
||||
@@ -613,57 +615,80 @@ const SealCube = forwardRef<Mesh>((props, ref) => {
|
||||
});
|
||||
SealCube.displayName = 'SealCube';
|
||||
|
||||
function Loader() {
|
||||
const { progress, active } = useProgress();
|
||||
const [visible, setVisible] = useState(true);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!active && progress === 100) {
|
||||
const timeout = setTimeout(() => setVisible(false), 500);
|
||||
return () => clearTimeout(timeout);
|
||||
}
|
||||
}, [progress, active]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`loader ${!visible ? 'loader hidden' : ''}`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
const [sealMesh, setSealMesh] = useState<Mesh | null>(null);
|
||||
|
||||
return (
|
||||
<Canvas
|
||||
shadows
|
||||
camera={{ position: [0, 5, 15], fov: 50, far: 100 }}
|
||||
gl={{ antialias: true }}
|
||||
className='canvas'
|
||||
>
|
||||
<EffectComposer>
|
||||
<DepthOfField target={[0, 3, 0]} focalLength={10} bokehScale={5} />
|
||||
<Vignette />
|
||||
<Noise opacity={0.05} />
|
||||
<Bloom
|
||||
intensity={2}
|
||||
luminanceThreshold={0.5}
|
||||
luminanceSmoothing={0.1}
|
||||
<>
|
||||
<Loader />
|
||||
<Canvas
|
||||
shadows
|
||||
camera={{ position: [0, 5, 15], fov: 50, far: 100 }}
|
||||
gl={{ antialias: true }}
|
||||
className='canvas'
|
||||
>
|
||||
<EffectComposer>
|
||||
<DepthOfField target={[0, 3, 0]} focalLength={10} bokehScale={5} />
|
||||
<Vignette />
|
||||
<Noise opacity={0.05} />
|
||||
<Bloom
|
||||
intensity={2}
|
||||
luminanceThreshold={0.5}
|
||||
luminanceSmoothing={0.1}
|
||||
/>
|
||||
<SMAA />
|
||||
</EffectComposer>
|
||||
|
||||
<Environment files={'hdr/sky.hdr'} environmentIntensity={1} background />
|
||||
<fogExp2 attach='fog' args={[0x9A9A9A, 0.01]} />
|
||||
|
||||
|
||||
<Terrain
|
||||
chunks={16}
|
||||
chunkSize={10.0}
|
||||
resolution={8.0}
|
||||
scale={1}
|
||||
hillScale={0.1}
|
||||
hillHeight={6.0}
|
||||
detailScale={1.0}
|
||||
detailHeight={0.2}
|
||||
wireframe={false}
|
||||
grassCount={6000}
|
||||
grassSize={0.6}
|
||||
grassLOD={60}
|
||||
/>
|
||||
<SealCube
|
||||
ref={(mesh) => {
|
||||
if (mesh && !sealMesh) setSealMesh(mesh);
|
||||
}}
|
||||
/>
|
||||
<SMAA />
|
||||
</EffectComposer>
|
||||
|
||||
<Environment files={'hdr/sky.hdr'} environmentIntensity={1} background />
|
||||
|
||||
<Terrain
|
||||
chunks={16}
|
||||
chunkSize={10.0}
|
||||
resolution={8.0}
|
||||
scale={1}
|
||||
hillScale={0.1}
|
||||
hillHeight={6.0}
|
||||
detailScale={1.0}
|
||||
detailHeight={0.2}
|
||||
wireframe={false}
|
||||
grassCount={6000}
|
||||
grassSize={0.6}
|
||||
grassLOD={60}
|
||||
/>
|
||||
<SealCube
|
||||
ref={(mesh) => {
|
||||
if (mesh && !sealMesh) setSealMesh(mesh);
|
||||
}}
|
||||
/>
|
||||
|
||||
<OrbitControls
|
||||
target={[0, 3, 0]}
|
||||
enablePan={false}
|
||||
makeDefault
|
||||
minDistance={2}
|
||||
maxDistance={10}
|
||||
/>
|
||||
</Canvas>
|
||||
<OrbitControls
|
||||
target={[0, 3, 0]}
|
||||
enablePan={false}
|
||||
makeDefault
|
||||
minDistance={2}
|
||||
maxDistance={10}
|
||||
/>
|
||||
</Canvas>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user