ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# 2D to spherical coordinate script

FAQ   •   VIEW ALL
 2D to spherical coordinate script on Oct 4, 2017 at 9:12:21 pm

I have a new head-scratcher. I need to write a script that will convert a selected 2D layer into a 3D layer that is locked to a sphere around the camera, with the radius of that sphere being the distance the layer is from the camera's origin. I have part of this working, but not quite what I want. I'm converting the 2D coordinates to 3D based on the width of the frame, but I think this is what is throwing off the result, so as I move the layer in 3D, its center of orbit seems offset from the camera. Anyone out there with better trig knowledge than I that could offer some advice?

``` r= transform.position[2]*-1; //convert X,Y 2D coordinates with the comp dimensions as a reference convertX = transform.position[0]/thisComp.width*2*Math.PI-Math.PI; convertY = transform.position[1]/thisComp.width*2*Math.PI-Math.PI/2; //spherical coordinates sX = -(Math.cos(convertX-Math.PI/2))*(Math.cos(convertY))*r; sY = -Math.sin(convertY)*r; sZ = (Math.sin(convertX-Math.PI/2))*(Math.cos(convertY))*r; [sX,sY,sZ]```

 Return to posts index

 Re: 2D to spherical coordinate scripton Oct 4, 2017 at 11:45:22 pm

It sounds interesting, but I'm having a hard time picturing how it's supposed to work and what you're trying to do exactly.

Dan

 Return to posts index

 Re: 2D to spherical coordinate scripton Oct 5, 2017 at 5:28:51 pm

Well, part of this code comes from another script I wrote that would take a 2D coordinate on a latlong image, and represent it on a sphere in a separate comp. This was great for motion tracking an object in a 360 video. Track the object in the footage as a latlong, then concert that tracking data to spherical coordinates and attach other objects, masks, etc. to it. The code above is a conversion from cartesian to spherical coordinates, and requires the width of the image and assumes a 2:1 aspect ratio for a lat long image.

I'm trying to adapt this code to now work independently of any dimensions. Essentially I'm trying to get a 3D layer to be able to orbit around the camera in a scene. As I drag the layer up/down/left/right, it maintains its distance away from the camera and moves in an arc around the camera, being locked to a sphere. The distance from the camera to the object is the sphere's radius.

My biggest issue is that I have to set my camera to be at world 0 for this to work as I intend. However, AE cameras when first created, are not placed exactly at world 0, but have an offset of about 1500 in the neg Z.

Hope that helps. If you take my code right now and apply it to the position of a 3D object with a camera in the scene at world 0, it will work correctly and you can see how it orbits around you as you drag the object around.

 Return to posts index

 Re: 2D to spherical coordinate scripton Oct 5, 2017 at 6:04:30 pm

Sorry, I'm not seeing that behavior.

Dan

 Return to posts index

 Re: 2D to spherical coordinate scripton Oct 8, 2017 at 6:55:22 pm

I was able to figure out a solution to my problem. I needed to add the camera's current positions to the spherical coordinates in order to offset the origin. Because its based around the active camera, regardless what camera is used, the objects will always remain locked in a sphere around the current camera.

Try copy/pasting the below script onto a 3D layer's position, with a single-node camera in the scene. Now reposition the 3D layer, and you'll see it moves around in a sphere. Switch to top view and move the 3D layer around for a better idea of how it works.

``` r= transform.position[2]+1000*-1; //convert X,Y 2D coordinates with the comp dimensions as a reference convertX = transform.position[0]/thisComp.width*2*Math.PI-Math.PI; convertY = transform.position[1]/thisComp.width*2*Math.PI-Math.PI/2; //spherical coordinates sX = -(Math.cos(convertX-Math.PI/2))*(Math.cos(convertY))*r+thisComp.activeCamera.position[0]; sY = -Math.sin(convertY)*r+thisComp.activeCamera.position[1]; sZ = (Math.sin(convertX-Math.PI/2))*(Math.cos(convertY))*r+thisComp.activeCamera.position[2]; [sX,sY,sZ]```

 Return to posts index

© 2018 CreativeCOW.net All Rights Reserved
[TOP]