-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional 'bounds' option to navmesh generators
- Loading branch information
1 parent
6314be8
commit 93d383e
Showing
6 changed files
with
141 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'recast-navigation': minor | ||
'@recast-navigation/generators': minor | ||
--- | ||
|
||
feat: add optional 'bounds' config to navmesh generators | ||
|
||
If provided, it will be used as the bounds for the navmesh heightfield during generation. If not provided, the bounds will be calculated from the input geometry. If the bounds are known ahead of time, providing them can save some time during generation. |
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
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
76 changes: 76 additions & 0 deletions
76
packages/recast-navigation/.storybook/stories/nav-mesh/custom-bounds.stories.tsx
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,76 @@ | ||
import { OrbitControls } from '@react-three/drei'; | ||
import { NavMesh, NavMeshQuery, Vector3Tuple } from '@recast-navigation/core'; | ||
import { threeToSoloNavMesh } from '@recast-navigation/three'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { Box3, Group, Mesh } from 'three'; | ||
import { Debug } from '../../common/debug'; | ||
import { NavTestEnvironment } from '../../common/nav-test-environment'; | ||
import { decorators } from '../../decorators'; | ||
import { parameters } from '../../parameters'; | ||
|
||
export default { | ||
title: 'NavMesh / Custom Bounds', | ||
decorators, | ||
parameters, | ||
}; | ||
|
||
const BOUNDS = new Box3(); | ||
BOUNDS.min.set(-3, -1, -5); | ||
BOUNDS.max.set(9, 5, 3); | ||
|
||
export const CustomBounds = () => { | ||
const [group, setGroup] = useState<Group | null>(null); | ||
|
||
const [navMesh, setNavMesh] = useState<NavMesh | undefined>(); | ||
const [navMeshQuery, setNavMeshQuery] = useState<NavMeshQuery | undefined>(); | ||
|
||
useEffect(() => { | ||
if (!group) return; | ||
|
||
const meshes: Mesh[] = []; | ||
|
||
group.traverse((child) => { | ||
if (child instanceof Mesh) { | ||
meshes.push(child); | ||
} | ||
}); | ||
|
||
const walkableRadiusWorld = 0.1; | ||
const cellSize = 0.05; | ||
|
||
const { success, navMesh } = threeToSoloNavMesh(meshes, { | ||
cs: cellSize, | ||
ch: 0.2, | ||
walkableRadius: Math.ceil(walkableRadiusWorld / cellSize), | ||
bounds: [BOUNDS.min.toArray(), BOUNDS.max.toArray()], | ||
}); | ||
|
||
if (!success) { | ||
return; | ||
} | ||
|
||
setNavMesh(navMesh); | ||
setNavMeshQuery(navMeshQuery); | ||
|
||
return () => { | ||
navMesh.destroy(); | ||
|
||
setNavMesh(undefined); | ||
setNavMeshQuery(undefined); | ||
}; | ||
}, [group]); | ||
|
||
return ( | ||
<> | ||
<group ref={setGroup}> | ||
<NavTestEnvironment /> | ||
</group> | ||
|
||
<box3Helper args={[BOUNDS]} /> | ||
|
||
<Debug navMesh={navMesh} /> | ||
|
||
<OrbitControls makeDefault /> | ||
</> | ||
); | ||
}; |