3
$\begingroup$

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?

materiales faces normals

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?

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Brenticus
    Nov 22 '17 at 17:43
  • $\begingroup$ @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? $\endgroup$
    – yhoyo
    Nov 22 '17 at 19:27
  • $\begingroup$ 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. $\endgroup$
    – Brenticus
    Nov 22 '17 at 20:40
  • $\begingroup$ @Brenticus yes. That is obvious.😜. But how get that with scripting? $\endgroup$
    – yhoyo
    Nov 22 '17 at 20:50
  • $\begingroup$ Sorry didn't see it was a scripting question. I think the answer below will be better than anything I can give. $\endgroup$
    – Brenticus
    Nov 22 '17 at 23:31
4
+100
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.