Inverting normals when rendering backfaces

Started by
6 comments, last by Norman Barrows 8 years, 2 months ago

I've turned off culling because I have some geometry that I want to render both front and backfaces of. Is there a way to invert the normal when rendering the backface? Or should I turn culling back on and make the geometry two-sided...

Advertisement
Use the SV_IsFrontFace system value semantic:



float4 PS(float3 normal: NORMAL, bool face: SV_IsFrontFace): SV_Target
{
if(!face)
    normal = -normal;
    //...
}

I should have specified that I am looking to use the normal in the vertex shader. And now that I've said that I realize that there's no way for the vertex shader to know if it is part of a front/back face yet, so I'll have to duplicate the geometry to get both normals, or try to trick it by transforming it and using abs(). Thanks though!

a vertex can be shared between a front and a back face. you'd need to simply calculate both and pass both to the pixel shader and decide on pixel shader side which side to use.

You can determine the vertex "facing" by looking at the sign of the view space normal's z component (this can be determined in the vertex shader). This is just an estimate though since vertex normals aren't face normals. "Facing" can now change within a triangle. (But has it's use: I've seen similar to achieve early back face culling of whole patches in a hull shader - using a bias).

Just throwing around ideas.

Edit: Seeing the whole picture might help. What are you after exactly ? Maybe another setup is warranted (geo shader, tesselation shader).

You can try taking the dot product of the view vector and the vertex normal, and then multiplying the normal by the sign of the result.


vertexNormal *= sign(dot(viewVector, vertexNormal));

You can also do this via a conditional operation, which may be a bit faster than using sign().


vertexNormal *= dot(viewVector, vertexNormal) < 0 ? -1.0f : 1.0f;
Depending on the complexity of the model and performance, the quickest solution sounds like using "doublesided" geometry

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

>> Is there a way to invert the normal when rendering the backface?

https://en.wikipedia.org/wiki/Normal_(geometry)

"For a convex polygon (such as a triangle), a surface normal can be calculated as the vector cross product of two (non-parallel) edges of the polygon."

so you should be able to calculate normals for both faces by reversing the order (winding) of the vertices used in the calculation.

see also:

https://en.wikipedia.org/wiki/Vertex_normal

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement