mirror of
https://github.com/Floriansylvain/generic-threejs-game.git
synced 2026-08-19 11:43:18 +02:00
fix: light position fix + tweaks
This commit is contained in:
+7
-4
@@ -3,7 +3,7 @@ import "./App.css";
|
|||||||
import { Canvas } from "@react-three/fiber";
|
import { Canvas } from "@react-three/fiber";
|
||||||
import { KeyboardControls } from "@react-three/drei";
|
import { KeyboardControls } from "@react-three/drei";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { Vector3 } from "three";
|
import { PCFSoftShadowMap, Vector3 } from "three";
|
||||||
import { Player } from "./components/Player";
|
import { Player } from "./components/Player";
|
||||||
import { Bloom, EffectComposer, Vignette } from "@react-three/postprocessing";
|
import { Bloom, EffectComposer, Vignette } from "@react-three/postprocessing";
|
||||||
import { Cubes } from "./components/Cubes";
|
import { Cubes } from "./components/Cubes";
|
||||||
@@ -34,8 +34,8 @@ function App() {
|
|||||||
<Canvas
|
<Canvas
|
||||||
className="canvas"
|
className="canvas"
|
||||||
ref={container}
|
ref={container}
|
||||||
shadows
|
shadows={{ type: PCFSoftShadowMap }}
|
||||||
camera={{ fov: 50, frustumCulled: true, near: 0.1, far: 100 }}
|
camera={{ fov: 50, frustumCulled: true, near: 0.1, far: 500 }}
|
||||||
>
|
>
|
||||||
<Cubes cubesPosition={cubes}></Cubes>
|
<Cubes cubesPosition={cubes}></Cubes>
|
||||||
|
|
||||||
@@ -45,7 +45,10 @@ function App() {
|
|||||||
receiveShadow
|
receiveShadow
|
||||||
castShadow
|
castShadow
|
||||||
>
|
>
|
||||||
<planeGeometry attach={"geometry"} args={[50, 50]}></planeGeometry>
|
<planeGeometry
|
||||||
|
attach={"geometry"}
|
||||||
|
args={[5000, 5000]}
|
||||||
|
></planeGeometry>
|
||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
toneMapped={false}
|
toneMapped={false}
|
||||||
color={"green"}
|
color={"green"}
|
||||||
|
|||||||
+14
-10
@@ -1,6 +1,6 @@
|
|||||||
import { useAnimations, useGLTF, useKeyboardControls } from "@react-three/drei";
|
import { useAnimations, useGLTF, useKeyboardControls } from "@react-three/drei";
|
||||||
import { useFrame, useThree } from "@react-three/fiber";
|
import { useFrame, useThree } from "@react-three/fiber";
|
||||||
import { useEffect, useRef } from "react";
|
import { MutableRefObject, useEffect, useRef } from "react";
|
||||||
import {
|
import {
|
||||||
DirectionalLight,
|
DirectionalLight,
|
||||||
Euler,
|
Euler,
|
||||||
@@ -21,8 +21,8 @@ export interface KeyPressed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const PLAYER_SPEED = 4.75 * 10;
|
const PLAYER_SPEED = 4.75 * 10;
|
||||||
const DRAW_DISTANCE = 50;
|
const DRAW_DISTANCE = 256;
|
||||||
const SHADOW_RESOLUTION = 2048;
|
const SHADOW_RESOLUTION = 4096;
|
||||||
const MOUSE_X_SENSITIVITY = 0.002;
|
const MOUSE_X_SENSITIVITY = 0.002;
|
||||||
const MOUSE_Y_SENSITIVITY = 0.001;
|
const MOUSE_Y_SENSITIVITY = 0.001;
|
||||||
const ANIM_SPEED = 0.2;
|
const ANIM_SPEED = 0.2;
|
||||||
@@ -35,8 +35,8 @@ export function Player(): JSX.Element {
|
|||||||
const model = useGLTF("/Adventurer.glb");
|
const model = useGLTF("/Adventurer.glb");
|
||||||
const anims = useAnimations(model.animations, model.scene);
|
const anims = useAnimations(model.animations, model.scene);
|
||||||
const [, getKeys] = useKeyboardControls();
|
const [, getKeys] = useKeyboardControls();
|
||||||
const lightRef = useRef<DirectionalLight>(null);
|
|
||||||
const camera = useThree((state) => state.camera);
|
const camera = useThree((state) => state.camera);
|
||||||
|
const dirLightRef: MutableRefObject<DirectionalLight | null> = useRef(null);
|
||||||
|
|
||||||
const pivot = new Vector2();
|
const pivot = new Vector2();
|
||||||
const moveVector = new Vector3();
|
const moveVector = new Vector3();
|
||||||
@@ -174,6 +174,14 @@ export function Player(): JSX.Element {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setDirLightPosition(): void {
|
||||||
|
dirLightRef.current?.position.set(
|
||||||
|
model.scene.position.x + 100,
|
||||||
|
100,
|
||||||
|
model.scene.position.z + 100
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
useFrame((_, delta) => {
|
useFrame((_, delta) => {
|
||||||
setMoveVector(delta);
|
setMoveVector(delta);
|
||||||
setAnimationsState();
|
setAnimationsState();
|
||||||
@@ -181,6 +189,7 @@ export function Player(): JSX.Element {
|
|||||||
if (someKeyPressed()) setPlayerPosition();
|
if (someKeyPressed()) setPlayerPosition();
|
||||||
setCameraPosition();
|
setCameraPosition();
|
||||||
setPlayerAnimations();
|
setPlayerAnimations();
|
||||||
|
setDirLightPosition();
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -201,14 +210,9 @@ export function Player(): JSX.Element {
|
|||||||
position={model.scene.position}
|
position={model.scene.position}
|
||||||
></primitive>
|
></primitive>
|
||||||
<directionalLight
|
<directionalLight
|
||||||
ref={lightRef}
|
ref={dirLightRef}
|
||||||
target={model.scene}
|
target={model.scene}
|
||||||
castShadow
|
castShadow
|
||||||
position={[
|
|
||||||
model.scene.position.x + 100,
|
|
||||||
100,
|
|
||||||
model.scene.position.z + 100,
|
|
||||||
]}
|
|
||||||
shadow-mapSize={[SHADOW_RESOLUTION, SHADOW_RESOLUTION]}
|
shadow-mapSize={[SHADOW_RESOLUTION, SHADOW_RESOLUTION]}
|
||||||
intensity={6}
|
intensity={6}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user