Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nav mesh source with area support #229

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
141 changes: 78 additions & 63 deletions Assets/Examples/Scripts/NavMeshSourceTag.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,94 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Collections.Generic;

// Tagging component for use with the LocalNavMeshBuilder
// Supports mesh-filter and terrain - can be extended to physics and/or primitives
[DefaultExecutionOrder(-200)]
public class NavMeshSourceTag : MonoBehaviour
{
// Global containers for all active mesh/terrain tags
public static List<MeshFilter> m_Meshes = new List<MeshFilter>();
public static List<Terrain> m_Terrains = new List<Terrain>();
public static List<Tuple<MeshFilter, int>> m_Meshes =
new List<Tuple<MeshFilter, int>>();
public static List<Tuple<Terrain, int>> m_Terrains =
new List<Tuple<Terrain, int>>();

[SerializeField]
public int areaNum = -1; //Set to zero for default

void OnEnable()
{
var m = GetComponent<MeshFilter>();
if (m != null)
{
m_Meshes.Add(m);
}
void OnEnable()
{
if(areaNum == -1) {return;}
addToList();
}

//This is not the best way to do this, as you can easily call this multiple times are mess things up
//TODO refractor this to avoid multiple adds
public void addToList()
{
var m = GetComponent<MeshFilter>();
if (m != null)
{
m_Meshes.Add(new Tuple<MeshFilter, int>(m, areaNum));
}

var t = GetComponent<Terrain>();
if (t != null)
{
m_Terrains.Add(t);
}
}
var t = GetComponent<Terrain>();
if (t != null)
{
m_Terrains.Add(
new Tuple<Terrain, int>(t, areaNum));
}
}

void OnDisable()
{
var m = GetComponent<MeshFilter>();
if (m != null)
{
m_Meshes.Remove(m);
}
void OnDisable()
{
var m = GetComponent<MeshFilter>();
if (m != null)
{
m_Meshes.Remove(new Tuple<MeshFilter, int>(
m, areaNum));
}

var t = GetComponent<Terrain>();
if (t != null)
{
m_Terrains.Remove(t);
}
}
var t = GetComponent<Terrain>();
if (t != null)
{
m_Terrains.Remove(
new Tuple<Terrain, int>(t, areaNum));
}
}

// Collect all the navmesh build sources for enabled objects tagged by this component
public static void Collect(ref List<NavMeshBuildSource> sources)
{
sources.Clear();
public static void Collect(ref List<NavMeshBuildSource> sources)
{
sources.Clear();

for (var i = 0; i < m_Meshes.Count; ++i)
{
var mf = m_Meshes[i];
if (mf == null) continue;
for (var i = 0; i < m_Meshes.Count; ++i)
{
var mf = m_Meshes[i];
if (mf.Item1 == null) continue;

var m = mf.sharedMesh;
if (m == null) continue;
var m = mf.Item1.sharedMesh;
if (m == null) continue;

var s = new NavMeshBuildSource();
s.shape = NavMeshBuildSourceShape.Mesh;
s.sourceObject = m;
s.transform = mf.transform.localToWorldMatrix;
s.area = 0;
sources.Add(s);
}
var s = new NavMeshBuildSource();
s.shape = NavMeshBuildSourceShape.Mesh;
s.sourceObject = m;
s.transform = mf.Item1.transform.localToWorldMatrix;
s.area = mf.Item2;
sources.Add(s);
}

for (var i = 0; i < m_Terrains.Count; ++i)
{
var t = m_Terrains[i];
if (t == null) continue;
for (var i = 0; i < m_Terrains.Count; ++i)
{
var t = m_Terrains[i];
if (t.Item1 == null) continue;

var s = new NavMeshBuildSource();
s.shape = NavMeshBuildSourceShape.Terrain;
s.sourceObject = t.terrainData;
// Terrain system only supports translation - so we pass translation only to back-end
s.transform = Matrix4x4.TRS(t.transform.position, Quaternion.identity, Vector3.one);
s.area = 0;
sources.Add(s);
}
}
var s = new NavMeshBuildSource();
s.shape = NavMeshBuildSourceShape.Terrain;
s.sourceObject = t.Item1.terrainData;
// Terrain system only supports translation - so we pass translation
// only to back-end
s.transform = Matrix4x4.TRS(
t.Item1.transform.position, Quaternion.identity, Vector3.one);
s.area = t.Item2;
sources.Add(s);
}
}
}