Screen coordinates (x,y,z=depth) of a Vertex?

Software: Away3D 4.x

Josh Strike, Newbie
Posted: 15 October 2011 09:10 PM   Total Posts: 11

Hey all.
Still trying to understand… without the Camera3D.screen function, how do you get the x.y.z of a vertex on a mesh, from the camera’s point of view? (in Away 4.0)
Thanks!
Josh

   

Avatar
Alexander Seifert, Moderator
Posted: 17 October 2011 07:36 AM   Total Posts: 129   [ # 1 ]

Hey Josh,

I don’t know about a .screen function, however, View3D offers a method named ‘project’ that takes a Vector3D (world space) and projects it onto the screen and returns a Point with screen pixel coordinates.

To get the world coordinates of an object use the ObjectContainer3D.scenePosition method.

Cheers!
Alex

 Signature 
signature_image

http://www.deltastrike.org

   

Josh Strike, Newbie
Posted: 17 October 2011 05:26 PM   Total Posts: 11   [ # 2 ]

Hi Alex,
If you have a 3d point in global space, what’s the best way to translate that so it’s a 3d point relative to the camera’s point of view? e.g. to get the same x,y that you get from the project() function, but also a z coordinate that tells you the distance from the camera?
Thanks!

   

Avatar
Alexander Seifert, Moderator
Posted: 18 October 2011 04:49 PM   Total Posts: 129   [ # 3 ]

Hey Yosh,

if you really simply want to get the distance of a Vector3D to the camera, simply subtract your point from Camera3D.scenePosition and read the resulting length.

However, if you want to have z-values (as used in z-buffer) in screen space (and hence distance from the screen projection plane), you’d need something like this:

function getScreenSpaceZ(objectContainer:ObjectContainer3Dcamera:Camera3D):Number {
 
var screenSpacePosition:Vector3D = new Vector3D();
 var 
modelViewProjection:Matrix3D objectContainer.sceneTransform.clone();
 
modelViewProjection.append(camera.viewProjection);
 
modelViewProjection.copyColumnTo(3screenSpacePosition);
 return -
screenSpacePosition.z;

where objectContainer could be any of your objects’ container and its sceneTransform basically is its model matrix (transformation in world space).

If you don’t have a ObjectContainer3D but only a simple Vector3D, you need to manually create that model matrix by translating a new Matrix3D with the vector.

Be advised that this function creates a new Vector3D and a new Matrix3D (via .clone()) object. If you have this function run within your update loop, you’ll waste quite some memory. Better use two field variables instead and reset them to not create both objects with every call.

Cheers!
Alex

 Signature 
signature_image

http://www.deltastrike.org

   

Josh Strike, Newbie
Posted: 18 October 2011 09:29 PM   Total Posts: 11   [ # 4 ]

Whew! Amazing. I’m going to take a little time to try and comprehend what’s going on in that code. Just FWIW; I think a screen function that lets you get a fishtank x,y,z coord of any point from the camera is a good idea that shouldn’t be thrown out. A 2D point from Camera.project isn’t sufficient. Many’s the time we’re moving things around in Flash that we want to interact with the View not just in flat space but in depth, including focal blurs and scaling. It’s especially true for things like textfields that appear to follow 3D objects but that you need outside the View so they can maintain sizing and positioning appropriate to the whole stage. The Camera.screen function has been indispensable in previous versions to achieve a wide variety of effects.

Anyway, thank you for the snippet; doubt I would have come to it myself anytime soon!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X