Strange edge on texture after hiding pixels


I’m working on a project that changes a texture’s alpha to define edges. However, when I set a texture, I’m getting a strange extra line on the bottom. [135550-strangeline.png*|135550] I’ve concluded its some kind of wrapping from the other edge, as when Delete the part of the top, the corresponding part of the extra bottom line disappears.

I made a new scene that just makes a 100x50 blank texture on an empty object with a sprite renderer and a script with this code in the start function:

void Start()
    //setup texture and sprite.
    int width = 100;
    int height = 50;
    int pixelsPerUnit = 10;
    var textureCopy = new Texture2D(width, height, TextureFormat.RGBA32, false, true);
    GetComponent<SpriteRenderer>().sprite = Sprite.Create(textureCopy, new Rect(0, 0, width, height),, pixelsPerUnit);
    //Hide the bottom half of the texture
    var colors = GetComponent<SpriteRenderer>().sprite.texture.GetPixels32();
    for (int i = 0; i < colors.Length / 2; i++)
        colors*.a = 0;*

//Set and redraw.
GetComponent().sprite.texture.SetPixels32(colors, 0);
Here’s another picture of a blank texture with some edges erased. Its as if a pixel wraps around.
I’ve tried changing various settings, but I haven’t managed to get those edges to disapear.

Yes textures can wrap around. This behavior is controlled in the import settings of the texture, via wrap mode. You probably want to set it to clamp.

It happens because when a texture is read it does a sort of average of the the pixel specified in the UV coordinates with adjacent pixels. When the pixel is at an edge, some of the adjacent pixels will be beyond the range of the texture so you have to tell unity what to do in the situation. Clamp means force the pixel to the nearest point in the texture while wrap means go to the opposite side.

Another thing to try is changing the filter mode. If you change it to point, then the adjacent pixels have no effect. If the filter mode is bi linear, it does this averaging behavior above while trilinear also includes the different mip map textures in the average.

It was indeed the wrap mode. When making a new texture it was defaulting to Repeat. After setting it to clamp it works perfectly. I had fiddled with wrap settings on the texture I was importing on another script, but it was leaving the new texture generated by the script unchanged.

Thank you @digipaul , @Millerah !