Permalink
Browse files

Various experimental changes to reduce memory usage.

  • Loading branch information...
Jon Frisby
Jon Frisby committed Dec 18, 2009
1 parent d674cde commit be0e4312a67b413e836af692ace7838b77c404a1
Showing with 293 additions and 245 deletions.
  1. +65 −48 Source/Control.cs
  2. +9 −3 Source/Inspector.cs
  3. +31 −12 Source/NavmeshAsset.cs
  4. +188 −182 Source/Seeker.cs
View
@@ -26,6 +26,7 @@
using UnityEngine;
using System.Collections;
+using System.Collections.Generic;
namespace PathLibrary
{
@@ -34,7 +35,7 @@ public class Control
private class CachedSeeker
{
private NetworkNodeAsset start, end;
- private ArrayList solution;
+ private List<ConnectionAsset> solution;
private float lifespan, endTime;
@@ -44,7 +45,8 @@ public CachedSeeker( Seeker seeker )
{
this.start = seeker.Start;
this.end = seeker.End;
- this.solution = new ArrayList( seeker.Solution );
+ // TODO: Do we NEED to copy this?
+ this.solution = new List<ConnectionAsset>( seeker.Solution );
Restart( seeker.CacheLifespan );
}
@@ -58,7 +60,7 @@ public bool Match( Seeker seeker )
- public ArrayList Solution
+ public List<ConnectionAsset> Solution
{
get
{
@@ -93,14 +95,16 @@ public bool DoesUse( NetworkAsset network )
return true;
}
- foreach( ConnectionAsset connection in Solution )
- {
- if( connection.From.Network == network )
- {
- return true;
- }
+ for(int i = 0; i < Solution.Count; i++) {
+ if(Solution[i].From.Network == network) return true;
}
+/* PathData current = Solution;
+ while(current != null) {
+ if(current.Connection.From.Network == network) return true;
+ current = current.Next;
+ } */
+
return false;
}
@@ -113,28 +117,33 @@ public bool DoesUse( NetworkNodeAsset node )
return true;
}
- foreach( ConnectionAsset connection in Solution )
- {
- if( connection.From == node )
- {
- return true;
- }
+ for(int i = 0; i < Solution.Count; i++) {
+ if(Solution[i].From == node) return true;
}
+
+/* PathData current = Solution;
+ while(current != null) {
+ if(current.Connection.From == node) return true;
+ current = current.Next;
+ } */
+
return false;
}
public bool DoesUse( ConnectionAsset connection )
{
- foreach( ConnectionAsset connectionAsset in Solution )
- {
- if( connectionAsset == connection )
- {
- return true;
- }
+ for(int i = 0; i < Solution.Count; i++) {
+ if(Solution[i] == connection) return true;
}
+
+/* PathData current = Solution;
+ while(current != null) {
+ if(current.Connection == connection) return true;
+ current = current.Next;
+ } */
return false;
}
@@ -255,8 +264,9 @@ private IEnumerator UpdateCache()
- public ArrayList GetCache( Seeker seeker )
+ public List<ConnectionAsset> GetCache( Seeker seeker )
{
+ // TODO: Make this an O(1) lookup?
foreach( CachedSeeker cache in cachedSeekers )
{
if( cache.Match( seeker ) )
@@ -385,22 +395,23 @@ public int CachedSeekers
+ // TODO: Is caching this safe?
+ private ArrayList gridNetworks = null;
public ArrayList GridNetworks
{
get
{
- ArrayList gridNetworks;
-
- gridNetworks = new ArrayList();
+ if(gridNetworks == null) {
+ gridNetworks = new ArrayList();
- foreach( NetworkAsset network in networks )
- {
- if( network is GridNetworkAsset )
+ for( int i = 0; i < networks.Count; i++ )
{
- gridNetworks.Add( network );
+ if( networks[i] is GridNetworkAsset )
+ {
+ gridNetworks.Add( networks[i] );
+ }
}
}
-
return gridNetworks;
}
}
@@ -409,11 +420,11 @@ public ArrayList GridNetworks
public NetworkAsset GetNetwork( string name )
{
- foreach( NetworkAsset network in networks )
+ for( int i = 0; i < networks.Count; i++ )
{
- if( network.Name == name )
+ if( ((NetworkAsset)networks[i]).Name == name )
{
- return network;
+ return (NetworkAsset)networks[i];
}
}
@@ -422,23 +433,24 @@ public NetworkAsset GetNetwork( string name )
+ private ArrayList gridNodes = null;
public ArrayList GetGridNodes( NetworkNodeAsset node )
{
- ArrayList result;
-
- result = new ArrayList();
- foreach( GridNetworkAsset network in GridNetworks )
- {
- foreach( GridNodeAsset gridNode in network.Nodes )
+ // TODO: Is caching this safe, or should we be clearing and starting over?
+ if(gridNodes == null) {
+ gridNodes = new ArrayList();
+ ArrayList gridNets = GridNetworks;
+ for(int i = 0; i < gridNets.Count; i++)
{
- if( gridNode.Target == node )
+ GridNetworkAsset network = (GridNetworkAsset)gridNets[i];
+ for(int j = 0; j < network.Nodes.Length; j++)
{
- result.Add( gridNode );
+ GridNodeAsset gridNode = (GridNodeAsset)network.Nodes[j];
+ if(gridNode.Target == node) gridNodes.Add(gridNode);
}
}
}
-
- return result;
+ return gridNodes;
}
@@ -638,23 +650,28 @@ public void OnDisabled( ConnectionAsset connection )
}
-
+// private static int count = 0;
+ private Dictionary <NetworkAsset,Bounds> networkBoundsCache = new Dictionary <NetworkAsset,Bounds>();
public NetworkNodeAsset NearestNode( Vector3 position, Seeker seeker )
{
+// count++;
+// Debug.Log("NearestNode: " + count);
NetworkNodeAsset nearest;
- Bounds networkBounds;
+// Bounds networkBounds;
nearest = null;
- foreach( NetworkAsset network in networks )
+ for( int i = 0; i < networks.Count; i++ )
{
+ NetworkAsset network = (NetworkAsset)networks[i];
if( network is GridNetworkAsset || !network.Enabled || ( seeker.ValidateNetworks && !seeker.Valid( network ) ) )
// Reject disabled networks and invalid ones if we're checking for that
{
continue;
}
- networkBounds = new Bounds( network.Position + Owner.transform.position, network.Size );
- if( !networkBounds.Contains( position ) )
+ if(!networkBoundsCache.ContainsKey(network))
+ networkBoundsCache[network] = new Bounds( network.Position + Owner.transform.position, network.Size );
+ if( !networkBoundsCache[network].Contains( position ) )
// Reject networks whose bounds do not contain the point
{
continue;
View
@@ -146,6 +146,9 @@ public void OnGUI( IInspector inspector )
private Texture2D[] tagsToggle = null;
+ private bool useSlopeChecking = false;
+ private float threshold = 0f;
+ private Vector2 heightRange = new Vector2(0f, 600f);
public void OnNetworkGUI( IInspector inspector )
{
Vector3 newPosition, newSize;
@@ -242,17 +245,20 @@ public void OnNetworkGUI( IInspector inspector )
if( GUILayout.Button( "Autosize" ) )
{
Bounds b = meshReference.bounds;
- Editor.Instance.SelectedNetwork.Position = b.center;
- Editor.Instance.SelectedNetwork.Size = b.size;
+ Editor.Instance.SelectedNetwork.Position = new Vector3(b.center.x, 0f, b.center.z);
+ Editor.Instance.SelectedNetwork.Size = b.size + (Vector3.up * b.center.y);
Editor.Instance.SaveCollection();
}
if( GUILayout.Button( "Build" ) && ( Editor.Instance.SelectedNetwork.Nodes.Length == 0 || EditorUtility.DisplayDialog( "Rebuild?", "Are you certain that you wish to rebuild the navmesh '" + Editor.Instance.SelectedNetwork.ToString() + "'?\n\nWARNING: All current data in the navmesh will be lost. This cannot be undone.", "Rebuild", "Cancel" ) ) )
{
- ( ( NavmeshAsset )Editor.Instance.SelectedNetwork ).Generate( meshReference );
+ ( ( NavmeshAsset )Editor.Instance.SelectedNetwork ).Generate( meshReference, useSlopeChecking, threshold, heightRange );
Editor.Instance.SaveCollection();
}
}
GUILayout.EndHorizontal();
+ useSlopeChecking = EditorGUILayout.Toggle("Skip steep faces?", useSlopeChecking);
+ threshold = Mathf.Clamp01(EditorGUILayout.FloatField("Threshold", threshold));
+ heightRange = EditorGUILayout.Vector2Field("Walkable Height", heightRange);
}
else if( Editor.Instance.SelectedNetwork is GridNetworkAsset )
{
View
@@ -26,6 +26,7 @@
using System;
using UnityEngine;
using System.Collections;
+using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
@@ -153,35 +154,53 @@ public bool Remove( CellAsset cell )
return true;
}
-
-
- public bool Generate( Mesh mesh )
+ public bool Generate( Mesh mesh, bool useSlopeChecking, float threshold, Vector2 heightRange )
{
Vector3[] points;
Vector3 position, vector;
TriangleAsset[] triangles;
- cells = new CellAsset[ mesh.triangles.Length / 3 ];
+// cells = new CellAsset[ mesh.triangles.Length / 3 ];
+ List<CellAsset> tmpCells = new List<CellAsset>(mesh.triangles.Length / 3);
for( int i = 0, cellID = 0; i < mesh.triangles.Length; i += 3, cellID++ )
{
- triangles = new TriangleAsset[ 1 ];
points = new Vector3[ 3 ];
-
+
points[ 0 ] = mesh.vertices[ mesh.triangles[ i ] ];
points[ 1 ] = mesh.vertices[ mesh.triangles[ i + 1 ] ];
points[ 2 ] = mesh.vertices[ mesh.triangles[ i + 2 ] ];
+ Vector3 normal;
+ bool isSafeToProceed = true;
+ if(useSlopeChecking) {
+ normal = Vector3.Cross(points[1] - points[0], points[2] - points[1]).normalized;
+ float dp = Vector3.Dot(normal, Vector3.up);
+ if(dp < threshold)
+ isSafeToProceed = false;
+ else {
+ for(int j = 0; j < 3; j++) {
+ if((points[j].y < heightRange.x) || (points[j].y > heightRange.y))
+ isSafeToProceed = false;
+ }
+ }
+ }
+ if(isSafeToProceed) {
+ triangles = new TriangleAsset[ 1 ];
- vector = points[ 0 ] - points[ 1 ];
- position = points[ 1 ] + vector.normalized * vector.magnitude / 2;
+ vector = points[ 0 ] - points[ 1 ];
+ position = points[ 1 ] + vector.normalized * vector.magnitude / 2;
- vector = points[ 2 ] - position;
- position = position + vector.normalized * vector.magnitude / 2;
+ vector = points[ 2 ] - position;
+ position = position + vector.normalized * vector.magnitude / 2;
- triangles[ 0 ] = new TriangleAsset( points, null );
+ triangles[ 0 ] = new TriangleAsset( points, null );
- cells[ cellID ] = new CellAsset( "Cell " + cellID, position, triangles, this, Collection );
+ tmpCells.Add(new CellAsset( "Cell " + cellID, position, triangles, this, Collection ));
+ } else {
+ Debug.Log("Discarding cell #" + cellID);
+ }
}
+ cells = tmpCells.ToArray();
return GenerateConnections();
}
Oops, something went wrong.

0 comments on commit be0e431

Please sign in to comment.