void DrawOcclussion(Vector2 offset, float z) { // Collider Ambient Occlusion GL.PushMatrix(); LightingManager2D.Get().occlusionEdgeMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.ambientOcclusion == true) { // Do not call Create From Collider Polygon2D poly = Polygon2DList.CreateFromGameObject(id.gameObject)[0]; poly = poly.ToWorldSpace(id.gameObject.transform); foreach (DoublePair2D p in DoublePair2D.GetList(poly.pointsList)) { Vector2D vA = new Vector2D(p.A.ToVector2() + offset); Vector2D vB = new Vector2D(p.B.ToVector2() + offset); Vector2D vC = new Vector2D(p.B.ToVector2() + offset); Vector2D pA = new Vector2D(p.A.ToVector2() + offset); Vector2D pB = new Vector2D(p.B.ToVector2() + offset); vA.Push(Vector2D.Atan2(p.A, p.B) + Mathf.PI / 2, 1); vB.Push(Vector2D.Atan2(p.A, p.B) + Mathf.PI / 2, 1); vC.Push(Vector2D.Atan2(p.B, p.C) + Mathf.PI / 2, 1); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv1, uv0); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vC, z); } } } GL.End(); GL.PopMatrix(); }
static void DrawSoftShadowTileBlur(Vector2D offset, float z, float height) { float sunDirection = LightingManager2D.GetSunDirection(); Polygon2D poly = Polygon2DList.CreateFromRect(new Vector2(1, 1) / 2); offset += new Vector2D(0.5f, 0.5f); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, height); foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList)) { Vector2D zA = new Vector2D(p.A + offset); Vector2D zB = new Vector2D(p.B + offset); Vector2D zC = zB.Copy(); Vector2D pA = zA.Copy(); Vector2D pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } }
static void DrawTileOcclussion(Vector2D offset, float z, LightingTilemapCollider2D id) { Polygon2D poly = Polygon2DList.CreateFromRect(new Vector2(0.5f, 0.5f)); foreach (DoublePair2D p in DoublePair2D.GetList(poly.pointsList)) { Vector2D vA = p.A + offset; Vector2D vB = p.B + offset; Vector2D vC = p.B + offset; Vector2D pA = p.A + offset; Vector2D pB = p.B + offset; vA.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, -1); vB.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, -1); vC.Push(Vector2D.Atan2(p.B, p.C) - Mathf.PI / 2, -1); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv1, uv0); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vC, z); } }
// Not finished - still has some artifacts static public List <Vector2D> PreparePolygon(Polygon2D polygon) { Polygon2D newPolygon = new Polygon2D(); polygon.Normalize(); foreach (DoublePair2D pair in DoublePair2D.GetList(polygon.pointsList)) { float rotA = Vector2D.Atan2(pair.B, pair.A); float rotC = Vector2D.Atan2(pair.B, pair.C); Vector2D pairA = new Vector2D(pair.A); pairA.Push(rotA - Mathf.PI / 2, precision); Vector2D pairC = new Vector2D(pair.C); pairC.Push(rotC + Mathf.PI / 2, precision); Vector2D vecA = new Vector2D(pair.B); vecA.Push(rotA - Mathf.PI / 2, precision); vecA.Push(rotA, 10f); Vector2D vecC = new Vector2D(pair.B); vecC.Push(rotC + Mathf.PI / 2, precision); vecC.Push(rotC, 10f); Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC)); if (result != null) { newPolygon.AddPoint(result); // } else { //Debug.LogError("Null"); } } return(newPolygon.pointsList); }
public static void Draw(Vector2D offset, float z) { float sunDirection = LightingManager2D.GetSunDirection(); // Day Soft Shadows GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } List <Polygon2D> polygons = null; switch (id.colliderType) { case LightingCollider2D.ColliderType.Collider: polygons = id.GetColliderPolygons(); break; case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape: polygons = id.GetShapePolygons(); break; } Polygon2D poly; Vector3 vecA; Vector3 vecB; Vector3 vecC; foreach (Polygon2D polygon in polygons) { poly = polygon.ToWorldSpace(id.gameObject.transform); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height); Mesh mesh = convexHull.CreateMesh(Vector2.zero, Vector2.zero); for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { vecA = mesh.vertices [mesh.triangles [i]]; vecB = mesh.vertices [mesh.triangles [i + 1]]; vecC = mesh.vertices [mesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(vecA.x, vecA.y, vecB.x, vecB.y, vecC.x, vecC.y, offset, z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); // Null Check? LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } List <Polygon2D> polygons = null; switch (id.colliderType) { case LightingCollider2D.ColliderType.Collider: polygons = id.GetColliderPolygons(); break; case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape: polygons = id.GetShapePolygons(); break; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height); foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList)) { Vector2D zA = new Vector2D(p.A + offset); Vector2D zB = new Vector2D(p.B + offset); Vector2D zC = zB.Copy(); Vector2D pA = zA.Copy(); Vector2D pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } } } GL.End(); GL.PopMatrix(); #if UNITY_2018_1_OR_NEWER GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); // Day Soft Shadows foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } if (id.dayHeight == false) { continue; } for (int x = 0; x < id.area.size.x; x++) { for (int y = 0; y < id.area.size.y; y++) { if (id.map[x, y] == null) { DrawSoftShadowTile(offset + new Vector2D(x, y), z, id.height); } } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); // Day Soft Shadows foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } if (id.dayHeight == false) { continue; } for (int x = 0; x < id.area.size.x; x++) { for (int y = 0; y < id.area.size.y; y++) { if (id.map[x, y] == null) { DrawSoftShadowTileBlur(offset + new Vector2D(x, y), z, id.height); } } } } GL.End(); GL.PopMatrix(); #endif /* * * Material material = LightingManager2D.Get().whiteSpriteMaterial; * foreach (LightingSprite2D id in LightingSprite2D.GetList()) { * if (id.GetSpriteRenderer() == null) { * continue; * } * material.mainTexture = id.GetSpriteRenderer().sprite.texture; //Debug.Log(sprite.pivot); * * Vector2 p = id.transform.position; * Vector2 scale = id.transform.lossyScale; * * if (id.GetSpriteRenderer().flipX) { * scale.x = -scale.x; * } * * if (id.GetSpriteRenderer().flipY) { * scale.y = -scale.y; * } * * Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z); * } */ float ratio = (float)Screen.width / Screen.height; Camera bufferCamera = LightingMainBuffer2D.Get().bufferCamera; Vector2 size = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize); Vector3 pos = Camera.main.transform.position; Max2D.DrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z); // Day Lighting Masking foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.generateDayMask == false) { continue; } switch (id.maskType) { case LightingCollider2D.MaskType.SpriteCustomPhysicsShape: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.GetShapeMesh(), id.transform, offset, z); break; case LightingCollider2D.MaskType.Collider: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.GetColliderMesh(), id.transform, offset, z); break; case LightingCollider2D.MaskType.Sprite: if (id.spriteRenderer == null || id.spriteRenderer.sprite == null) { break; } Material material = LightingManager2D.Get().whiteSpriteMaterial; material.mainTexture = id.spriteRenderer.sprite.texture; Max2D.DrawSprite(material, id.spriteRenderer, new Vector2(id.transform.position.x, id.transform.position.y) + offset.ToVector2(), new Vector2(1, 1), id.transform.rotation.eulerAngles.z, z); break; } } }
static public void DrawColliders(Vector2D offset, float z) { float sunDirection = LightingManager2D.GetSunDirection(); LightingManager2D manager = LightingManager2D.Get(); // Day Soft Shadows GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); Vector3 vecA, vecB, vecC; Vector2D zA, zB, zC; Vector2D pA, pB; foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.shape.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } // Distance Check? if (id.InCamera() == false) { continue; } DayLightingShadowCollider shadow = id.GetDayLightingShadow(sunDirection * Mathf.Rad2Deg); foreach (Mesh mesh in shadow.meshes) { for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { vecA = mesh.vertices [mesh.triangles [i]]; vecB = mesh.vertices [mesh.triangles [i + 1]]; vecC = mesh.vertices [mesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(vecA.x, vecA.y, vecB.x, vecB.y, vecC.x, vecC.y, offset + new Vector2D(id.transform.position), z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); manager.materials.GetShadowBlur().SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.shape.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } // Distance Check? if (id.InCamera() == false) { continue; } DayLightingShadowCollider shadow = id.GetDayLightingShadow(sunDirection * Mathf.Rad2Deg); Vector2D gameObjectOffset = new Vector2D(id.transform.position); foreach (Polygon2D polygon in shadow.polygons) { foreach (DoublePair2D p in DoublePair2D.GetList(polygon.pointsList)) { zA = p.A.Copy(); zA += offset + gameObjectOffset; zB = p.B.Copy(); zB += offset + gameObjectOffset; zC = zB.Copy(); pA = zA.Copy(); pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } } } GL.End(); GL.PopMatrix(); }
void DrawSoftShadows(Vector2D offset, float z) { float sunDirection = LightingManager2D.GetSunDirection(); // Day Soft Shadows foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight && id.height > 0) { Polygon2D poly = id.GetPolygon(); poly = poly.ToWorldSpace(id.gameObject.transform); GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(sunDirection, id.height); vB.Push(sunDirection, id.height); Max2DMatrix.DrawTriangle(p.A, p.B, vA, offset, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, offset, z); } GL.End(); GL.PopMatrix(); Polygon2D convexHull = id.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection); Max2D.SetColor(Color.white); GL.PushMatrix(); LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList)) { Vector2D zA = new Vector2D(p.A + offset); Vector2D zB = new Vector2D(p.B + offset); Vector2D zC = new Vector2D(p.B + offset); Vector2D pA = zA.Copy(); Vector2D pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } GL.End(); GL.PopMatrix(); } } foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Max2D.SetColor(Color.white); Max2D.iDrawMesh(id.GetMesh(), id.transform, offset, z); } Vector2 size = new Vector2(bufferCamera.orthographicSize * ((float)Screen.width / Screen.height), bufferCamera.orthographicSize); Vector3 pos = Camera.main.transform.position; Max2D.iDrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z); }
public List <Vector2D> GetPointsList(float multiplier = 1f) { if (pointsList == null) { Debug.LogError("Complex Cut generation issue"); return(new List <Vector2D>()); } float sizeM = size * multiplier; float sizeM2 = 2 * sizeM; List <Vector2D> list = new List <Vector2D>(pointsList); if (list.Count < 2) { return(new List <Vector2D>()); } List <Vector2D> newPointsListA = new List <Vector2D>(); List <Vector2D> newPointsListB = new List <Vector2D>(); if (list.Count > 2) { foreach (DoublePair2D pair in DoublePair2D.GetList(list)) { float rotA = (float)Vector2D.Atan2(pair.B, pair.A); float rotC = (float)Vector2D.Atan2(pair.B, pair.C); Vector2D pairA = new Vector2D(pair.A); pairA.Push(rotA - Mathf.PI / 2, sizeM); Vector2D pairC = new Vector2D(pair.C); pairC.Push(rotC + Mathf.PI / 2, sizeM); Vector2D vecA = new Vector2D(pair.B); vecA.Push(rotA - Mathf.PI / 2, sizeM); vecA.Push(rotA, 10f); Vector2D vecC = new Vector2D(pair.B); vecC.Push(rotC + Mathf.PI / 2, sizeM); vecC.Push(rotC, 10f); Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC)); if (result != null) { newPointsListA.Add(result); } } if (newPointsListA.Count > 2) { newPointsListA.Remove(newPointsListA.First()); newPointsListA.Remove(newPointsListA.Last()); } foreach (DoublePair2D pair in DoublePair2D.GetList(list, false)) { float rotA = (float)Vector2D.Atan2(pair.B, pair.A); float rotC = (float)Vector2D.Atan2(pair.B, pair.C); Vector2D pairA = new Vector2D(pair.A); pairA.Push(rotA - Mathf.PI / 2, -sizeM); Vector2D pairC = new Vector2D(pair.C); pairC.Push(rotC + Mathf.PI / 2, -sizeM); Vector2D vecA = new Vector2D(pair.B); vecA.Push(rotA - Mathf.PI / 2, -sizeM); vecA.Push(rotA, 10f); Vector2D vecC = new Vector2D(pair.B); vecC.Push(rotC + Mathf.PI / 2, -sizeM); vecC.Push(rotC, 10f); Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC)); if (result != null) { newPointsListB.Add(result); } } if (newPointsListB.Count > 2) { newPointsListB.Remove(newPointsListB.First()); newPointsListB.Remove(newPointsListB.Last()); } } List <Vector2D> newPointsList = new List <Vector2D>(); foreach (Vector2D p in newPointsListA) { newPointsList.Add(p); } Vector2D prevA = new Vector2D(list.ElementAt(list.Count - 2)); Vector2D pA = new Vector2D(list.Last()); pA.Push(Vector2D.Atan2(pA, prevA) - Mathf.PI / 6, sizeM2); newPointsList.Add(pA); pA = new Vector2D(list.Last()); pA.Push(Vector2D.Atan2(pA, prevA) + Mathf.PI / 6, sizeM2); newPointsList.Add(pA); newPointsListB.Reverse(); foreach (Vector2D p in newPointsListB) { newPointsList.Add(p); } Vector2D prevB = new Vector2D(list.ElementAt(1)); Vector2D pB = new Vector2D(list.First()); pB.Push(Vector2D.Atan2(pB, prevB) - Mathf.PI / 6, sizeM2); newPointsList.Add(pB); pB = new Vector2D(list.First()); pB.Push(Vector2D.Atan2(pB, prevB) + Mathf.PI / 6, sizeM2); newPointsList.Add(pB); return(newPointsList); }
public static void Draw(Vector2D offset, float z) { // Collider Ambient Occlusion GL.PushMatrix(); LightingManager2D.Get().occlusionBlurMaterial.SetPass(0); GL.Begin(GL.QUADS); GL.Color(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.ambientOcclusion == false || id.smoothOcclusionEdges == true) { continue; } // Do not call Create From Collider List <Polygon2D> polygons = id.GetPolygons(); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly.Normalize(); Vector2D first = null; List <Pair2D> iterate1 = Pair2D.GetList(poly.pointsList); List <Pair2D> iterate2 = Pair2D.GetList(PreparePolygon(poly, id.occlusionSize).pointsList); int i = 0; foreach (Pair2D pA in iterate1) { if (id.edgeCollider2D == true && first == null) { first = pA.A; continue; } Pair2D pB = iterate2[i]; GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pA.A + offset, z); GL.TexCoord2(uv1, uv0); Max2D.Vertex3(pA.B + offset, z); GL.TexCoord2(uv1, uv1); Max2D.Vertex3(pB.B + offset, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(pB.A + offset, z); i++; } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().occlusionEdgeMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.ambientOcclusion == false || id.smoothOcclusionEdges == false) { continue; } // Do not call Create From Collider List <Polygon2D> polygons = id.GetPolygons(); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly.Normalize(); foreach (DoublePair2D p in DoublePair2D.GetList(poly.pointsList)) { Vector2D vA = p.A + offset; Vector2D vB = p.B + offset; Vector2D vC = p.B + offset; Vector2D pA = p.A + offset; Vector2D pB = p.B + offset; vA.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, id.occlusionSize); vB.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, id.occlusionSize); vC.Push(Vector2D.Atan2(p.B, p.C) - Mathf.PI / 2, id.occlusionSize); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv1, uv0); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vC, z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().occlusionEdgeMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.End(); GL.PopMatrix(); }
public static void Draw(Vector2D offset, float z) { float sunDirection = LightingManager2D.GetSunDirection(); // Day Soft Shadows GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } List <Polygon2D> polygons = id.GetPolygons(); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(sunDirection, id.height); vB.Push(sunDirection, id.height); Max2DMatrix.DrawTriangle(p.A, p.B, vA, offset, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, offset, z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } List <Polygon2D> polygons = id.GetPolygons(); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height); foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList)) { Vector2D zA = new Vector2D(p.A + offset); Vector2D zB = new Vector2D(p.B + offset); Vector2D zC = zB.Copy(); Vector2D pA = zA.Copy(); Vector2D pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); GL.End(); GL.PopMatrix(); Material material = LightingManager2D.Get().whiteSpriteMaterial; foreach (LightingSprite2D id in LightingSprite2D.GetList()) { if (id.GetSpriteRenderer() == null) { continue; } material.mainTexture = id.GetSpriteRenderer().sprite.texture; //Debug.Log(sprite.pivot); Vector2 p = id.transform.position; Vector2 scale = id.transform.lossyScale; if (id.GetSpriteRenderer().flipX) { scale.x = -scale.x; } if (id.GetSpriteRenderer().flipY) { scale.y = -scale.y; } Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z); } material = LightingManager2D.Get().additiveMaterial; foreach (LightingSpriteRenderer2D id in LightingSpriteRenderer2D.GetList()) { if (id.sprite == null) { continue; } material.mainTexture = id.sprite.texture; //Debug.Log(sprite.pivot); Vector2 position = id.transform.position; Vector2 scale = id.transform.lossyScale; float scaleX = (float)id.sprite.texture.width / (id.sprite.pixelsPerUnit * 2); float scaleY = (float)id.sprite.texture.width / (id.sprite.pixelsPerUnit * 2); //Debug.Log(scaleX + " " + scaleY); scale.x *= scaleX; scale.y *= scaleY; scale.x *= id.scale.x; scale.y *= id.scale.y; if (id.flipX) { scale.x = -scale.x; } if (id.flipY) { scale.y = -scale.y; } //material.color = id.color; Color color = id.color; color.a = id.alpha; material.SetColor("_TintColor", color); Max2D.DrawImage(material, offset.ToVector2() + position + id.offset, scale, id.transform.rotation.eulerAngles.z, z); } material.mainTexture = null; float ratio = (float)Screen.width / Screen.height; Camera bufferCamera = LightingMainBuffer2D.Get().bufferCamera; Vector2 size = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize); Vector3 pos = Camera.main.transform.position; Max2D.iDrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z); }
static void Draw_Collider_Smooth(Vector2D offset, float z) { GL.PushMatrix(); LightingManager2D.Get().occlusionEdgeMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.ambientOcclusion == false || id.smoothOcclusionEdges == false) { continue; } // Do not call Create From Collider List <Polygon2D> polygons = null; switch (id.colliderType) { case LightingCollider2D.ColliderType.Collider: polygons = id.GetColliderPolygons(); break; case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape: polygons = id.GetShapePolygons(); break; } if (polygons == null || polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); poly.Normalize(); foreach (DoublePair2D p in DoublePair2D.GetList(poly.pointsList)) { Vector2D vA = p.A + offset; Vector2D vB = p.B + offset; Vector2D vC = p.B + offset; Vector2D pA = p.A + offset; Vector2D pB = p.B + offset; vA.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, id.occlusionSize); vB.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, id.occlusionSize); vC.Push(Vector2D.Atan2(p.B, p.C) - Mathf.PI / 2, id.occlusionSize); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(vA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv1, uv0); Max2D.Vertex3(vB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(vC, z); } } } GL.End(); GL.PopMatrix(); }
public void OnRenderObject() { Max2D.SetLineWidth(lineWidth); Max2D.SetColor(Color.white); Max2D.SetLineMode(Max2D.LineMode.Smooth); Max2D.SetBorder(false); if (polygon == null) { return; } Polygon2D poly = polygon.ToWorldSpace(transform); float z = transform.position.z + lineOffset; List <DoublePair2D> list = DoublePair2D.GetList(poly.pointsList); DoublePair2D lastPair = list.Last(); GL.PushMatrix(); materialAdditive.SetPass(0); GL.Begin(GL.QUADS); foreach (DoublePair2D p in list) { Vector2D vA = GetPoint(lastPair); Vector2D vB = GetPoint(p); if (vA == null || vB == null) { continue; } float c = (Mathf.Cos(Vector2D.Atan2(vA, vB) + Mathf.PI / 4) + 1f) / 8.75f; GL.Color(new Color(c, c, c)); GL.TexCoord2(uv0, uv0); GL.Vertex3(p.A.vector.x, p.A.vector.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3(p.B.vector.x, p.B.vector.y, z); GL.TexCoord2(uv1, uv1); GL.Vertex3(vB.vector.x, vB.vector.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3(vA.vector.x, vA.vector.y, z); lastPair = p; } GL.End(); materialMultiply.SetPass(0); GL.Begin(GL.QUADS); foreach (DoublePair2D p in list) { Vector2D vA = GetPoint(lastPair); Vector2D vB = GetPoint(p); if (vA == null || vB == null) { continue; } float c = (Mathf.Cos(Vector2D.Atan2(vA, vB) + Mathf.PI / 4) + 1f) / 1.75f + 0.5f; GL.Color(new Color(c, c, c)); GL.TexCoord2(uv0, uv0); GL.Vertex3(p.A.vector.x, p.A.vector.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3(p.B.vector.x, p.B.vector.y, z); GL.TexCoord2(uv1, uv1); GL.Vertex3(vB.vector.x, vB.vector.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3(vA.vector.x, vA.vector.y, z); lastPair = p; } GL.End(); GL.PopMatrix(); }