Skip to content

Commit

Permalink
feat: temporarily add heightfield helper 2 using duDebugDrawHeightfie…
Browse files Browse the repository at this point in the history
…ldWalkable
  • Loading branch information
isaac-mason committed Apr 23, 2024
1 parent b503744 commit 87f43f1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Raw, RecastHeightfield } from '@recast-navigation/core';
import { Object3D } from 'three';
import { DebugDraw } from '../debug-draw';

export type HeightfieldHelper2Params = {
heightfields: RecastHeightfield[];
};

export class HeightfieldHelper2 extends Object3D {
private heightfields: RecastHeightfield[];

private debugDraw: DebugDraw;

constructor({ heightfields }: HeightfieldHelper2Params) {
super();

this.heightfields = heightfields;

this.debugDraw = new DebugDraw();
this.add(this.debugDraw.group);

this.update();
}

resize(width: number, height: number) {
this.debugDraw.resize(width, height);
}

update(): void {
this.debugDraw.reset();

for (const heightfield of this.heightfields) {
Raw.RecastDebugDraw.debugDrawHeightfieldWalkable(
this.debugDraw.raw,
heightfield.raw
);
}
}
}
4 changes: 4 additions & 0 deletions packages/recast-navigation-three/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export * from './heightfield-helper';
export * from './nav-mesh-helper';
export * from './off-mesh-connections-helper';
export * from './tile-cache-helper';

// tmp
export * from './nav-mesh-helper-2'
export * from './heightfield-helper-2'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NavMesh, Raw } from '@recast-navigation/core';
import { Object3D } from 'three';
import { DebugDraw } from '../debug-draw';

export type NavMeshHelperParams = {
export type NavMeshHelper2Params = {
navMesh: NavMesh;
};

Expand All @@ -11,7 +11,7 @@ export class NavMeshHelper2 extends Object3D {

private debugDraw: DebugDraw;

constructor({ navMesh }: NavMeshHelperParams) {
constructor({ navMesh }: NavMeshHelper2Params) {
super();

this.navMesh = navMesh;
Expand All @@ -37,6 +37,8 @@ export class NavMeshHelper2 extends Object3D {
* This should be called after updating the nav mesh.
*/
update() {
this.debugDraw.reset();

Raw.DetourDebugDraw.debugDrawNavMesh(
this.debugDraw.raw,
this.navMesh.raw.getNavMesh(),
Expand Down
2 changes: 2 additions & 0 deletions packages/recast-navigation-wasm/recast-navigation.idl
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,8 @@ interface DebugDrawImpl {
interface RecastDebugDraw {
void RecastDebugDraw();

void debugDrawHeightfieldSolid(duDebugDraw dd, [Const, Ref] rcHeightfield hf);
void debugDrawHeightfieldWalkable(duDebugDraw dd, [Const, Ref] rcHeightfield hf);
void debugDrawPolyMesh(duDebugDraw dd, [Const, Ref] rcPolyMesh mesh);
};

Expand Down
12 changes: 11 additions & 1 deletion packages/recast-navigation-wasm/src/DebugDraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ class RecastDebugDraw
{
duDebugDrawPolyMesh(dd, mesh);
}

void debugDrawHeightfieldSolid(duDebugDraw* dd, const rcHeightfield& hf)
{
duDebugDrawHeightfieldSolid(dd, hf);
}

void debugDrawHeightfieldWalkable(duDebugDraw* dd, const rcHeightfield& hf)
{
duDebugDrawHeightfieldWalkable(dd, hf);
}
};

class DetourDebugDraw
Expand All @@ -40,4 +50,4 @@ class DetourDebugDraw
{
duDebugDrawNavMesh(dd, mesh, flags);
}
};
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { OrbitControls, PerspectiveCamera } from '@react-three/drei';
import { NavMesh, RecastConfig } from '@recast-navigation/core';
import { NavMesh } from '@recast-navigation/core';
import {
threeToSoloNavMesh,
threeToTileCache,
threeToTiledNavMesh,
NavMeshHelper2,
threeToSoloNavMesh
} from '@recast-navigation/three';
import React, { useEffect, useState } from 'react';
import { Group, Mesh, MeshBasicMaterial } from 'three';
import { Debug } from '../../common/debug';
import { DungeonEnvironment } from '../../common/dungeon-environment';
import { Group, Mesh } from 'three';
import { NavTestEnvironment } from '../../common/nav-test-environment';
import { decorators } from '../../decorators';
import { parameters } from '../../parameters';
import { NavMeshHelper2 } from '@recast-navigation/three/src/helpers/nav-mesh-helper-2';

export default {
title: 'Debug Draw',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OrbitControls } from '@react-three/drei';
import { RecastHeightfield } from '@recast-navigation/core';
import {
HeightfieldHelper,
HeightfieldHelper2,
threeToTiledNavMesh,
} from '@recast-navigation/three';
import React, { useEffect, useState } from 'react';
Expand All @@ -20,7 +21,7 @@ export const HeightfieldHelperExample = () => {
const [group, setGroup] = useState<Group | null>(null);

const [heightfieldHelper, setHeightfieldHelper] = useState<
HeightfieldHelper | undefined
HeightfieldHelper2 | undefined
>();

useEffect(() => {
Expand Down Expand Up @@ -49,11 +50,10 @@ export const HeightfieldHelperExample = () => {
.map(({ heightfield }) => heightfield)
.filter(Boolean) as RecastHeightfield[];

const heightfieldHelper = new HeightfieldHelper({
const heightfieldHelper = new HeightfieldHelper2({
heightfields,
material: new MeshStandardMaterial(),
highlightWalkable: true,
});
heightfieldHelper.resize(window.innerWidth, window.innerHeight);
heightfieldHelper.update();

setHeightfieldHelper(heightfieldHelper);
Expand Down

0 comments on commit 87f43f1

Please sign in to comment.