Exemplo n.º 1
0
        /// <summary>This allows you to rebuild the destruction state using the specified sprites.</summary>
        public void Rebuild(int optimizeCount)
        {
            if (shape == null)
            {
                shape = CachedSpriteRenderer.sprite;
            }

            if (shape != null)
            {
                var shapeRect = D2dHelper.SpritePixelRect(shape);
                var ppu       = shape.pixelsPerUnit;

                ready          = true;
                alphaData      = D2dHelper.ReadPixels(shape.texture, shapeRect.x, shapeRect.y, shapeRect.width, shapeRect.height);
                alphaWidth     = shapeRect.width;
                alphaHeight    = shapeRect.height;
                alphaOffset.x  = (shape.textureRectOffset.x - shape.pivot.x) / ppu;
                alphaOffset.y  = (shape.textureRectOffset.y - shape.pivot.y) / ppu;
                alphaScale.x   = shape.textureRect.width / ppu;
                alphaScale.y   = shape.textureRect.height / ppu;
                alphaSharpness = 1.0f;

                if (solidRangeMask != null && SolidRange > 0)
                {
                    var maskRect = D2dHelper.SpritePixelRect(solidRangeMask);
                    var maskSize = maskRect.size;
                    var maskData = D2dHelper.ReadPixels(solidRangeMask.texture, maskRect.x, maskRect.y, maskRect.width, maskRect.height);
                    var maskMin  = System.Math.Max(0, 255 - SolidRange);
                    var stepH    = 1.0f / alphaWidth;
                    var stepV    = 1.0f / alphaHeight;

                    for (var y = 0; y < alphaHeight; y++)
                    {
                        for (var x = 0; x < alphaWidth; x++)
                        {
                            var index      = x + y * alphaWidth;
                            var alphaPixel = alphaData[index];

                            if (alphaPixel.a > maskMin)
                            {
                                var maskCoord = new Vector2(x * stepH, y * stepV);
                                var maskPixel = D2dHelper.Sample(maskData, maskSize, maskCoord);

                                alphaPixel.a = (byte)Mathf.Lerp(maskMin, 255, maskPixel.a);

                                alphaData[index] = alphaPixel;
                            }
                        }
                    }
                }

                originalAlphaCount = CalculateAlphaCount();

                if (channels == ChannelType.AlphaWithWhiteRGB)
                {
                    var total = shapeRect.width * shapeRect.height;

                    for (var i = 0; i < total; i++)
                    {
                        var alphaPixel = alphaData[i]; alphaData[i] = new Color32(255, 255, 255, alphaPixel.a);
                    }
                }

                for (var i = 0; i < optimizeCount; i++)
                {
                    Optimize();
                }

                NotifyRebuilt();
            }
            else
            {
                Clear();
            }
        }