@@ -15,15 +15,20 @@ public static class CameraEditorUtils
1515
1616 static int s_MovingHandleId = 0 ;
1717 static Vector3 s_InitialFarMid ;
18- static readonly int [ ] s_FrustrumHandleIds =
18+ static readonly int [ ] s_FrustumHandleIds =
1919 {
20- "CameraEditor_FrustrumHandleTop " . GetHashCode ( ) ,
21- "CameraEditor_FrustrumHandleBottom " . GetHashCode ( ) ,
22- "CameraEditor_FrustrumHandleLeft " . GetHashCode ( ) ,
23- "CameraEditor_FrustrumHandleRight " . GetHashCode ( )
20+ "CameraEditor_FrustumHandleTop " . GetHashCode ( ) ,
21+ "CameraEditor_FrustumHandleBottom " . GetHashCode ( ) ,
22+ "CameraEditor_FrustumHandleLeft " . GetHashCode ( ) ,
23+ "CameraEditor_FrustumHandleRight " . GetHashCode ( )
2424 } ;
2525
2626 public static void HandleFrustum ( Camera c )
27+ {
28+ HandleFrustum ( c , 0 ) ;
29+ }
30+
31+ public static void HandleFrustum ( Camera c , int cameraEditorTargetIndex )
2732 {
2833 Color orgHandlesColor = Handles . color ;
2934 Color slidersColor = k_ColorThemeCameraGizmo ;
@@ -47,12 +52,12 @@ public static void HandleFrustum(Camera c)
4752 Vector3 farMid = Vector3 . Lerp ( leftBottomFar , rightTopFar , 0.5f ) ;
4853 if ( s_MovingHandleId != 0 )
4954 {
50- if ( ! s_FrustrumHandleIds . Contains ( GUIUtility . hotControl ) )
55+ if ( ! s_FrustumHandleIds . Contains ( GUIUtility . hotControl - cameraEditorTargetIndex ) )
5156 s_MovingHandleId = GUIUtility . hotControl ;
5257 else
5358 farMid = s_InitialFarMid ;
5459 }
55- else if ( s_FrustrumHandleIds . Contains ( GUIUtility . hotControl ) )
60+ else if ( s_FrustumHandleIds . Contains ( GUIUtility . hotControl - cameraEditorTargetIndex ) )
5661 {
5762 s_MovingHandleId = GUIUtility . hotControl ;
5863 s_InitialFarMid = farMid ;
@@ -61,17 +66,17 @@ public static void HandleFrustum(Camera c)
6166 // FOV handles
6267 // Top and bottom handles
6368 float halfHeight = - 1.0f ;
64- Vector3 changedPosition = MidPointPositionSlider ( s_FrustrumHandleIds [ 0 ] , leftTopFar , rightTopFar , c . transform . up ) ;
69+ Vector3 changedPosition = MidPointPositionSlider ( s_FrustumHandleIds [ 0 ] + cameraEditorTargetIndex , leftTopFar , rightTopFar , c . transform . up ) ;
6570 if ( ! GUI . changed )
66- changedPosition = MidPointPositionSlider ( s_FrustrumHandleIds [ 1 ] , leftBottomFar , rightBottomFar , - c . transform . up ) ;
71+ changedPosition = MidPointPositionSlider ( s_FrustumHandleIds [ 1 ] + cameraEditorTargetIndex , leftBottomFar , rightBottomFar , - c . transform . up ) ;
6772 if ( GUI . changed )
6873 halfHeight = ( changedPosition - farMid ) . magnitude ;
6974
7075 // Left and right handles
7176 GUI . changed = false ;
72- changedPosition = MidPointPositionSlider ( s_FrustrumHandleIds [ 2 ] , rightBottomFar , rightTopFar , c . transform . right ) ;
77+ changedPosition = MidPointPositionSlider ( s_FrustumHandleIds [ 2 ] + cameraEditorTargetIndex , rightBottomFar , rightTopFar , c . transform . right ) ;
7378 if ( ! GUI . changed )
74- changedPosition = MidPointPositionSlider ( s_FrustrumHandleIds [ 3 ] , leftBottomFar , leftTopFar , - c . transform . right ) ;
79+ changedPosition = MidPointPositionSlider ( s_FrustumHandleIds [ 3 ] + cameraEditorTargetIndex , leftBottomFar , leftTopFar , - c . transform . right ) ;
7580 if ( GUI . changed )
7681 halfHeight = ( changedPosition - farMid ) . magnitude / frustumAspect ;
7782
@@ -126,26 +131,56 @@ public static bool TryGetFrustum(Camera camera, Vector3[] near, Vector3[] far, o
126131
127132 if ( far != null )
128133 {
129- far [ 0 ] = new Vector3 ( 0 , 0 , camera . farClipPlane ) ; // leftBottomFar
130- far [ 1 ] = new Vector3 ( 0 , 1 , camera . farClipPlane ) ; // leftTopFar
131- far [ 2 ] = new Vector3 ( 1 , 1 , camera . farClipPlane ) ; // rightTopFar
132- far [ 3 ] = new Vector3 ( 1 , 0 , camera . farClipPlane ) ; // rightBottomFar
133- for ( int i = 0 ; i < 4 ; ++ i )
134- far [ i ] = camera . ViewportToWorldPointWithoutGateFit ( far [ i ] ) ;
134+ if ( camera . projectionMatrixMode == ( int ) Camera . ProjectionMatrixMode . Explicit )
135+ {
136+ far [ 0 ] = new Vector3 ( 0 , 0 , camera . farClipPlane ) ; // leftBottomFar
137+ far [ 1 ] = new Vector3 ( 0 , 1 , camera . farClipPlane ) ; // leftTopFar
138+ far [ 2 ] = new Vector3 ( 1 , 1 , camera . farClipPlane ) ; // rightTopFar
139+ far [ 3 ] = new Vector3 ( 1 , 0 , camera . farClipPlane ) ; // rightBottomFar
140+ for ( int i = 0 ; i < 4 ; ++ i )
141+ far [ i ] = camera . ViewportToWorldPoint ( far [ i ] ) ;
142+ }
143+ else
144+ {
145+ CalculateFrustumPlaneAt ( camera , camera . farClipPlane , far ) ;
146+ }
135147 }
136148
137149 if ( near != null )
138150 {
139- near [ 0 ] = new Vector3 ( 0 , 0 , camera . nearClipPlane ) ; // leftBottomNear
140- near [ 1 ] = new Vector3 ( 0 , 1 , camera . nearClipPlane ) ; // leftTopNear
141- near [ 2 ] = new Vector3 ( 1 , 1 , camera . nearClipPlane ) ; // rightTopNear
142- near [ 3 ] = new Vector3 ( 1 , 0 , camera . nearClipPlane ) ; // rightBottomNear
143- for ( int i = 0 ; i < 4 ; ++ i )
144- near [ i ] = camera . ViewportToWorldPointWithoutGateFit ( near [ i ] ) ;
151+ if ( camera . projectionMatrixMode == ( int ) Camera . ProjectionMatrixMode . Explicit )
152+ {
153+ near [ 0 ] = new Vector3 ( 0 , 0 , camera . nearClipPlane ) ; // leftBottomNear
154+ near [ 1 ] = new Vector3 ( 0 , 1 , camera . nearClipPlane ) ; // leftTopNear
155+ near [ 2 ] = new Vector3 ( 1 , 1 , camera . nearClipPlane ) ; // rightTopNear
156+ near [ 3 ] = new Vector3 ( 1 , 0 , camera . nearClipPlane ) ; // rightBottomNear
157+ for ( int i = 0 ; i < 4 ; ++ i )
158+ near [ i ] = camera . ViewportToWorldPoint ( near [ i ] ) ;
159+ }
160+ else
161+ {
162+ CalculateFrustumPlaneAt ( camera , camera . nearClipPlane , near ) ;
163+ }
145164 }
146165 return true ;
147166 }
148167
168+ private static void CalculateFrustumPlaneAt ( Camera camera , float distance , Vector3 [ ] plane )
169+ {
170+ Vector2 planeSize = camera . GetFrustumPlaneSizeAt ( distance ) * .5f ;
171+ Vector3 rightOffset = camera . gameObject . transform . right * planeSize . x ;
172+ Vector3 upOffset = camera . gameObject . transform . up * planeSize . y ;
173+ Vector3 localAim = camera . GetLocalSpaceAim ( ) * distance ;
174+ localAim . z = - localAim . z ;
175+
176+ Vector3 planePosition = camera . cameraToWorldMatrix . MultiplyPoint ( localAim ) ;
177+
178+ plane [ 0 ] = planePosition - rightOffset - upOffset ; // leftBottom
179+ plane [ 1 ] = planePosition - rightOffset + upOffset ; // leftTop
180+ plane [ 2 ] = planePosition + rightOffset + upOffset ; // rightTop
181+ plane [ 3 ] = planePosition + rightOffset - upOffset ; // rightBottom
182+ }
183+
149184 public static bool IsViewportRectValidToRender ( Rect normalizedViewPortRect )
150185 {
151186 if ( normalizedViewPortRect . width <= 0f || normalizedViewPortRect . height <= 0f )
@@ -163,8 +198,7 @@ public static float GetFrustumAspectRatio(Camera camera)
163198 if ( normalizedViewPortRect . width <= 0f || normalizedViewPortRect . height <= 0f )
164199 return - 1f ;
165200
166- return camera . usePhysicalProperties ?
167- camera . sensorSize . x / camera . sensorSize . y : GameViewAspectRatio * normalizedViewPortRect . width / normalizedViewPortRect . height ;
201+ return GameViewAspectRatio * normalizedViewPortRect . width / normalizedViewPortRect . height ;
168202 }
169203
170204 public static Vector3 PerspectiveClipToWorld ( Matrix4x4 clipToWorld , Vector3 viewPositionWS , Vector3 positionCS )
0 commit comments