feat: ajoute les potagers à la map
This commit is contained in:
@@ -4,7 +4,7 @@ import { useGLTF } from "@react-three/drei";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
|
||||
import { useTerrainHeightSampler } from "@/hooks/three/useTerrainHeight";
|
||||
import type { VegetationInstance } from "@/hooks/world/useVegetationData";
|
||||
import type { VegetationInstance } from "@/types/map/mapScene";
|
||||
import { useWind } from "@/hooks/world/useWind";
|
||||
import { optimizeGLTFSceneTextures } from "@/utils/three/optimizeGLTFScene";
|
||||
|
||||
@@ -15,6 +15,7 @@ interface InstancedVegetationProps {
|
||||
castShadow: boolean;
|
||||
receiveShadow: boolean;
|
||||
windStrength: number;
|
||||
rotationOffset: readonly [number, number, number];
|
||||
}
|
||||
|
||||
interface MeshData {
|
||||
@@ -186,6 +187,7 @@ function extractMeshes(scene: THREE.Group): MeshData[] {
|
||||
function createInstanceMatrices(
|
||||
instances: VegetationInstance[],
|
||||
scaleMultiplier: number,
|
||||
rotationOffset: readonly [number, number, number],
|
||||
geometryBottomY: number,
|
||||
): THREE.Matrix4[] {
|
||||
const matrices: THREE.Matrix4[] = [];
|
||||
@@ -203,7 +205,11 @@ function createInstanceMatrices(
|
||||
|
||||
position.set(...instance.position);
|
||||
position.y += -geometryBottomY * scaleMultiplier;
|
||||
rotation.set(...instance.rotation);
|
||||
rotation.set(
|
||||
instance.rotation[0] + rotationOffset[0],
|
||||
instance.rotation[1] + rotationOffset[1],
|
||||
instance.rotation[2] + rotationOffset[2],
|
||||
);
|
||||
quaternion.setFromEuler(rotation);
|
||||
matrix.compose(position, quaternion, scale);
|
||||
matrices.push(matrix);
|
||||
@@ -233,6 +239,7 @@ export function InstancedVegetation({
|
||||
castShadow,
|
||||
receiveShadow,
|
||||
windStrength,
|
||||
rotationOffset,
|
||||
}: InstancedVegetationProps): React.JSX.Element | null {
|
||||
const { scene } = useGLTF(modelPath);
|
||||
const wind = useWind();
|
||||
@@ -269,9 +276,10 @@ export function InstancedVegetation({
|
||||
createInstanceMatrices(
|
||||
groundedInstances,
|
||||
scaleMultiplier,
|
||||
rotationOffset,
|
||||
getMeshBottomY(meshDataList),
|
||||
),
|
||||
[groundedInstances, meshDataList, scaleMultiplier],
|
||||
[groundedInstances, meshDataList, rotationOffset, scaleMultiplier],
|
||||
);
|
||||
|
||||
const instancedMeshes = useMemo(() => {
|
||||
|
||||
@@ -8,16 +8,19 @@ import {
|
||||
useMapPerformanceStore,
|
||||
} from "@/managers/stores/useMapPerformanceStore";
|
||||
import { InstancedVegetation } from "@/world/vegetation/InstancedVegetation";
|
||||
import {
|
||||
type VegetationInstance,
|
||||
useVegetationData,
|
||||
} from "@/hooks/world/useVegetationData";
|
||||
import { useVegetationData } from "@/hooks/world/useVegetationData";
|
||||
import type { VegetationInstance } from "@/types/map/mapScene";
|
||||
import {
|
||||
VEGETATION_TYPE_KEYS,
|
||||
VEGETATION_TYPES,
|
||||
type VegetationType,
|
||||
} from "@/data/world/vegetationConfig";
|
||||
|
||||
interface VegetationSystemProps {
|
||||
onlyModelName?: string | null;
|
||||
streaming?: boolean;
|
||||
}
|
||||
|
||||
interface VegetationChunk {
|
||||
key: string;
|
||||
type: VegetationType;
|
||||
@@ -26,6 +29,7 @@ interface VegetationChunk {
|
||||
castShadow: boolean;
|
||||
receiveShadow: boolean;
|
||||
windStrength: number;
|
||||
rotationOffset: readonly [number, number, number];
|
||||
centerX: number;
|
||||
centerZ: number;
|
||||
instances: VegetationInstance[];
|
||||
@@ -73,6 +77,7 @@ function createVegetationChunks(
|
||||
castShadow: config.castShadow,
|
||||
receiveShadow: config.receiveShadow,
|
||||
windStrength: config.windStrength,
|
||||
rotationOffset: config.rotationOffset,
|
||||
centerX: center.x / chunkInstances.length,
|
||||
centerZ: center.z / chunkInstances.length,
|
||||
instances: chunkInstances,
|
||||
@@ -80,14 +85,20 @@ function createVegetationChunks(
|
||||
});
|
||||
}
|
||||
|
||||
export function VegetationSystem(): React.JSX.Element | null {
|
||||
export function VegetationSystem({
|
||||
onlyModelName = null,
|
||||
streaming = true,
|
||||
}: VegetationSystemProps): React.JSX.Element | null {
|
||||
const cameraMode = useCameraMode();
|
||||
const sceneMode = useSceneMode();
|
||||
const groups = useMapPerformanceStore((state) => state.groups);
|
||||
const models = useMapPerformanceStore((state) => state.models);
|
||||
const { data, isLoading } = useVegetationData();
|
||||
const streamingEnabled =
|
||||
CHUNK_CONFIG.enabled && sceneMode === "game" && cameraMode === "player";
|
||||
streaming &&
|
||||
CHUNK_CONFIG.enabled &&
|
||||
sceneMode === "game" &&
|
||||
cameraMode === "player";
|
||||
|
||||
const chunks = useMemo(() => {
|
||||
if (!data) return [];
|
||||
@@ -95,6 +106,8 @@ export function VegetationSystem(): React.JSX.Element | null {
|
||||
return VEGETATION_TYPE_KEYS.flatMap((type) => {
|
||||
const config = VEGETATION_TYPES[type];
|
||||
|
||||
if (onlyModelName && config.mapName !== onlyModelName) return [];
|
||||
|
||||
if (!config.enabled) return [];
|
||||
if (!isMapModelVisible(config.mapName, { groups, models })) return [];
|
||||
|
||||
@@ -103,7 +116,7 @@ export function VegetationSystem(): React.JSX.Element | null {
|
||||
|
||||
return createVegetationChunks(type, entry.instances);
|
||||
});
|
||||
}, [data, groups, models]);
|
||||
}, [data, groups, models, onlyModelName]);
|
||||
|
||||
const visibleChunks = useVisibleWorldChunks(chunks, streamingEnabled);
|
||||
|
||||
@@ -122,6 +135,7 @@ export function VegetationSystem(): React.JSX.Element | null {
|
||||
castShadow={chunk.castShadow}
|
||||
receiveShadow={chunk.receiveShadow}
|
||||
windStrength={chunk.windStrength}
|
||||
rotationOffset={chunk.rotationOffset}
|
||||
/>
|
||||
</Suspense>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user