Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random layer position constrained by another layer’s alpha

  • Random layer position constrained by another layer’s alpha

    Posted by Chris Coleman on January 6, 2008 at 8:55 am

    As per Mike’s suggestion, I am moving this discussion over here…

    I am trying to create a 2.5D landscape with several hundred layers randomly positioned all over the landscape. I see how to have the position be random, but is there a way to limit the positions to only within the “island” of the landscape as defined by its alpha channel? This is something simple to do with trapcode’s particulate, but i need the shadows cast onto the ground and thats where trapcode is failing me.
    thanks

    Name: Mike Clasby
    Date: Jan 6, 2008 at 6:26:45 am
    Subject: Re: Random layer position constrained by another layer’s alpha

    This modified Dan Ebberts’ expression is a good place to start. It goes on Position.

    minX = 0;//Minimum x depth value
    maxX = 500;//Maximum x depth value
    minZ = 0;//Minimum z depth value
    maxZ = 1000;//Maximum z depth value
    op = thisComp.layer(“opLeader”).opacity;

    opX = linear(op,0,100,minX,maxX);
    opZ = linear(op,0,100,minZ,maxZ);

    seedRandom(index, true);
    x = Math.round(random(opX));
    z = Math.round(random(opZ));

    [x, position[1], z]

    Change the min/max X and Z as needed. All the Y’s are the same, I’m assuming you have a Floor if you want to cast shadows, so you Y needs to be at (or near) the Floor.

    Is this close to what you want?

    Name: Chris Coleman
    Date: Jan 6, 2008 at 7:29:42 am
    Subject: Re: Random layer position constrained by another layer’s alpha

    close, but I think the opacity is one value for the entire layer. Is there an attribute than can look at the alpha channel at any given point? It is an illustrator file layer serving as the island map, and i want to make sure the trees randomly place themselves on the land. ( I did try your suggestion but to no avail. Thank you for puzzling thru this with me.

    Name: Mike Clasby
    Date: Jan 6, 2008 at 8:10:49 am
    Subject: Re: Random layer position constrained by another layer’s alpha

    Oh, I see what you’re trying to do.

    Yes, you probably can, err, maybe Dan can, ask over at the Cow Expression Forum:

    https://forums.creativecow.net/viewforum/227

    I think if you combine this position expression from Colin:

    minX = 0;//Minimum x depth value
    maxX = 1000;//Maximum x depth value
    minZ = 0;//Minimum z depth value
    maxZ = 2000;//Maximum z depth value
    seedRandom(index, true);
    x = random(minX,maxX);
    z = random(minZ, maxZ);
    [Math.round( x), position[1], Math.round( z )]

    With an Opacity expression that is 100 when there is a Collision Detection, and 0 when not, from Dan’s page:

    https://www.motionscript.com/design-guide/collision.html

    But I don’t have AE CS3 or the expression finesse to do it.

    Alternatively, you could just use Colin’s Position expression above, with the min/max set to hit the island, and then turn off the eyeballs of all trees landing off-island, and shy them so they don’t clutter up the stack. That way no collision detection necessary… er, actually you would be performing the collision detection, manually.

    Name: Chris Coleman
    Date: Jan 6, 2008 at 8:44:42 am
    Subject: Re: Random layer position constrained by another layer’s alpha

    Hmm. You have provided some good clues. I see where the two for loops in the collision test could look for the alpha. I am curious tho that if I turn off the opacity of the off island trees manually or with this script, will it still tie up processor time? I am looking to add 1000-2000 of these trees add if I am paying for the invisable ones, it would suck render time. Can an expression turn off the visibility switch or should I try to force a new seed for any tree that lands in the water?
    (I never feel like I am accomplishing anything unless I am pushing AE to its limits, hehehe)

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

    January 6, 2008 at 8:43 pm

    This seems to work for 2D. I’m not sure how you have your 2.5 D set up, but you should be able to do something similar.

    L = thisComp.layer(“island”);
    seedRandom(index,true);
    P = L.transform.position;
    offset = [L.width,L.height]/2;
    minPos = P – offset;
    maxPos = P + offset;

    while(true){
    pos = random(minPos, maxPos);
    if (L.sampleImage(L.fromWorld(pos), [.5, .5], true, time)[3] > 0) break;
    }
    pos

    Dan

  • Chris Coleman

    January 7, 2008 at 1:06 am

    Ahha!
    You gave me all the right clues and with some fudging to make it 3d, it works great! My only issue is that I had to mix up the positions as y became z. I am sure it is because the island has been rotated -90 to make it a flat plane. This fix of mixing the y and z works fine except now i have to adjust the light falloff expressions and im feeling sloppy. Anyway, you remain my Expressions hero. I send all my students to your site when they start to think they know everything about AE. For anyone in the future that wants to populate an island with trees, try this in the position attribute:
    (note that 3000×6000 was the size of my island layer, and -110 was the vertical position I needed each tree to be to just touch the ground)

    L = thisComp.layer(“islandlayer”);
    seedRandom(index,true);

    while(true){

    i= random(0,3000);
    j= random(0, 6000);
    pos = [i,j,-110];
    if (L.sampleImage([i,j])[3] > 0) break;
    }
    pos

  • Darby Edelen

    January 7, 2008 at 6:10 am

    Please check my reply in the AE forum and let me know if that works as well, I think it should and there shouldn’t be any problems with layer rotations.

    Darby Edelen
    Designer
    Left Coast Digital
    Santa Cruz, CA

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