SetPixel giving artefacts

I wanted to see if I could render the results of some algorithms to my screen, so I went about looking up how to draw textures within the OnGUI function.

One thing I’ve noticed though, is that at the point of texture generation (spefiically when using SetPixel), artefacts are introduced. Randomly pixels being coloured random colours. One thing I’ve found is that these artefacts appear less frequently when the width and height are a power of 2. I have no idea why this might be.

Has anyone had any experience with this? If so, any idea on how I could go about rectifying the problem?

Thanks in advance.

Just incase I haven’t clear enough I’ve posted some screenshots below of what I mean:

What it should be:

Random artefacts that happen:

How bad it gets:

The seemingly random black padding is because of my inaccurate cropping skills. The grids in these pictures are 128x128. All I did was set every other pixel to white (the rest stayed transparent)

Could be bad VRAM, or else make sure you’re initializing the texture properly. (e.g., “var tex = new Texture2D(512, 512); renderer.material.mainTexture = tex;” will likely result in a garbage texture, because all the pixels are undefined.)

–Eric

Thanks for the reply Eric!

I’m drawing the texture to the screen using OnGUI so I don’t have the renderer.material.mainTexture line in there.

I am also defining it exactly like you said. But, what did you mean by bad VRAM? Is that a driver issue? Or a faulty graphics card?

Faulty graphics card. Bad VRAM = VRAM that doesn’t work right.

That part’s not really relevant. If just you’re doing the first part, you have to define the texture somehow, such as by using SetPixels. If you leave an undefined texture and do some SetPixel calls on top, it’s still mostly undefined, unless you do SetPixel for every pixel.

–Eric

Thank you Eric, that made complete sense! I wasn’t defining every pixel, rather only the ones I wanted to appear white. Defining every other colour with Color.clear resolved my problem.

Thanks again!