Notifications
Clear all

[Closed] Caculate camera viewport size?

Hi,
Does anyone know how to caculate camera viewport size? I want to draw a rectangle(or a box) to fit the camera viewport just like below image,the rectangle’s width and height as same size as camera viewport’s ,how to do that?

16 Replies

Search for Martin Breidt’s ImagePlane script. It’s free!

Try this:

(
	fn CreateCameraFrustumPlane cam dist =
	(
		verticalFOV = 2.0 * atan (tan(cam.fov/2.0) / getRendImageAspect())
		width  = 2 * dist * tan(cam.fov/2.0)
		length = 2 * dist * tan(verticalFOV/2.0)
		plane width:width length:length dir:cam.dir pos:(cam.pos-(dist*cam.dir))
	)

	theCamera = $
	CreateCameraFrustumPlane theCamera theCamera.targetdistance
)

to be a little more correct:

fn cameraFrustumTM cam dist: = if isvalidnode cam and iskindof cam Camera do
(
	if dist == unsupplied do dist = cam.baseobject.targetDistance
	x = 2 * dist * tan (cam.fov/2)
	y = x / getRendImageAspect()
	(translate (scalematrix [x,y,1]) [0,0,-dist]) * cam.transform
)
tm = cameraFrustumTM $ -- where selection is a camera
p = plane width:1 length:1 name:"frustum_plane" transform:tm 
1 Reply
(@momo2012)
Joined: 11 months ago

Posts: 0

Hi,dnies,there maybe something wrong with the target camera after I tried the codes,free camera works fine!

More correct?

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

yes. it’s more correct. targetdistance is a parameter of the ‘camera’ superclass. every camera has to be derived from this superclass, but it doesn’t have to expose ‘targetdistance’ property.
if you write your own mxs ‘camera’ plugin which extends ‘freecamera’ you should see that this property returns ‘undefined’

also the not using of ‘dir’ is more correct too.

i don’t have max around to test it but if my logic is correct the:

(freecamera()).targetdistance

has to return undefined

but… i’ve thought a little more. you are right.

a user can override ‘targetdistance’ property. so it’s safer to ask this property from the object instead of its base.
but in this case we have to check that the object returns anything that makes sense

I know, and so the function I wrote does not use the ‘targetdistance’ property.

We should ask the developers to remove it from MXS then.

BTW, ‘targetdistance’ is a buggy property. In your function you should verify the camera type before assuming it will return the correct value.

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

well… but the example of the function using…

fair enough… but see a post above. in general case we should just ask this property and check if it makes a sense.

3 Replies
(@polytools3d)
Joined: 11 months ago

Posts: 0

Basically this should fix it:

if cam.type == #free then cam.baseobject.targetdistance else cam.targetdistance
(@momo2012)
Joined: 11 months ago

Posts: 0

After tried this ,target camera and free camera both works well,

(@momo2012)
Joined: 11 months ago

Posts: 0

I forgot one thing,if a camera using camera correction,the codes may need more advance,I just uploaded a max scene for test,is that possible to fix?

Ok, you win…

No, you don’t

Page 1 / 2