Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Extensions;
- namespace Occlusion
- {
- public class Area : MonoBehaviour
- {
- public float turnInvisibleInSec = 10;
- public Dictionary<Area,Portal> portals = new Dictionary<Area,Portal>();
- /*void OnDrawGizmos()
- {
- Gizmos.color = Color.green;
- Gizmos.DrawWireCube(bounds.center, bounds.size);
- foreach (var kvp in portals) kvp.Value.polygon.DebugDrawLines(Color.red);
- }*/
- void OnEnable()
- {
- /*var rs=this.gameObject.GetComponentsInChildren<Renderer>();
- if (rs.Length <= 0) throw new System.Exception("gameObject of Occlusion.Area has no renderers in children");
- bounds.center = rs[0].bounds.center;
- foreach (var r in rs)
- {
- bounds.Encapsulate(r.bounds);
- }*/
- }
- void LateUpdate()
- {
- if (turnInvisibleInSec>0)
- {
- turnInvisibleInSec-=Time.deltaTime;
- if (turnInvisibleInSec<=0)
- this.gameObject.SetActive(false);
- }
- }
- public void TurnVisible()
- {
- turnInvisibleInSec = 10;
- this.gameObject.SetActive(true);
- }
- public void StartPropagate(Camera cam)
- {
- Vector3 origin = cam.transform.position;
- var planes = GeometryUtility.CalculateFrustumPlanes(cam);
- var toUsePlanes = new List<Plane>();
- var excludeNormal = cam.transform.forward;
- foreach (var plane in planes)
- {
- var d = plane.normal.Dot(excludeNormal);
- if (d < 0.9f && d > -0.9f) toUsePlanes.Add(plane);
- }
- this.Propagate_r(null, origin, toUsePlanes.ToArray());
- }
- void Propagate_r(Portal parentPortal, Vector3 origin, Plane[] planes)
- {
- TurnVisible();
- //DebugUtility.DebugBounds(bounds, Color.green);
- foreach (var kvp in portals)
- {
- var portal = kvp.Value;
- if (parentPortal == portal) continue;
- var area = kvp.Key;
- var polygon = portal.polygon.Clone();
- bool isVisible = true;
- foreach (var plane in planes)
- {
- isVisible &= polygon.Clip(plane);
- }
- if (isVisible)
- {
- portal.polygon.DebugDrawLines(Color.blue);
- polygon.DebugDrawLines(Color.red);
- }
- else
- {
- portal.polygon.DebugDrawLines(Color.yellow);
- continue;
- }
- var points = polygon.points;
- int numPoints = points.Count;
- Plane[] portalPlanes = new Plane[numPoints];
- for (int i = 0; i < numPoints; i++)
- {
- portalPlanes[i] = new Plane(points[i], points[(i + 1) % numPoints], origin);
- }
- area.Propagate_r(portal, origin, portalPlanes);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement