@@ -22,6 +22,8 @@ public class ARRenderer
2222 private int thisFrameWidth = 0 ;
2323 private int thisFrameHeight = 0 ;
2424
25+ private OverlaySettings overlaySettings ;
26+
2527 // These are all variables that are needed for
2628 // rendering ImGui windows in 3D space.
2729 private GL gl ;
@@ -53,7 +55,16 @@ public ARRenderer(GL gl)
5355 cameraData = CameraProvider . Current . GetCurrentData ( ) ;
5456 telemetryData = GameTelemetry . Current . GetCurrentData ( ) ;
5557
58+ overlaySettings = OverlaySettingsHandler . Current . GetSettings ( ) ;
59+ OverlaySettingsHandler . Current . OnSettingsUpdated += OnOverlaySettingsUpdated ;
60+
5661 ImGui . SetCurrentContext ( mainContext ) ;
62+
63+ }
64+
65+ private void OnOverlaySettingsUpdated ( OverlaySettings newSettings )
66+ {
67+ overlaySettings = newSettings ;
5768 }
5869
5970 /// <summary>
@@ -65,6 +76,7 @@ public void Render()
6576 {
6677 thisFrameProjection = default ;
6778 thisFrameView = default ;
79+ GetViewMatrix ( ) ;
6880
6981 thisFrameWidth = ( int ) OverlayHandler . Current . OverlayWidth ;
7082 thisFrameHeight = ( int ) OverlayHandler . Current . OverlayHeight ;
@@ -185,12 +197,11 @@ public Matrix4x4 GetProjectionMatrix()
185197 }
186198
187199 /// <summary>
188- /// This function will return the world space coordinates of the provided ARCoordinate
189- /// while also taking into account the coordinate center. Truck positions are interpolated
190- /// to avoid jitter.
200+ /// This function will return the camera space coordinates of the provided ARCoordinate
201+ /// while also taking into account the coordinate center.
191202 /// </summary>
192203 /// <param name="coord">ARCoordinate to convert.</param>
193- /// <returns>World space coordinates.</returns>
204+ /// <returns>camera space coordinates.</returns>
194205 /// <exception cref="ArgumentException"></exception>
195206 public Vector3 ARCoordinateToVector3 ( ARCoordinate coord )
196207 {
@@ -226,6 +237,22 @@ private uint ConvertColor(uint rgba)
226237 ( ( rgba & 0x000000FF ) << 24 ) ;
227238 }
228239
240+ private bool AllPointsOutsideRenderDistance ( ARCoordinate [ ] points )
241+ {
242+ float maxDistance = overlaySettings . MaxARDistance ;
243+ Vector3 cameraPos = cameraData . position ;
244+
245+ foreach ( var point in points )
246+ {
247+ Vector3 worldPos = ARCoordinateToVector3 ( point ) ;
248+ float distance = Vector3 . Distance ( worldPos , cameraPos ) ;
249+ if ( distance <= maxDistance )
250+ return false ;
251+ }
252+
253+ return true ;
254+ }
255+
229256 /// <summary>
230257 /// Draw a line in 3D space. The line will be transformed and projected
231258 /// onto the AR overlay.
@@ -236,6 +263,9 @@ private uint ConvertColor(uint rgba)
236263 /// <param name="thickness">Thickness of the line in pixels.</param>
237264 public void Draw3DLine ( ARCoordinate start , ARCoordinate end , UInt32 color , float thickness = 1.0f )
238265 {
266+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { start , end } ) )
267+ return ;
268+
239269 Vector2 ? p1 = WorldToScreen ( ARCoordinateToVector3 ( start ) , thisFrameWidth , thisFrameHeight ) ;
240270 Vector2 ? p2 = WorldToScreen ( ARCoordinateToVector3 ( end ) , thisFrameWidth , thisFrameHeight ) ;
241271
@@ -257,6 +287,9 @@ public void Draw3DLine(ARCoordinate start, ARCoordinate end, UInt32 color, float
257287 /// <param name="thickness">The thickness of the circle outline.</param>
258288 public void Draw3DCircle ( ARCoordinate center , float radius , UInt32 color , bool filled = false , float thickness = 1 )
259289 {
290+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { center } ) )
291+ return ;
292+
260293 Vector2 ? centerScreen = WorldToScreen ( ARCoordinateToVector3 ( center ) , thisFrameWidth , thisFrameHeight ) ;
261294 if ( ! centerScreen . HasValue )
262295 return ;
@@ -282,6 +315,9 @@ public void Draw3DCircle(ARCoordinate center, float radius, UInt32 color, bool f
282315 /// <param name="thickness">The thickness of the polygon outline.</param>
283316 public void Draw3DPolygon ( ARCoordinate [ ] points , UInt32 color , bool filled = false , float thickness = 1 )
284317 {
318+ if ( AllPointsOutsideRenderDistance ( points ) )
319+ return ;
320+
285321 if ( points == null || points . Length < 3 )
286322 return ;
287323
@@ -328,6 +364,9 @@ public void Draw3DPolygon(ARCoordinate[] points, UInt32 color, bool filled = fal
328364 /// <param name="thickness">The thickness of a non filled quad.</param>
329365 public void Draw3DQuad ( ARCoordinate p1 , ARCoordinate p2 , ARCoordinate p3 , ARCoordinate p4 , UInt32 color , bool filled = false , float thickness = 1 )
330366 {
367+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { p1 , p2 , p3 , p4 } ) )
368+ return ;
369+
331370 Vector2 ? p1s = WorldToScreen ( ARCoordinateToVector3 ( p1 ) , thisFrameWidth , thisFrameHeight ) ;
332371 Vector2 ? p2s = WorldToScreen ( ARCoordinateToVector3 ( p2 ) , thisFrameWidth , thisFrameHeight ) ;
333372 Vector2 ? p3s = WorldToScreen ( ARCoordinateToVector3 ( p3 ) , thisFrameWidth , thisFrameHeight ) ;
@@ -363,6 +402,9 @@ public void Draw3DQuad(ARCoordinate p1, ARCoordinate p2, ARCoordinate p3, ARCoor
363402 /// <param name="thickness">The thickness of a non filled triangle.</param>
364403 public void Draw3DTriangle ( ARCoordinate p1 , ARCoordinate p2 , ARCoordinate p3 , UInt32 color , bool filled = false , float thickness = 1 )
365404 {
405+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { p1 , p2 , p3 } ) )
406+ return ;
407+
366408 Vector2 ? p1s = WorldToScreen ( ARCoordinateToVector3 ( p1 ) , thisFrameWidth , thisFrameHeight ) ;
367409 Vector2 ? p2s = WorldToScreen ( ARCoordinateToVector3 ( p2 ) , thisFrameWidth , thisFrameHeight ) ;
368410 Vector2 ? p3s = WorldToScreen ( ARCoordinateToVector3 ( p3 ) , thisFrameWidth , thisFrameHeight ) ;
@@ -396,6 +438,9 @@ public void Draw3DTriangle(ARCoordinate p1, ARCoordinate p2, ARCoordinate p3, UI
396438 /// <param name="color"></param>
397439 public void Draw3DText ( ARCoordinate position , string text , UInt32 color )
398440 {
441+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { position } ) )
442+ return ;
443+
399444 Vector2 ? screenPos = WorldToScreen ( ARCoordinateToVector3 ( position ) , thisFrameWidth , thisFrameHeight ) ;
400445 if ( ! screenPos . HasValue ) return ;
401446
@@ -471,6 +516,12 @@ public void EndWindow(ARCoordinate center, Quaternion rotation, float width, boo
471516 // onto the main overlay background.
472517 ImGui . SetCurrentContext ( oldContext ) ;
473518
519+ if ( AllPointsOutsideRenderDistance ( new ARCoordinate [ ] { center } ) )
520+ {
521+ isWindowContextInitialized = false ;
522+ return ;
523+ }
524+
474525 float windowAspectRatio = windowSize . Y / windowSize . X ;
475526 float height = width * windowAspectRatio ;
476527
0 commit comments