From 5eb88c72a1d608045faa66ee8e7c06ff6195d787 Mon Sep 17 00:00:00 2001 From: Jens Pitkanen Date: Tue, 21 Apr 2020 03:40:23 +0300 Subject: [PATCH] Update lorax culler, fixing it finally maybe --- Assets/Scenes/MainScene.unity | 37 +++++++++++++++++- Assets/Scripts/LoraxCuller.cs | 73 +++++++++++++++++------------------ 2 files changed, 71 insertions(+), 39 deletions(-) diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 5fd7db0..9217924 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -24761,6 +24761,16 @@ PrefabInstance: propertyPath: Loraces.Array.size value: 3 objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Radius.Array.size + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Omnidirectional.Array.size + value: 3 + objectReference: {fileID: 0} - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, type: 3} propertyPath: Loraces.Array.data[0] @@ -24776,6 +24786,31 @@ PrefabInstance: propertyPath: Loraces.Array.data[2] value: objectReference: {fileID: 675824348} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Radius.Array.data[0] + value: 40 + objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Radius.Array.data[1] + value: 20 + objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Radius.Array.data[2] + value: 50 + objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Omnidirectional.Array.data[1] + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 8702400494665140028, guid: 558201eae20fa5540a826edb23937665, + type: 3} + propertyPath: Omnidirectional.Array.data[2] + value: 1 + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 558201eae20fa5540a826edb23937665, type: 3} --- !u!1001 &6166170816655763453 @@ -24793,7 +24828,7 @@ PrefabInstance: - target: {fileID: 5133887194504435357, guid: 8cf3939d3cb6e684c8ac9618e68c8259, type: 3} propertyPath: Regen - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 5133887194504435357, guid: 8cf3939d3cb6e684c8ac9618e68c8259, type: 3} diff --git a/Assets/Scripts/LoraxCuller.cs b/Assets/Scripts/LoraxCuller.cs index fc7ebd9..f01f1e7 100644 --- a/Assets/Scripts/LoraxCuller.cs +++ b/Assets/Scripts/LoraxCuller.cs @@ -4,56 +4,53 @@ using UnityEngine; public class LoraxCuller : MonoBehaviour { public Lorax[] Loraces; - public Transform ChunkKeepAliveTransformsParent; + public int[] Radius; + public bool[] Omnidirectional; - private List ChunkKeepAliveTransforms = new List(); - private List[] EnabledChunks; - - private void Awake() { - EnabledChunks = new List[Loraces.Length]; - foreach (Transform Child in ChunkKeepAliveTransformsParent) { - ChunkKeepAliveTransforms.Add(Child); - } - for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) { - EnabledChunks[LoraxIndex] = new List(); - for (int ChunkIndex = 0; ChunkIndex < ChunkKeepAliveTransforms.Count; ChunkIndex++) { - EnabledChunks[LoraxIndex].Add(null); - } - } - } + private HashSet OldChunks = new HashSet(); + private int LastX = int.MaxValue; + private int LastY = int.MaxValue; + private int LastAngle = int.MaxValue; private void Update() { - Hashtable Refs = new Hashtable(); + int BaseX = Mathf.FloorToInt(transform.position.x / 20) * 20 + 10; + int BaseY = Mathf.FloorToInt(transform.position.z / 20) * 20 + 10; + int Angle = Mathf.FloorToInt(transform.localEulerAngles.y / 5) * 5; + if (BaseX == LastX && BaseY == LastY && Angle == LastAngle) { + return; + } + LastX = BaseX; + LastY = BaseY; + LastAngle = Angle; + + HashSet EnabledChunks = new HashSet(); for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) { Lorax Lorax = Loraces[LoraxIndex]; - for (int ChunkIndex = 0; ChunkIndex < ChunkKeepAliveTransforms.Count; ChunkIndex++) { - Vector3 KeepAlivePosition = ChunkKeepAliveTransforms[ChunkIndex].position; - GameObject NewChunk = Lorax.GetChunkAt(KeepAlivePosition.x, KeepAlivePosition.z); - GameObject OldChunk = EnabledChunks[LoraxIndex][ChunkIndex]; - NewChunk.SetActive(true); + for (float X = BaseX - Radius[LoraxIndex]; X <= BaseX + Radius[LoraxIndex]; X += 10) { + for (float Y = BaseY - Radius[LoraxIndex]; Y <= BaseY + Radius[LoraxIndex]; Y += 10) { + if (!Omnidirectional[LoraxIndex]) { + Vector3 Delta = new Vector3(X - BaseX, 0, Y - BaseY); + if (Delta.magnitude > Mathf.Sqrt(Mathf.Pow(20, 2) * 2)) { + if (Vector3.Dot(Delta.normalized, transform.forward) < -0.2) { + continue; + } + } + } - if (Refs.ContainsKey(NewChunk)) { - Refs[NewChunk] = (int)Refs[NewChunk] + 1; - } else { - Refs[NewChunk] = 1; - } - if (NewChunk != OldChunk && OldChunk != null) { - // No longer pointing to OldChunk, remove ref - if (Refs.ContainsKey(OldChunk)) { - Refs[OldChunk] = (int)Refs[OldChunk] - 1; - } else { - Refs[OldChunk] = -1; + GameObject Chunk = Lorax.GetChunkAt(X, Y); + if (Chunk != null) { + Chunk.SetActive(true); + EnabledChunks.Add(Chunk); } } - - EnabledChunks[LoraxIndex][ChunkIndex] = NewChunk; } } - foreach (GameObject Key in Refs.Keys) { - if ((int)Refs[Key] < 0) { - Key.SetActive(false); + foreach (GameObject OldChunk in OldChunks) { + if (!EnabledChunks.Contains(OldChunk)) { + OldChunk.SetActive(false); } } + OldChunks = EnabledChunks; } }