MultiTerrainBoost.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace WorldComposer
  4. {
  5. public class MultiTerrainBoost : MonoBehaviour
  6. {
  7. Camera MainCamera;
  8. bool[] active1;
  9. Terrain[] terrains;
  10. Bounds[] bounds;
  11. Plane[] planes;
  12. float distance;
  13. int count_terrain;
  14. void Start()
  15. {
  16. MainCamera = GetComponent<Camera>();
  17. terrains = Resources.FindObjectsOfTypeAll(typeof(Terrain)) as Terrain[];
  18. bounds = new Bounds[terrains.Length];
  19. active1 = new bool[terrains.Length];
  20. calcBounds();
  21. }
  22. void LateUpdate()
  23. {
  24. calcFrustrum();
  25. for (count_terrain = 0; count_terrain < bounds.Length; ++count_terrain)
  26. {
  27. if (IsRenderedFrom(bounds[count_terrain]))
  28. {
  29. if (!active1[count_terrain])
  30. {
  31. terrains[count_terrain].enabled = true;
  32. active1[count_terrain] = true;
  33. }
  34. }
  35. else
  36. {
  37. if (active1[count_terrain])
  38. {
  39. terrains[count_terrain].enabled = false;
  40. active1[count_terrain] = false;
  41. }
  42. }
  43. }
  44. }
  45. void calcBounds()
  46. {
  47. for (count_terrain = 0; count_terrain < terrains.Length; ++count_terrain)
  48. {
  49. bounds[count_terrain].size = terrains[count_terrain].terrainData.size;
  50. 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));
  51. active1[count_terrain] = true;
  52. }
  53. }
  54. void calcFrustrum()
  55. {
  56. planes = GeometryUtility.CalculateFrustumPlanes(MainCamera);
  57. }
  58. bool IsRenderedFrom(Bounds bound)
  59. {
  60. return GeometryUtility.TestPlanesAABB(planes, bound);
  61. }
  62. }
  63. }