Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<!-- Runtime dependencies -->
<ItemGroup>
<PackageVersion Include="BepuPhysics" Version="2.5.0-beta.25" />
<PackageVersion Include="DotRecast.Core" Version="2024.1.1" />
<PackageVersion Include="DotRecast.Detour" Version="2024.2.1" />
<PackageVersion Include="DotRecast.Recast" Version="2024.2.1" />
<PackageVersion Include="DotRecast.Recast.Toolset" Version="2024.3.1" />
<PackageVersion Include="DotRecast.Core" Version="2025.2.1" />
<PackageVersion Include="DotRecast.Detour" Version="2025.2.1" />
<PackageVersion Include="DotRecast.Recast" Version="2025.2.1" />
<PackageVersion Include="DotRecast.Recast.Toolset" Version="2025.2.1" />
<PackageVersion Include="FFmpeg.AutoGen" Version="3.4.0.2" />
<PackageVersion Include="K4os.Compression.LZ4.Legacy" Version="1.3.6" />
<PackageVersion Include="Microsoft.Management.Infrastructure" Version="3.0.0-preview.4" />
Expand Down Expand Up @@ -148,4 +148,4 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" PrivateAssets="all" />
</ItemGroup>
</Project>
</Project>
32 changes: 18 additions & 14 deletions sources/engine/Stride.Navigation/DtMeshDataSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Diagnostics;
using DotRecast.Core.Numerics;
using DotRecast.Detour;
using Stride.Core.Serialization;
Expand Down Expand Up @@ -40,14 +41,14 @@ internal void WriteDtMeshBvTree(DtBVNode[] dataBvTree)
if (isNull)
continue;

for (int i = 0; i < 3; i++)
{
stream.Write(t.bmin[i]);
}
for (int i = 0; i < 3; i++)
{
stream.Write(t.bmax[i]);
}
stream.Write(t.bmin.X);
stream.Write(t.bmin.Y);
stream.Write(t.bmin.Z);

stream.Write(t.bmax.X);
stream.Write(t.bmax.Y);
stream.Write(t.bmax.Z);

stream.Write(t.i);
}
}
Expand Down Expand Up @@ -173,10 +174,13 @@ internal DtBVNode[] ReadDtMeshBvTree()
continue;
}
var node = new DtBVNode();
for (int j = 0; j < 3; j++)
node.bmin[j] = stream.Read<int>();
for (int j = 0; j < 3; j++)
node.bmax[j] = stream.Read<int>();
node.bmin.X = stream.Read<int>();
node.bmin.Y = stream.Read<int>();
node.bmin.Z = stream.Read<int>();

node.bmax.X = stream.Read<int>();
node.bmax.Y = stream.Read<int>();
node.bmax.Z = stream.Read<int>();
node.i = stream.Read<int>();

arr[i] = node;
Expand Down Expand Up @@ -207,7 +211,7 @@ internal DtPolyDetail[] ReadDtMeshDetailMeshes()
int count = stream.Read<int>();
var arr = new DtPolyDetail[count];
for (int i = 0; i < count; i++)
arr[i] = new DtPolyDetail(stream.Read<int>(),stream.Read<int>(),stream.Read<int>(),stream.Read<int>());
arr[i] = new DtPolyDetail(stream.Read<int>(),stream.Read<int>(),stream.Read<byte>(),stream.Read<byte>());
return arr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task<NavigationMeshBuildResult> Rebuild()
});
await result;

FinilizeRebuild(result);
FinalizeRebuild(result);

return result.Result;
}
Expand All @@ -191,7 +191,7 @@ internal void InitializeSettingsFromNavigationSettings(NavigationSettings naviga
pendingRebuild = true;
}

private void FinilizeRebuild(Task<NavigationMeshBuildResult> resultTask)
private void FinalizeRebuild(Task<NavigationMeshBuildResult> resultTask)
{
var result = resultTask.Result;
if (result.Success)
Expand Down
50 changes: 35 additions & 15 deletions sources/engine/Stride.Navigation/InternalNavigationMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using DotRecast.Core.Numerics;
using DotRecast.Detour;
using Stride.Core.Mathematics;
Expand Down Expand Up @@ -40,7 +42,8 @@ public InternalNavigationMesh(float cellTileSize)
meshParams.maxPolys = 1 << polyBits;

// Initialize the query object
navMesh = new DtNavMesh(meshParams, 2048);
navMesh = new DtNavMesh();
navMesh.Init(meshParams, 2048);
navQuery = new DtNavMeshQuery(navMesh);
}

Expand All @@ -51,7 +54,7 @@ public bool LoadTile(DtMeshData navData)
if (navData == null)
return false;

long tileRef = navMesh.AddTile(navData, 0, 0);
var status = navMesh.AddTile(navData, 0, 0, out long tileRef);
return tileRef != 0;
}

Expand All @@ -75,15 +78,34 @@ public void DoPathFindQuery(PathFindQuery query, ref PathFindResult result)
status = navQuery.FindNearestPoly(query.Target.ToDotRecastVector(), query.FindNearestPolyExtent.ToDotRecastVector(), filter, out long endPoly, out RcVec3f endPoint, out _);
if (status.Failed())
return;

List<long> polys = new(query.MaxPathPoints);
status = navQuery.FindPath(startPoly, endPoly, startPoint, endPoint, filter, ref polys, DtFindPathOption.NoOption);
if (status.Failed() || status.IsPartial())
return;

status = navQuery.FindStraightPath(startPoint, endPoint, polys, ref result.PathPoints, query.MaxPathPoints, 0);
if (status.Failed())

long[] polys = new long[query.MaxPathPoints];
navQuery.FindPath(startPoly, endPoly, startPoint, endPoint, filter, polys, out var pathCount, polys.Length);

if (0 >= pathCount)
{
return;
}

// In case of partial path, make sure the end point is clamped to the last polygon.
var endPosition = new RcVec3f(endPoint.X, endPoint.Y, endPoint.Z);
if (polys[pathCount - 1] != endPoly)
{
status = navQuery.ClosestPointOnPoly(polys[pathCount - 1], endPoint, out var closest, out var _);
if (status.Succeeded())
{
endPosition = closest;
}
}

// Due to Dotrecast using Spans, we need to allocate the array with the max size possible then resize it later to remove empty entries.
// TODO: By default we allocate 1024 points which is way more than enough for most cases and should maybe be defaulted to a smaller value in the future.
result.PathPoints = new DtStraightPath[query.MaxPathPoints];
navQuery.FindStraightPath(startPoint, endPosition, polys, pathCount, result.PathPoints, out var straightPathCount, query.MaxPathPoints, 0);

// cut out the empty entries
Array.Resize(ref result.PathPoints, straightPathCount);

result.PathFound = true;
}

Expand All @@ -96,12 +118,10 @@ public void DoRaycastQuery(RaycastQuery query, out NavigationRaycastResult resul
DtStatus status = navQuery.FindNearestPoly(query.Source.ToDotRecastVector(), query.FindNearestPolyExtent.ToDotRecastVector(), filter, out long startPoly, out _, out _);
if (status.Failed())
return;
List<long> polys = new (query.MaxPathPoints);

long[] polys = new long[query.MaxPathPoints];
var normal = result.Normal.ToDotRecastVector();
status = navQuery.Raycast(startPoly, query.Source.ToDotRecastVector(),
query.Target.ToDotRecastVector(), filter,
out float t, out normal, ref polys);
status = navQuery.Raycast(startPoly, query.Source.ToDotRecastVector(), query.Target.ToDotRecastVector(), filter, out float t, out normal, polys, out var pathCount, query.MaxPathPoints);
result.Normal = new(normal.X, normal.Y, normal.Z);

if (status.Failed())
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Stride.Navigation/Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal struct PathFindQuery
internal struct PathFindResult
{
public bool PathFound;
public List<DtStraightPath> PathPoints;
public DtStraightPath[] PathPoints;
}

internal struct BuildSettings
Expand Down
6 changes: 3 additions & 3 deletions sources/engine/Stride.Navigation/NavigationBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System;
Expand Down Expand Up @@ -96,7 +96,7 @@ public GeneratedData BuildNavmesh(ref Vector3[] vertices, ref int[] indices)
}

// Find walkable triangles and rasterize into heightfield
triAreas = RcCommons.MarkWalkableTriangles(context, buildSettings.AgentMaxSlope, verts, indices, numTriangles, new RcAreaModification(RcAreaModification.RC_AREA_FLAGS_MASK));
triAreas = RcRecast.MarkWalkableTriangles(context, buildSettings.AgentMaxSlope, verts, indices, numTriangles, new RcAreaModification(RcAreaModification.RC_AREA_FLAGS_MASK));
RcRasterizations.RasterizeTriangles(context, verts, indices, triAreas, numTriangles, solid, walkableClimb);

// Filter walkable surfaces.
Expand Down Expand Up @@ -129,7 +129,7 @@ public GeneratedData BuildNavmesh(ref Vector3[] vertices, ref int[] indices)
// Update poly flags from areas.
for (int i = 0; i < polyMesh.npolys; ++i)
{
if (polyMesh.areas[i] == RcConstants.RC_WALKABLE_AREA)
if (polyMesh.areas[i] == RcRecast.RC_WALKABLE_AREA)
polyMesh.areas[i] = 0;

if (polyMesh.areas[i] == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ public bool TryFindPath(Vector3 start, Vector3 end, ICollection<Vector3> path, N
query.FindNearestPolyExtent = querySettings.FindNearestPolyExtent;
PathFindResult queryResult = default;

queryResult.PathPoints = new List<DtStraightPath>(querySettings.MaxPathPoints);
queryResult.PathPoints = new DtStraightPath[querySettings.MaxPathPoints];
navmesh.DoPathFindQuery(query, ref queryResult);
if (!queryResult.PathFound)
return false;

for (int i = 0; i < queryResult.PathPoints.Count; i++)
for (int i = 0; i < queryResult.PathPoints.Length; i++)
{
path.Add(queryResult.PathPoints[i].pos.ToStrideVector());
}
Expand Down
1 change: 1 addition & 0 deletions sources/engine/Stride.Navigation/Stride.Navigation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="DotRecast.Core" />
<PackageReference Include="DotRecast.Detour" />
<PackageReference Include="DotRecast.Recast" />
</ItemGroup>
Expand Down