public static Vector2 [][] Trace(Sprite sprite, Rect pixelRect) { return(Trace( sprite.texture, pixelRect, RasterHelper.GetPivot(sprite), RasterHelper.GetPixelsPerUnit(sprite) )); }
public static Vector2 [][] Trace(Sprite sprite) { var texture = sprite.texture; var pixelRect = new Rect(0, 0, texture.width, texture.height); return(Trace( sprite.texture, pixelRect, RasterHelper.GetPivot(sprite), RasterHelper.GetPixelsPerUnit(sprite) )); }
public static void CloneTextureAndSprite(SpriteRenderer spriteRenderer) { var sprite = spriteRenderer.sprite; var texClone = Object.Instantiate(sprite.texture) as Texture2D; spriteRenderer.sprite = Sprite.Create( texClone, sprite.rect, RasterHelper.GetPivot(sprite), RasterHelper.GetPixelsPerUnit(sprite) ); spriteRenderer.sprite.name = sprite.name + "(Clone)"; }
private void PerformTrace() { var spriteRenderer = GetComponent <SpriteRenderer> (); var sprite = spriteRenderer.sprite; var ms = new MarchingSquares( sprite.texture, alphaThreshold: AlphaThreshold, clockWise: ClockWise, mipLevel: MipLevel ); polyPath = ms.TraceContours(); float pixelsPerUnit = RasterHelper.GetPixelsPerUnit(sprite); float scale = (1 << MipLevel) / pixelsPerUnit; var pivot = RasterHelper.GetPivot(sprite); var offset = -Vector2.Scale(sprite.bounds.size, pivot); polyPath.Scale(scale); polyPath.Translate(offset); int pointCountBeforeOpt = polyPath.TotalPointCount; float worldScale = Common.WorldScale(spriteRenderer.transform); if (ReduceByMinDistance) { polyPath.ReduceByMinDistance(MinDistance, MinVertexCount); } if (ReduceByMinTriangleArea) { float globalScaleSq = worldScale * worldScale; polyPath.ReduceByMinTriangleArea(MinTriangleArea / globalScaleSq, MinVertexCount); } if (ReduceCodirected) { polyPath.ReduceCodirected(MinAngle * Mathf.Deg2Rad, MinVertexCount); } int pointCountAfterOpt = polyPath.TotalPointCount; print(string.Format( "Reduction: {0}/{1} ({2:P})", pointCountAfterOpt, pointCountBeforeOpt, 1.0f - ( float )pointCountAfterOpt / pointCountBeforeOpt )); }
private PolygonPath BuildPolyPath(SpriteRenderer spriteRenderer) { var sprite = spriteRenderer.sprite; float buildPathStartTime = Time.realtimeSinceStartup; float msStartTime = Time.realtimeSinceStartup; var ms = new MarchingSquares( sprite.texture, alphaThreshold: AlphaThreshold, clockWise: ClockWise, mipLevel: MipLevel ); float msTimeElapsed = Time.realtimeSinceStartup - msStartTime; float traceStartTime = Time.realtimeSinceStartup; var polyPath = ms.TraceContours(); float traceTimeElapsed = Time.realtimeSinceStartup - traceStartTime; float pixelsPerUnit = RasterHelper.GetPixelsPerUnit(sprite); float scale = (1 << MipLevel) / pixelsPerUnit; var pivot = RasterHelper.GetPivot(sprite); var offset = -Vector2.Scale(sprite.bounds.size, pivot); float transformStartTime = Time.realtimeSinceStartup; polyPath.Scale(scale); polyPath.Translate(offset); float transformTimeElapsed = Time.realtimeSinceStartup - transformStartTime; #if MUTABLE_COLLIDER_STATS float buildPathTimeElapsed = Time.realtimeSinceStartup - buildPathStartTime; print(string.Format( "Build path timing -- Trace: {0}, Transform: {1}, Get pixels: {2}, Total: {3}", traceTimeElapsed, transformTimeElapsed, msTimeElapsed, buildPathTimeElapsed )); #endif return(polyPath); }