-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
features: layout style and data flow refactor.
- Loading branch information
1 parent
84b1e60
commit 30d6e12
Showing
6 changed files
with
342 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,88 @@ | ||
import Layout from "./three/Layout"; | ||
import { ThreeProvider } from "./three"; | ||
import useLayoutGeometry from "./three/useLayoutGeometry"; | ||
import NavMesh from "./three/NavMesh"; | ||
|
||
import Layout from "./three/Layout" | ||
import {ThreeProvider} from "./three"; | ||
const wallConfig = { | ||
wallThickness: 0.4, | ||
width: 20, | ||
length: 20, | ||
wallHeight: 1, | ||
}; | ||
|
||
const walls = [ | ||
[ | ||
[1, -9.75], | ||
[1, 1.2], | ||
], | ||
[ | ||
[-4.6, 1.2], | ||
[1.02, 1.2], | ||
], | ||
[ | ||
[-9.55, 1.2], | ||
[-7.1, 1.2], | ||
], | ||
[ | ||
[4, -9.75], | ||
[4, -4], | ||
], | ||
[ | ||
[4, -0.6], | ||
[4, 0.75], | ||
], | ||
[ | ||
[4, 0.55], | ||
[8.34, 0.55], | ||
], | ||
[ | ||
[-9.65, 8.5], | ||
[-6.8, 8.5], | ||
], | ||
[ | ||
[-3.75, 8.5], | ||
[2.5, 8.5], | ||
], | ||
[ | ||
[5.55, 8.5], | ||
[8.34, 8.5], | ||
], | ||
[ | ||
[-9.9, -9.75], | ||
[1.2, -9.75], | ||
], | ||
[ | ||
[3.8, -9.75], | ||
[8.7, -9.75], | ||
], | ||
[ | ||
[-9.7, -9.75], | ||
[-9.7, 8.7], | ||
], | ||
[ | ||
[8.5, -9.75], | ||
[8.5, 8.7], | ||
], | ||
].map( | ||
([start, end]) => | ||
[ | ||
[start[0], 0, start[1]], | ||
[end[0], wallConfig.wallHeight, end[1]], | ||
] as [number[], number[]] | ||
); | ||
|
||
const App = () => { | ||
return <ThreeProvider> | ||
<Layout/> | ||
</ThreeProvider>; | ||
const geometry = useLayoutGeometry({ | ||
...wallConfig, | ||
walls, | ||
}); | ||
|
||
return ( | ||
<ThreeProvider> | ||
<Layout geometry={geometry} {...wallConfig} /> | ||
<NavMesh geometry={geometry} wallThickness={wallConfig.wallThickness} /> | ||
</ThreeProvider> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as THREE from "three"; | ||
import { init, NavMesh } from "recast-navigation"; | ||
import { threeToSoloNavMesh, NavMeshHelper } from "recast-navigation/three"; | ||
|
||
import { useThree } from "."; | ||
import { useEffect, useState } from "react"; | ||
|
||
interface NavMeshProps { | ||
geometry: THREE.BufferGeometry; | ||
wallThickness: number; | ||
} | ||
|
||
export const useNavMesh = ({ geometry, wallThickness }: NavMeshProps) => { | ||
const [navMesh, setNavMesh] = useState<NavMesh | undefined>(); | ||
useEffect(() => { | ||
if (!geometry) return; | ||
|
||
init().then(() => { | ||
const { navMesh } = threeToSoloNavMesh([new THREE.Mesh(geometry)], { | ||
ch: 1e-2, | ||
cs: wallThickness + 1e-2, | ||
walkableHeight: 1, | ||
}); | ||
if (navMesh) { | ||
setNavMesh(navMesh); | ||
} | ||
}); | ||
}, [geometry, wallThickness]); | ||
|
||
return navMesh; | ||
}; | ||
|
||
const NavMeshRenderer = ({ geometry, wallThickness }: NavMeshProps) => { | ||
const { scene } = useThree(); | ||
const navMesh = useNavMesh({ geometry, wallThickness }); | ||
|
||
useEffect(() => { | ||
if (!navMesh) return; | ||
|
||
const navMeshHelper = new NavMeshHelper({ navMesh }); | ||
scene.add(navMeshHelper); | ||
|
||
return () => { | ||
scene.remove(navMeshHelper); | ||
}; | ||
}, [navMesh]); | ||
|
||
return null; | ||
}; | ||
|
||
export default NavMeshRenderer; |
Oops, something went wrong.