TC_MultiTerrainBoost.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TC_MultiTerrainBoost : MonoBehaviour
  4. {
  5. Camera MainCamera;
  6. bool[] active1;
  7. Terrain[] terrains;
  8. Bounds[] bounds;
  9. Plane[] planes;
  10. float distance;
  11. int count_terrain;
  12. void Start() {
  13. MainCamera = GetComponent<Camera>();
  14. terrains = Resources.FindObjectsOfTypeAll(typeof(Terrain)) as Terrain[];
  15. bounds = new Bounds[terrains.Length];
  16. active1 = new bool[terrains.Length];
  17. calcBounds();
  18. }
  19. void LateUpdate () {
  20. calcFrustrum();
  21. for (count_terrain = 0;count_terrain < bounds.Length;++count_terrain) {
  22. if (IsRenderedFrom(bounds[count_terrain])) {
  23. if (!active1[count_terrain]) {
  24. terrains[count_terrain].enabled = true;
  25. active1[count_terrain] = true;
  26. }
  27. }
  28. else {
  29. if (active1[count_terrain]) {
  30. terrains[count_terrain].enabled = false;
  31. active1[count_terrain] = false;
  32. }
  33. }
  34. }
  35. }
  36. void calcBounds() {
  37. for (count_terrain = 0;count_terrain < terrains.Length;++count_terrain) {
  38. bounds[count_terrain].size = terrains[count_terrain].terrainData.size;
  39. bounds[count_terrain].center = new Vector3(terrains[count_terrain].transform.position.x+(bounds[count_terrain].size.x/2),terrains[count_terrain].transform.position.y+(bounds[count_terrain].size.y/2),terrains[count_terrain].transform.position.z+(bounds[count_terrain].size.z/2));
  40. active1[count_terrain] = true;
  41. }
  42. }
  43. void calcFrustrum() {
  44. planes = GeometryUtility.CalculateFrustumPlanes(MainCamera);
  45. }
  46. bool IsRenderedFrom(Bounds bound) {
  47. return GeometryUtility.TestPlanesAABB(planes,bound);
  48. }
  49. }