PDA

View Full Version : 3D position to Othographic 2D position.


PEN
01-26-2009, 03:48 PM
So using the provided code in the help I'm able to convert the 3D space of an object to the 2D render coordinates but only if it is using a camera or perspective viewport. For Ortho views what do I need to change?

Here is my code so far.

width=RenderWidth
height=RenderHeight

thePos = (selection[1].pos)* viewport.getTM()
screen_origin = mapScreenToView [0,0] (thePos.z) [width,height]
end_screen = mapScreenToView [width,height] (thePos.z) [width,height]

worldSize = screen_origin-end_screen
xAspect = width/(abs worldSize.x)
yAspect = height/(abs worldSize.y)

renderCoords = point2 (xAspect*(thePos.x- screen_origin.x)) (-(yAspect*(thePos.y-screen_origin.y)))
format "Render Coordinates: %\n" renderCoords

DaveWortley
01-26-2009, 04:57 PM
Interesting solution Paul, I was looking at the similar problem a few weeks ago.

Will be interested to see your solution. I ended up using the world co-ords of the objects, and just rendering from 0,0 in bottom left corner, using a Plane object to Crop render down to size if that makes sense.

PEN
01-26-2009, 05:10 PM
I don't follow, but I can't use some general hack, it will need to work correctly.

Mr-JosE
01-26-2009, 08:40 PM
I managed to get the opposite working by creating a viewport of the rendersize required using gw.setPos, and then finding a point on the construction plane using mapScreenToCP. To get the viewport back to its original state i stored the size and position before making any changes to it. All i wanted to do was work-out the bounds of the viewport in 3D space for a given rendersize for some work pipeline i am developing. You could do a similar thing for finding the 3D space of an object. This is the only reliable way i have found so far.

PEN
01-27-2009, 10:03 AM
I'm not sure I follow, going the other way would require sampling of some sort to find all the objects in the scene. There has to be a way since you can render an ortho view.

ZeBoxx2
01-27-2009, 01:43 PM
Yeah, but plugins cheat because they get access to some of the 3ds Max API functions that give the result like *snaps fingers* that :)

I haven't touched ortho view bits in some time.. just from playing around, though...

viewport.getscreenscalefactor [0,0,0]
Should give you the height of the viewport (including any areas outside of the safe frame(!)) in scene units. Can easily check this by making a plane, aligning that with a camera, link it up, and play with the 'height' of that plane, matching it to the value you get from that function when applied to an ortho view - should always span the full height of the viewport.

From there I think you should be able to get the width in scene units as well, take actual output resolution (and thus 'camera aspect') into account as relative to the viewport's on-screen aspect, etc.

After that, transform the world coordinate to the viewport's coordinate system using yea olde <pos> * <transform>, ignore the Z result, map the X and Y results to the values calculated earlier; if the transformed position is [0,0,Z], then it's right in the middle, [-X,0,Z] it's on the left half of the screen, [X,+Y,Z] it's on the top half and so forth. If abs(-X) is exactly half of the calculated scene unit width of the view, then it's exactly on the left edge of that view.

In theory.. my brain's a jumble and my maxscript listener looks like it might as well be written in sanskrit.

drdubosc
01-27-2009, 01:57 PM
...After that, transform the world coordinate to the viewport's coordinate system using yea olde <pos> * <transform>,...

yesbut yesbut ... which transform? It's probably a doddle .. how do you get at the view transform?

PEN
01-27-2009, 06:47 PM
I'll give it a whirl Richard, sanskrit eh, is that anything like Perl? ;)

ZeBoxx2
01-27-2009, 08:18 PM
Doc: viewport.getTM() should do the trick just fine

PEN: heavens, no! at least there's translation services for sanskrit ;)

drdubosc
01-27-2009, 09:51 PM
Doc: viewport.getTM() should do the trick just fine
Thanks. I read the docs to mean 'not Ortho' .... but only the setTM is restricted.

CGTalk Moderation
01-27-2009, 09:51 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.