mirror of
https://github.com/Floriansylvain/generic-threejs-game.git
synced 2026-08-20 03:53:40 +02:00
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import { useState } from "react";
|
|
import { Vector3 } from "three";
|
|
import { useFrame } from "@react-three/fiber";
|
|
|
|
export function Cubes(props: { cubesPosition: Vector3[] }) {
|
|
const CUBE_SPEED = 1;
|
|
const CUBE_AMPLITUDE = 3;
|
|
const CUBE_HEIGHT = 1;
|
|
|
|
const [cubes, setCubes] = useState<Vector3[]>(
|
|
Array.from(props.cubesPosition)
|
|
);
|
|
|
|
useFrame((state) => {
|
|
setCubes((cubes) => {
|
|
return cubes.map((cube) => {
|
|
return new Vector3(
|
|
cube.x,
|
|
Math.sin(cube.x + state.clock.elapsedTime * CUBE_SPEED) /
|
|
CUBE_AMPLITUDE +
|
|
Math.sin(cube.z + state.clock.elapsedTime * CUBE_SPEED) /
|
|
CUBE_AMPLITUDE +
|
|
CUBE_HEIGHT,
|
|
cube.z
|
|
);
|
|
});
|
|
});
|
|
});
|
|
|
|
return cubes.map((cube) => {
|
|
if (!cube) return;
|
|
return (
|
|
<mesh
|
|
position={cube}
|
|
key={`${cube.x}-${cube.z}`}
|
|
castShadow
|
|
receiveShadow
|
|
scale={[1, 1, 1]}
|
|
>
|
|
<boxGeometry attach={"geometry"}></boxGeometry>
|
|
<meshLambertMaterial
|
|
color={"hotpink"}
|
|
toneMapped={false}
|
|
></meshLambertMaterial>
|
|
</mesh>
|
|
);
|
|
});
|
|
}
|