//public bool InCamera(Camera camera) { // return(false); //} static public Polygon2D CreateFromCamera(Camera camera) { float sizeY = Camera.main.orthographicSize; Polygon2D polygon = Polygon2D.CreateFromRect(new Vector2(sizeY * (((float)Screen.width) / Screen.height), sizeY)); return(polygon); }
public Polygon2D GetBoundPolygon() { float sizeY = GetOrtographicSize() / transform.localScale.x; float sizeX = sizeY * ((float)outputTexture.width / (float)outputTexture.height); return(Polygon2D.CreateFromRect(new Vector2(sizeX, sizeY))); }
public static Mesh GetStaticTileMesh() { if (staticTileMesh == null) { Polygon2D tilePoly = Polygon2D.CreateFromRect(new Vector2(0.5f + 0.01f, 0.5f + 0.01f)); staticTileMesh = tilePoly.CreateMesh(Vector2.zero, Vector2.zero); } return(staticTileMesh); }
public static Mesh GetRenderMesh() { if (preRenderMesh == null) { Polygon2D tilePoly = Polygon2D.CreateFromRect(new Vector2(1f, 1f)); Mesh staticTileMesh = tilePoly.CreateMesh(new Vector2(2f, 2f), Vector2.zero); preRenderMesh = staticTileMesh; } return(preRenderMesh); }
public void Draw() { if (lineBorder && meshBorder.Count > 0) { if (meshBorder.Count > 0) { foreach (Mesh m in meshBorder) { Max2DMesh.Draw(m, GetBorderMaterial()); } } } if (mesh.Count > 0) { foreach (Mesh m in mesh) { Max2DMesh.Draw(m, GetFillMaterial()); } } if (customEndingsPosition.Count > 0) { Matrix4x4 matrix; foreach (Pair2D pair in customEndingsPosition) { Polygon2D polyA = Polygon2D.CreateFromRect(new Vector2(1, 1)); //polyA.ToOffset(pair.A); Mesh mA = polyA.CreateMesh(new Vector2(2, 2), Vector2.zero); matrix = Matrix4x4.TRS(pair.A.ToVector3(zPosition), Quaternion.Euler(0, 0, 0), new Vector3(1, 1, 1)); Graphics.DrawMesh(mA, matrix, customEndingImageMaterial, 0); matrix = Matrix4x4.TRS(pair.B.ToVector3(zPosition), Quaternion.Euler(0, 0, 0), new Vector3(1, 1, 1)); Graphics.DrawMesh(mA, matrix, customEndingImageMaterial, 0); } } }
public List <Polygon2D> GetPolygon(LightingTilemapCollider2D tilemap) { if (world_polygon == null) { if (tilemap.colliderType == LightingTilemapCollider2D.ColliderType.SpriteCustomPhysicsShape) { if (GetShapePolygons().Count < 1) { return(null); } world_polygon = GetShapePolygons(); //poly.ToScaleItself(defaultSize); // scale? } else { Vector2 size = tilemap.cellSize * 0.5f; world_polygon = new List <Polygon2D>(); world_polygon.Add(Polygon2D.CreateFromRect(size)); } } return(world_polygon); }
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(); }
// Visuals //GameObject visualsGameObject; //MeshFilter visualMeshFilter; //MeshRenderer visualMeshRenderer; public void Draw() { if (lineBorder && meshBorder.Count > 0) { if (meshBorder.Count > 0) { foreach (Mesh m in meshBorder) { RendererObject renderObject = GetFreeRenderObject(); renderObject.drawn = true; renderObject.meshRenderer.sharedMaterial = GetBorderMaterial(); renderObject.meshFilter.sharedMesh = m; renderObject.transform.position = Vector3.zero; Vector3 lScale = renderObject.transform.parent.lossyScale; lScale.x = 1f / lScale.x; lScale.y = 1f / lScale.y; lScale.z = 1f / lScale.z; renderObject.transform.localScale = lScale; } } } if (mesh.Count > 0) { foreach (Mesh m in mesh) { RendererObject renderObject = GetFreeRenderObject(); renderObject.drawn = true; renderObject.meshRenderer.sharedMaterial = GetFillMaterial(); renderObject.meshFilter.sharedMesh = m; renderObject.transform.position = Vector3.zero; Vector3 lScale = renderObject.transform.parent.lossyScale; lScale.x = 1f / lScale.x; lScale.y = 1f / lScale.y; lScale.z = 1f / lScale.z; renderObject.transform.localScale = lScale; } } // Does not use Mesh Renderer Object if (customEndingsPosition.Count > 0) { Matrix4x4 matrix; foreach (Pair2 pair in customEndingsPosition) { Polygon2D polyA = Polygon2D.CreateFromRect(new Vector2(1, 1)); //polyA.ToOffset(pair.A); Mesh mA = polyA.CreateMesh(new Vector2(2, 2), Vector2.zero); matrix = Matrix4x4.TRS(pair.a.ToVector3(zPosition), Quaternion.Euler(0, 0, 0), new Vector3(1, 1, 1)); Graphics.DrawMesh(mA, matrix, customEndingImageMaterial, 0); matrix = Matrix4x4.TRS(pair.b.ToVector3(zPosition), Quaternion.Euler(0, 0, 0), new Vector3(1, 1, 1)); Graphics.DrawMesh(mA, matrix, customEndingImageMaterial, 0); } } }
public void AddModifier(Texture2D texture, Vector2 position, Vector2 size, float rotation) { Polygon2D poly = Polygon2D.CreateFromRect(size); poly.ToRotationItself(rotation * Mathf.Deg2Rad); poly.ToOffsetItself(new Vector2D(position)); List <Polygon2D> polys = shape.GetWorld(); if (polys.Count > 0) { bool touch = false; foreach (Polygon2D p in polys) { if (Math2D.PolyCollidePoly(poly, p) == true) { touch = true; } } if (touch == false) { return; } } else { Polygon2D p = GetBoundPolygon(); bool touch = false; p = p.ToWorldSpace(transform); if (Math2D.PolyCollidePoly(p, poly) == true) { touch = true; } if (touch == false) { return; } } Vector2 pos = transform.InverseTransformPoint(position); float ratioX = 1; if (transform.localScale.x != transform.localScale.y) { ratioX = transform.localScale.x / transform.localScale.y; size.y = size.y * ratioX; } size.x /= transform.localScale.x; size.y /= transform.localScale.y; size.y /= ratioX; DestructionModifier modifier = new DestructionModifier(texture, pos, size, rotation); modifiers.Add(modifier); modifiersAdded = true; // add only if colides if (textureType == TextureType.SpriteShape) { buffer.renderCamera.enabled = true; } else { Destruction2DManager.RequestBufferEvent(this); // Check if it is already requested } }