Example #1
0
        void CalculateSpriteBased(Sprite sprite, BorderMode borderMode)
        {
            var lastSprite = _lastUsedSprite;

            _lastUsedSprite = sprite;
            var spriteErrors = Diagnostics.CheckSprite(sprite);

            if (spriteErrors != Errors.NoError)
            {
                if (lastSprite != sprite)
                {
                    WarnSpriteErrors(spriteErrors);
                }
                CalculateSolidFill();
                return;
            }
            if (!sprite)
            {
                CalculateSolidFill();
                return;
            }
            FillCommonParameters();
            var spriteRect    = Mathr.Move(Mathr.ToVector(sprite.rect), sprite.textureRect.position - sprite.rect.position - sprite.textureRectOffset);
            var textureRect   = Mathr.ToVector(sprite.textureRect);
            var textureBorder = Mathr.BorderOf(spriteRect, textureRect);
            var textureSize   = Vector2.zero;

            if (sprite.texture != null)
            {
                textureSize = new Vector2(sprite.texture.width, sprite.texture.height);
            }
            var fullMaskRect = LocalMaskRect(Vector4.zero);

            _parameters.maskRectUV = Mathr.Div(textureRect, textureSize);
            if (borderMode == BorderMode.Simple)
            {
                var textureRectInFullRect = Mathr.Div(textureBorder, Mathr.Size(spriteRect));
                _parameters.maskRect = Mathr.ApplyBorder(fullMaskRect, Mathr.Mul(textureRectInFullRect, Mathr.Size(fullMaskRect)));
            }
            else
            {
                _parameters.maskRect = Mathr.ApplyBorder(fullMaskRect, textureBorder * GraphicToCanvasScale(sprite));
                var fullMaskRectUV = Mathr.Div(spriteRect, textureSize);
                var adjustedBorder = AdjustBorders(sprite.border * GraphicToCanvasScale(sprite), fullMaskRect);
                _parameters.maskBorder   = LocalMaskRect(adjustedBorder);
                _parameters.maskBorderUV = Mathr.ApplyBorder(fullMaskRectUV, Mathr.Div(sprite.border, textureSize));
            }
            _parameters.texture    = sprite.texture;
            _parameters.borderMode = borderMode;
            if (borderMode == BorderMode.Tiled)
            {
                _parameters.tileRepeat = MaskRepeat(sprite, _parameters.maskBorder);
            }
        }
Example #2
0
        static Vector4 AdjustBorders(Vector4 border, Vector4 rect)
        {
            // Copied from Unity's Image.
            var size = Mathr.Size(rect);

            for (int axis = 0; axis <= 1; axis++)
            {
                // If the rect is smaller than the combined borders, then there's not room for
                // the borders at their normal size. In order to avoid artefacts with overlapping
                // borders, we scale the borders down to fit.
                float combinedBorders = border[axis] + border[axis + 2];
                if (size[axis] < combinedBorders && combinedBorders != 0)
                {
                    float borderScaleRatio = size[axis] / combinedBorders;
                    border[axis]     *= borderScaleRatio;
                    border[axis + 2] *= borderScaleRatio;
                }
            }
            return(border);
        }
Example #3
0
        Vector2 MaskRepeat(Sprite sprite, Vector4 centralPart)
        {
            var textureCenter = Mathr.ApplyBorder(Mathr.ToVector(sprite.textureRect), sprite.border);

            return(Mathr.Div(Mathr.Size(centralPart) * GraphicToCanvasScale(sprite), Mathr.Size(textureCenter)));
        }
Example #4
0
        void CalculateSpriteBased(Sprite sprite, BorderMode borderMode)
        {
            var lastSprite = _lastUsedSprite;

            _lastUsedSprite = sprite;
            var spriteErrors = Diagnostics.CheckSprite(sprite);

            if (spriteErrors != Errors.NoError)
            {
                if (lastSprite != sprite)
                {
                    WarnSpriteErrors(spriteErrors);
                }
                CalculateSolidFill();
                return;
            }
            if (!sprite)
            {
                CalculateSolidFill();
                return;
            }
            FillCommonParameters();
            var inner        = DataUtility.GetInnerUV(sprite);
            var outer        = DataUtility.GetOuterUV(sprite);
            var padding      = DataUtility.GetPadding(sprite);
            var fullMaskRect = LocalMaskRect(Vector4.zero);

            _parameters.maskRectUV = outer;
            if (borderMode == BorderMode.Simple)
            {
                var normalizedPadding = Mathr.Div(padding, sprite.rect.size);
                _parameters.maskRect = Mathr.ApplyBorder(fullMaskRect, Mathr.Mul(normalizedPadding, Mathr.Size(fullMaskRect)));
            }
            else
            {
                _parameters.maskRect = Mathr.ApplyBorder(fullMaskRect, padding * SpriteToCanvasScale(sprite));
                var adjustedBorder = AdjustBorders(sprite.border * SpriteToCanvasScale(sprite), fullMaskRect);
                _parameters.maskBorder   = LocalMaskRect(adjustedBorder);
                _parameters.maskBorderUV = inner;
            }
            _parameters.texture    = sprite.texture;
            _parameters.borderMode = borderMode;
            if (borderMode == BorderMode.Tiled)
            {
                _parameters.tileRepeat = MaskRepeat(sprite, _parameters.maskBorder);
            }
        }