c# – Getting a pixel of Sprite on the cursor place in Unity

c# – Getting a pixel of Sprite on the cursor place in Unity

[ad_1]

Beneath I’ve posted one thing I mocked up going for deformable pixel terrain. You might be able to get one thing from it… It’s possible you’ll not… Both method, It could actually receive the pixels of a picture across the mouse place. The names are fairly self explanatory however if you happen to do not perceive one thing, submit a remark right here.

utilizing UnityEngine;
utilizing System.Collections;
utilizing System.Collections.Generic;

[RequireComponent (typeof (SpriteRenderer))]
public class BitmapMask : MonoBehaviour 
{
    public Texture2D spriteTexture;
    public Texture2D maskedTexture;
    public int diameter = 50;

    public Listing <bool> masks;

    non-public float _width;
    non-public float _height;
    non-public Sprite _image;
    non-public SpriteRenderer _spriteRenderer;

    non-public Coloration[] tempColors;
    non-public Coloration[] maskColors;

    void Begin () 
    {
        _spriteRenderer = GetComponent <SpriteRenderer> ();
        maskColors = maskedTexture.GetPixels ();

        _image = GetMaskedSprite (spriteTexture, maskedTexture);

        _spriteRenderer.sprite = _image;

        _width = _image.bounds.measurement.x * _image.pixelsPerUnit;
        _height = _image.bounds.measurement.y * _image.pixelsPerUnit;
    }

    void Replace () 
    {
        Vector2 mousePos = Digital camera.foremost.ScreenToWorldPoint (Enter.mousePosition);

        mousePos += (Vector2)_spriteRenderer.sprite.bounds.extents;
        mousePos *= _spriteRenderer.sprite.pixelsPerUnit;

        if (Enter.GetMouseButton (0))
        {
            ChangePixels ((int)mousePos.x, (int)mousePos.y, diameter, Coloration.black);
        }

        if (Enter.GetMouseButtonDown (1))
        {
            ChangePixels ((int)mousePos.x, (int)mousePos.y, 50, Coloration.white);

        }
    }


    Sprite GetMaskedSprite (Texture2D spriteTexture, Texture2D maskTexture)
    {
        Texture2D texture = new Texture2D (spriteTexture.width, spriteTexture.peak);
        Coloration[] spriteColors = spriteTexture.GetPixels ();

        tempColors = new Coloration[spriteTexture.width * spriteTexture.height];

        for (int y = 0; y < maskTexture.peak; y++)
        {
            for (int x = 0; x < maskTexture.width; x++)
            {
                bool masked = (maskColors[x + y * maskTexture.width]) == Coloration.white; // change to white and alter precise picture

                if (masked)
                {
                    tempColors[x + y * maskTexture.width] = new Coloration (0, 0, 0, 0);
                }
                else
                {
                    tempColors[x + y * maskTexture.width] = spriteColors[x + y * maskTexture.width];
                }
            }
        }
        texture.SetPixels (tempColors);
        texture.Apply ();

        return Sprite.Create (texture, new Rect (0, 0, texture.width, texture.peak), new Vector2 (0.5f, 0.5f));
    }

    void ChangePixels (int centreX, int centreY, int diameter, Coloration shade)
    {
        int radius = diameter / 2;
        int a;
        int b;

        for (int y = centreY - (diameter/2); y < centreY + (diameter/2); y++)
        {
            for (int x = centreX - (diameter/2); x < centreX + (diameter/2); x++)
            
        }

        ChangeSprite (GetMaskedSprite (spriteTexture, maskedTexture));
    }

    void ChangeSprite (Sprite updatedSprite)
    {
        _spriteRenderer.sprite = updatedSprite;
    }

}

Mainly it takes in a spriteTexture and a maskedTexture. The masked texture will get altered to both black or white across the mouse place when a click on is detected. The masked texture is then used to find out whether or not pixels from the sprite Texture must be proven, clearly based mostly on the color.

It could not precisely be what you are searching for however its a begin in the best course. Oh yeh and its not optimized and I don’t know concerning the bugs…

Goodluck.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply