Search code examples
pythonapiopenglmaya

Calculating a half vector from eye/camera vector and surface normal


Is there any formula to calculate a half vector from a given eye/camera vector and surface normal/position of an object.

This is the best example I can show of what I mean:

Diagram

My end goal is to calculate the correct light vector from these two values. (Camera vector and surface normal).


Solution

  • You can't derive the light vector from the surface normal and the cam vector alone - if you think about it, that would imply every unique normal and camera combination would result in a different light position. However you can derive it easily as

    light_vect = light_position - face_center_position
    cam_vect = cam_position - face_center_position
    halfangle_vect = (light_vect.normal() + cam_vect.normal()).normal()
    

    where light_position, face_center_position and cam_position are all OpenMaya.MVectors