# How to know which side of a face is being rendered?

I have a plane that I divide using knife in some faces.
Using nodes, I Apply a material to each side of the face.

I want to know which side of each face is being captured by the camera?... I mean, which side is being rendered?

Actually, I tried to capture the order of the vertices of each of the faces, the normal of each face, try to get the Reversed faces because have a negative dot product value and many ideas!., but nothing work.

do you know some solution?

• Are you trying to apply a material to either side of a face or are you trying to figure out which side the material is being applied to? Or are you trying to have all the faces pointing up have one material and all of the faces pointing down have another? A bit of clarification would be great and I may be able to answer your question. Nov 22 '17 at 17:43
• @Brenticus I don't want any of that. As you read I want to know which side of a face is being rendered. I mean, one face has two sides, which of this faces are being rendered? Nov 22 '17 at 19:27
• Which ever side faces the camera will be rendered. If you have a face with two materials, one on the bottom and one on the top, if you render it from the top it will have the top material, and if you render it from the bottom it will have the bottom material. You can think of it like a piece of paper: you can have a different color on each side, and whatever side of the paper you can see will be rendered. Nov 22 '17 at 20:40
• @Brenticus yes. That is obvious.😜. But how get that with scripting? Nov 22 '17 at 20:50
• Sorry didn't see it was a scripting question. I think the answer below will be better than anything I can give. Nov 22 '17 at 23:31

You need 2 vectors for calculating this:

• A = the face normal
• B = a vector pointing from camera to the face center (face.calc_center_median(), = point of face normal origin)

You get both from the python API from mesh or bmesh data and from Camera object location.

Then you calculate a Dot product between these 2:

from mathutils import Vector

C = A.dot(B)

• if the dot product is negative, the vectors point towards each other (face is oriented towards the camera)

• if the dot product is 0, the vectors are orthogonal and the face is not visible

• if the dot product is positive, the vectors point in the same direction and you see the backface from camera

You can see how dot product works here: www.falstad.com/dotproduct/

Alternatively to solve this inside shaders you can use the Geometry node. In Cycles the socket is named Backfacing, in Blender Internal the socket is named Front/Back. You can have a material that chooses from 2 materials based on this mix factor. This approach will dynamically switch between materials even if the camera is animated, this does not happen if you assign materials with script and then during animation the camera angles change.

• really good answer. in order to clarify if someone read this in the future I did b= camera.location - face.calc_center_median() Nov 23 '17 at 3:03
• @yhoyo thanks, added the face center info in, that is good detail Nov 23 '17 at 10:06