How to tell if NORMAL faces down or away?

itgiawaitgiawa Posts: 99Member

Ive gota third-person camera with 8 fixed angles and my terrain is blocky like minecraft and sometimes I cut off the skyward face of my cubes so you can see inside

My goal is to identify if I'm drawing on a backface (inside of a cube) and if so then cover it up with a solid color or maybe a special design.

But I don't know what the NORMALs look like, I assume they are all normalized.

I tried this code to try and figure out what different normals literally look like

    if (NORMAL == vec3(0, -1, 0)) {
            ALBEDO = vec3(.5, .5, .5)
        }
        if (NORMAL == vec3(0, 1, 0)) {
            ALBEDO = vec3(.1, .1, .5)
        }
        if (NORMAL == vec3(1, 0, 0)) {
            ALBEDO = vec3(.1, .9, .9)
        }
        if (NORMAL == vec3(-1, 0, 0)) {
            ALBEDO = vec3(.2, .2, 0)
        }
        if (NORMAL == vec3(0, 0, 1)) {
            ALBEDO = vec3(.7, .3, .3)
        }
        if (NORMAL == vec3(0, 0, -1)) {
            ALBEDO = vec3(.2, .7, .7)
        }

None of the inside faces were
colored, however outer right facing sides were colored light blue

, but no other faces changed color

Any idea how to get the downward or backward facing parts of the cubes?

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 5,251Admin

    My hunch is that the reason it's not working is because there is some floating point precision issues, so it doesn't directly point on a single axis. Instead of checking to see if it's exactly equal to a direction, you probably will need to check if it's close to the direction.

    There are probably better ways to do this, but I think this should work around the floating point precision issues (assuming it's causing the issue):

    if (length(NORMAL - vec3(0, -1, 0) < 0.1)
    {
        ALBEDO = vec3(.5, .5, .5);
    }
    else if (length(NORMAL - vec3(0, 1, 0) < 0.1)
    {
        ALBEDO = vec3(.1, .1, .5)
    }
    else if (length(NORMAL - vec3(1, 0, 0) < 0.1)
    {
        ALBEDO = vec3(.1, .9, .9)
    }
    else if (length(NORMAL - vec3(-1, 0, 0) < 0.1)
    {
        ALBEDO = vec3(.2, .2, 0)
    }
    else if (length(NORMAL - vec3(0, 0, 1) < 0.1)
    {
        ALBEDO = vec3(.7, .3, .3)
    }
    else if (length(NORMAL - vec3(0, 0, -1) < 0.1)
    {
        ALBEDO = vec3(.2, .7, .7)
    }
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file