// Lighting Buffer TILE static public void MaskShapeDepthWithAtlas(LightingBuffer2D buffer, LightingTile tile, LightRenderingOrder lightSourceOrder, LightingTilemapCollider2D id, Vector2D offset, float z) { Mesh tileMesh = null; if (id.maskType == LightingTilemapCollider2D.MaskType.Tile) { tileMesh = LightingTile.GetStaticTileMesh(); } else if (id.maskType == LightingTilemapCollider2D.MaskType.SpriteCustomPhysicsShape) { tileMesh = tile.GetTileDynamicMesh(); } if (tileMesh == null) { return; } // Set Color Black Or White? GL.Color(Color.white); int triangleCount = tileMesh.triangles.GetLength(0); for (int i = 0; i < triangleCount; i = i + 3) { vecA = tileMesh.vertices [tileMesh.triangles [i]]; vecB = tileMesh.vertices [tileMesh.triangles [i + 1]]; vecC = tileMesh.vertices [tileMesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(vecA, vecB, vecC, offset.ToVector2(), z, new Vector2D(1, 1)); } LightingDebug.maskGenerations++; }
static public void iDrawMesh(Mesh mesh, Transform transform, Vector2D offset, float z) { if (mesh == null) { return; } GL.PushMatrix(); defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(setColor); List <Vector2> list = new List <Vector2>(); for (int i = 0; i < mesh.triangles.GetLength(0); i++) { list.Add(transform.TransformPoint(mesh.vertices [mesh.triangles [i]])); if (list.Count > 2) { Max2DMatrix.DrawTriangle(list [0].x, list [0].y, list [1].x, list [1].y, list [2].x, list [2].y, offset, z); list.Clear(); } } GL.End(); GL.PopMatrix(); }
static public void DrawMesh(Material material, Mesh mesh, Vector2D offset, float z = 0f) { if (mesh == null) { return; } GL.PushMatrix(); material.SetPass(0); GL.Begin(GL.TRIANGLES); List <Vector2> list = new List <Vector2>(); for (int i = 0; i < mesh.triangles.GetLength(0); i++) { list.Add(mesh.vertices [mesh.triangles [i]]); if (list.Count > 2) { Max2DMatrix.DrawTriangle(list [0].x, list [0].y, list [1].x, list [1].y, list [2].x, list [2].y, offset, z); list.Clear(); } } GL.End(); GL.PopMatrix(); }
static public void DrawTrianglef(float x0, float y0, float x1, float y1, float x2, float y2, Vector2D offset, float z = 0f) { GL.PushMatrix(); defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(setColor); Max2DMatrix.DrawTriangle(x0, y0, x1, y1, x2, y2, offset, z); GL.End(); GL.PopMatrix(); }
static public void Fill_Component(LightingBuffer2D buffer, LightingTilemapCollider2D id, float lightSizeSquared, float z) { Vector2D vA, vB; foreach (Polygon2D polygon in id.edgeColliders) { Vector2D polyOffset = new Vector2D(-buffer.lightSource.transform.position); Polygon2D poly = polygon.Copy(); poly.ToOffsetItself(polyOffset); foreach (Pair2D p in Pair2D.GetList(poly.pointsList, false)) { vA = p.A.Copy(); vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), lightSizeSquared); vB.Push(Vector2D.Atan2(vB, zero), lightSizeSquared); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } LightingDebug.shadowGenerations++; } foreach (Polygon2D polygon in id.polygonColliders) { Vector2D polyOffset = new Vector2D(-buffer.lightSource.transform.position); Polygon2D poly = polygon.Copy(); poly.ToOffsetItself(polyOffset); foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { vA = p.A.Copy(); vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), lightSizeSquared); vB.Push(Vector2D.Atan2(vB, zero), lightSizeSquared); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } LightingDebug.shadowGenerations++; } }
static void DrawSoftShadowTile(Vector2D offset, float z, float height) { float sunDirection = LightingManager2D.GetSunDirection(); Polygon2D poly = Polygon2DList.CreateFromRect(new Vector2(1, 1) / 2); poly = poly.ToOffset(new Vector2D(0.5f, 0.5f)); foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { Vector2D vA = p.A.Copy(); Vector2D vB = p.B.Copy(); vA.Push(sunDirection, height); vB.Push(sunDirection, height); Max2DMatrix.DrawTriangle(p.A, p.B, vA, offset, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, offset, z); } }
static public void DrawMesh(Material material, Mesh mesh, Transform transform, Vector2D offset, float z = 0f) { if (mesh == null) { return; } GL.PushMatrix(); material.SetPass(0); GL.Begin(GL.TRIANGLES); for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { Vector3 a = transform.TransformPoint(mesh.vertices [mesh.triangles [i]]); Vector3 b = transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]); Vector3 c = transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]); Max2DMatrix.DrawTriangle(a.x, a.y, b.x, b.y, c.x, c.y, offset, z); } GL.End(); GL.PopMatrix(); }
static public void DrawMesh(Material material, Mesh mesh, Transform transform, Vector2D offset, float z = 0f, Vector2D scale = null) { if (mesh == null) { return; } if (scale == null) { scale = new Vector2D(1, 1); } GL.PushMatrix(); material.SetPass(0); GL.Begin(GL.TRIANGLES); for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { Vector2 vA = mesh.vertices [mesh.triangles [i]]; Vector2 vB = mesh.vertices [mesh.triangles [i + 1]]; Vector2 vC = mesh.vertices [mesh.triangles [i + 2]]; vA.x *= (float)scale.x; vA.y *= (float)scale.y; vB.x *= (float)scale.x; vB.y *= (float)scale.y; vC.x *= (float)scale.x; vC.y *= (float)scale.y; Vector2 pA = transform.TransformPoint(vA); Vector2 pB = transform.TransformPoint(vB); Vector2 pC = transform.TransformPoint(vC); Max2DMatrix.DrawTriangle(pA, pB, pC, offset, z); } GL.End(); GL.PopMatrix(); }
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(); }
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 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 public void DrawShadow(LightingBuffer2D buffer, LightingTile tile, Vector2D polyOffset, LightingTilemapCollider2D tilemap, float lightSizeSquared, float z) { if (tile.GetPairs(tilemap).Count < 1) { return; } LightingBufferTilemapRectangle.pairList = tile.GetPairs(tilemap)[0]; Pair2D p; Vector2D zero = Vector2D.Zero(); GL.Color(Color.black); for (int s = 0; s < LightingBufferTilemapRectangle.pairList.Count; s++) { p = LightingBufferTilemapRectangle.pairList[s]; vA.x = p.A.x + polyOffset.x; vA.y = p.A.y + polyOffset.y; vB.x = p.B.x + polyOffset.x; vB.y = p.B.y + polyOffset.y; vC.x = p.A.x + polyOffset.x; vC.y = p.A.y + polyOffset.y; vD.x = p.B.x + polyOffset.x; vD.y = p.B.y + polyOffset.y; vA.Push(Vector2D.Atan2(vA, zero), lightSizeSquared); vB.Push(Vector2D.Atan2(vB, zero), lightSizeSquared); Max2DMatrix.DrawTriangle(vC, vD, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, vD, zero, z); } Sprite penumbraSprite = LightingManager2D.Get().materials.GetAtlasPenumbraSprite(); float angleA, angleB; Rect uvRect = new Rect((float)penumbraSprite.rect.x / penumbraSprite.texture.width, (float)penumbraSprite.rect.y / penumbraSprite.texture.height, (float)penumbraSprite.rect.width / penumbraSprite.texture.width, (float)penumbraSprite.rect.height / penumbraSprite.texture.height); uvRect.width += uvRect.x; uvRect.height += uvRect.y; uvRect.x += 1f / 2048; uvRect.y += 1f / 2048; uvRect.width -= 1f / 2048; uvRect.height -= 1f / 2048; GL.Color(Color.white); for (int s = 0; s < LightingBufferTilemapRectangle.pairList.Count; s++) { p = LightingBufferTilemapRectangle.pairList[s]; vA.x = p.A.x + polyOffset.x; vA.y = p.A.y + polyOffset.y; pA.x = p.A.x + polyOffset.x; pA.y = p.A.y + polyOffset.y; pB.x = p.B.x + polyOffset.x; pB.y = p.B.y + polyOffset.y; vB.x = p.B.x + polyOffset.x; vB.y = p.B.y + polyOffset.y; vC.x = p.A.x + polyOffset.x; vC.y = p.A.y + polyOffset.y; vD.x = p.B.x + polyOffset.x; vD.y = p.B.y + polyOffset.y; angleA = (float)Vector2D.Atan2(vA, zero); angleB = (float)Vector2D.Atan2(vB, zero); vA.Push(angleA, lightSizeSquared); pA.Push(angleA - Mathf.Deg2Rad * buffer.lightSource.occlusionSize, lightSizeSquared); vB.Push(angleB, lightSizeSquared); pB.Push(angleB + Mathf.Deg2Rad * buffer.lightSource.occlusionSize, lightSizeSquared); GL.TexCoord2(uvRect.x, uvRect.y); GL.Vertex3((float)vC.x, (float)vC.y, z); GL.TexCoord2(uvRect.width, uvRect.y); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uvRect.x, uvRect.height); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uvRect.x, uvRect.y); GL.Vertex3((float)vD.x, (float)vD.y, z); GL.TexCoord2(uvRect.width, uvRect.y); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uvRect.x, uvRect.height); GL.Vertex3((float)pB.x, (float)pB.y, z); } LightingDebug.shadowGenerations++; }
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 static void Shadow(LightingBuffer2D buffer, LightingCollider2D id, float lightSizeSquared, float z) { if (id.shape.colliderType != LightingCollider2D.ColliderType.Mesh) { return; } if (id.isVisibleForLight(buffer) == false) { return; } if (id.meshFilter == null) { return; } List <Polygon2D> polygons = new List <Polygon2D>(); Mesh mesh = id.meshFilter.sharedMesh; Vector3 vecA, vecB, vecC; Vector2D vA, pA, vB, pB; float angleA, angleB; for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { vecA = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i]]); vecB = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 1]]); vecC = buffer.transform.TransformPoint(mesh.vertices [mesh.triangles [i + 2]]); Polygon2D poly = new Polygon2D(); poly.AddPoint(vecA.x, vecA.y); poly.AddPoint(vecB.x, vecB.y); poly.AddPoint(vecC.x, vecC.y); //polygons.Add(poly); } if (polygons.Count < 1) { return; } Sprite penumbraSprite = LightingManager2D.Get().materials.GetAtlasPenumbraSprite(); Rect uvRect = new Rect((float)penumbraSprite.rect.x / penumbraSprite.texture.width, (float)penumbraSprite.rect.y / penumbraSprite.texture.height, (float)penumbraSprite.rect.width / penumbraSprite.texture.width, (float)penumbraSprite.rect.height / penumbraSprite.texture.height); uvRect.width += uvRect.x; uvRect.height += uvRect.y; uvRect.x += 1f / 2048; uvRect.y += 1f / 2048; uvRect.width -= 1f / 2048; uvRect.height -= 1f / 2048; GL.Color(Color.white); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); poly.ToOffsetItself(new Vector2D(-buffer.lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList)) { 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 * buffer.lightSource.occlusionSize, lightSizeSquared); vB.Push(angleB, lightSizeSquared); pB.Push(angleB + Mathf.Deg2Rad * buffer.lightSource.occlusionSize, lightSizeSquared); GL.TexCoord2(uvRect.x, uvRect.y); GL.Vertex3((float)p.A.x, (float)p.A.y, z); GL.TexCoord2(uvRect.width, uvRect.y); GL.Vertex3((float)vA.x, (float)vA.y, z); GL.TexCoord2((float)uvRect.x, uvRect.height); GL.Vertex3((float)pA.x, (float)pA.y, z); GL.TexCoord2(uvRect.x, uvRect.y); GL.Vertex3((float)p.B.x, (float)p.B.y, z); GL.TexCoord2(uvRect.width, uvRect.y); GL.Vertex3((float)vB.x, (float)vB.y, z); GL.TexCoord2(uvRect.x, uvRect.height); GL.Vertex3((float)pB.x, (float)pB.y, z); } //LightingDebug.penumbraGenerations ++; } GL.Color(Color.black); foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); poly.ToOffsetItself(new Vector2D(-buffer.lightSource.transform.position)); if (poly.PointInPoly(zero)) { continue; } foreach (Pair2D p in Pair2D.GetList(poly.pointsList, false)) { vA = p.A.Copy(); vB = p.B.Copy(); vA.Push(Vector2D.Atan2(vA, zero), lightSizeSquared); vB.Push(Vector2D.Atan2(vB, zero), lightSizeSquared); Max2DMatrix.DrawTriangle(p.A, p.B, vA, zero, z); Max2DMatrix.DrawTriangle(vA, vB, p.B, zero, z); } LightingDebug.shadowGenerations++; } }
public static void Mask(LightingBuffer2D buffer, LightingCollider2D id, LayerSetting layerSetting, Vector2D offset, float z) { if (false == (id.shape.maskType == LightingCollider2D.MaskType.Collider || id.shape.maskType == LightingCollider2D.MaskType.SpriteCustomPhysicsShape || id.shape.maskType == LightingCollider2D.MaskType.Mesh)) { return; } if (id.isVisibleForLight(buffer) == false) { return; } Mesh mesh = null; MeshVertices vertices = null; if (id.shape.maskType == LightingCollider2D.MaskType.Mesh) { if (id.meshFilter == null) { return; } mesh = id.meshFilter.sharedMesh; } else { mesh = id.shape.GetMesh_MaskType(id.transform); vertices = id.shape.GetMesh_Vertices_MaskType(id.transform); } if (mesh == null) { return; } bool maskEffect = (layerSetting.effect == LightingLayerEffect.InvisibleBellow); LightingMaskMode maskMode = id.maskMode; MeshVertice vertice; if (maskMode == LightingMaskMode.Invisible) { GL.Color(Color.black); } else if (layerSetting.effect == LightingLayerEffect.InvisibleBellow) { float c = (float)offset.y / layerSetting.maskEffectDistance + layerSetting.maskEffectDistance * 2; if (c < 0) { c = 0; } color.r = c; color.g = c; color.b = c; color.a = 1; GL.Color(color); } else { GL.Color(Color.white); } for (int i = 0; i < vertices.list.Count; i++) { vertice = vertices.list[i]; Max2DMatrix.DrawTriangle(vertice.a, vertice.b, vertice.c, offset, z); } LightingDebug.maskGenerations++; }
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 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); }
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(); }
void DrawLightTexture() { float z = 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); // Light Rotation!!! GL.PushMatrix(); Max2D.SetColor(Color.black); GL.Begin(GL.TRIANGLES); Max2D.defaultMaterial.color = Color.black; Max2D.defaultMaterial.SetPass(0); float rotation = lightSource.transform.rotation.eulerAngles.z * Mathf.Deg2Rad + Mathf.PI / 4; 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 / 2).ToVector2() * squaredSize; Vector2 p2 = Vector2D.RotToVec((double)rotation + Mathf.PI).ToVector2() * squaredSize; Vector2 p3 = Vector2D.RotToVec((double)rotation - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 up0 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize; Vector2 up1 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize; up1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize; Vector2 up2 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4).ToVector2() * squaredSize; up2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 up3 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 down0 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize; Vector2 down1 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI).ToVector2() * squaredSize; down1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize; Vector2 down2 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI).ToVector2() * squaredSize; down2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize; Vector2 down3 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI).ToVector2() * squaredSize; Vector2 left0 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 left1 = p0 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize; left1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 left2 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2).ToVector2() * squaredSize; left2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 left3 = p3 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 - Mathf.PI / 2).ToVector2() * squaredSize; Vector2 right0 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize; Vector2 right1 = p2 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize; left1 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize; Vector2 right2 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 + Mathf.PI / 2).ToVector2() * squaredSize; left2 += Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize; Vector2 right3 = p1 + Vector2D.RotToVec((double)rotation + Mathf.PI / 4 - Mathf.PI / 2 + Mathf.PI / 2).ToVector2() * squaredSize; Max2DMatrix.DrawTriangle(right0, right1, right2, Vector2.zero, z); Max2DMatrix.DrawTriangle(right2, right3, right0, Vector2.zero, z); Max2DMatrix.DrawTriangle(left0, left1, left2, Vector2.zero, z); Max2DMatrix.DrawTriangle(left2, left3, left0, Vector2.zero, z); Max2DMatrix.DrawTriangle(down0, down1, down2, Vector2.zero, z); Max2DMatrix.DrawTriangle(down2, down3, down0, Vector2.zero, z); Max2DMatrix.DrawTriangle(up0, up1, up2, Vector2.zero, z); Max2DMatrix.DrawTriangle(up2, up3, up0, Vector2.zero, z); GL.End(); GL.PopMatrix(); Max2D.defaultMaterial.color = Color.white; } else { Max2D.DrawImage(lightSource.GetMaterial(), Vector2.zero, size, 0, z); } }
static public void MaskShape(LightingBuffer2D buffer, LightingTilemapCollider2D id, Vector2D offset, float z) { if (id.maskType == LightingTilemapCollider2D.MaskType.SpriteCustomPhysicsShape || id.maskType == LightingTilemapCollider2D.MaskType.Tile) { } else { return; } if (id.map == null) { return; } SetupLocation(buffer, id); Vector2 vecA, vecB, vecC; LightingTile tile; Mesh tileMesh = null; int triangleCount; tileSize2.x = 1; tileSize2.y = 1; if (id.maskType == LightingTilemapCollider2D.MaskType.Tile) { tileMesh = LightingTile.GetStaticTileMesh(); } for (int x = newPositionInt.x - sizeInt; x < newPositionInt.x + sizeInt; x++) { for (int y = newPositionInt.y - sizeInt; y < newPositionInt.y + sizeInt; y++) { if (x < 0 || y < 0) { continue; } if (x >= id.area.size.x || y >= id.area.size.y) { continue; } tile = id.map[x, y]; if (tile == null) { continue; } polyOffset.x = x + tilemapOffset.x; polyOffset.y = y + tilemapOffset.y; polyOffset.x *= scale.x; polyOffset.y *= scale.y; polyOffset2.x = (float)polyOffset.x; polyOffset2.y = (float)polyOffset.y; if (LightingManager2D.culling && Vector2.Distance(polyOffset2, buffer.lightSource.transform.position) > (id.cellSize.x * 2f) + buffer.lightSource.lightSize) { LightingDebug.culled++; continue; } polyOffset.x += offset.x; polyOffset.y += offset.y; if (id.maskType == LightingTilemapCollider2D.MaskType.SpriteCustomPhysicsShape) { tileMesh = null; tileMesh = tile.GetTileDynamicMesh(); } if (tileMesh == null) { continue; } polyOffset2.x = (float)polyOffset.x; polyOffset2.y = (float)polyOffset.y; // Batch and Optimize??? triangleCount = tileMesh.triangles.GetLength(0); for (int i = 0; i < triangleCount; i = i + 3) { vecA = tileMesh.vertices [tileMesh.triangles [i]]; vecB = tileMesh.vertices [tileMesh.triangles [i + 1]]; vecC = tileMesh.vertices [tileMesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(vecA, vecB, vecC, polyOffset2, z, tileSize2); } LightingDebug.maskGenerations++; } } }
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; }