SSBump2Normal

From Valve Developer Community
Jump to: navigation, search

Project

rob5300's "ssbumpToNormal" github

How It Is Done

The Conversion Matrix in Rust

const OO_SQRT_3: f32 = 0.57735025882720947f32;
static BUMP_BASIS_TRANSPOSE: [Vector3<f32>; 3] = [
    Vector3::new( 0.81649661064147949f32, -0.40824833512306213f32, -0.40824833512306213f32 ),
    Vector3::new(  0.0f32, 0.70710676908493042f32, -0.7071068286895752f32 ),
    Vector3::new(  OO_SQRT_3, OO_SQRT_3, OO_SQRT_3 )
];

Converting each pixel of the SSBump Map

for pixel in pixels {
        //Convert pixel colour to a vector
        let rgb = pixel.2;
        let pixel_vector = Vector3::new(rgb[0] as f32 / 255f32, rgb[1] as f32 / 255f32, rgb[2] as f32 / 255f32);
        //Convert normal vector back to traditional tangent normal
        let new_rgb = new_image.get_pixel_mut(pixel.0, pixel.1);
        new_rgb[0] = convert_vector(&pixel_vector, 0);
        new_rgb[1] = convert_vector(&pixel_vector, 1);
        new_rgb[2] = convert_vector(&pixel_vector, 2);
    }

The math function to convert each pixel

fn convert_vector(pixel: &Vector3<f32>, index: usize) -> u8 {
    return (((pixel.dot(BUMP_BASIS_TRANSPOSE[index]) * 0.5f32) + 0.5f32) * 255f32) as u8;
}


Note.pngNote:After the normal map is generated you should invert the green channel if the map looks bad.
//From
new_rgb[1] = convert_vector(&pixel_vector, 1);
//To
new_rgb[1] = 255u8 - convert_vector(&pixel_vector, 1);

Other Links

SSBump Page

NormalMap Page