'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'	Subregion Viewer Script
'	-----------------------
'	Jean-Louis Billard - Oct 2000
'
'	This script will create a null and an implicit square as a child of the null.
'	The square will resize according to the subregion render slider settings.
'	It applies expressions to the null and square to control them, and thus
'		the script needs to only be run once, usually just before rendering.
'	However it only tests wether the camera FOV is set Vertical or Horizontal
'		when the script is run, so if you switch this you will have to run the
'		script again.
'	When you are done with setting the subregion, you may delete the 
'		null & square hierarchy.
'
'	Any suggestions/improvements/bugs, email: jean-louis@imajonline.com
'	(also see notes at end of file)
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Setup

dim SubRegionRef, SubRegionObj, FieldOfView, WhichPass, WhichCamera		'Define variables
WhichPass = GetCurrentPass												'Find the current pass name
WhichCamera = GetValue(WhichPass &".RenderOptions.PassCamera")			'Find the current Camera name
set SubRegionRef = GetPrim("Null")										'Create a null object
set SubRegionObj = GetPrim("Square")										'Create an implicit square
ParentObj SubRegionRef, SubRegionObj										'Make square child of null
ApplyCns "Position", SubRegionRef, "Camera_Interest"						'Constrain the hierarchy to the camera interest
ApplyCns "Direction", SubRegionRef, WhichCamera							'Constrain null to point to camera
SetValue SubRegionRef & ".kine.dircns.dirx", 0.000						'\
SetValue SubRegionRef & ".kine.dircns.diry", 0.000						'-Make null's z axis direction constraint reference 
SetValue SubRegionRef & ".kine.dircns.dirz", 1.000						'/
SetValue SubRegionRef & ".kine.dircns.upvct_active", True					'Activate UpVector on constraint

'Expression on null to take account of any camera roll
SetExpr SubRegionRef & ".kine.dircns.roll", "-1*" & WhichCamera & ".kine.dircns.roll"

'Expression to fit square to camera fov
SetExpr SubRegionObj & ".square.length", "2*( ctr_dist_cam_int( " & WhichCamera & ". )*tan(" & WhichCamera & ".camera.fov /2 ) )"

'Test to see if fov is measured horizontally or vertically...
'...then scale null in appropriate axis - this way there's no need to take it into account on the square's expressions
FieldOfView = GetValue(WhichCamera & ".camera.fovtype")
If FieldOfView = 0 Then
	SetExpr SubRegionRef & ".kine.local.sclx", WhichCamera & ".camera.aspect"
Else
	SetExpr SubRegionRef & ".kine.local.scly", "1/(" & WhichCamera & ".camera.aspect)"
End If

'Now the expressions to link the subregion values to offset and scale the square
SetExpr SubRegionObj & ".kine.local.scly", "(" & WhichPass & ".RenderOptions.SubWindowYMax - " & WhichPass & ".RenderOptions.SubWindowYMin ) / " & WhichPass & ".RenderOptions.CameraYRes"
SetExpr SubRegionObj & ".kine.local.posy", "((" & WhichPass & ".RenderOptions.SubWindowYMin + ( (" & WhichPass & ".RenderOptions.SubWindowYMax - " & WhichPass & ".RenderOptions.SubWindowYMin ) / 2 ) - ( " & WhichPass & ".RenderOptions.CameraYRes / 2 ) ) / " & WhichPass & ".RenderOptions.CameraYRes) * (tan(" & WhichCamera & ".camera.fov/2) * ctr_dist_cam_int( " & WhichCamera & ".) * 2)"
SetExpr SubRegionObj & ".kine.local.sclx", "(" & WhichPass & ".RenderOptions.SubWindowXMax - " & WhichPass & ".RenderOptions.SubWindowXMin ) / " & WhichPass & ".RenderOptions.CameraXRes"
SetExpr SubRegionObj & ".kine.local.posx", "((" & WhichPass & ".RenderOptions.SubWindowXMin + ( (" & WhichPass & ".RenderOptions.SubWindowXMax - " & WhichPass & ".RenderOptions.SubWindowXMin ) / 2 ) - ( " & WhichPass & ".RenderOptions.CameraXRes / 2 ) ) / " & WhichPass & ".RenderOptions.CameraXRes) * (tan(" & WhichCamera & ".camera.fov/2) * ctr_dist_cam_int( " & WhichCamera & ".) * 2)"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'	NOTES
'
'	1 - There is no line to get the camera interest name, in the unlikely event that it has been changed.
'	2 - The test for horizontal or vertical fov *could* be incorporated into the expression easily, but 
'	I figured it would be unecessary since that is probably not something that would be changed
'	just before rendering.
'	3 - There *must* be more elegant expressions to apply on the square and achieve the same result!!
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''