/*--------------------------------------------------------------------------------------*/ /* */ /* FindEdge: Performs optimization for drawing LightPolygon over adjacent edges */ /* by drawing a ray on the egde always */ /* param: */ /* LightPolygonInfo min - The global angle for light Polygon */ /* LightPolygonInfo max - The global angle for light Polygon */ /* */ /* returns: */ /* EdgeInfo - with hit points and distacen to draw LightPolygon */ /* */ /*--------------------------------------------------------------------------------------*/ EdgeInfo FindEdge(LightPolygonInfo min, LightPolygonInfo max) { float minAngle = min.angle; float maxAngle = max.angle; Vector3 minPoint = Vector3.zero; Vector3 maxPoint = Vector3.zero; for (int i = 0; i < edgeResolveIterations; i++) { float angle = (minAngle + maxAngle) / 2; LightPolygonInfo newLightPolygon = ViewLightPolygon(angle); bool edgeDistanceThresholdExceeded = Mathf.Abs(min.distance - newLightPolygon.distance) > edgeDistanceThreshold; if (newLightPolygon.hit == min.hit && !edgeDistanceThresholdExceeded) { minAngle = angle; minPoint = newLightPolygon.point; } else { maxAngle = angle; maxPoint = newLightPolygon.point; } } return(new EdgeInfo(minPoint, maxPoint)); }
/*--------------------------------------------------------------------------------------*/ /* */ /* DrawLightPolygon: Draws the LightPolygonMesh in gameview */ /* */ /*--------------------------------------------------------------------------------------*/ void DrawLightPolygon() { int stepCount = Mathf.RoundToInt(viewAngle * meshResolution); // Determines how many degrees are in each step float stepAngleSize = viewAngle / stepCount; // List of all the points the RayCast2D hit List <Vector3> viewPoints = new List <Vector3> (); LightPolygonInfo oldLightPolygon = new LightPolygonInfo(); for (int i = 0; i <= stepCount; i++) { // Roates clockwise until we reach thr right most ray. float angle = transform.eulerAngles.y - viewAngle / 2 + stepAngleSize * i; LightPolygonInfo newLightPolygon = ViewLightPolygon(angle); if (i > 0) { bool edgeDistanceThresholdExceeded = Mathf.Abs(oldLightPolygon.distance - newLightPolygon.distance) > edgeDistanceThreshold; if (oldLightPolygon.hit != newLightPolygon.hit || (oldLightPolygon.hit && newLightPolygon.hit && edgeDistanceThresholdExceeded)) { EdgeInfo edge = FindEdge(oldLightPolygon, newLightPolygon); if (edge.pointA != Vector3.zero) { viewPoints.Add(edge.pointA); } if (edge.pointB != Vector3.zero) { viewPoints.Add(edge.pointB); } } } viewPoints.Add(newLightPolygon.point); oldLightPolygon = newLightPolygon; } // Actually draws the viewMesh int vertextCount = viewPoints.Count + 1; Vector3 [] verticies = new Vector3[vertextCount]; int [] triangles = new int[(vertextCount - 2) * 3]; verticies [0] = Vector2.zero; for (int i = 0; i < vertextCount - 2; i++) { // Makes vertices local space instead of global space verticies [i + 1] = transform.InverseTransformPoint(viewPoints [i]); // Creates the viewMesh from the GameObject's origin point if (i < vertextCount - 2) { triangles [i * 3] = 0; triangles [i * 3 + 1] = i + 1; triangles [i * 3 + 2] = i + 2; } } _ViewMesh.Clear(); _ViewMesh.vertices = verticies; _ViewMesh.triangles = triangles; _ViewMesh.RecalculateNormals(); }