Tuesday, 29 April 2014

Scale a texture from the centre.


Nice and easy today.
We're starting off with this texture, which is clamped. 


When you scale it down (= tile it at a value greater than 1) it moves to the upper left corner. That's because 0,0 is in this corner.


With the following setup, you'll scale the texture from the visual centre of the material:


What is happening exactly?

First we subtract 0.5,0.5 to our texture coordinates. We're shifting the texture and bringing 0,0 to the centre.

Then we scale it.

Finally we add 0.5, 0.5 to bring the texture back to the original position. Simple :)

This was used in Enslaved in the dragonfly scan post process. The values subtracted/added back were exposed and so was the scaling factor, so that the glowy reticle could be animated in matinee.

A screenshot won't tell you much about it but you can see in it in my enslaved showreel from 2:00. It's super short but I couldn't find any specific youtube video. Well, gotta play Enslaved to see it at its best ;)


Now, that was a bit short so let's talk some more.
When reducing the size of a clamped texture a lot, you can come across mipmap artifacts.

One fix is the Preserve Border option.
The tooltip says: ‘If true, the color border pixels are preserved by mipmap generation. One flag per color channel.’

Now the result is much better, but we can still see some artifacts.

Here is the texture I created. The last two pixels are plain black but pixel 3 and 4 start to be white. The dxt compression is done with blocks of 4 pixels so since I've got both black and white in a 4 pixel group, my mipmaps get messed up.

I reimported the texture and made sure I've got a clean 4 pixels border. Even with preserve border off, I don't get any artifacts.

They will appear at a lower mipmap though, but won't ever appear with a correct border and the preserve border option.

No comments:

Post a Comment