Roixo
December 21, 2017, 2:55am
1
Hi,
I am developing a game where I create 3d tiles at runtime , and I use an empty Game Object with the LocalNavMeshBuilder.cs attached (which use Volume for baking), and all the prefab tiles have the NavMeshSourceTag.cs attached and will be created as child objects at runtime, like is showed in this tutorial:
It works almost fine, but it does not bake some isolated tiles like showed in the picture. Other isolated tiles are ok .
But it works fine if I move the fail-baked isolated tiles inside a bake line, or if i attach the tile to other tile , like showed:
How does the NavMesh baking the surfaces? Any orientation or solution? Does any other NavMesh method work fine for isolated tiles?
Thanks
Jabrils
December 21, 2017, 3:23am
2
it’s hard to tell what’s going on here, can you show a debug visual of the NavMesh Builder? How many are you using? What’s the size of it? Worst case upload a project file that I can take a look at
Roixo
December 21, 2017, 3:36am
3
I imported it from:
https://github.com/Unity-Technologies/NavMeshComponents
I am using just 1 GameObject with LocalNavMeshBuilder and each prefab tile has the NavMeshSourceTag attached.
This is the LocalNavMeshBuilder:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
using System.Collections.Generic;
using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
// Build and update a localized navmesh from the sources marked by NavMeshSourceTag
[DefaultExecutionOrder(-102)]
public class LocalNavMeshBuilder : MonoBehaviour
{
// The center of the build
public Transform m_Tracked;
// The size of the build bounds
public Vector3 m_Size = new Vector3(80.0f, 20.0f, 80.0f);
NavMeshData m_NavMesh;
AsyncOperation m_Operation;
NavMeshDataInstance m_Instance;
List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
IEnumerator Start()
{
while (true)
{
UpdateNavMesh(true);
yield return m_Operation;
}
}
void OnEnable()
{
// Construct and add navmesh
m_NavMesh = new NavMeshData();
m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
if (m_Tracked == null)
m_Tracked = transform;
UpdateNavMesh(false);
}
void OnDisable()
{
// Unload navmesh and clear handle
m_Instance.Remove();
}
void UpdateNavMesh(bool asyncUpdate = false)
{
NavMeshSourceTag.Collect(ref m_Sources);
var defaultBuildSettings = NavMesh.GetSettingsByID(0);
var bounds = QuantizedBounds();
if (asyncUpdate)
m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
else
NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
}
static Vector3 Quantize(Vector3 v, Vector3 quant)
{
float x = quant.x * Mathf.Floor(v.x / quant.x);
float y = quant.y * Mathf.Floor(v.y / quant.y);
float z = quant.z * Mathf.Floor(v.z / quant.z);
return new Vector3(x, y, z);
}
Bounds QuantizedBounds()
{
// Quantize the bounds to update only when theres a 10% change in size
var center = m_Tracked ? m_Tracked.position : transform.position;
return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
}
void OnDrawGizmosSelected()
{
if (m_NavMesh)
{
Gizmos.color = Color.green;
Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
}
Gizmos.color = Color.yellow;
var bounds = QuantizedBounds();
Gizmos.DrawWireCube(bounds.center, bounds.size);
Gizmos.color = Color.green;
var center = m_Tracked ? m_Tracked.position : transform.position;
Gizmos.DrawWireCube(center, m_Size);
}
}
Do you need anything else?
Roixo
December 22, 2017, 1:15am
4
I think I figured out what the problem is. It is just about the tile size. If I increase the size it bakes it nice.
But still there were some tiles with the same sizes, and someones were baked and others didn’t, so I dont really understand how unity do the baking.
My guess would be the tile sizes are so small that it only fits around 2 or 3 regions on the tile. (Can’t really tell much without the scene)
If this is indeed the problem there are two possible fixes:
Change the Min Region Area in advanced bake options (decrease the number until you happy with the results)
Change the Voxel Size under advanced bake options so that there are more regions on that tile.