campfire/Assets/Scripts/LoraxCuller.cs

69 lines
2.5 KiB
C#
Raw Normal View History

2020-04-20 03:14:45 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2020-06-13 01:12:10 +02:00
using UnityEngine.SceneManagement;
2020-04-20 03:14:45 +02:00
public class LoraxCuller : MonoBehaviour {
public Lorax[] Loraces;
public int[] Radius;
public bool[] Omnidirectional;
2020-04-29 23:31:45 +02:00
public bool CullingStatusChanged = false;
2020-04-20 03:14:45 +02:00
private HashSet<GameObject> OldChunks = new HashSet<GameObject>();
private int LastX = int.MaxValue;
private int LastY = int.MaxValue;
private int LastAngle = int.MaxValue;
2020-04-20 03:14:45 +02:00
private void Update() {
2020-06-13 01:12:10 +02:00
if (SceneManager.GetActiveScene().name != "VRScene") {
LastX = LastY = LastAngle = int.MaxValue;
2020-06-12 21:28:40 +02:00
return;
}
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;
2020-04-20 11:31:15 +02:00
}
LastX = BaseX;
LastY = BaseY;
LastAngle = Angle;
2020-04-20 03:14:45 +02:00
HashSet<GameObject> EnabledChunks = new HashSet<GameObject>();
2020-04-20 03:14:45 +02:00
for (int LoraxIndex = 0; LoraxIndex < Loraces.Length; LoraxIndex++) {
Lorax Lorax = Loraces[LoraxIndex];
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;
}
}
}
2020-04-20 20:22:14 +02:00
GameObject Chunk = Lorax.GetChunkAt(X, Y);
if (Chunk != null) {
Chunk.SetActive(true);
EnabledChunks.Add(Chunk);
2020-04-20 20:22:14 +02:00
}
}
2020-04-20 03:14:45 +02:00
}
}
2020-04-20 20:22:14 +02:00
2020-04-29 23:31:45 +02:00
CullingStatusChanged = false;
foreach (GameObject OldChunk in OldChunks) {
if (!EnabledChunks.Contains(OldChunk)) {
OldChunk.SetActive(false);
2020-04-29 23:31:45 +02:00
CullingStatusChanged = true;
2020-04-20 20:22:14 +02:00
}
}
2020-04-29 23:31:45 +02:00
if (EnabledChunks.Count > OldChunks.Count) {
CullingStatusChanged = true;
}
OldChunks = EnabledChunks;
2020-04-20 03:14:45 +02:00
}
}