Update lorax culler, fixing it finally maybe

This commit is contained in:
Jens Pitkänen 2020-04-21 03:40:23 +03:00
parent 4ab5cb6dbf
commit 5eb88c72a1
2 changed files with 71 additions and 39 deletions

View File

@ -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}

View File

@ -4,56 +4,53 @@ using UnityEngine;
public class LoraxCuller : MonoBehaviour {
public Lorax[] Loraces;
public Transform ChunkKeepAliveTransformsParent;
public int[] Radius;
public bool[] Omnidirectional;
private List<Transform> ChunkKeepAliveTransforms = new List<Transform>();
private List<GameObject>[] EnabledChunks;
private void Awake() {
EnabledChunks = new List<GameObject>[Loraces.Length];
foreach (Transform Child in ChunkKeepAliveTransformsParent) {
ChunkKeepAliveTransforms.Add(Child);
}
for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) {
EnabledChunks[LoraxIndex] = new List<GameObject>();
for (int ChunkIndex = 0; ChunkIndex < ChunkKeepAliveTransforms.Count; ChunkIndex++) {
EnabledChunks[LoraxIndex].Add(null);
}
}
}
private HashSet<GameObject> OldChunks = new HashSet<GameObject>();
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<GameObject> EnabledChunks = new HashSet<GameObject>();
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;
}
}