Advertisement
aeroson

C# Occlusion.Area

Nov 8th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Extensions;
  5.  
  6. namespace Occlusion
  7. {
  8. public class Area : MonoBehaviour
  9. {
  10.  
  11. public float turnInvisibleInSec = 10;
  12.  
  13. public Dictionary<Area,Portal> portals = new Dictionary<Area,Portal>();
  14.  
  15. /*void OnDrawGizmos()
  16. {
  17. Gizmos.color = Color.green;
  18. Gizmos.DrawWireCube(bounds.center, bounds.size);
  19. foreach (var kvp in portals) kvp.Value.polygon.DebugDrawLines(Color.red);
  20. }*/
  21.  
  22. void OnEnable()
  23. {
  24. /*var rs=this.gameObject.GetComponentsInChildren<Renderer>();
  25. if (rs.Length <= 0) throw new System.Exception("gameObject of Occlusion.Area has no renderers in children");
  26. bounds.center = rs[0].bounds.center;
  27. foreach (var r in rs)
  28. {
  29. bounds.Encapsulate(r.bounds);
  30. }*/
  31. }
  32.  
  33. void LateUpdate()
  34. {
  35. if (turnInvisibleInSec>0)
  36. {
  37. turnInvisibleInSec-=Time.deltaTime;
  38. if (turnInvisibleInSec<=0)
  39. this.gameObject.SetActive(false);
  40. }
  41. }
  42.  
  43.  
  44. public void TurnVisible()
  45. {
  46. turnInvisibleInSec = 10;
  47. this.gameObject.SetActive(true);
  48. }
  49.  
  50. public void StartPropagate(Camera cam)
  51. {
  52. Vector3 origin = cam.transform.position;
  53. var planes = GeometryUtility.CalculateFrustumPlanes(cam);
  54. var toUsePlanes = new List<Plane>();
  55. var excludeNormal = cam.transform.forward;
  56. foreach (var plane in planes)
  57. {
  58. var d = plane.normal.Dot(excludeNormal);
  59. if (d < 0.9f && d > -0.9f) toUsePlanes.Add(plane);
  60. }
  61.  
  62. this.Propagate_r(null, origin, toUsePlanes.ToArray());
  63. }
  64.  
  65. void Propagate_r(Portal parentPortal, Vector3 origin, Plane[] planes)
  66. {
  67. TurnVisible();
  68. //DebugUtility.DebugBounds(bounds, Color.green);
  69.  
  70. foreach (var kvp in portals)
  71. {
  72. var portal = kvp.Value;
  73. if (parentPortal == portal) continue;
  74. var area = kvp.Key;
  75.  
  76. var polygon = portal.polygon.Clone();
  77.  
  78.  
  79. bool isVisible = true;
  80. foreach (var plane in planes)
  81. {
  82. isVisible &= polygon.Clip(plane);
  83. }
  84.  
  85. if (isVisible)
  86. {
  87. portal.polygon.DebugDrawLines(Color.blue);
  88. polygon.DebugDrawLines(Color.red);
  89. }
  90. else
  91. {
  92. portal.polygon.DebugDrawLines(Color.yellow);
  93. continue;
  94. }
  95.  
  96. var points = polygon.points;
  97. int numPoints = points.Count;
  98. Plane[] portalPlanes = new Plane[numPoints];
  99. for (int i = 0; i < numPoints; i++)
  100. {
  101. portalPlanes[i] = new Plane(points[i], points[(i + 1) % numPoints], origin);
  102. }
  103.  
  104. area.Propagate_r(portal, origin, portalPlanes);
  105.  
  106. }
  107. }
  108.  
  109.  
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement