- Home /
Strange edge on texture after hiding pixels
Hi,
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. 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), Vector2.zero, 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[i].a = 0;
//Set and redraw.
GetComponent<SpriteRenderer>().sprite.texture.SetPixels32(colors, 0);
GetComponent<SpriteRenderer>().sprite.texture.Apply();
}
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.
Answer by digipaul · Mar 29, 2019 at 09:42 PM
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.
Answer by Millerah · Mar 29, 2019 at 09:57 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity UpdateExternalTexture from MovieTexture windows crash 0 Answers
Is it impossible to swap the Texture2D of a Sprite Renderer at runtime? 1 Answer
Drawing onto the Texture2D of a sprite 1 Answer
Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer
How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer