Weird issue on Texture2D.setPixels, please help

So when I trying to make a “worms” style destructible 2d terrain, I changed default sprite shader a bit to let it recieve a alpha map, and then I can edit the alpha map pixel wise at runtime to make part of the original texture transparent.

The manipulation of the alpha map texture is done by Texture2D.setPixels

By my understanding, Texture2D.getPixels() will return a color array that in forms like:
08 09 10 11
04 05 06 07
00 01 02 03
Am I correct?

The shader is basicly the default Sprite shader except the frag:

fixed4 frag(v2f IN) : COLOR
{
	return tex2D(_MainTex, IN.texcoord) * tex2D(_AlphaMask, IN.texcoord) * IN.color;
}

This is the code I use to initialize the alpha map texture:

    Texture2D t2d;
    Color32[] colorArray;
    int h;
    int w;
    // Use this for initialization
    void Start()
    {
        Texture2D t = GetComponent<SpriteRenderer>().sprite.texture;
        w = t.width;
        h = t.height;
        t2d = new Texture2D(w, h);
        colorArray = new Color32[t.width * t.height];
        for (int i = 0; i < colorArray.Length; i++)
        {
            colorArray[i] = Color.red * 255;
        }
        t2d.SetPixels32(colorArray);
        t2d.Apply();
        renderer.material.SetTexture("_AlphaMask", t2d);
    }

This the code I use to test setPixels:

public void draw()
    {
        //cut top
        for (int i = colorArray.Length * 3 / 4; i < colorArray.Length; i++)
        {
            colorArray[i].a = 0;
        }

        //cut bottom
        for (int i = 0; i < colorArray.Length / 4; i++)
        {
            colorArray[i].a = 0;
        }
        t2d.SetPixels32(colorArray);
        t2d.Apply();
    }

this is the original texture:
1434588--76534--$original.jpg

this is the result after start before draw:
1434588--76535--$withalpha.jpg

this is the result of only run cut top:
1434588--76532--$cuttop.jpg

this is the result of only run cut bottom:
1434588--76536--$cutbut.jpg

this is the result of run both cut top and cut bottom:
1434588--76537--$cutboth.jpg

If you look closely at the result of cut top only and cut bottom only, you will see both of them have a line of pixels remain unchanged, and those lines disappeared in cut both. SInce the line is red, the shader should works correctly, and I have no clue what cause this weird issue, please help.

Another question:is there a way to change the texture of a Sprite? Sprite.texture is readonly

totally guessing here, but check order of operations in your iteration through pixels, like make sure this means what it says:

for (int i = colorArray.Length * 3 / 4; i < colorArray.Length; i++)

and not:

for (int i = (colorArray.Length * 3) / 4; i < colorArray.Length; i++)

or:

for (int i = colorArray.Length * (3 / 4); i < colorArray.Length; i++)

which would have different effects of the value of i.

Since each post only can have 5 pictures, I opened this new post.

after running the code below:

int index;
        for (int i = x; i < w-1; i++)
        {
            for (int j = y; j < h-1; j++)
            {

                if (i >= 0  i < w  j >= 0  j < h)
                {
                    index = i + j * w;
                    colorArray[index].a = 0;
                }
            }
        }

I get this:
1434710--76567--$cutcube.jpg

w and h here are the width and height of the texture, so the code should left the right edge and the top edge unchanged and cut down the rest. But if you look closely to the picture, you will see a rectangular border, which is so weird.

It seems what happened is that the right and top edges wrap half pixels to the left and bottom edge, don’t understand why…

Edit: x and y are 0 here

Hmm, what is the “x” in the iteration? Anyway it does seem odd by just looking at your code and this, but I am probably missing something.

x and y are 0 here, I forget change them

I find out the reason, it is the default filter mode!, after I change the filter mode to Point, bug get fixed. The bilinear mode somehow draws the opposite edge’s pixels…

Ahh yes - figures, that kinda stuff always kinda sneaks up on you with unity haha