I’m currently working on a sprite editor for bump map.
So far generating filters to create normals works great, but the precision seems slightly off.
I’m using this:
// RGB to normal: normal.x = rgb.r / 127.5 - 1 ; normal.y = rgb.g / 127.5 - 1 ; normal.z = rgb.b / 127.5 - 1 ; // Normal to RGB: rgb.r = Math.round( 127.5 * ( 1 + normal.x ) ) ; rgb.g = Math.round( 127.5 * ( 1 + normal.y ) ) ; rgb.b = Math.round( 127.5 * ( 1 + normal.z ) ) ;
(Z could be inverted depending on left/right handed)
But there are few things that are bothering me:
- there is no way to have a perfect orthogonal normal since R and G couldn’t be translated to exactly 0 (127 is a bit below 0 and 128 a bit above)
- I heard that some engine have 128 as 0, and other have 127 (like the Source engine), what about Babylonjs?
- Some existing tools I use seems to generate “exaggerated” normals, e.g. the blue channel drop below 127 (sometime around 110 or even 100), it’s supposed to create a normal aiming slightly in the backward direction: is it a bug of those tools, or am I missing something???
It would be nice if a dev could give me the exact formula for converting back and forth RGB and normals.