Search Unity

NavMesh Baking at Runtime - Issue with some isolated 3D tiles

Discussion in 'Navigation' started by Roixo, Dec 21, 2017.

  1. Roixo

    Roixo

    Joined:
    Nov 30, 2017
    Posts:
    16
    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
     
  2. Jabrils

    Jabrils

    Joined:
    Apr 10, 2015
    Posts:
    4
    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
     
  3. Roixo

    Roixo

    Joined:
    Nov 30, 2017
    Posts:
    16
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
    6.  
    7. // Build and update a localized navmesh from the sources marked by NavMeshSourceTag
    8. [DefaultExecutionOrder(-102)]
    9. public class LocalNavMeshBuilder : MonoBehaviour
    10. {
    11.  
    12.     // The center of the build
    13.     public Transform m_Tracked;
    14.  
    15.     // The size of the build bounds
    16.     public Vector3 m_Size = new Vector3(80.0f, 20.0f, 80.0f);
    17.  
    18.  
    19.  
    20.     NavMeshData m_NavMesh;
    21.     AsyncOperation m_Operation;
    22.     NavMeshDataInstance m_Instance;
    23.     List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
    24.  
    25.     IEnumerator Start()
    26.     {
    27.         while (true)
    28.         {
    29.             UpdateNavMesh(true);
    30.             yield return m_Operation;
    31.         }
    32.     }
    33.  
    34.     void OnEnable()
    35.     {
    36.         // Construct and add navmesh
    37.         m_NavMesh = new NavMeshData();
    38.         m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
    39.         if (m_Tracked == null)
    40.             m_Tracked = transform;
    41.         UpdateNavMesh(false);
    42.     }
    43.  
    44.     void OnDisable()
    45.     {
    46.         // Unload navmesh and clear handle
    47.         m_Instance.Remove();
    48.     }
    49.  
    50.     void UpdateNavMesh(bool asyncUpdate = false)
    51.     {
    52.         NavMeshSourceTag.Collect(ref m_Sources);
    53.         var defaultBuildSettings = NavMesh.GetSettingsByID(0);
    54.         var bounds = QuantizedBounds();
    55.  
    56.         if (asyncUpdate)
    57.             m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
    58.         else
    59.             NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
    60.     }
    61.  
    62.     static Vector3 Quantize(Vector3 v, Vector3 quant)
    63.     {
    64.         float x = quant.x * Mathf.Floor(v.x / quant.x);
    65.         float y = quant.y * Mathf.Floor(v.y / quant.y);
    66.         float z = quant.z * Mathf.Floor(v.z / quant.z);
    67.         return new Vector3(x, y, z);
    68.     }
    69.  
    70.     Bounds QuantizedBounds()
    71.     {
    72.         // Quantize the bounds to update only when theres a 10% change in size
    73.         var center = m_Tracked ? m_Tracked.position : transform.position;
    74.         return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
    75.     }
    76.  
    77.     void OnDrawGizmosSelected()
    78.     {
    79.         if (m_NavMesh)
    80.         {
    81.             Gizmos.color = Color.green;
    82.             Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
    83.         }
    84.  
    85.         Gizmos.color = Color.yellow;
    86.         var bounds = QuantizedBounds();
    87.         Gizmos.DrawWireCube(bounds.center, bounds.size);
    88.  
    89.         Gizmos.color = Color.green;
    90.         var center = m_Tracked ? m_Tracked.position : transform.position;
    91.         Gizmos.DrawWireCube(center, m_Size);
    92.     }
    93. }
    Do you need anything else?
     
    Last edited: Dec 21, 2017
  4. Roixo

    Roixo

    Joined:
    Nov 30, 2017
    Posts:
    16
    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.
     
  5. Grimm9186

    Grimm9186

    Joined:
    Apr 24, 2017
    Posts:
    3
    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:
    1) Change the Min Region Area in advanced bake options (decrease the number until you happy with the results)
    2) Change the Voxel Size under advanced bake options so that there are more regions on that tile.