Baking Rect Offset Not Working As Expected

Godot Version

Godot_v4.3-beta3_mono_win64

Question

Hey all,
I’ve been working on baking nav regions procedurally using this change:
Add NavigationPolygon `border_size` property for tile baking by smix8 · Pull Request #87961 · godotengine/godot · GitHub to chunk my map into regions for baking. Right now I’m just trying to get the initial generated map parsed and baked before moving onto dynamically updating each chunk.

So I have the following nav region output from my baking and parsing of obstacles + a traversable region covering the whole map.


bakevalues
The yellow rect is my BakingRect, the white square is what I expect the nav region to encompass when using the BakingRectOffset of 128,128.

Should the Offset not cut into the BakingRect by 128px on all sides? Did I misunderstand? I’m hoping someone very knowledgeable like @smix8 has time to explain to me. My goal is to have the first image look like the screenshots from the PR where the sections in between the squares is cut out so that other white squares will line up on the exact same pixel column and can be merged without using edge connections.

Two related but less important questions:

  1. Before running the following line I added a traversable outline to the sourceGeometry:

NavigationServer2D.ParseSourceGeometryData(newNavigationPolygon, sourceGeometry, GetNode(“ObjectTileMapLayer”), new Callable(this, nameof(OnChunkSourceGeometryParsed)));

However when the callback function OnChunkSourceGeometryParsed runs that TraversableOutlines array is empty and I have to re-add the outline. I.e. It works fine if I add the outline in OnChunkSourceGeometryParsed but not if the outline is added before parsing. Does the parsing of the collision obstacles create a new sourceGeometry? The docs say it adds to the sourceGeometry not replaces.

  1. After I run the following line:
  NavigationServer2D.BakeFromSourceGeometryData(newNavigationPolygon, sourceGeometry, new Callable(this, nameof(OnChunkBakingDone)));

During OnChunkBakingDone I make calls to NavigationServer2D to create a region and set values like:

nextNavigationRegionRid = NavigationServer2D.RegionCreate();
NavigationServer2D.RegionSetEnabled(nextNavigationRegionRid, true);
NavigationServer2D.RegionSetMap(nextNavigationRegionRid, GetWorld2D().GetNavigationMap());
NavigationServer2D.RegionSetUseEdgeConnections(nextNavigationRegionRid, false);
NavigationServer2D.RegionSetNavigationLayers(nextNavigationRegionRid, 1);
NavigationServer2D.RegionSetNavigationPolygon(nextNavigationRegionRid, newNavigationPolygon);

However the NavRegions leak when I close the running app which implies to me that the created regions are orphaned nodes. Plus I am unable to see the regions created this way even with Visible Navigation turned on. They do seem to be working properly as my test unit paths around.

When I instead create the regions like this:

  var region = new NavigationRegion2D();
	region.UseEdgeConnections = false;
	region.NavigationLayers = 1;
	region.SetNavigationPolygon(newNavigationPolygon);
	region.Enabled = true;
	AddChild(region);

It is visible in debug mode and I am able to both see them in the node tree mid run and I don’t get a leak. From what I’ve read NavigationServer is the preferred way to create and modify regions (with RIDs) so is there some aspect I missed of creating them?

Thank you for your time all!

The transparent “red” Rect without edge lines is the debug from your baking rect, that is also what got baked, it is not your yellow rect. The baking_rect_offset is just a position offset for that baking_rect. Your border_size property is what does the inner offset from that rect so you would also need to set that to something else than zero.

NavigationServer2D.ParseSourceGeometryData() does not create a new object but it clears the existing data. That is why it does not work when you add the outline upfront but works when you add it later.

That clear on parse was done because too many users forgot to do that them-self and stacked the same geometry over and over in the same source geometry data object. The 2D baking is done with outline paths (due to the inflated scale that many 2D project uses a “voxel” grid like in 3D has too many practical limitations and does not really work). Having overlapping polygons or stacked outlines causes a lot of bugs with polygon clipping operations so needs to be avoided.

The navigation debug visuals are currently all rendered by nodes e.g. the NavigationRegion2D. Without a node, when you use the server directly, you dont see any debug because there is no node that can render it.

The “leak” is because when you create server objects manually you also need to free those RIDs manually with NavigationServer2D.free_rid(), e.g. in the exit_tree() function. If you dont do that you get errors on quit because the objects were not freed.

In general if you use nodes you dont need to do that because the nodes do it for you. If you use servers directly you need to do all those manual steps yourself.

1 Like

Awesome thank you smix, that answers a lot!

That immediately resolved my issue:

I knew the red rect was some sort of debug value but I didn’t realize it was the actual baking rect.

edit: Realized my new question was better being a new topic since smix8 already answered this question and my new question wasn’t related to the baking rect offset.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.