@@ -49,12 +49,14 @@ private enum FrustumPlanes
4949
5050 private static int frustumLastUpdated = - 1 ;
5151
52- private static Plane [ ] indicatorVolume ;
52+ private static Plane [ ] frustumPlanes ;
5353 private static Vector3 cameraForward ;
5454 private static Vector3 cameraPosition ;
5555 private static Vector3 cameraRight ;
5656 private static Vector3 cameraUp ;
5757
58+ private Plane [ ] indicatorVolume ;
59+
5860 private void Start ( )
5961 {
6062 Depth = Mathf . Clamp ( Depth , Camera . main . nearClipPlane , Camera . main . farClipPlane ) ;
@@ -71,6 +73,18 @@ private void Start()
7173 // offsetting its position.
7274 pointer . transform . parent = transform ;
7375 pointer . transform . position = - Pivot ;
76+
77+ // Allocate the space to hold the indicator volume planes. Later portions of the algorithm take for
78+ // granted that these objects have been initialized.
79+ indicatorVolume = new Plane [ ]
80+ {
81+ new Plane ( ) ,
82+ new Plane ( ) ,
83+ new Plane ( ) ,
84+ new Plane ( ) ,
85+ new Plane ( ) ,
86+ new Plane ( )
87+ } ;
7488 }
7589
7690 // Update the direction indicator's position and orientation every frame.
@@ -90,23 +104,21 @@ private void Update()
90104 // Collect the updated camera information for the current frame
91105 CacheCameraTransform ( Camera . main ) ;
92106
93- // Use the updated camera information to create the new bounding volume
94- UpdateIndicatorVolume ( Camera . main ) ;
95-
96107 frustumLastUpdated = currentFrameCount ;
97108 }
98109
99110 UpdatePointerTransform ( Camera . main , indicatorVolume , TargetObject . transform . position ) ;
100111 }
101112 }
102113
114+ // Cache data from the camera state that are costly to retrieve.
103115 private void CacheCameraTransform ( Camera camera )
104116 {
105- // Cache the camera transform information for the current frame
106117 cameraForward = camera . transform . forward ;
107118 cameraPosition = camera . transform . position ;
108119 cameraRight = camera . transform . right ;
109120 cameraUp = camera . transform . up ;
121+ frustumPlanes = GeometryUtility . CalculateFrustumPlanes ( camera ) ;
110122 }
111123
112124 // Assuming the target object is outside the view which of the four "wall" planes should
@@ -267,6 +279,9 @@ private bool TryIntersectPlanes(Plane p, Plane q, out Ray intersection)
267279 // planes.
268280 private void UpdatePointerTransform ( Camera camera , Plane [ ] planes , Vector3 targetPosition )
269281 {
282+ // Use the camera information to create the new bounding volume
283+ UpdateIndicatorVolume ( camera ) ;
284+
270285 // Start by assuming the pointer should be placed at the target position.
271286 Vector3 indicatorPosition = cameraPosition + Depth * ( targetPosition - cameraPosition ) . normalized ;
272287
@@ -324,25 +339,29 @@ private void UpdateIndicatorVolume(Camera camera)
324339 {
325340 // The top, bottom and side frustum planes are used to restrict the movement
326341 // of the pointer. These reside at indices 0-3;
327- indicatorVolume = GeometryUtility . CalculateFrustumPlanes ( camera ) ;
328342 for ( int i = 0 ; i < 4 ; ++ i )
329343 {
330344 // We can make the frustum smaller by rotating the walls "in" toward the
331345 // camera's forward vector.
332346
333347 // First find the angle between the Camera's forward and the plane's normal
334- float angle = Mathf . Acos ( Vector3 . Dot ( indicatorVolume [ i ] . normal . normalized , cameraForward ) ) ;
348+ float angle = Mathf . Acos ( Vector3 . Dot ( frustumPlanes [ i ] . normal . normalized , cameraForward ) ) ;
335349
336350 // Then we calculate how much we should rotate the plane in based on the
337351 // user's setting. 90 degrees is our maximum as at that point we no longer
338352 // have a valid frustum.
339353 float angleStep = IndicatorMarginPercent * ( 0.5f * Mathf . PI - angle ) ;
340354
341- // Because the frustum plane normal's face in must actually rotate away from the forward to vector
355+ // Because the frustum plane normals face in we must actually rotate away from the forward vector
342356 // to narrow the frustum.
343- Vector3 normal = Vector3 . RotateTowards ( indicatorVolume [ i ] . normal , cameraForward , - angleStep , 0.0f ) ;
357+ Vector3 normal = Vector3 . RotateTowards ( frustumPlanes [ i ] . normal , cameraForward , - angleStep , 0.0f ) ;
358+
344359 indicatorVolume [ i ] . normal = normal . normalized ;
360+ indicatorVolume [ i ] . distance = frustumPlanes [ i ] . distance ;
345361 }
362+
363+ indicatorVolume [ 4 ] = frustumPlanes [ 4 ] ;
364+ indicatorVolume [ 5 ] = frustumPlanes [ 5 ] ;
346365 }
347366 }
348367}
0 commit comments