public void LoadIndices() { //load indices, vertices and edges var indices = Onv.Indices; var vertices = Onv.VerticesWS; var edges = Onv.Edges; if ((indices == null) || (vertices == null) || (edges == null)) { return; } var vc = vertices.Count; var ic = IndexCount; var startid = IndexID; var endid = startid + ic; if (startid >= indices.Count) { return; } if (endid > indices.Count) { return; } if (endid > edges.Count) { return; } Indices = new int[ic]; Vertices = new Vector3[ic]; Edges = new OnvEdge[ic]; int i = 0; for (int id = startid; id < endid; id++) { var ind = indices[id]; Indices[i] = ind; Vertices[i] = (ind < vc) ? vertices[ind] : Vector3.Zero; Edges[i] = edges[id]; i++; } }
public void Load(string onvfile) { Name = Path.GetFileNameWithoutExtension(onvfile).ToLowerInvariant(); var lines = File.ReadAllLines(onvfile); bool inverts = false; bool ininds = false; bool inedges = false; bool inpolys = false; bool insectors = false; bool insectordata = false; bool insectorpolyinds = false; bool insectorbounds = false; bool inportals = false; int depth = 0; int cdepth = 0; OnvSector csector = SectorTree; var spacedelim = new[] { ' ' }; var cult = CultureInfo.InvariantCulture; foreach (var line in lines) { string tline = line.Trim(); if (string.IsNullOrEmpty(tline)) { continue; //blank line } //if (tline.StartsWith("#")) continue; //commented out string[] parts = tline.Split(spacedelim, StringSplitOptions.RemoveEmptyEntries); if (tline.StartsWith("{")) { depth++; continue; } if (tline.StartsWith("}")) { depth--; } //need to handle the closing cases if (inverts) { if (depth <= 0) { inverts = false; } else if (parts.Length == 3) { NavMeshVertex v = new NavMeshVertex(); v.X = ushort.Parse(parts[0].Trim(), cult); v.Y = ushort.Parse(parts[1].Trim(), cult); v.Z = ushort.Parse(parts[2].Trim(), cult); Vertices.Add(v); } } else if (ininds) { if (depth <= 0) { ininds = false; } else { for (int i = 0; i < parts.Length; i++) { int ind = int.Parse(parts[i]); Indices.Add(ind); } } } else if (inedges) { if (depth <= 0) { inedges = false; } else { OnvEdge edge = new OnvEdge(parts); Edges.Add(edge); } } else if (inpolys) { if (depth <= 0) { inpolys = false; } else { OnvPoly poly = new OnvPoly(parts); Polys.Add(poly); } } else if (insectors) { if (depth <= 0) { insectors = false; insectordata = false; insectorpolyinds = false; insectorbounds = false; } else if (insectordata) { if (depth <= cdepth) { insectordata = false; insectorpolyinds = false; insectorbounds = false; } else if (insectorpolyinds) { if (depth <= (cdepth + 1)) { insectorpolyinds = false; } else { for (int i = 0; i < parts.Length; i++) { int ind = int.Parse(parts[i]); csector.SectorData.PolyIndices.Add(ind); } } } else if (insectorbounds) { if (depth <= (cdepth + 1)) { insectorbounds = false; } else { OnvBounds bounds = new OnvBounds(parts); csector.SectorData.Bounds.Add(bounds); } } else { string idstr = parts[0].Trim(); if (idstr == "PolyIndices") { csector.SectorData.PolyIndicesCount = int.Parse(parts[1].Trim(), cult); insectorpolyinds = csector.SectorData.PolyIndicesCount > 0; if (insectorpolyinds) { csector.SectorData.PolyIndices = new List <int>(); } } else if (idstr == "Bounds") { csector.SectorData.BoundsCount = int.Parse(parts[1].Trim(), cult); insectorbounds = csector.SectorData.BoundsCount > 0; if (insectorbounds) { csector.SectorData.Bounds = new List <OnvBounds>(); } } } } else { if (depth < cdepth) { csector = csector.Parent; } cdepth = depth; string idstr = parts[0].Trim(); if (idstr == "AABBMin") { csector.AABBMin = Util.GetVector3(parts, 1); } else if (idstr == "AABBMax") { csector.AABBMax = Util.GetVector3(parts, 1); } else if ((parts.Length < 2) || (parts[1].Trim() != "null")) { if (idstr == "SectorData") { csector.SectorData = new OnvSectorData(); insectordata = true; } else if (idstr == "SubTree0") { csector.SubTree0 = new OnvSector(); csector.SubTree0.Parent = csector; csector = csector.SubTree0; } else if (idstr == "SubTree1") { csector.SubTree1 = new OnvSector(); csector.SubTree1.Parent = csector; csector = csector.SubTree1; } else if (idstr == "SubTree2") { csector.SubTree2 = new OnvSector(); csector.SubTree2.Parent = csector; csector = csector.SubTree2; } else if (idstr == "SubTree3") { csector.SubTree3 = new OnvSector(); csector.SubTree3.Parent = csector; csector = csector.SubTree3; } } } } else if (inportals) { if (depth <= 0) { inportals = false; } else { OnvPortal portal = new OnvPortal(parts); Portals.Add(portal); } } else { //at root level, look for identifier depth = 0; //reset just in case string idstr = parts[0].Trim(); if (idstr == "Version") { VersionMaj = int.Parse(parts[1].Trim(), cult); VersionMin = int.Parse(parts[2].Trim(), cult); } else if (idstr == "Sizes") { Sizes = Util.GetVector3(parts, 1); } else if (idstr == "Flags") { Flags = int.Parse(parts[1].Trim(), cult); } else if (idstr == "Vertices") { VerticesCount = int.Parse(parts[1].Trim(), cult); inverts = VerticesCount > 0; } else if (idstr == "Indices") { IndicesCount = int.Parse(parts[1].Trim(), cult); ininds = IndicesCount > 0; } else if (idstr == "Edges") { EdgesCount = int.Parse(parts[1].Trim(), cult); inedges = EdgesCount > 0; } else if (idstr == "Polys") { PolysCount = int.Parse(parts[1].Trim(), cult); inpolys = PolysCount > 0; } else if (idstr == "SectorTree") { insectors = true; } else if (idstr == "Portals") { PortalsCount = int.Parse(parts[1].Trim(), cult); inportals = PortalsCount > 0; } else if (idstr == "SectorID") { SectorID = int.Parse(parts[1].Trim(), cult); } } } }