示例#1
0
    public static byte[] ExtractAlphaData(Texture2D texture)
    {
        if (texture != null)
        {
#if UNITY_EDITOR
            D2D_Helper.MakeTextureReadable(texture);
#endif
            var width  = texture.width;
            var height = texture.height;
            var total  = width * height;
            var data   = new byte[total];

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    data[x + y * width] = (byte)(texture.GetPixel(x, y).a * 255.0f);
                }
            }

            return(data);
        }

        return(null);
    }
    public D2D_Pixels(Texture2D texture)
    {
        if (texture == null)
        {
            throw new System.ArgumentNullException();
        }

#if UNITY_EDITOR
        D2D_Helper.MakeTextureReadable(texture);
#endif

        width  = texture.width;
        height = texture.height;
        pixels = texture.GetPixels32();
    }
	protected virtual void Update()
	{
		// Copy alpha from main tex
		if (AlphaTex == null && MainTex != null)
		{
			ReplaceAlphaWith(MainTex);
		}
		
#if UNITY_EDITOR
		D2D_Helper.MakeTextureReadable(DensityTex);
#endif
		
		UpdateSprite();
		UpdateColliders();
	}
示例#4
0
    protected virtual void Update()
    {
        if (spriteRenderer == null)
        {
            spriteRenderer = GetComponent <SpriteRenderer>();
        }

        // Copy alpha from main tex
        if (AlphaTex == null && spriteRenderer.sprite != null)
        {
            ReplaceAlphaWith(spriteRenderer.sprite);

            RecalculateOriginalSolidPixelCount();
        }

#if UNITY_EDITOR
        D2D_Helper.MakeTextureReadable(DensityTex);
#endif
    }
示例#5
0
    public static bool ExtractAlphaData(Sprite sprite, ref byte[] data, ref int width, ref int height)
    {
        if (sprite != null && sprite.texture != null)
        {
#if UNITY_EDITOR
            D2D_Helper.MakeTextureReadable(sprite.texture);
#endif
            var rect         = sprite.textureRect;
            var sourceWidth  = sprite.texture.width;
            var sourcePixels = sprite.texture.GetPixels32();
            var sourceOffset = sourceWidth * Mathf.CeilToInt(rect.y) + Mathf.CeilToInt(rect.x);
            var targetOffset = 0;

            width  = Mathf.FloorToInt(rect.width);
            height = Mathf.FloorToInt(rect.height);

            var total = width * height;

            if (data == null || data.Length != total)
            {
                data = new byte[width * height];
            }

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    data[targetOffset + x] = sourcePixels[sourceOffset + x].a;
                }

                sourceOffset += sourceWidth;
                targetOffset += width;
            }

            return(true);
        }

        return(false);
    }
示例#6
0
 protected virtual void OnValidate()
 {
     D2D_Helper.MakeTextureReadable(DensityTex);
 }
示例#7
0
    public void Stamp(Matrix4x4 stampMatrix, Texture2D stampTex, float hardness)
    {
        if (alphaData != null && stampTex != null)
        {
            var stampToPixelMatrix = WorldToPixelMatrix * stampMatrix;
            var pixelToStampMatrix = stampToPixelMatrix.inverse;
#if UNITY_EDITOR
            D2D_Helper.MakeTextureReadable(stampTex);
            D2D_Helper.MakeTextureReadable(DensityTex);
#endif
            // Project corners of stamp
            // TODO: account for non-orthogonal matrices?
            var bl = stampToPixelMatrix.MultiplyPoint(new Vector3(0.0f, 0.0f, 0.0f));
            var br = stampToPixelMatrix.MultiplyPoint(new Vector3(1.0f, 0.0f, 0.0f));
            var tl = stampToPixelMatrix.MultiplyPoint(new Vector3(0.0f, 1.0f, 0.0f));
            var tr = stampToPixelMatrix.MultiplyPoint(new Vector3(1.0f, 1.0f, 0.0f));

            // Find AABB of stamp
            var xMin = Mathf.FloorToInt(Mathf.Min(Mathf.Min(bl.x, br.x), Mathf.Min(tl.x, tr.x)));
            var xMax = Mathf.FloorToInt(Mathf.Max(Mathf.Max(bl.x, br.x), Mathf.Max(tl.x, tr.x)));
            var yMin = Mathf.FloorToInt(Mathf.Min(Mathf.Min(bl.y, br.y), Mathf.Min(tl.y, tr.y)));
            var yMax = Mathf.FloorToInt(Mathf.Max(Mathf.Max(bl.y, br.y), Mathf.Max(tl.y, tr.y)));

            xMin = Mathf.Clamp(xMin, 0, alphaWidth - 1);
            xMax = Mathf.Clamp(xMax, 0, alphaWidth - 1);
            yMin = Mathf.Clamp(yMin, 0, alphaHeight - 1);
            yMax = Mathf.Clamp(yMax, 0, alphaHeight - 1);

            // Is the size greater than 1?
            if (xMax > xMin && yMax > yMin)
            {
                // Make sure the texture is up to date
                DeserializeAlphaTex();

                // Only dirty the solid pixel count
                cachedSolidPixelCount = -1;

                // Write alpha tex and alpha data?
                if (alphaTex != null)
                {
                    for (var y = yMin; y <= yMax; y++)
                    {
                        for (var x = xMin; x <= xMax; x++)
                        {
                            var uv = pixelToStampMatrix.MultiplyPoint(new Vector3(x, y, 0.0f));

                            if (uv.x >= 0.0f && uv.y >= 0.0f && uv.x < 1.0f && uv.y < 1.0f)                             // Is this pixel within the alpha?
                            {
                                var stamp = stampTex.GetPixel(Mathf.FloorToInt(uv.x * stampTex.width), Mathf.FloorToInt(uv.y * stampTex.height));

                                FastPaintBoth(x + y * alphaWidth, x, y, stamp.a * hardness);
                            }
                        }
                    }

                    alphaTex.Apply();
                }
                // Write just alpha data?
                else
                {
                    // The texture needs to be rebuilt, so mark all as dirty
                    MarkAsDirty();

                    for (var y = yMin; y <= yMax; y++)
                    {
                        for (var x = xMin; x <= xMax; x++)
                        {
                            var uv = pixelToStampMatrix.MultiplyPoint(new Vector3(x, y, 0.0f));

                            if (uv.x >= 0.0f && uv.y >= 0.0f && uv.x < 1.0f && uv.y < 1.0f)                             // Is this pixel within the alpha?
                            {
                                var stamp = stampTex.GetPixel(Mathf.FloorToInt(uv.x * stampTex.width), Mathf.FloorToInt(uv.y * stampTex.height));

                                FastPaint(x + y * alphaWidth, x, y, stamp.a * hardness);
                            }
                        }
                    }
                }

                NotifyChanges(xMin, xMax, yMin, yMax);
            }
        }
    }
示例#8
0
    public void Stamp(Matrix4x4 stampMatrix, Texture2D stampTex, float hardness)
    {
        DeserializeAlphaTex();

        if (alphaTex != null && stampTex != null)
        {
            var stampToPixelMatrix = WorldToPixelMatrix * stampMatrix;
            var pixelToStampMatrix = stampToPixelMatrix.inverse;
#if UNITY_EDITOR
            D2D_Helper.MakeTextureReadable(stampTex);
            D2D_Helper.MakeTextureReadable(DensityTex);
#endif
            // Project corners of stamp
            // TODO: account for non-orthogonal matrices?
            var bl = stampToPixelMatrix.MultiplyPoint(new Vector3(0.0f, 0.0f, 0.0f));
            var br = stampToPixelMatrix.MultiplyPoint(new Vector3(1.0f, 0.0f, 0.0f));
            var tl = stampToPixelMatrix.MultiplyPoint(new Vector3(0.0f, 1.0f, 0.0f));
            var tr = stampToPixelMatrix.MultiplyPoint(new Vector3(1.0f, 1.0f, 0.0f));

            // Find AABB of stamp
            var xMin = Mathf.FloorToInt(Mathf.Min(Mathf.Min(bl.x, br.x), Mathf.Min(tl.x, tr.x)));
            var xMax = Mathf.FloorToInt(Mathf.Max(Mathf.Max(bl.x, br.x), Mathf.Max(tl.x, tr.x)));
            var yMin = Mathf.FloorToInt(Mathf.Min(Mathf.Min(bl.y, br.y), Mathf.Min(tl.y, tr.y)));
            var yMax = Mathf.FloorToInt(Mathf.Max(Mathf.Max(bl.y, br.y), Mathf.Max(tl.y, tr.y)));

            xMin = Mathf.Clamp(xMin, 0, alphaTex.width - 1);
            xMax = Mathf.Clamp(xMax, 0, alphaTex.width - 1);
            yMin = Mathf.Clamp(yMin, 0, alphaTex.height - 1);
            yMax = Mathf.Clamp(yMax, 0, alphaTex.height - 1);

            if (xMax > xMin && yMax > yMin)
            {
                Dirty = true;

                for (var y = yMin; y <= yMax; y++)
                {
                    for (var x = xMin; x <= xMax; x++)
                    {
                        var uv = pixelToStampMatrix.MultiplyPoint(new Vector3(x, y, 0.0f));

                        // Is this pixel within the stamp?
                        if (uv.x >= 0.0f && uv.y >= 0.0f && uv.x < 1.0f && uv.y < 1.0f)
                        {
                            var stamp = stampTex.GetPixel(Mathf.FloorToInt(uv.x * stampTex.width), Mathf.FloorToInt(uv.y * stampTex.height));

                            FastPaint(x, y, stamp.a * hardness);
                        }
                    }
                }

                alphaTex.Apply();

                var split = false;

                if (AllowSplit == true)
                {
                    split = D2D_SplitCalculator.Generate(this, SplitOrder);
                }

                if (split == false)
                {
                    UpdateRect(xMin, xMax, yMin, yMax);

                    NotifyChanges();
                }
            }
        }
    }