Getting a one pixel wrap around on my gradient texture. (UE4)

So I’m using a color gradient as a color lookup table to tint some black and white textures, but I’m getting a single pixel from one side wrapping around to the other, which is giving me some weird results when I hit pure black in my black and white textures.

I have my gradient to clamp on X, and there’s no issue with the gradient in photoshop. Is there a way to correct whatever the engine is doing to my gradient? I’ve tried setting the compression to High, changing the Filter types, the Compression settings, and just about everything else. My gradient is 1024x4, Compress Without Alpha is checked, and my Texture Group is set to Effects.

The issue here is that if you’re using a texture, the uv of “0.0” is the same position on the texture as “1.0”. We tend to say that (0.0, 0.0) is the bottom right (or top right for Unreal), and (1.0, 1.0) is the opposite corner, but that’s not entirely true. The UV positions of (0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0), (153.0, -7.0), etc. … are all the same position on a texture. They’re all 4 corners of the texture simultaneously, in between the 4 corner pixels.

For a “1d” texture, ie: 4x1 pixels, the center of the first pixel is at (0.125, 0.5), and the center of the last pixel is at (0.875, 0.5). The width of each pixel is the reciprocal of the resolution, or 1 / resolution, and the pixel center is half of that again. So the proper way to sample a non-looping gradient texture is to do something like this:

float2 pixelSize = 1.0 / textureDimension;
float2 halfPixel = pixelSize * 0.5;
float2 gradientUV = lerp(halfPixel, 1.0 - halfPixel, gradientInput);
1 Like

So how could I integrate that solution into Unreal?

Either hardcore the min and max values to lerp between in the material (assuming you’ll always be using a 1024x4 texture), set them manually as properties, or set them in using a blue print.

After looking at my other gradients (the one in question being the only one I’ve had an issue with) turns out I was tired or was working too fast, and I had Y clamped instead of X, so it was looping around by a pixel… Big derp

@Travis are you using a gradient texture to remap it onto a black and white texture or are you doing it in engine?

Unreal has gradients and LUTs under miscellaneous if that helps at all

Even with wrapping disabled, you should still be using the inset UVs for sampling. On a 1024 wide texture it might not matter as much, but you’re still getting half a pixel’s worth of extra solid color on either end.

Using my gradients in a material to color grey scale textures that are RGB packed

Does this help? Not sure if you’re using a texture or doing it in the engine.

2 Likes

Holy cow, I didn’t know these were even in Unreal! Thanks for pointing me to that! I’ll have to mess with them when I have a bit more time to experiment and play with it

It’s super straight forward, it’s cheaper and you can update it on the fly. A really cool thing of Unreal that’s hidden.

1 Like