public void UpdatePolygonCollider(ref PolygonParameters p) { if (spriteRenderer.sprite == null || spriteRenderer.sprite.texture == null) { return; } dirty = false; List <Vector2[]> cached; if (Application.isPlaying && p.UseCache) { CacheKey key = new CacheKey(); key.Texture = p.Texture; key.Rect = p.Rect; if (cache.TryGetValue(key, out cached)) { polygonCollider.pathCount = cached.Count; for (int i = 0; i < cached.Count; i++) { polygonCollider.SetPath(i, cached[i]); } return; } } PopulateCollider(polygonCollider, ref p); }
private void AddEditorCache(ref PolygonParameters p, List <Vector2[]> list) { CacheKey key = new CacheKey(); key.Texture = p.Texture; key.Rect = p.Rect; CacheEntry e = new CacheEntry(); e.Key = key; e.Value = new ListWrapper(); e.Value.List = new List <ArrayWrapper>(); foreach (Vector2[] v in list) { ArrayWrapper w = new ArrayWrapper(); w.Array = v; e.Value.List.Add(w); } for (int i = 0; i < editorCache.Count; i++) { if (editorCache[i].Key.Equals(key)) { editorCache.RemoveAt(i); editorCache.Insert(i, e); return; } } editorCache.Add(e); }
public override bool Equals(object obj) { if (obj is PolygonParameters) { PolygonParameters p = (PolygonParameters)obj; return(Texture == p.Texture && Rect == p.Rect && XMultiplier == p.XMultiplier && YMultiplier == p.YMultiplier && AlphaTolerance == p.AlphaTolerance && DistanceThreshold == p.DistanceThreshold && Decompose == p.Decompose); } return(false); }
/// <summary> /// Populate the vertices of a collider /// </summary> /// <param name="collider">Collider to setup vertices in.</param> /// <param name="p">Polygon creation parameters</param> public void PopulateCollider(PolygonCollider2D collider, ref PolygonParameters p) { try { if (p.Texture.format != TextureFormat.ARGB32 && p.Texture.format != TextureFormat.BGRA32 && p.Texture.format != TextureFormat.RGBA32 && p.Texture.format != TextureFormat.RGB24 && p.Texture.format != TextureFormat.Alpha8 && p.Texture.format != TextureFormat.RGBAFloat && p.Texture.format != TextureFormat.RGBAHalf && p.Texture.format != TextureFormat.RGB565) { Debug.LogWarning("Advanced Polygon Collider works best with a non-compressed texture in ARGB32, BGRA32, RGB24, RGBA4444, RGB565, RGBAFloat or RGBAHalf format"); } int width = (int)p.Rect.width; int height = (int)p.Rect.height; int x = (int)p.Rect.x; int y = (int)p.Rect.y; UnityEngine.Color[] pixels = p.Texture.GetPixels(x, y, width, height, 0); List <Vertices> verts = geometryDetector.DetectVertices(pixels, width, p.AlphaTolerance); int pathIndex = 0; List <Vector2[]> list = new List <Vector2[]>(); for (int i = 0; i < verts.Count; i++) { ProcessVertices(collider, verts[i], list, ref p, ref pathIndex); } #if UNITY_EDITOR if (Application.isPlaying) { #endif if (p.UseCache) { CacheKey key = new CacheKey(); key.Texture = p.Texture; key.Rect = p.Rect; cache[key] = list; } #if UNITY_EDITOR } else if (p.UseCache) { AddEditorCache(ref p, list); } #endif Debug.Log("Updated polygon."); } catch (Exception ex) { Debug.LogError("Error creating collider: " + ex); } }
public void RecalculatePolygon() { if (spriteRenderer.sprite != null) { PolygonParameters p = new PolygonParameters(); p.AlphaTolerance = AlphaTolerance; p.Decompose = Decompose; p.DistanceThreshold = DistanceThreshold; p.Rect = spriteRenderer.sprite.rect; p.Offset = spriteRenderer.sprite.pivot; p.Texture = spriteRenderer.sprite.texture; p.XMultiplier = (spriteRenderer.sprite.rect.width * 0.5f) / spriteRenderer.sprite.pixelsPerUnit; p.YMultiplier = (spriteRenderer.sprite.rect.height * 0.5f) / spriteRenderer.sprite.pixelsPerUnit; p.UseCache = UseCache; UpdatePolygonCollider(ref p); } }
private List <Vector2[]> ProcessVertices(PolygonCollider2D collider, Vertices v, List <Vector2[]> list, ref PolygonParameters p, ref int pathIndex) { Vector2 offset = p.Offset; float flipXMultiplier = (spriteRenderer.flipX ? -1.0f : 1.0f); float flipYMultiplier = (spriteRenderer.flipY ? -1.0f : 1.0f); if (p.DistanceThreshold > 1) { v = SimplifyTools.DouglasPeuckerSimplify(v, p.DistanceThreshold); } if (p.Decompose) { List <List <Vector2> > points = BayazitDecomposer.ConvexPartition(v); for (int j = 0; j < points.Count; j++) { List <Vector2> v2 = points[j]; for (int i = 0; i < v2.Count; i++) { float xValue = (2.0f * (((v2[i].x - offset.x) + 0.5f) / p.Rect.width)); float yValue = (2.0f * (((v2[i].y - offset.y) + 0.5f) / p.Rect.height)); v2[i] = new Vector2(xValue * p.XMultiplier * Scale * flipXMultiplier, yValue * p.YMultiplier * Scale * flipYMultiplier); } Vector2[] arr = v2.ToArray(); collider.pathCount = pathIndex + 1; collider.SetPath(pathIndex++, arr); list.Add(arr); } } else { collider.pathCount = pathIndex + 1; for (int i = 0; i < v.Count; i++) { float xValue = (2.0f * (((v[i].x - offset.x) + 0.5f) / p.Rect.width)); float yValue = (2.0f * (((v[i].y - offset.y) + 0.5f) / p.Rect.height)); v[i] = new Vector2(xValue * p.XMultiplier * Scale * flipXMultiplier, yValue * p.YMultiplier * Scale * flipYMultiplier); } Vector2[] arr = v.ToArray(); collider.SetPath(pathIndex++, arr); list.Add(arr); } return(list); }