From 5c5cae497aaa58d7bcb5df11b200f505c426449d Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Thu, 14 Mar 2024 04:37:50 +0100 Subject: [PATCH] feat: more post processing + shadow """culling""" --- package.json | 4 ++- pnpm-lock.yaml | 10 ++++++ src/App.tsx | 65 +++++++++++++++++++++++++-------------- src/components/Cubes.tsx | 12 +++----- src/components/Player.tsx | 45 ++++++++++++++++++++++++--- 5 files changed, 101 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index b299faf..f1f77f4 100644 --- a/package.json +++ b/package.json @@ -14,9 +14,11 @@ "@react-three/fiber": "^8.15.19", "@react-three/postprocessing": "^2.16.2", "@types/three": "^0.162.0", + "postprocessing": "^6.35.2", "react": "^18.2.0", "react-dom": "^18.2.0", - "three": "^0.162.0" + "three": "^0.162.0", + "three-csm": "^4.2.1" }, "devDependencies": { "@types/react": "^18.2.56", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a6b5986..86fdcc5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ dependencies: '@types/three': specifier: ^0.162.0 version: 0.162.0 + postprocessing: + specifier: ^6.35.2 + version: 6.35.2(three@0.162.0) react: specifier: ^18.2.0 version: 18.2.0 @@ -26,6 +29,9 @@ dependencies: three: specifier: ^0.162.0 version: 0.162.0 + three-csm: + specifier: ^4.2.1 + version: 4.2.1 devDependencies: '@types/react': @@ -2237,6 +2243,10 @@ packages: resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} dev: true + /three-csm@4.2.1: + resolution: {integrity: sha512-yHxKNPB2XMUBgWQTIWR0pESXzGYOdID49f2LdTbz+pNJRzBIMMVEJp5JuRSeCU6c75lmL5L4+FXRHQ1tQIgWEg==} + dev: false + /three-mesh-bvh@0.7.3(three@0.162.0): resolution: {integrity: sha512-3W6KjzmupjfE89GuHPT31kxKWZ4YGZPEZJNysJpiOZfQRsBQQgmK7v/VJPpjG6syhAvTnY+5Fr77EvIkTLpGSw==} peerDependencies: diff --git a/src/App.tsx b/src/App.tsx index a7d1784..8ad16e7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,13 +3,20 @@ import "./App.css"; import { Canvas } from "@react-three/fiber"; import { KeyboardControls } from "@react-three/drei"; import { MutableRefObject, useEffect, useRef, useState } from "react"; -import { Vector3 } from "three"; +import { Vector2, Vector3 } from "three"; import { Cubes } from "./components/Cubes"; import { Player } from "./components/Player"; -import { Bloom, EffectComposer, Vignette } from "@react-three/postprocessing"; +import { + Bloom, + ChromaticAberration, + EffectComposer, + SSAO, + Vignette, +} from "@react-three/postprocessing"; +import { BlendFunction } from "postprocessing"; function App() { - const CUBES_QT = 20; + const CUBES_QT = 10; const container = useRef(null); @@ -38,12 +45,10 @@ function App() { rotation={[-Math.PI / 2, 0, 0]} position={[0, 0, 0]} receiveShadow + castShadow > - + - - - - - - + {/* */} + {/* */} + + + + + {/* */} diff --git a/src/components/Cubes.tsx b/src/components/Cubes.tsx index 40f8197..07b343a 100644 --- a/src/components/Cubes.tsx +++ b/src/components/Cubes.tsx @@ -3,9 +3,9 @@ import { Vector3 } from "three"; import { useFrame } from "@react-three/fiber"; export function Cubes(props: { cubesPosition: Vector3[] }) { - const CUBE_SPEED = 10; - const CUBE_AMPLITUDE = 2; - const CUBE_HEIGHT = 4; + const CUBE_SPEED = 1; + const CUBE_AMPLITUDE = 3; + const CUBE_HEIGHT = 1; const [cubes, setCubes] = useState( Array.from(props.cubesPosition) @@ -35,12 +35,10 @@ export function Cubes(props: { cubesPosition: Vector3[] }) { key={`${cube.x}-${cube.z}`} castShadow receiveShadow + scale={[1, 1, 1]} > - + ); }); diff --git a/src/components/Player.tsx b/src/components/Player.tsx index f6c6fc3..c2d5ebb 100644 --- a/src/components/Player.tsx +++ b/src/components/Player.tsx @@ -1,7 +1,20 @@ import { useAnimations, useGLTF, useKeyboardControls } from "@react-three/drei"; import { useFrame, useThree } from "@react-three/fiber"; -import { MutableRefObject, useCallback, useEffect, useState } from "react"; -import { Euler, Mesh, Quaternion, Vector2, Vector3 } from "three"; +import { + MutableRefObject, + useCallback, + useEffect, + useRef, + useState, +} from "react"; +import { + DirectionalLight, + Euler, + Mesh, + Quaternion, + Vector2, + Vector3, +} from "three"; import { degToRad } from "three/src/math/MathUtils.js"; export interface KeyPressed { @@ -13,7 +26,8 @@ export interface KeyPressed { sprint: boolean; } -const PLAYER_SPEED = 4.75; // TODO NERF +const PLAYER_SPEED = 4.75; +const SHADOW_DRAW_DISTANCE = 30; export function Player(props: { container: MutableRefObject; @@ -33,6 +47,8 @@ export function Player(props: { const [player, setPlayer] = useState(new Vector3(0, 0, 0)); const [mouseRef, setMouseRef] = useState(new Vector2()); + const lightRef = useRef(null); + const camera = useThree((state) => state.camera); const radius = 2; @@ -41,7 +57,10 @@ export function Player(props: { useEffect(() => { idle?.play(); model.scene.traverse((child) => { - if (child instanceof Mesh) child.castShadow = true; + if (child instanceof Mesh) { + child.castShadow = true; + child.receiveShadow = true; + } }); }); @@ -135,6 +154,24 @@ export function Player(props: { return ( <> + + + ); }