public void Update(Vector2D pos) { if (input.GetInputClicked()) { pointsList.Clear(); complexTracker.trackerList.Clear(); pointsList.Add(pos); } if (input.GetInputPressed() && pointsList.Count > 0) { Vector2D posMove = pointsList.Last().Copy(); int loopCount = 0; while ((Vector2D.Distance(posMove, pos) > minVertexDistance)) { float direction = (float)Vector2D.Atan2(pos, posMove); posMove.Push(direction, minVertexDistance); Slicer2D.complexSliceType = complexSliceType; pointsList.Add(posMove.Copy()); complexTracker.Update(posMove.ToVector2(), 0); loopCount++; if (loopCount > 150) { break; } } complexTracker.Update(posMove.ToVector2(), minVertexDistance); } if (input.GetInputReleased()) { pointsList.Clear(); complexTracker.trackerList.Clear(); } }
private void UpdateComplexTracked(Vector2D pos) { if (Input.GetMouseButtonDown(0)) { pointsList.Clear(); complexTracker.trackerList.Clear(); pointsList.Add(pos); } if (Input.GetMouseButton(0) && pointsList.Count > 0) { Vector2D posMove = pointsList.Last().Copy(); int loopCount = 0; while ((Vector2D.Distance(posMove, pos) > minVertexDistance)) { float direction = (float)Vector2D.Atan2(pos, posMove); posMove.Push(direction, minVertexDistance); Slicer2D.complexSliceType = complexSliceType; pointsList.Add(posMove.Copy()); complexTracker.Update(posMove.ToVector2(), 0); loopCount++; if (loopCount > 150) { break; } } mouseDown = true; complexTracker.Update(posMove.ToVector2(), minVertexDistance); } else { mouseDown = false; } }
void Update() { Vector3 pos = transform.position; if (Vector2.Distance(moveTo.ToVector2(), pos) > speed * 1.5f) { pos += Vector2D.RotToVec(Vector2D.Atan2(moveTo, new Vector2D(pos))).ToVector3(0) * speed; pos.z = -3f; transform.position = pos; } else { Destroy(gameObject); } }
static public void DrawPolygon(Polygon2D polygon, Vector3 position) { Vector3 a = Vector3.zero; Vector3 b = Vector3.zero; for (int i = 0; i < polygon.pointsList.Count; i++) { Vector2D p0 = polygon.pointsList[i]; Vector2D p1 = polygon.pointsList[(i + 1) % polygon.pointsList.Count]; a = LightingPosition.GetPosition3DWorld(p0.ToVector2(), position); b = LightingPosition.GetPosition3DWorld(p1.ToVector2(), position); Gizmos.DrawLine(a, b); } }
public void Spawn(Vector2D pos) { if (modifierTextures.Length > 0) { Destruction2D.AddModifierAll(modifierTextures[modifierID], pos.ToVector2(), modifierSize, modifierRotation, destructionLayer); if (randomRotation) { modifierRotation = Random.Range(0, 360); } if (randomModifierID) { modifierID = Random.Range(0, modifierTextures.Length - 1); } } }
void Start() { Vector2D position = new Vector2D(anchorBody.transform.position); material = MaterialManager.GetSpriteCopy().material; texture = Resources.Load("Sprites/Rope/SmallRope") as Texture; material.mainTexture = texture; GameObject prev = anchorBody; int ropeId = 1; GameObject gObject = null; float distance = 1.5f; while (Vector2D.Distance(position, new Vector2D(connectedBody.transform.position)) > distance) { double direction = Vector2D.Atan2(new Vector2D(connectedBody.transform.position), position); gObject = new GameObject(); HingeJoint2D hingeJoint = gObject.AddComponent <HingeJoint2D>(); hingeJoint.connectedBody = prev.GetComponent <Rigidbody2D>(); gObject.transform.parent = transform; gObject.transform.position = position.ToVector2(); gObject.name = "Rope " + ropeId; //gObject.AddComponent<JointRenderer2D> ().color = color; gObject.AddComponent <CircleCollider2D> ().radius = .25f; nodes.Add(hingeJoint); position.Push(direction, distance); prev = gObject; ropeId++; } if (gameObject != null) { HingeJoint2D joint = gObject.AddComponent <HingeJoint2D>(); joint.connectedBody = connectedBody.GetComponent <Rigidbody2D>(); nodes.Add(joint); } }
///// Create ///// public void GenerateCreateMesh(Vector2 pos, Polygon2D.PolygonType polygonType, float polygonSize, Slicer2DCreateControllerObject.CreateType createType, List <Vector2D> complexSlicerPointsList, Pair2D linearPair, float minVertexDistance, Transform transform, float lineWidth, float zPosition, float squareSize, Slicer2DLineEndingType endingType, int edges) { float size = squareSize; if (createType == Slicer2DCreateControllerObject.CreateType.Slice) { if (complexSlicerPointsList.Count > 0) { linearPair.A = new Vector2D(complexSlicerPointsList.First()); linearPair.B = new Vector2D(complexSlicerPointsList.Last()); GenerateSquare(linearPair.A.ToVector2(), size, transform, lineWidth, zPosition, endingType, edges); GenerateSquare(linearPair.B.ToVector2(), size, transform, lineWidth, zPosition, endingType, edges); Vector2D vA, vB; foreach (Pair2 pair in Pair2.GetList(complexSlicerPointsList, true)) { vA = new Vector2D(pair.a); vB = new Vector2D(pair.b); vA.Push(Vector2D.Atan2(pair.a, pair.b), -minVertexDistance / 5); vB.Push(Vector2D.Atan2(pair.a, pair.b), minVertexDistance / 5); CreateLine(new Pair2(vA.ToVector2(), vB.ToVector2()), transform.localScale, lineWidth, zPosition); } } } else { Polygon2D poly = Polygon2D.Create(polygonType, polygonSize).ToOffset(pos); Vector2D vA, vB; foreach (Pair2 pair in Pair2.GetList(poly.pointsList, true)) { vA = new Vector2D(pair.a); vB = new Vector2D(pair.b); vA.Push(Vector2D.Atan2(pair.a, pair.b), -minVertexDistance / 5); vB.Push(Vector2D.Atan2(pair.a, pair.b), minVertexDistance / 5); CreateLine(new Pair2(vA.ToVector2(), vB.ToVector2()), transform.localScale, lineWidth, zPosition); } } }
public void OnRenderObject() { if (Camera.current != bufferCamera) { return; } float z = transform.position.z; Vector2D offset = new Vector2D(-Camera.main.transform.position); Max2D.Check(); DrawSoftShadows(offset, z); DrawRooms(offset, z); DrawLightingBuffers(z); DrawOcclussion(offset.ToVector2(), z); }
public void Update(Vector2D pos) { if (input.GetInputClicked()) { pointsList.Clear(); linearTracker.trackerList.Clear(); pointsList.Add(pos); } if (input.GetInputPressed() && pointsList.Count > 0) { linearTracker.Update(pos.ToVector2(), minVertexDistance); } if (input.GetInputReleased()) { pointsList.Clear(); linearTracker.trackerList.Clear(); } }
private void UpdateLinearTracked(Vector2D pos) { if (Input.GetMouseButtonDown(0)) { pointsList.Clear(); linearTracker.trackerList.Clear(); pointsList.Add(pos); } if (Input.GetMouseButton(0) && pointsList.Count > 0) { Vector2D posMove = pointsList.Last().Copy(); mouseDown = true; linearTracker.Update(pos.ToVector2(), minVertexDistance); } else { mouseDown = false; } }
static public void DrawMask(LightingBuffer2D buffer, LightingTile tile, LightingLayerEffect maskEffect, Material materialA, Material materialB, Vector2D polyOffset, LightingTilemapCollider2D tilemap, float lightSizeSquared, float z) { VirtualSpriteRenderer spriteRenderer = new VirtualSpriteRenderer(); spriteRenderer.sprite = tile.GetOriginalSprite(); if (spriteRenderer.sprite == null) { return; } Vector3 rot = Math2D.GetPitchYawRollRad(tilemap.transform.rotation); float rotationYScale = Mathf.Sin(rot.x + Mathf.PI / 2); float rotationXScale = Mathf.Sin(rot.y + Mathf.PI / 2); float scaleX = tilemap.transform.lossyScale.x * rotationXScale * tilemap.cellSize.x; float scaleY = tilemap.transform.lossyScale.y * rotationYScale * tilemap.cellSize.y; Vector2 tileSize = new Vector2(scaleX / tilemap.cellSize.x, scaleY / tilemap.cellSize.y); Material material = materialA; if (tilemap.maskMode == LightingMaskMode.Invisible || (maskEffect == LightingLayerEffect.InvisibleBellow && polyOffset.y < 0)) { material = materialB; } material.mainTexture = spriteRenderer.sprite.texture; Max2D.DrawSprite(material, spriteRenderer, polyOffset.ToVector2(), tileSize, 0, z); material.mainTexture = null; LightingDebug.maskGenerations++; }
static public void MaskSpriteWithAtlas(LightingBuffer2D buffer, LightingTilemapCollider2D id, Vector2D offset, float z) { if (id.maskType != LightingTilemapCollider2D.MaskType.Sprite) { return; } if (id.map == null) { return; } SetupLocation(buffer, id); Sprite reqSprite; PartiallyBatched_Tilemap batched; 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; } if (tile.GetOriginalSprite() == 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) > 2 + buffer.lightSource.lightSize) { LightingDebug.culled++; continue; } polyOffset.x += offset.x; polyOffset.y += offset.y; spriteRenderer.sprite = tile.GetAtlasSprite(); if (spriteRenderer.sprite == null) { reqSprite = SpriteAtlasManager.RequestSprite(tile.GetOriginalSprite(), SpriteRequest.Type.WhiteMask); if (reqSprite == null) { // Add Partialy Batched batched = new PartiallyBatched_Tilemap(); batched.virtualSpriteRenderer = new VirtualSpriteRenderer(); batched.virtualSpriteRenderer.sprite = tile.GetOriginalSprite(); batched.polyOffset = polyOffset.ToVector2(); batched.tileSize = tileSize; buffer.partiallyBatchedList_Tilemap.Add(batched); continue; } else { tile.SetAtlasSprite(reqSprite); spriteRenderer.sprite = reqSprite; } } polyOffset2.x = (float)polyOffset.x; polyOffset2.y = (float)polyOffset.y; Max2D.DrawSpriteBatched_Tris(spriteRenderer, buffer.lightSource.layerSetting[0], id.maskMode, polyOffset2, tileSize, 0, 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 SpriteToMesh3D(GameObject gameObject, VirtualSpriteRenderer spriteRenderer, float zSize, PolygonTriangulator2D.Triangulation triangulation = PolygonTriangulator2D.Triangulation.Advanced, Vector2D customUVOffset = null) { Texture2D texture = null; Sprite sprite = null; if (spriteRenderer.sprite != null) { sprite = spriteRenderer.sprite; texture = sprite.texture; } if (customUVOffset == null) { customUVOffset = Vector2D.Zero(); } float spriteSheetU = (float)(texture.width) / sprite.rect.width; float spriteSheetV = (float)(texture.height) / sprite.rect.height; Rect rect = sprite.rect; Rect uvRect = new Rect((float)rect.x / texture.width, (float)rect.y / texture.height, (float)rect.width / texture.width, (float)rect.height / texture.height); Vector2 scale = new Vector2(spriteSheetU * rect.width / sprite.pixelsPerUnit, spriteSheetV * rect.height / sprite.pixelsPerUnit); if (spriteRenderer.flipX) { scale.x = -scale.x; } if (spriteRenderer.flipY) { scale.y = -scale.y; } float pivotX = sprite.pivot.x / sprite.rect.width - 0.5f; float pivotY = sprite.pivot.y / sprite.rect.height - 0.5f; float ix = -0.5f + pivotX / spriteSheetU; float iy = -0.5f + pivotY / spriteSheetV; Vector2 uvOffset = new Vector2(uvRect.center.x + ix, uvRect.center.y + iy); customUVOffset.x /= scale.x; customUVOffset.y /= scale.y; uvOffset += customUVOffset.ToVector2(); Polygon2D polygon2D = Polygon2DList.CreateFromGameObject(gameObject)[0]; polygon2D.CreateMesh3D(gameObject, zSize, scale, uvOffset, 0, triangulation); MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer> (); if (meshRenderer == null) { meshRenderer = gameObject.AddComponent <MeshRenderer> (); } meshRenderer.sharedMaterial = spriteRenderer.material; meshRenderer.sharedMaterial.mainTexture = texture; meshRenderer.sharedMaterial.color = spriteRenderer.color; meshRenderer.sortingLayerName = spriteRenderer.sortingLayerName; meshRenderer.sortingLayerID = spriteRenderer.sortingLayerID; meshRenderer.sortingOrder = spriteRenderer.sortingOrder; }
void GenerateMesh_Soft() { double angleA, angleB, angleC; List <DoublePair2> iterate3; DoublePair2 p; List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); List <Vector2> uvs = new List <Vector2>(); Vector2D vA = Vector2D.Zero(), vB = Vector2D.Zero(), vC = Vector2D.Zero(), pA = Vector2D.Zero(), pB = Vector2D.Zero(); int count = 0; GameObject gameObject = GetOcclusionGameObject(); MeshRenderer meshRenderer = GetMeshRenderer(); MeshFilter meshFilter = GetMeshFilter(); occlusionShape = GetOcclusionShape(); for (int x = 0; x < occlusionShape.polygonPairs.Count; x++) { iterate3 = occlusionShape.polygonPairs[x]; for (int y = 0; y < iterate3.Count; y++) { p = iterate3[y]; vA.x = p.A.x; vA.y = p.A.y; vB.x = p.B.x; vB.y = p.B.y; pA.x = p.A.x; pA.y = p.A.y; pB.x = p.B.x; pB.y = p.B.y; vC.x = p.B.x; vC.y = p.B.y; angleA = p.A.Atan2(p.B) - Mathf.PI / 2; angleB = p.A.Atan2(p.B) - Mathf.PI / 2; angleC = p.B.Atan2(p.C) - Mathf.PI / 2; vA.Push(angleA, occlusionSize); vB.Push(angleB, occlusionSize); vC.Push(angleC, occlusionSize); Vector2D ps = (vB + vC) / 2; float distance = Vector2.Distance(p.B, vB.ToVector2()) - 180f * Mathf.Deg2Rad; float rot = p.B.Atan2(ps.ToVector2()); ps = new Vector2D(p.B); ps.Push(rot, distance); vertices.Add(pA.ToVector2()); uvs.Add(new Vector2(0f, 0f)); vertices.Add(pB.ToVector2()); uvs.Add(new Vector2(0.5f, 0f)); vertices.Add(vB.ToVector2()); uvs.Add(new Vector2(0.5f, 1f)); vertices.Add(vA.ToVector2()); uvs.Add(new Vector2(0f, 1f)); vertices.Add(ps.ToVector2()); uvs.Add(new Vector2(1f, 1f)); vertices.Add(vC.ToVector2()); uvs.Add(new Vector2(0.5f, 1f)); triangles.Add(count + 0); triangles.Add(count + 1); triangles.Add(count + 2); triangles.Add(count + 2); triangles.Add(count + 3); triangles.Add(count + 0); triangles.Add(count + 1); triangles.Add(count + 2); triangles.Add(count + 4); triangles.Add(count + 4); triangles.Add(count + 5); triangles.Add(count + 1); count += 6; } } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); meshFilter.mesh = mesh; meshRenderer.sharedMaterial = Lighting2D.materials.GetOcclusionEdge(); }
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); }
public Vector2List GetPointsList(float multiplier = 1f) { float sizeM = size * multiplier; float sizeM2 = 2 * sizeM; Vector2List list = pointsList; if (list.Count() < 2) { return(new Vector2List(true)); } List <Vector2> newPointsListA = new List <Vector2>(); List <Vector2> newPointsListB = new List <Vector2>(); Pair2D pair0 = Pair2D.Zero(); Pair2D pair1 = Pair2D.Zero(); if (list.Count() > 2) { List <DoublePair2> pairList = DoublePair2.GetList(list); foreach (DoublePair2 pair in pairList) { float rotA = pair.b.Atan2(pair.a); float rotC = pair.b.Atan2(pair.c); Vector2 pairA = pair.a; pairA = pairA.Push(rotA - Mathf.PI / 2, sizeM); Vector2 pairC = pair.c; pairC = pairC.Push(rotC + Mathf.PI / 2, sizeM); Vector2 vecA = pair.b; vecA = vecA.Push(rotA - Mathf.PI / 2, sizeM); vecA = vecA.Push(rotA, 10f); Vector2 vecC = pair.b; vecC = vecC.Push(rotC + Mathf.PI / 2, sizeM); vecC = vecC.Push(rotC, 10f); pair0.A.x = pairA.x; pair0.A.y = pairA.y; pair0.B.x = vecA.x; pair0.B.y = vecA.y; pair1.A.x = pairC.x; pair1.A.y = pairC.y; pair1.B.x = vecC.x; pair1.B.y = vecC.y; Vector2D result = Math2D.GetPointLineIntersectLine(pair0, pair1); if (result != null) { newPointsListA.Add(result.ToVector2()); } } if (newPointsListA.Count > 2) { newPointsListA.Remove(newPointsListA.First()); newPointsListA.Remove(newPointsListA.Last()); } foreach (DoublePair2 pair in pairList) { float rotA = pair.b.Atan2(pair.a); float rotC = pair.b.Atan2(pair.c); Vector2 pairA = pair.a; pairA = pairA.Push(rotA - Mathf.PI / 2, -sizeM); Vector2 pairC = pair.c; pairC = pairC.Push(rotC + Mathf.PI / 2, -sizeM); Vector2 vecA = pair.b; vecA = vecA.Push(rotA - Mathf.PI / 2, -sizeM); vecA = vecA.Push(rotA, 10f); Vector2 vecC = pair.b; vecC = vecC.Push(rotC + Mathf.PI / 2, -sizeM); vecC = vecC.Push(rotC, 10f); pair0.A.x = pairA.x; pair0.A.y = pairA.y; pair0.B.x = vecA.x; pair0.B.y = vecA.y; pair1.A.x = pairC.x; pair1.A.y = pairC.y; pair1.B.x = vecC.x; pair1.B.y = vecC.y; Vector2D result = Math2D.GetPointLineIntersectLine(pair0, pair1); if (result != null) { newPointsListB.Add(result.ToVector2()); } } if (newPointsListB.Count > 2) { newPointsListB.Remove(newPointsListB.First()); newPointsListB.Remove(newPointsListB.Last()); } } Vector2List newPointsList = new Vector2List(true); foreach (Vector2 p in newPointsListA) { newPointsList.Add(p); } Vector2 prevA = list.points.ElementAt(list.points.Count - 2); Vector2 pA = list.Last(); pA = pA.Push(pA.Atan2(prevA) - Mathf.PI / 6, sizeM2); newPointsList.Add(pA); pA = list.Last(); pA = pA.Push(pA.Atan2(prevA) + Mathf.PI / 6, sizeM2); newPointsList.Add(pA); newPointsListB.Reverse(); foreach (Vector2 p in newPointsListB) { newPointsList.Add(p); } Vector2 prevB = list.points.ElementAt(1); Vector2 pB = list.First(); pB = pB.Push(pB.Atan2(prevB) - Mathf.PI / 6, sizeM2); newPointsList.Add(pB); pB = list.First(); pB = pB.Push(pB.Atan2(prevB) + Mathf.PI / 6, sizeM2); newPointsList.Add(pB); return(newPointsList); }
public Pair2 ToPair2() { return(new Pair2(A.ToVector2(), B.ToVector2())); }
public static void Draw(Vector2D offset, float z) { List <LightingSpriteRenderer2D> list = LightingSpriteRenderer2D.GetList(); LightingSpriteRenderer2D id; Material material; Vector2 position, scale; float rot; Color color; for (int i = 0; i < list.Count; i++) { id = list[i]; if (id.GetSprite() == null) { continue; } if (id.InCamera() == false) { continue; } LightingManager2D.LightingDebug.SpriteRenderersDrawn++; position = id.transform.position; scale = id.transform.lossyScale; scale.x *= id.offsetScale.x; scale.y *= id.offsetScale.y; rot = id.offsetRotation; if (id.applyTransformRotation) { rot += id.transform.rotation.eulerAngles.z; } switch (id.type) { case LightingSpriteRenderer2D.Type.Particle: color = id.color; color.a = id.alpha; material = LightingManager2D.Get().additiveMaterial; material.mainTexture = id.GetSprite().texture; material.SetColor("_TintColor", color); Max2D.DrawSpriteRenderer(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; break; case LightingSpriteRenderer2D.Type.WhiteMask: material = LightingManager2D.Get().whiteSpriteMaterial; material.mainTexture = id.GetSprite().texture; Max2D.DrawSpriteRenderer(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; break; case LightingSpriteRenderer2D.Type.BlackMask: material = LightingManager2D.Get().blackSpriteMaterial; material.mainTexture = id.sprite.texture; Max2D.DrawSpriteRenderer(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; break; } } }
void UpdateAI(int id) { if (controller.Playing(id)) { return; } // Slicing the biggest object in the scene only Sliceable2D biggestObject = GetSlicerInZone(); if (biggestObject != null) { controller.ClearActions(id); Polygon2D poly = biggestObject.shape.GetWorld(); // Predict rigidbody movement Rigidbody2D rb = biggestObject.GetComponent <Rigidbody2D>(); Vector2 pos = rb.position; Vector2 gravity = Physics2D.gravity; Vector2 force = rb.velocity; // Get center of the object Vector2 centerOffset = poly.GetBounds().center - pos; float timer = 0; while (timer < 0.25f) { float delta = 0.1f; pos += force * delta; force += gravity * delta; timer += delta; } centerOffset += pos; // Random slice rotation double sliceRotation = Random.Range(0f, Mathf.PI * 2); // Get bounds of an object to know the slice size Rect bounds = poly.GetBounds(); float sliceSize = Mathf.Sqrt(bounds.width * bounds.width + bounds.height * bounds.height) * 1f; Vector2D firstPoint = new Vector2D(centerOffset); firstPoint.Push(sliceRotation, sliceSize); Vector2D secondPoint = new Vector2D(centerOffset); secondPoint.Push(sliceRotation, -sliceSize); // How fast to perform actions? float actionTime = 0.125f * 0.6f; controller.SetMouse(firstPoint.ToVector2(), actionTime, id); controller.PressMouse(actionTime, id); controller.MoveMouse(secondPoint.ToVector2(), actionTime, id); controller.ReleaseMouse(actionTime, id); controller.SetMouse(Vector2.zero, id); controller.Play(id); } }
public static void Draw(Vector2D offset, float z) { float sunDirection = LightingManager2D.GetSunDirection(); // Day Soft Shadows GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } List <Polygon2D> polygons = null; switch (id.colliderType) { case LightingCollider2D.ColliderType.Collider: polygons = id.GetColliderPolygons(); break; case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape: polygons = id.GetShapePolygons(); break; } Polygon2D poly; Vector3 vecA; Vector3 vecB; Vector3 vecC; foreach (Polygon2D polygon in polygons) { poly = polygon.ToWorldSpace(id.gameObject.transform); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height); Mesh mesh = convexHull.CreateMesh(Vector2.zero, Vector2.zero); for (int i = 0; i < mesh.triangles.GetLength(0); i = i + 3) { vecA = mesh.vertices [mesh.triangles [i]]; vecB = mesh.vertices [mesh.triangles [i + 1]]; vecC = mesh.vertices [mesh.triangles [i + 2]]; Max2DMatrix.DrawTriangle(vecA.x, vecA.y, vecB.x, vecB.y, vecC.x, vecC.y, offset, z); } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); // Null Check? LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.dayHeight == false || id.height <= 0) { continue; } if (id.colliderType == LightingCollider2D.ColliderType.Mesh) { continue; } List <Polygon2D> polygons = null; switch (id.colliderType) { case LightingCollider2D.ColliderType.Collider: polygons = id.GetColliderPolygons(); break; case LightingCollider2D.ColliderType.SpriteCustomPhysicsShape: polygons = id.GetShapePolygons(); break; } foreach (Polygon2D polygon in polygons) { Polygon2D poly = polygon.ToWorldSpace(id.gameObject.transform); Polygon2D convexHull = Polygon2D.GenerateShadow(new Polygon2D(poly.pointsList), sunDirection, id.height); foreach (DoublePair2D p in DoublePair2D.GetList(convexHull.pointsList)) { Vector2D zA = new Vector2D(p.A + offset); Vector2D zB = new Vector2D(p.B + offset); Vector2D zC = zB.Copy(); Vector2D pA = zA.Copy(); Vector2D pB = zB.Copy(); zA.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zB.Push(Vector2D.Atan2(p.A, p.B) + pi2, .5f); zC.Push(Vector2D.Atan2(p.B, p.C) + pi2, .5f); GL.TexCoord2(uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zA, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(uv0, uv1); Max2D.Vertex3(zB, z); GL.TexCoord2(0.5f - uv0, uv0); Max2D.Vertex3(pB, z); GL.TexCoord2(0.5f - uv0, uv1); Max2D.Vertex3(zC, z); } } } GL.End(); GL.PopMatrix(); #if UNITY_2018_1_OR_NEWER GL.PushMatrix(); Max2D.defaultMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); GL.Color(Color.black); // Day Soft Shadows foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } if (id.dayHeight == false) { continue; } for (int x = 0; x < id.area.size.x; x++) { for (int y = 0; y < id.area.size.y; y++) { if (id.map[x, y] == null) { DrawSoftShadowTile(offset + new Vector2D(x, y), z, id.height); } } } } GL.End(); GL.PopMatrix(); GL.PushMatrix(); LightingManager2D.Get().shadowBlurMaterial.SetPass(0); GL.Begin(GL.TRIANGLES); Max2D.SetColor(Color.white); // Day Soft Shadows foreach (LightingTilemapCollider2D id in LightingTilemapCollider2D.GetList()) { if (id.map == null) { continue; } if (id.dayHeight == false) { continue; } for (int x = 0; x < id.area.size.x; x++) { for (int y = 0; y < id.area.size.y; y++) { if (id.map[x, y] == null) { DrawSoftShadowTileBlur(offset + new Vector2D(x, y), z, id.height); } } } } GL.End(); GL.PopMatrix(); #endif /* * * Material material = LightingManager2D.Get().whiteSpriteMaterial; * foreach (LightingSprite2D id in LightingSprite2D.GetList()) { * if (id.GetSpriteRenderer() == null) { * continue; * } * material.mainTexture = id.GetSpriteRenderer().sprite.texture; //Debug.Log(sprite.pivot); * * Vector2 p = id.transform.position; * Vector2 scale = id.transform.lossyScale; * * if (id.GetSpriteRenderer().flipX) { * scale.x = -scale.x; * } * * if (id.GetSpriteRenderer().flipY) { * scale.y = -scale.y; * } * * Max2D.DrawImage(material, offset.ToVector2() + p, scale, id.transform.rotation.eulerAngles.z, z); * } */ float ratio = (float)Screen.width / Screen.height; Camera bufferCamera = LightingMainBuffer2D.Get().bufferCamera; Vector2 size = new Vector2(bufferCamera.orthographicSize * ratio, bufferCamera.orthographicSize); Vector3 pos = Camera.main.transform.position; Max2D.DrawImage(LightingManager2D.Get().additiveMaterial, new Vector2D(pos), new Vector2D(size), pos.z); // Day Lighting Masking foreach (LightingCollider2D id in LightingCollider2D.GetList()) { if (id.generateDayMask == false) { continue; } switch (id.maskType) { case LightingCollider2D.MaskType.SpriteCustomPhysicsShape: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.GetShapeMesh(), id.transform, offset, z); break; case LightingCollider2D.MaskType.Collider: Max2D.SetColor(Color.white); Max2D.DrawMesh(Max2D.defaultMaterial, id.GetColliderMesh(), id.transform, offset, z); break; case LightingCollider2D.MaskType.Sprite: if (id.spriteRenderer == null || id.spriteRenderer.sprite == null) { break; } Material material = LightingManager2D.Get().whiteSpriteMaterial; material.mainTexture = id.spriteRenderer.sprite.texture; Max2D.DrawSprite(material, id.spriteRenderer, new Vector2(id.transform.position.x, id.transform.position.y) + offset.ToVector2(), new Vector2(1, 1), id.transform.rotation.eulerAngles.z, z); break; } } }
static public void DrawWithAtlas(Vector2D offset, float z) { material = manager.materials.GetAdditive(); material.SetColor("_TintColor", Color.white); material.mainTexture = SpriteAtlasManager.Get().atlasTexture.texture; material.SetPass(0); GL.Begin(GL.TRIANGLES); for (int i = 0; i < spriteRendererList.Count; i++) { id = spriteRendererList[i]; if (id.type != LightingSpriteRenderer2D.Type.Particle) { continue; } if (id.GetSprite() == null) { continue; } if (id.InCamera() == false) { continue; } position = id.transform.position; scale = id.transform.lossyScale; scale.x *= id.offsetScale.x; scale.y *= id.offsetScale.y; rot = id.offsetRotation; if (id.applyTransformRotation) { rot += id.transform.rotation.eulerAngles.z; } color = id.color; color.a = id.alpha; GL.Color(color); spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.Normal); Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); LightingDebug.SpriteRenderersDrawn++; } GL.End(); material = manager.materials.GetAtlasMaterial(); material.SetPass(0); GL.Begin(GL.TRIANGLES); for (int i = 0; i < spriteRendererList.Count; i++) { id = spriteRendererList[i]; if (id.type == LightingSpriteRenderer2D.Type.Particle) { continue; } if (id.GetSprite() == null) { continue; } if (id.InCamera() == false) { continue; } position = id.transform.position; scale = id.transform.lossyScale; scale.x *= id.offsetScale.x; scale.y *= id.offsetScale.y; rot = id.offsetRotation; if (id.applyTransformRotation) { rot += id.transform.rotation.eulerAngles.z; } switch (id.type) { case LightingSpriteRenderer2D.Type.WhiteMask: GL.Color(Color.white); spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.WhiteMask); Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); break; case LightingSpriteRenderer2D.Type.BlackMask: GL.Color(Color.black); spriteRenderer.sprite = SpriteAtlasManager.RequestSprite(id.GetSprite(), SpriteRequest.Type.WhiteMask); Max2D.DrawSpriteBatched_Tris_Day(spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); break; } LightingDebug.SpriteRenderersDrawn++; } GL.End(); }
static public Vector2List GetPoints(Vector2List list, float distance) { if (list.Count() < 2) { return(list); } Vector2List result = list.Copy(); float directionA = list.points[0].Atan2(list.points[1]); float directionB = list.points[list.points.Count - 2].Atan2(list.points[list.points.Count - 1]); Vector2 pointA = list.points[0]; Vector2 pointB = list.points[list.points.Count - 1]; pointA.Push(directionA, distance); pointB.Push(directionB, -distance); Slicer2D slicerA = Slicer2D.PointInSlicerComponent(list.points[0].ToVector2D()); Slicer2D slicerB = Slicer2D.PointInSlicerComponent(list.points[list.points.Count - 1].ToVector2D()); Pair2 thresholdPairA = new Pair2(list.points[0], pointA); Pair2 thresholdPairB = new Pair2(list.points[list.points.Count - 1], pointB); Vector2D resultA = null; Vector2D resultB = null; if (slicerA != null) { List <Vector2D> pointsA = slicerA.shape.GetWorld().GetListLineIntersectPoly(thresholdPairA.ToPair2D()); if (pointsA.Count > 0) { pointsA = Vector2DList.GetListSortedToPoint(pointsA, pointA.ToVector2D()); resultA = pointsA[pointsA.Count - 1]; resultA.Push(directionA, 0.05f); } } if (slicerB != null) { List <Vector2D> pointsB = slicerB.shape.GetWorld().GetListLineIntersectPoly(thresholdPairB.ToPair2D()); if (pointsB.Count > 0) { pointsB = Vector2DList.GetListSortedToPoint(pointsB, pointB.ToVector2D()); resultB = pointsB[pointsB.Count - 1]; resultB.Push(directionB, -0.05f); } } if (resultA != null) { result.Insert(0, resultA.ToVector2()); } if (resultB != null) { result.Add(resultB.ToVector2()); } return(result); }
// ToTransform? public Polygon2D ToOffset(Vector2D pos) { return(ToOffset(pos.ToVector2())); }
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; } } }
void SliceJointEvent(Slice2D sliceResult) { RecalculateJoints(); // Remove Slicer Component Duplicated From Sliced Components foreach (GameObject g in sliceResult.GetGameObjects()) { List <Utilities2D.Joint2D> joints = Utilities2D.Joint2D.GetJoints(g); foreach (Utilities2D.Joint2D joint in joints) { if (Polygon2DList.CreateFromGameObject(g)[0].PointInPoly(new Vector2D(joint.anchoredJoint2D.anchor)) == false) { Destroy(joint.anchoredJoint2D); } else { if (joint.anchoredJoint2D != null && joint.anchoredJoint2D.connectedBody != null) { Sliceable2D slicer2D = joint.anchoredJoint2D.connectedBody.gameObject.GetComponent <Sliceable2D>(); if (slicer2D != null) { slicer2D.RecalculateJoints(); } } } } } if (GetRigibody() == null) { return; } // Reconnect Joints To Sliced Bodies foreach (Utilities2D.Joint2D joint in joints) { if (joint.anchoredJoint2D == null) { continue; } foreach (GameObject g in sliceResult.GetGameObjects()) { Sliceable2D slicer = g.GetComponent <Sliceable2D>(); //Polygon2D poly = slicer.shape.GetLocal(); Polygon2D polyWorld = slicer.shape.GetWorld(); Vector2D connectedAnchor2D = new Vector2D(joint.anchoredJoint2D.connectedAnchor); Vector2 connectedAnchorWorld = joint.anchoredJoint2D.connectedBody.transform.TransformPoint(connectedAnchor2D.ToVector2()); Vector2D connectedAnchorWorld2D = new Vector2D(connectedAnchorWorld); switch (joint.jointType) { case Utilities2D.Joint2D.Type.HingeJoint2D: if (polyWorld.PointInPoly(connectedAnchorWorld2D)) { joint.anchoredJoint2D.connectedBody = g.GetComponent <Rigidbody2D> (); } break; default: if (polyWorld.PointInPoly(connectedAnchorWorld2D)) { joint.anchoredJoint2D.connectedBody = g.GetComponent <Rigidbody2D> (); } else { } break; } } } }
static public void DrawWithoutAtlas(Vector2D offset, float z) { for (int i = 0; i < spriteRendererList.Count; i++) { id = spriteRendererList[i]; if (id.GetSprite() == null) { continue; } if (id.InCamera() == false) { continue; } LightingDebug.SpriteRenderersDrawn++; position = id.transform.position; scale = id.transform.lossyScale; scale.x *= id.offsetScale.x; scale.y *= id.offsetScale.y; rot = id.offsetRotation; if (id.applyTransformRotation) { rot += id.transform.rotation.eulerAngles.z; } switch (id.type) { case LightingSpriteRenderer2D.Type.Particle: color = id.color; color.a = id.alpha; material = manager.materials.GetAdditive(); material.SetColor("_TintColor", color); material.mainTexture = id.GetSprite().texture; Max2D.DrawSprite(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; break; case LightingSpriteRenderer2D.Type.WhiteMask: material = manager.materials.GetWhiteSprite(); material.mainTexture = id.GetSprite().texture; Max2D.DrawSprite(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; break; case LightingSpriteRenderer2D.Type.BlackMask: material = manager.materials.GetBlackSprite(); material.mainTexture = id.sprite.texture; Max2D.DrawSprite(material, id.spriteRenderer, offset.ToVector2() + position + id.offsetPosition, scale, rot, z); material.mainTexture = null; 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); } } 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(); }
// Lighting 2D public static List <Vector2D> GetConvexHull(List <Vector2D> points) { //If we have just 3 points, then they are the convex hull, so return those if (points.Count == 3) { //These might not be ccw, and they may also be colinear return(points); } //If fewer points, then we cant create a convex hull if (points.Count < 3) { return(null); } //The list with points on the convex hull List <Vector2D> convexHull = new List <Vector2D>(); //Step 1. Find the vertex with the smallest x coordinate //If several have the same x coordinate, find the one with the smallest z Vector2D startVertex = points[0]; Vector2 startPos = startVertex.ToVector2(); for (int i = 1; i < points.Count; i++) { Vector2 testPos = points[i].ToVector2(); //Because of precision issues, we use Mathf.Approximately to test if the x positions are the same if (testPos.x < startPos.x || (Mathf.Approximately(testPos.x, startPos.x) && testPos.y < startPos.y)) { startVertex = points[i]; startPos = startVertex.ToVector2(); } } //This vertex is always on the convex hull convexHull.Add(startVertex); points.Remove(startVertex); //Step 2. Loop to generate the convex hull Vector2D currentPoint = convexHull[0]; //Store colinear points here - better to create this list once than each loop List <Vector2D> colinearPoints = new List <Vector2D>(); int counter = 0; while (true) { //After 2 iterations we have to add the start position again so we can terminate the algorithm //Cant use convexhull.count because of colinear points, so we need a counter if (counter == 2) { points.Add(convexHull[0]); } //Pick next point randomly Vector2D nextPoint = points[Random.Range(0, points.Count)]; //To 2d space so we can see if a point is to the left is the vector ab Vector2 a = currentPoint.ToVector2(); Vector2 b = nextPoint.ToVector2(); //Test if there's a point to the right of ab, if so then it's the new b for (int i = 0; i < points.Count; i++) { //Dont test the point we picked randomly if (points[i].Equals(nextPoint)) { continue; } Vector2 c = points[i].ToVector2(); //Where is c in relation to a-b // < 0 -> to the right // = 0 -> on the line // > 0 -> to the left float relation = IsAPointLeftOfVectorOrOnTheLine(a, b, c); //Colinear points //Cant use exactly 0 because of floating point precision issues //This accuracy is smallest possible, if smaller points will be missed if we are testing with a plane float accuracy = 0.00001f; if (relation < accuracy && relation > -accuracy) { colinearPoints.Add(points[i]); } //To the right = better point, so pick it as next point on the convex hull else if (relation < 0f) { nextPoint = points[i]; b = nextPoint.ToVector2(); //Clear colinear points colinearPoints.Clear(); } //To the left = worse point so do nothing } //If we have colinear points if (colinearPoints.Count > 0) { colinearPoints.Add(nextPoint); //Sort this list, so we can add the colinear points in correct order colinearPoints = colinearPoints.OrderBy(n => Vector3.SqrMagnitude(n.ToVector2() - currentPoint.ToVector2())).ToList(); convexHull.AddRange(colinearPoints); currentPoint = colinearPoints[colinearPoints.Count - 1]; //Remove the points that are now on the convex hull for (int i = 0; i < colinearPoints.Count; i++) { points.Remove(colinearPoints[i]); } colinearPoints.Clear(); } else { convexHull.Add(nextPoint); points.Remove(nextPoint); currentPoint = nextPoint; } //Have we found the first point on the hull? If so we have completed the hull if (currentPoint.Equals(convexHull[0])) { //Then remove it because it is the same as the first point, and we want a convex hull with no duplicates convexHull.RemoveAt(convexHull.Count - 1); break; } counter += 1; } return(convexHull); }
static public void SortTilemap(LightingBuffer2D buffer, LightingTilemapCollider2D id) { //if (id.maskType != LightingTilemapCollider2D.MaskType.Sprite) { // return; //} if (id.map == null) { return; } Vector3 rot = Math2D.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 * id.cellSize.x; float scaleY = id.transform.lossyScale.y * rotationYScale * id.cellSize.y; int sizeInt = LightingBufferTilemapRectangle.LightTilemapSize(id, buffer); LightingBufferTilemapRectangle.LightTilemapOffset(id, new Vector2(scaleX, scaleY), buffer); Vector2Int newPositionInt = LightingBufferTilemapRectangle.newPositionInt; Vector2D tilemapOffset = LightingBufferTilemapRectangle.tilemapOffset; Vector2D polyOffset = LightingBufferTilemapRectangle.polyOffset; Vector2D inverseOffset = LightingBufferTilemapRectangle.inverseOffset; tilemapOffset.x = id.area.position.x + id.cellAnchor.x + id.transform.position.x; tilemapOffset.y = id.area.position.y + id.cellAnchor.y + id.transform.position.y; offset.x = -buffer.lightSource.transform.position.x; offset.y = -buffer.lightSource.transform.position.y; VirtualSpriteRenderer spriteRenderer = new VirtualSpriteRenderer(); Vector2 tileSize = new Vector2(scaleX / id.cellSize.x, scaleY / id.cellSize.y); LightingTile tile; 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; } if (tile.GetOriginalSprite() == null) { return; } polyOffset.x = x + tilemapOffset.x; polyOffset.y = y + tilemapOffset.y; polyOffset.x *= scaleX; polyOffset.y *= scaleY; if (LightingManager2D.culling && Vector2.Distance(polyOffset.ToVector2(), buffer.lightSource.transform.position) > 2 + buffer.lightSource.lightSize) { LightingDebug.culled++; continue; } polyOffset.x += offset.x; polyOffset.y += offset.y; //inverseOffset.x = -polyOffset.x; //inverseOffset.y = -polyOffset.y; //if (poly.PointInPoly (inverseOffset)) { // continue; //} if (layerSettings.renderingOrder == LightRenderingOrder.YAxis) { list.Add(id, tile, -(float)polyOffset.y - (float)offset.y, polyOffset); } else { list.Add(id, tile, -Vector2.Distance(polyOffset.ToVector2() - offset.ToVector2(), buffer.lightSource.transform.position), polyOffset); } } } }