Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Projecting 3D to 2D with toComp()

  • Projecting 3D to 2D with toComp()

    Posted by Darby Edelen on September 1, 2009 at 7:25 am

    I’m facing an issue with a project I’m working on in which I’m trying to draw a beam using the Generate > Beam effect in 3D space. The expressions I’m using for start and end point and thickness are all working well, except when the world coordinates they point to end up behind my camera. Here are the expressions I’m using (the ‘ending’ expressions are the same only they refer to a different layer):

    Starting Point:
    l = effect("Source")("Layer");
    l.toComp(l.anchorPoint);

    Starting Thickness:
    c = thisComp.activeCamera;
    l = effect("Source")("Layer");
    z = c.zoom;
    p1 = c.toWorld([0,0,0]);
    p2 = l.toWorld(l.anchorPoint);
    d = length(p1, p2);
    value * z/d;

    From what I know about projecting 3D coordinates into 2D space this is to be expected, but I wonder if anyone might have some insight into how I can effectively find the intersection point of the ray/beam I’m drawing and an infinite plane some short distance in front of my camera so that I can then use THAT world coordinate as my end point and prevent the world coordinate from passing behind the camera?

    Please help!

    Darby Edelen

    Darby Edelen replied 16 years, 8 months ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    September 1, 2009 at 7:24 pm

    Darby,

    This may help get you there. This expression calculates a positon on a plane 100 pixels (planeZ) in front of the camera that’s on the line formed by Layer 1 and Layer 2. Note that I didn’t put anything in there to defend against divide by zero errors if you let Layer 1 and Layer 2 occupy the same position.

    planeZ = 100;
    cam = thisComp.activeCamera;

    L1 = thisComp.layer(“Layer 1”);
    L2 = thisComp.layer(“Layer 2”);
    A = L1.toWorld(L1.anchorPoint);
    B = L2.toWorld(L2.anchorPoint);

    C = cam.toWorld([0,0,planeZ]);
    n = cam.toWorldVec([0,0,1]);

    t = dot((C-A),n)/dot((B-A),n);
    A + t*(B-A)

    Dan

  • Darby Edelen

    September 1, 2009 at 9:29 pm

    [Dan Ebberts] “This expression calculates a positon on a plane 100 pixels (planeZ) in front of the camera that’s on the line formed by Layer 1 and Layer 2.”

    Thanks for your help Dan!

    This is similar to some code I found online for finding the intersection of a line and a plane. The problem I keep having, though, is transforming the position that I get from this function into composition space. I can’t figure out what coordinate system the result of the last line is in:

    A + t*(B-A)

    The closest I can come to an accurate result is to transform this point from the layer space of the clipped layer into composition space, but it still seems to be a bit off. Any hints? 🙂

    Thanks so much for taking me this far!

    Darby Edelen

  • Darby Edelen

    September 1, 2009 at 9:44 pm

    Just figured it out through trial and error, and it works beautifully!

    v = A + t*(B-A);
    cam.toComp(cam.fromWorld(v));

    Of course my full code also includes a lot of testing to see which layer/point needs to be clipped as well. Here it is for the target ‘end’ point:

    nearClip = 5;
    cam = thisComp.activeCamera;
    l1 = effect("Source")("Layer");
    l2 = effect("Target")("Layer");
    n = cam.toWorldVec([0,0,1]);
    c = cam.toWorld([0,0,nearClip]);
    a = l1.toWorld(l1.anchorPoint);
    b = l2.toWorld(l2.anchorPoint);
    r = l2.toComp(l2.anchorPoint);

    da = dot(a - c, n);
    db = dot(b - c, n);

    if((da < nearClip)&&(db < nearClip)){ [-100,-100]; } else if((da > nearClip)&&(db > nearClip)) r;
    else if(db < nearClip){ t = dot((c-a),n)/dot((b-a),n); v = a + t * (b-a); cam.toComp(cam.fromWorld(v)); } else r;

    Darby Edelen

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