Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Calculate angle between Object and Camera in 3D

  • Calculate angle between Object and Camera in 3D

    Posted by Karim Cherif on January 5, 2009 at 11:21 am
    C = thisComp.layer("Camera 1");
    v1 = normalize(position - C.position)
    v2 = C.toWorldVec([0,0,1]);
    
    radiansToDegrees(Math.acos(dot(v1,v2))) 

    Hi people.
    I have a comp with a camera, and a layer positioned in 3D. What I would like to do is to move the camera around, and calculate the angle of the object relative to the camera.

    After some googling I found this piece of code on CreativeCow. But I’m not sure if this works, because currently the value depends on the xy position of the object in zspace. So not only the angle.

    Basically what I am looking for is to change the object’s opacity depending on its angle and distance to the camera. And also not show at all once the came sees the object’s backside. Could someone help me out in the right direction?

    Thanks!

    Dan Ebberts replied 17 years, 4 months ago 2 Members · 2 Replies
  • 2 Replies
  • Karim Cherif

    January 5, 2009 at 12:16 pm
    //check to see if it is facing the camera if so then:
    if (toCompVec([0, 0, 1])[2] > 0 ) {
    startFade = 500; // Start fade 500 pixels from camera.
    endFade = 1500;  // End fade 1500 pixels from camera.
    try{ // Check whether there's a camera.
    C = thisComp.activeCamera.toWorld([0,0,0]);
    }catch(err){ // No camera, so assume 50mm.
    w = thisComp.width * thisComp.pixelAspect;
    z = (w/2)/Math.tan(degreesToRadians(19.799));
    C = [0,0,-z];
    }
    P = toWorld(anchorPoint);
    d = length(C,P);
    //change the opacity value
    linear(d,startFade,endFade,100,0)
    }
    //if the object is not facing the camera than it is invisible
     else 0;

    I found snippets of code here and there. It is getting to work a bit. The only help I would need is to change/add the degrees it is facing away from the camera. I quite like that it changes the further it is from the camera. But how could I change the code so that it takes the degrees it faces away from the camera into account?

    I put this code in the Opacity of the Layer.

    Karim Cherif
    ————

  • Dan Ebberts

    January 5, 2009 at 4:23 pm

    I think you’re probably looking for something like this for the layer facing away from the camera part:

    C = thisComp.activeCamera;
    v1 = normalize(C.toWorld([0,0,0])-toWorld(anchorPoint))
    v2 = toWorldVec([0,0,1]);

    angle = radiansToDegrees(Math.acos(dot(v1,v2)));
    linear(angle,90,135,0,100)

    I think the simplest way to combine this with the distance expresssion would be to apply one to the layer’s opacity and the other to the opacity parameter of a Transform effect. Or, you could just multiply the results together and divide by 100.

    Dan

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy