static public void SortObjects(LightingBuffer2D buffer, int layer) { colliderList = LightingCollider2D.GetList(); tilemapList = LightingTilemapCollider2D.GetList(); list.Reset(); for (int id = 0; id < colliderList.Count; id++) { // Check If It's In Light Area? collider = colliderList[id]; if ((int)colliderList[id].lightingCollisionLayer != layer && (int)colliderList[id].lightingMaskLayer != layer) { continue; } if (layerSettings.renderingOrder == LightRenderingOrder.YAxis) { list.Add(collider, -collider.transform.position.y); } else { list.Add(collider, -Vector2.Distance(collider.transform.position, buffer.lightSource.transform.position)); } } for (int id = 0; id < tilemapList.Count; id++) { SortTilemap(buffer, tilemapList[id]); } list.Sort(); }
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 public void DrawCollidersMasking(Vector2D offset, float z) { LightingManager2D manager = LightingManager2D.Get(); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.generateDayMask == false) { continue; } // Distance Check? if (id.InCamera() == false) { continue; } switch (id.shape.maskType) { case LightingCollider2D.MaskType.SpriteCustomPhysicsShape: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.shape.GetMesh_Shape(), id.transform, offset, z); break; case LightingCollider2D.MaskType.Collider: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.shape.GetMesh_Collider(id.transform), id.transform, offset, z); break; case LightingCollider2D.MaskType.Sprite: if (id.spriteRenderer == null || id.spriteRenderer.sprite == null) { break; } Material material = manager.materials.GetWhiteSprite(); material.mainTexture = id.spriteRenderer.sprite.texture; GL.PushMatrix(); 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); GL.PopMatrix(); break; } } }
public void OnRenderObject() { if (Camera.current != bufferCamera) { return; } if (Camera.main == null) { return; } LightingManager2D.LightingDebug.LightMainBufferUpdates += 1; float z = transform.position.z; Vector2D offset = new Vector2D(-Camera.main.transform.position); Max2D.Check(); LightingSoftDayShadows.Draw(offset, z); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false) { continue; } Max2D.SetColor(Color.white); Max2D.iDrawMesh(id.GetMesh(), id.transform, offset, z); } float darkness = 1f - LightingManager2D.Get().shadowDarkness; LightingManager2D.Get().additiveMaterial.SetColor("_TintColor", new Color(0.5f, 0.5f, 0.5f, darkness)); float ratio = (float)Screen.width / Screen.height; Vector2 size = new Vector2(Camera.main.orthographicSize * ratio, Camera.main.orthographicSize); Max2D.DrawImage(LightingManager2D.Get().additiveMaterial, Vector2.zero, size, 0, z); DrawRooms(offset, z); DrawLightingBuffers(z); LightingOcclussion.Draw(offset, z); }
public static void DrawShadowsWithoutAtlas(LightingBuffer2D buffer, int layer) { float lightSizeSquared = Mathf.Sqrt(buffer.lightSource.lightSize * buffer.lightSource.lightSize + buffer.lightSource.lightSize * buffer.lightSource.lightSize); float z = buffer.transform.position.z; offset.x = -buffer.lightSource.transform.position.x; offset.y = -buffer.lightSource.transform.position.y; manager = LightingManager2D.Get(); colliderList = LightingCollider2D.GetList(); #if UNITY_2018_1_OR_NEWER tilemapList = LightingTilemapCollider2D.GetList(); #endif manager.materials.GetAtlasMaterial().SetPass(0); GL.Begin(GL.TRIANGLES); for (int id = 0; id < colliderList.Count; id++) { if ((int)colliderList[id].lightingCollisionLayer != layer) { continue; } LightingBufferMesh.Shadow(buffer, colliderList[id], lightSizeSquared, z); LightingBufferShape.Shadow(buffer, colliderList[id], lightSizeSquared, z, offset); } #if UNITY_2018_1_OR_NEWER for (int id = 0; id < tilemapList.Count; id++) { if ((int)tilemapList[id].lightingCollisionLayer != layer) { continue; } LightingBufferTilemapRectangle.Shadow(buffer, tilemapList[id], lightSizeSquared, z); } #endif GL.End(); }
static public void OnGUI() { if (timer == null) { LightingDebug.timer = TimerHelper.Create(); } if (timer.GetMillisecs() > 1000) { ShowLightBufferUpdates = LightBufferUpdates; LightBufferUpdates = 0; ShowLightMainBufferUpdates = LightMainBufferUpdates; LightMainBufferUpdates = 0; ShowLightMainCameraUpdates = LightMainCameraUpdates; LightMainCameraUpdates = 0; timer = TimerHelper.Create(); } ShowLightSourceDrawn = LightSourceDrawn; LightSourceDrawn = 0; GUI.Label(new Rect(10, 10, 200, 20), "Light Buffer Updates: " + ShowLightBufferUpdates); GUI.Label(new Rect(10, 30, 200, 20), "Light Main Buffer Updates: " + ShowLightMainBufferUpdates); GUI.Label(new Rect(10, 50, 200, 20), "Light Main Camera Updates: " + ShowLightMainCameraUpdates); GUI.Label(new Rect(10, 70, 200, 20), "Light Buffer Count: " + LightingBuffer2D.GetList().Count); GUI.Label(new Rect(10, 90, 200, 20), "Light Collider Count: " + LightingCollider2D.GetList().Count); Texture texture = LightingMainBuffer2D.Get().bufferCamera.activeTexture; GUI.Label(new Rect(10, 110, 200, 20), "Main Buffer Resolution: " + texture.width + "x" + texture.height); GUI.Label(new Rect(10, 130, 200, 20), "Light Sources Drawn: " + ShowLightSourceDrawn); }
static void Draw_Collider_Strict(Vector2D offset, float z) { 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 = 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(); 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; } if (i >= iterate2.Count) { 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(); }
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); }
void DrawShadows() { float z = transform.position.z; // Shadow Fill GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { List <Polygon2D> polygons = id.GetPolygons(); if (polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), 25); vB.Push(Vector2D.Atan2(vB, zero), 25); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } } } foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } Vector3 rot = GetPitchYawRollRad(id.transform.rotation); float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2); float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2); float scaleX = id.transform.lossyScale.x * rotationXScale; float scaleY = id.transform.lossyScale.y * rotationYScale; 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] == false) { continue; } Polygon2D poly = Polygon2D.CreateFromRect(new Vector2(0.5f * scaleX, 0.5f * scaleY)); Vector2D polyOffset = Vector2D.Zero(); polyOffset += new Vector2D(x + 0.5f, y + 0.5f); polyOffset += new Vector2D(id.area.position.x, id.area.position.y); polyOffset += new Vector2D(id.transform.position.x, id.transform.position.y); if (id.mapType == LightingTilemapCollider2D.MapType.SuperTilemapEditor) { polyOffset += new Vector2D(-id.area.size.x / 2, -id.area.size.y / 2); } polyOffset.x *= scaleX; polyOffset.y *= scaleY; polyOffset += new Vector2D(-lightSource.transform.position); poly = poly.ToOffset(polyOffset); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), 25); vB.Push(Vector2D.Atan2(vB, zero), 25); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } } } } GL.End(); // Penumbra LightingManager2D.Get().penumbraMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { List <Polygon2D> polygons = id.GetPolygons(); if (polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D pA = p.A.Copy(); Vector2D vB = p.B.Copy(); Vector2D pB = p.B.Copy(); float angleA = (float)Vector2D.Atan2(vA, zero); float angleB = (float)Vector2D.Atan2(vB, zero); vA.Push(angleA, lightSource.lightSize); pA.Push(angleA - Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); vB.Push(angleB, lightSource.lightSize); pB.Push(angleB + Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.A.x, (float)p.A.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uv0, uv1); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.B.x, (float)p.B.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3((float)pB.x, (float)pB.y, z); } } } foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } //Debug.Log(Mathf.Cos(id.transform.rotation.eulerAngles.x * Mathf.Deg2Rad)); Vector3 rot = GetPitchYawRollRad(id.transform.rotation); float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2); float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2); float scaleX = id.transform.lossyScale.x * rotationXScale; float scaleY = id.transform.lossyScale.y * rotationYScale; 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] == false) { continue; } Polygon2D poly = Polygon2D.CreateFromRect(new Vector2(0.5f * scaleX, 0.5f * scaleY)); Vector2D polyOffset = Vector2D.Zero(); polyOffset += new Vector2D(x + 0.5f, y + 0.5f); polyOffset += new Vector2D(id.area.position.x, id.area.position.y); polyOffset += new Vector2D(id.transform.position.x, id.transform.position.y); if (id.mapType == LightingTilemapCollider2D.MapType.SuperTilemapEditor) { polyOffset += new Vector2D(-id.area.size.x / 2, -id.area.size.y / 2); } polyOffset.x *= scaleX; polyOffset.y *= scaleY; polyOffset += new Vector2D(-lightSource.transform.position); poly = poly.ToOffset(polyOffset); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D pA = p.A.Copy(); Vector2D vB = p.B.Copy(); Vector2D pB = p.B.Copy(); float angleA = (float)Vector2D.Atan2(vA, zero); float angleB = (float)Vector2D.Atan2(vB, zero); vA.Push(angleA, lightSource.lightSize); pA.Push(angleA - Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); vB.Push(angleB, lightSource.lightSize); pB.Push(angleB + Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.A.x, (float)p.A.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uv0, uv1); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.B.x, (float)p.B.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3((float)pB.x, (float)pB.y, z); } } } } GL.End(); GL.PopMatrix(); }
void DrawCollideMask() { float z = transform.position.z; Vector2D offset = new Vector2D(-lightSource.transform.position); Material material = LightingManager2D.Get().whiteSpriteMaterial; // For Collider Sprite Mask foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Sprite sprite = id.lightSprite; if (sprite == null || id.spriteRenderer == null) { continue; } if (id.maskType != LightingCollider2D.MaskType.Sprite) { continue; } material.mainTexture = sprite.texture; Vector2 p = id.transform.position; Vector2 scale = id.transform.lossyScale; scale.x *= (float)sprite.texture.width / sprite.texture.height; if (id.spriteRenderer.flipX) { scale.x = -scale.x; } if (id.spriteRenderer.flipY) { scale.y = -scale.y; } Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z); } GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); // For Collider Mask foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Mesh mesh = id.GetMesh(); if (mesh == null) { continue; } if (id.maskType != LightingCollider2D.MaskType.Collider) { continue; } for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { Vector2 a = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]); Vector2 b = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]); Vector2 c = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]); Max2DMatrix.DrawTriangle(a, b, c, offset.ToVector2(), z); } } Mesh tileMesh = GetTileMesh(); foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } Vector3 rot = GetPitchYawRollRad(id.transform.rotation); float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2); float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2); float scaleX = id.transform.lossyScale.x * rotationXScale; float scaleY = id.transform.lossyScale.y * rotationYScale; 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] == false) { continue; } Vector2D polyOffset = Vector2D.Zero(); polyOffset += new Vector2D(x + 0.5f, y + 0.5f); polyOffset += new Vector2D(id.area.position.x, id.area.position.y); polyOffset += new Vector2D(id.transform.position.x, id.transform.position.y); if (id.mapType == LightingTilemapCollider2D.MapType.SuperTilemapEditor) { polyOffset += new Vector2D(-id.area.size.x / 2, -id.area.size.y / 2); } polyOffset.x *= scaleX; polyOffset.y *= scaleY; polyOffset += offset; for (int i = 0; i < tileMesh.triangles.GetLength(0); i = i + 3) { Vector2 a = tileMesh.vertices [tileMesh.triangles [i]]; Vector2 b = tileMesh.vertices [tileMesh.triangles [i + 1]]; Vector2 c = tileMesh.vertices [tileMesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(a, b, c, polyOffset.ToVector2(), z, new Vector2D(scaleX, scaleY)); } } } } GL.End(); GL.PopMatrix(); }
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 void OnRenderObject() { if (Camera.current != bufferCamera) { return; } LightingManager2D.LightingDebug.LightBufferUpdates++; if (lightSource == null) { return; } if (lightSource.update == false) { bufferCamera.enabled = false; } LateUpdate(); lightSource.update = false; material.SetColor("_TintColor", lightSource.color); Vector2D vZero = new Vector2D(0, 0); float z = transform.position.z; Max2D.Check(); GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Polygon2D poly = id.GetPolygon(); poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(vZero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, vZero), 15); vB.Push(Vector2D.Atan2(vB, vZero), 15); Max2DMatrix.DrawTriangle(p.A, p.B, vA, vZero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, vZero, z); } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().penumbraMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); const float uv0 = 1f / 128f; const float uv1 = 1f - uv0; foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Polygon2D poly = id.GetPolygon(); poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(vZero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D pA = p.A.Copy(); Vector2D vB = p.B.Copy(); Vector2D pB = p.B.Copy(); float angleA = (float)Vector2D.Atan2(vA, vZero); float angleB = (float)Vector2D.Atan2(vB, vZero); vA.Push(angleA, lightSource.lightSize); pA.Push(angleA - Mathf.Deg2Rad * 25, lightSource.lightSize); vB.Push(angleB, lightSource.lightSize); pB.Push(angleB + Mathf.Deg2Rad * 25, lightSource.lightSize); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.A.x, (float)p.A.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uv0, uv1); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.B.x, (float)p.B.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3((float)pB.x, (float)pB.y, z); } } GL.End(); GL.PopMatrix(); Max2D.SetColor(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Max2D.iDrawMesh(id.GetMesh(), id.transform, new Vector2D(-lightSource.transform.position), z); } Vector2 size = new Vector2(bufferCamera.orthographicSize, bufferCamera.orthographicSize); if (lightSource.rotationEnabled) { Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, lightSource.transform.rotation.eulerAngles.z, z); } else { Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, 0, z); } GL.PushMatrix(); Max2D.SetColor(Color.black); Max2D.defaultMaterial.color = Color.black; Max2D.defaultMaterial.SetPass(0); float rotation = lightSource.transform.rotation.eulerAngles.z; float squaredSize = Mathf.Sqrt((size.x * size.x) + (size.y * size.y)); Vector2 p0 = Vector2D.RotToVec((double)rotation).ToVector2() * squaredSize; Vector2 p1 = Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize; Max2DMatrix.DrawTriangle(Vector2.zero, p0, p1, Vector2.zero, z); Max2DMatrix.DrawTriangle(Vector2.zero, p1, p0, Vector2.zero, z); //Max2DMatrix.DrawTriangle(vA, vB, p.B, vZero, z); GL.End(); GL.PopMatrix(); Max2D.defaultMaterial.color = Color.white; //lightSource = null; //bufferCamera.enabled = false; }
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; } } }
void DrawCollideMask() { float z = transform.position.z; Vector2D offset = new Vector2D(-lightSource.transform.position); Material material = LightingManager2D.Get().whiteSpriteMaterial; // For Collider Sprite Mask foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Sprite sprite = id.lightSprite; if (sprite == null || id.spriteRenderer == null) { continue; } if (id.maskType != LightingCollider2D.MaskType.Sprite) { continue; } material.mainTexture = sprite.texture; Vector2 p = id.transform.position; Vector2 scale = id.transform.lossyScale; scale.x *= (float)sprite.texture.width / sprite.texture.height; if (id.spriteRenderer.flipX) { scale.x = -scale.x; } if (id.spriteRenderer.flipY) { scale.y = -scale.y; } Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z); } GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); // For Collider Mask foreach (LightingCollider2D id in LightingCollider2D.GetList()) { Mesh mesh = id.GetMesh(); if (mesh == null) { continue; } if (id.maskType != LightingCollider2D.MaskType.Collider) { continue; } for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { Vector2 a = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]); Vector2 b = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]); Vector2 c = id.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]); Max2DMatrix.DrawTriangle(a, b, c, offset.ToVector2(), z); } } GL.End(); GL.PopMatrix(); }
public static void DrawWithAtlas(LightingBuffer2D buffer, int layer) { float lightSizeSquared = Mathf.Sqrt(buffer.lightSource.lightSize * buffer.lightSource.lightSize + buffer.lightSource.lightSize * buffer.lightSource.lightSize); float z = buffer.transform.position.z; offset.x = -buffer.lightSource.transform.position.x; offset.y = -buffer.lightSource.transform.position.y; manager = LightingManager2D.Get(); colliderList = LightingCollider2D.GetList(); LayerSetting layerSetting = buffer.lightSource.layerSetting[layer]; bool maskEffect = (layerSetting.effect == LightingLayerEffect.InvisibleBellow); #if UNITY_2018_1_OR_NEWER tilemapList = LightingTilemapCollider2D.GetList(); #endif // Shadow Fill manager.materials.GetAtlasMaterial().SetPass(0); GL.Begin(GL.TRIANGLES); if (drawShadows) { for (int id = 0; id < colliderList.Count; id++) { if ((int)colliderList[id].lightingCollisionLayer != layer) { continue; } // Collider Shadow LightingBufferMesh.Shadow(buffer, colliderList[id], lightSizeSquared, z); LightingBufferShape.Shadow(buffer, colliderList[id], lightSizeSquared, z, offset); } #if UNITY_2018_1_OR_NEWER for (int id = 0; id < tilemapList.Count; id++) { if ((int)tilemapList[id].lightingCollisionLayer != layer) { continue; } // Tilemap Shadow LightingBufferTilemapRectangle.Shadow(buffer, tilemapList[id], lightSizeSquared, z); } #endif } if (drawMask) { if (colliderList.Count > 0) { for (int id = 0; id < colliderList.Count; id++) { if ((int)colliderList[id].lightingMaskLayer != layer) { continue; } // Collider Shape Mask LightingBufferShape.Mask(buffer, colliderList[id], layerSetting, offset, z); // Collider Sprite Mask LightingBufferSprite.MaskWithAtlas(buffer, colliderList[id], offset, z); } } #if UNITY_2018_1_OR_NEWER for (int id = 0; id < tilemapList.Count; id++) { if ((int)tilemapList[id].lightingMaskLayer != layer) { continue; } // Tilemap Shape Mask LightingBufferTilemapRectangle.MaskShape(buffer, tilemapList[id], offset, z); // Tilemap Sprite Mask LightingBufferTilemapRectangle.MaskSpriteWithAtlas(buffer, tilemapList[id], offset, z); } #endif } GL.End(); // Partialy Batched (Default Edition) if (buffer.partiallyBatchedList_Collider.Count > 0) { Material materialWhite = manager.materials.GetWhiteSprite(); Material materialBlack = manager.materials.GetBlackSprite(); Material material; PartiallyBatched_Collider batch; materialWhite.mainTexture = SpriteAtlasManager.Get().atlasTexture.texture; materialWhite.SetPass(0); for (int i = 0; i < buffer.partiallyBatchedList_Collider.Count; i++) { batch = buffer.partiallyBatchedList_Collider[i]; material = materialWhite; if (maskEffect && colliderList[i].transform.position.y < buffer.lightSource.transform.position.y) { material = materialBlack; } LightingBufferSprite.MaskWithoutAtlas(buffer, batch.collider2D, material, offset, z); } materialWhite.mainTexture = null; buffer.partiallyBatchedList_Collider.Clear(); } if (buffer.partiallyBatchedList_Tilemap.Count > 0) { Material materialWhite = manager.materials.GetWhiteSprite(); Material materialBlack = manager.materials.GetBlackSprite(); PartiallyBatched_Tilemap batch; Material material; // Draw Each Partialy Batched for (int i = 0; i < buffer.partiallyBatchedList_Tilemap.Count; i++) { batch = buffer.partiallyBatchedList_Tilemap[i]; material = materialWhite; if (maskEffect && batch.polyOffset.y < 0) { material = materialBlack; } material.mainTexture = batch.virtualSpriteRenderer.sprite.texture; Max2D.DrawSprite(material, batch.virtualSpriteRenderer, batch.polyOffset, batch.tileSize, 0, z); material.mainTexture = null; } buffer.partiallyBatchedList_Tilemap.Clear(); } }
static public void DrawMaskWithoutAtlas(LightingBuffer2D buffer, int layer) { float z = buffer.transform.position.z; offset.x = -buffer.lightSource.transform.position.x; offset.y = -buffer.lightSource.transform.position.y; manager = LightingManager2D.Get(); colliderList = LightingCollider2D.GetList(); LayerSetting layerSetting = buffer.lightSource.layerSetting[layer]; bool maskEffect = (layerSetting.effect == LightingLayerEffect.InvisibleBellow); Material materialWhite = manager.materials.GetWhiteSprite(); Material materialBlack = manager.materials.GetBlackSprite(); Material material; #if UNITY_2018_1_OR_NEWER tilemapList = LightingTilemapCollider2D.GetList(); #endif //materialWhite.SetPass(0); manager.materials.GetAtlasMaterial().SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); // Collider Shape Mask for (int id = 0; id < colliderList.Count; id++) { if ((int)colliderList[id].lightingMaskLayer != layer) { continue; } LightingBufferShape.Mask(buffer, colliderList[id], layerSetting, offset, z); } GL.Color(Color.white); // Tilemap Shape Mask #if UNITY_2018_1_OR_NEWER for (int id = 0; id < tilemapList.Count; id++) { if ((int)tilemapList[id].lightingMaskLayer != layer) { continue; } LightingBufferTilemapRectangle.MaskShape(buffer, tilemapList[id], offset, z); } #endif GL.End(); // Collider Sprite Mask if (colliderList.Count > 0) { for (int id = 0; id < colliderList.Count; id++) { if ((int)colliderList[id].lightingMaskLayer != layer) { continue; } material = materialWhite; if (colliderList[id].maskMode == LightingMaskMode.Invisible) { material = materialBlack; } else if (maskEffect && colliderList[id].transform.position.y < buffer.lightSource.transform.position.y) { material = materialBlack; } LightingBufferSprite.MaskWithoutAtlas(buffer, colliderList[id], material, offset, z); } } // Tilemap Sprite Mask #if UNITY_2018_1_OR_NEWER for (int id = 0; id < tilemapList.Count; id++) { if ((int)tilemapList[id].lightingMaskLayer != layer) { continue; } LightingBufferTilemapRectangle.MaskSpriteWithoutAtlas(buffer, tilemapList[id], materialWhite, materialBlack, offset, z); } #endif }
void DrawShadows() { float z = transform.position.z; // Shadow Fill GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { List <Polygon2D> polygons = id.GetPolygons(); if (polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), 25); vB.Push(Vector2D.Atan2(vB, zero), 25); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } } } GL.End(); // Penumbra LightingManager2D.Get().penumbraMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { List <Polygon2D> polygons = id.GetPolygons(); if (polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon; poly = poly.ToWorldSpace(id.gameObject.transform); poly = poly.ToOffset(new Vector2D(-lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D pA = p.A.Copy(); Vector2D vB = p.B.Copy(); Vector2D pB = p.B.Copy(); float angleA = (float)Vector2D.Atan2(vA, zero); float angleB = (float)Vector2D.Atan2(vB, zero); vA.Push(angleA, lightSource.lightSize); pA.Push(angleA - Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); vB.Push(angleB, lightSource.lightSize); pB.Push(angleB + Mathf.Deg2Rad * occlusionSize, lightSource.lightSize); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.A.x, (float)p.A.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uv0, uv1); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uv0, uv0); GL.Vertex3((float)p.B.x, (float)p.B.y, z); GL.TexCoord2(uv1, uv0); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uv0, uv1); GL.Vertex3((float)pB.x, (float)pB.y, z); } } } GL.End(); GL.PopMatrix(); }
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(); }
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(); }
void Update() { CheckIfUpdateNeeded(); LightingManager2D manager = LightingManager2D.Get(); bool disabled = manager.disableEngine; if (InCamera()) { if (update == true) { if (inScreen == false) { buffer = FBOManager.PullBuffer(LightingManager2D.GetTextureSize(textureSize), this); update = false; if (buffer != null) { if (disabled == false) { buffer.bufferCamera.enabled = true; // //UpdateLightBuffer(True) buffer.bufferCamera.orthographicSize = lightSize; } } //Debug.Log(3); inScreen = true; } else { update = false; if (buffer != null) { if (disabled == false) { buffer.bufferCamera.enabled = true; // //UpdateLightBuffer(True) buffer.bufferCamera.orthographicSize = lightSize; } } } } else { if (buffer != null) { // Debug.Log(1); } else { buffer = FBOManager.PullBuffer(LightingManager2D.GetTextureSize(textureSize), this); update = false; if (buffer != null) { if (disabled == false) { buffer.bufferCamera.enabled = true; // //UpdateLightBuffer(True) buffer.bufferCamera.orthographicSize = lightSize; } } inScreen = true; //Debug.Log(4); } } } else { ///// Free Buffer! if (buffer != null) { FBOManager.FreeBuffer(buffer); buffer = null; } inScreen = false; } if (eventHandling) { Vector2D zero = Vector2D.Zero(); float lightSizeSquared = Mathf.Sqrt(lightSize * lightSize + lightSize * lightSize); List <LightCollision2D> collisions = new List <LightCollision2D>(); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.colliderType == LightingCollider2D.ColliderType.None) { continue; } if (LightingManager2D.culling && Vector2.Distance(id.transform.position, transform.position) > id.GetCullingDistance() + lightSize) { continue; } LightCollision2D collision = new LightCollision2D(); collision.lightSource = this; collision.collider = id; collision.pointsColliding = id.GetShadowCollisionPolygons()[0].ToWorldSpace(id.transform).ToOffset(new Vector2D(-transform.position)).pointsList; collisions.Add(collision); } foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (LightingManager2D.culling && Vector2.Distance(id.transform.position, transform.position) > id.GetCullingDistance() + lightSize) { continue; } if (id.colliderType == LightingCollider2D.ColliderType.None) { continue; } List <Polygon2D> polygons = id.GetShadowCollisionPolygons(); if (polygons.Count < 1) { continue; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); poly.ToOffsetItself(new Vector2D(-transform.position)); if (poly.PointInPoly(zero)) { continue; } Vector2D vA, pA, vB, pB; float angleA, angleB; foreach (Pair2D p in Pair2D.GetList(poly.pointsList, false)) { vA = p.A.Copy(); pA = p.A.Copy(); vB = p.B.Copy(); pB = p.B.Copy(); angleA = (float)Vector2D.Atan2(vA, zero); angleB = (float)Vector2D.Atan2(vB, zero); vA.Push(angleA, lightSizeSquared); pA.Push(angleA - Mathf.Deg2Rad * occlusionSize, lightSizeSquared); vB.Push(angleB, lightSizeSquared); pB.Push(angleB + Mathf.Deg2Rad * occlusionSize, lightSizeSquared); if (eventHandling) { Polygon2D triPoly = new Polygon2D(); triPoly.AddPoint(p.A); triPoly.AddPoint(p.B); triPoly.AddPoint(pB); triPoly.AddPoint(pA); foreach (LightCollision2D col in new List <LightCollision2D>(collisions)) { if (col.collider == id) { continue; } foreach (Vector2D point in new List <Vector2D>(col.pointsColliding)) { if (triPoly.PointInPoly(point)) { col.pointsColliding.Remove(point); } } if (col.pointsColliding.Count < 1) { collisions.Remove(col); } } } } LightingManager2D.LightingDebug.shadowGenerations++; } } if (collisions.Count > 0) { foreach (LightCollision2D collision in collisions) { collision.collider.CollisionEvent(collision); } } } }
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); }