- implémentation de threejs.

- Création environnement 3D océan.
This commit is contained in:
iMax 2024-12-05 21:42:37 +01:00
parent c992014d53
commit 706d491236
6 changed files with 81 additions and 7 deletions

View file

@ -0,0 +1,49 @@
// src/components/Ocean.tsx
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
import { Water } from 'three/examples/jsm/objects/Water.js';
const Ocean: React.FC = () => {
const waterGeometry = new THREE.PlaneGeometry(10000, 10000);
const waterOption = {
textureWidth: 512,
textureHeight: 512,
waterNormals: new THREE.TextureLoader().load('https://threejs.org/examples/textures/waternormals.jpg', function (texture) {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
}),
alpha: 1.0,
sunDirection: new THREE.Vector3(),
sunColor: 0xffffff,
waterColor: 0x001e0f,
distortionScale: 3.7,
fog: false,
};
const water = new Water(waterGeometry, waterOption);
water.rotation.x = -Math.PI / 2;
const waterRef = useRef<THREE.Mesh>(null);
useEffect(() => {
if (waterRef.current) {
waterRef.current.add(water);
}
}, [waterRef]);
useEffect(() => {
const animate = () => {
requestAnimationFrame(animate);
water.material.uniforms['time'].value += 1.0 / 60.0;
};
animate();
}, []);
return (
<mesh ref={waterRef}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial />
</mesh>
);
};
export default Ocean;

View file

@ -3,7 +3,7 @@
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

View file

@ -2,7 +2,7 @@ import { BrowserRouter, Route, Routes } from "react-router";
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
import MainPage from "./pages/MainPage.tsx";
import MainPage from "./pages/main.tsx";
import GamePage from "./pages/GamePage.tsx";

View file

@ -1,9 +1,29 @@
import { Canvas } from "@react-three/fiber";
import { OrbitControls, Sky } from "@react-three/drei";
import * as THREE from "three";
import { useEffect, useRef } from "react";
import Ocean from "../components/3d/Ocean";
export default function GamePage() {
return (
<div>
<h1>Game Page</h1>
<div id="canvas-container">
<Canvas>
<mesh position={[0, 0, 0]} scale={[1, 1, 1]}>
<boxGeometry args={[1, 1, 1]} />
<meshStandardMaterial />
<ambientLight intensity={0.5} />
<directionalLight color="red" position={[5, 5, 5]} />
<Sky sunPosition={[100, 10, 100]} />
</mesh>
<OrbitControls />
<Ocean />
</Canvas>
</div>
)
}
}