diff --git a/FallUnity/Assets/IsoSorting/IsoHandler.cs b/FallUnity/Assets/IsoSorting/IsoHandler.cs index 25c952e..be48c44 100644 --- a/FallUnity/Assets/IsoSorting/IsoHandler.cs +++ b/FallUnity/Assets/IsoSorting/IsoHandler.cs @@ -9,13 +9,14 @@ private HashSet items = new HashSet(); private List spriteList = new List(); private List objectList = new List(); + private List allList = new List(); private Camera mainCamera; public int ChunksX = 40; public int ChunksY = 40; private int numChunks; - private List> chunks; + private List> chunks; // Use this for initialization void Start() @@ -24,10 +25,10 @@ mainCamera = GameObject.Find("Main Camera").GetComponent(); mainCamera.transparencySortMode = TransparencySortMode.Orthographic; - chunks = new List>(); + chunks = new List>(); for (int i = 0; i < numChunks; i++) { - chunks.Add(new HashSet()); + chunks.Add(new List()); } } @@ -43,6 +44,7 @@ { objectList.Add(item); } + allList.Add(item); } } public void RemoveIsoItem(IsoItem item) @@ -57,6 +59,7 @@ { objectList.Remove(item); } + allList.Remove(item); } } @@ -73,9 +76,8 @@ private Vector3 camRight; private Vector3 camUp; private Vector3 camPos; - private HashSet remaining = new HashSet(); private List ordered = new List(); - private List tmpSort = new List(); + private HashSet usedChunks = new HashSet(); void LateUpdate() { camForward = mainCamera.transform.forward; @@ -88,7 +90,7 @@ Vector3 worldBottomLeft = mainCamera.ViewportToWorldPoint(new Vector3()); - remaining.Clear(); + //remaining.Clear(); ordered.Clear(); nextDepth = 10; @@ -98,7 +100,7 @@ spriteList[i].Behind.Clear(); spriteList[i].MarkTemp = false; spriteList[i].MarkPerm = false; - remaining.Add(spriteList[i]); + //remaining.Add(spriteList[i]); } for (int i = 0; i < objectList.Count; i++) @@ -106,9 +108,17 @@ objectList[i].Behind.Clear(); objectList[i].MarkTemp = false; objectList[i].MarkPerm = false; - remaining.Add(objectList[i]); + //remaining.Add(objectList[i]); } + Ray outRay = new Ray(); + outRay.direction = camForward; + + Ray inRay = new Ray(); + inRay.direction = -camForward; + + usedChunks.Clear(); + for (int i = 0; i < spriteList.Count; i++) { IsoItem item = spriteList[i]; @@ -132,15 +142,59 @@ { for (int y = bottomBucket; y <= topBucket; y++) { - HashSet chunk = chunks[y * ChunksX + x]; ; - chunk.Add(item); + int chunkIndex = y * ChunksX + x; + List chunk = chunks[chunkIndex]; + bool placed = false; + for (var j = 0; j < chunk.Count; j++) + { + if (item == chunk[j]) + { + placed = true; + break; + } + float thisZ = item.CameraSpace.z; + float otherZ = chunk[j].CameraSpace.z; + if (thisZ > otherZ) + { + chunk.Insert(j, item); + /* + for (int k = 0; k < chunk.Count - 1; k++) + { + if (chunk[k].CameraSpace.z < chunk[k + 1].CameraSpace.z) + { + Debug.LogError("OUT OF ORDER ITEM"); + bool bp = true; + } + } + */ + placed = true; + break; + } + } + if (!placed) + { + chunk.Add(item); + /* + for (int k = 0; k < chunk.Count - 1; k++) + { + if (chunk[k].CameraSpace.z < chunk[k + 1].CameraSpace.z) + { + Debug.LogError("OUT OF ORDER ITEM"); + bool bp = true; + } + } + */ + } + usedChunks.Add(chunkIndex); } } } int layer = LayerMask.GetMask("iso_piece"); RaycastHit hitInfo = new RaycastHit(); - if (Physics.Raycast(new Ray(item.transform.position, -camForward), out hitInfo, item.CameraSpace.z, layer)) + outRay.origin = inRay.origin = item.transform.position; + + if (Physics.Raycast(inRay, out hitInfo, item.CameraSpace.z, layer)) { IsoItem otherItem = hitInfo.collider.GetComponentInParent(); if (otherItem != null) @@ -148,7 +202,8 @@ item.Behind.Add(otherItem); } } - if (Physics.Raycast(new Ray(item.transform.position, camForward), out hitInfo, 50, layer)) + + if (Physics.Raycast(outRay, out hitInfo, 50, layer)) { IsoItem otherItem = hitInfo.collider.GetComponentInParent(); if (otherItem != null) @@ -159,24 +214,22 @@ } - for (int i = 0; i < numChunks; i++) + foreach (int i in usedChunks) { - if (chunks[i].Count > 0) + List chunk = chunks[i]; + for (int j = 0; j < chunk.Count - 1; j++) { - tmpSort.Clear(); - tmpSort.AddRange(chunks[i]); - chunks[i].Clear(); - tmpSort.Sort(ItemSort); + chunk[j].Behind.Add(chunk[j + 1]); - for (int j = 0; j < tmpSort.Count - 1; j++) + /* + if (chunk[j].CameraSpace.z < chunk[j + 1].CameraSpace.z) { - tmpSort[j].Behind.Add(tmpSort[j + 1]); - if (tmpSort[j].CameraSpace.z < tmpSort[j + 1].CameraSpace.z) - { - bool bp = true; - } + Debug.LogError("OUT OF ORDER ITEM"); + bool bp = true; } + */ } + chunk.Clear(); } for (int i = 0; i < objectList.Count; i++) @@ -191,10 +244,15 @@ objectList[i].Behind.Add(objectList[i + 1]); } - while (remaining.Count > 0) + int nextUnmarked = 0; + while (nextUnmarked < allList.Count) { - IsoItem item = remaining.First(); - Visit(item); + if (!allList[nextUnmarked].MarkPerm) + { + IsoItem item = allList[nextUnmarked]; + Visit(item); + } + nextUnmarked++; } } @@ -203,10 +261,10 @@ if (item.MarkPerm) return; if (item.MarkTemp) { - Debug.LogError("CYCLICAL RENDER GRAPH"); + //Debug.LogError("CYCLICAL RENDER GRAPH"); item.MarkTemp = false; item.MarkPerm = true; - remaining.Remove(item); + //remaining.Remove(item); return; } item.MarkTemp = true; @@ -216,7 +274,7 @@ } item.MarkTemp = false; item.MarkPerm = true; - remaining.Remove(item); + //remaining.Remove(item); item.SetDepth = nextDepth;