Example #1
0
 static public Rect GetRect(this RectI2 item)
 {
     return(new Rect(
                item.GetLowerLeftPoint().GetVector2(),
                item.GetSize().GetVector2()
                ));
 }
Example #2
0
        static public bool IsSolid(this Texture2D item, RectI2 rect, float threshold = 1e-3f)
        {
            if (item.GetSolidness(rect) >= threshold)
            {
                return(true);
            }

            return(false);
        }
Example #3
0
        static public void Shrinkwrap(this Sprite item)
        {
            Texture2D texture  = item.texture.Sideload();
            RectI2    original = item.rect.GetRectI2();
            RectI2    shrunk   = texture.ShrinkwrapRect(original);

            VectorF2 original_pixel_pivot    = original.ConvertPercentPointToRangePoint(item.GetNormalizedPivot().GetVectorF2());
            VectorF2 shrunk_normalized_pivot = shrunk.ConvertRangePointToPercentPoint(original_pixel_pivot);

            Rect    new_rect  = shrunk.GetRect();
            Vector2 new_pivot = shrunk_normalized_pivot.GetVector2();

            item.ModifySprite(delegate(SerializedProperty property) {
                property.ForceProperty("m_Rect").rectValue = new_rect;

                property.ForceProperty("m_Pivot").vector2Value = new_pivot;
                property.ForceProperty("m_Alignment").intValue = (int)new_pivot.GetSpriteAlignment();
            });
        }
Example #4
0
        static public RectI2 ShrinkwrapSide(this Texture2D item, RectI2 rect, RectSide side)
        {
            RectI2 center;
            RectI2 edge;

            do
            {
                rect.SplitBySideOffset(1, side, out center, out edge);

                if (item.IsSolid(edge))
                {
                    return(rect);
                }

                rect = center;
            }while (center.IsCollapsed() == false);

            return(center);
        }
Example #5
0
 static public float GetSolidness(this Texture2D item, RectI2 rect)
 {
     return(rect.GetPoints()
            .Convert(p => item.GetSolidness(p))
            .Average());
 }
Example #6
0
 static public RectI2 ShrinkwrapRect(this Texture2D item, RectI2 rect)
 {
     return(typeof(RectSide).GetEnumValues <RectSide>()
            .Apply(rect, (r, s) => item.ShrinkwrapSide(r, s)));
 }