Skip to content

Commit

Permalink
Fix #1164 "Screen position out of view frustum"
Browse files Browse the repository at this point in the history
Raised by scene view when using 2D view and planar reflections.
  • Loading branch information
daleeidd committed Feb 8, 2025
1 parent e0bed6b commit b816684
Showing 1 changed file with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public partial class OceanPlanarReflection : CustomMonoBehaviour
const int CULL_DISTANCE_COUNT = 32;
float[] _cullDistances = new float[CULL_DISTANCE_COUNT];

#if UNITY_EDITOR
bool _isSceneCamera;
float _changeViewTimer;
#endif

private void Start()
{
if (OceanRenderer.Instance == null)
Expand Down Expand Up @@ -184,6 +189,7 @@ void LateUpdate()
if (!editorCamera.TryGetComponent<OceanPlanarReflection>(out var editor))
{
editor = editorCamera.gameObject.AddComponent<OceanPlanarReflection>();
editor._isSceneCamera = true;
}

if (editor != null)
Expand Down Expand Up @@ -212,6 +218,34 @@ void LateUpdate()
return;
}

#if UNITY_EDITOR
// Fix "Screen position out of view frustum" when 2D view activated.
// Always had an exception on exiting 2D mode, so resorted to a timer.
if (_isSceneCamera)
{
var sceneView = UnityEditor.SceneView.lastActiveSceneView;

if (sceneView == null)
{
_camReflections.enabled = false;
return;
}

if (sceneView.in2DMode)
{
_changeViewTimer = 1f;
}

_camReflections.enabled = _changeViewTimer <= 0f;

if (!_camReflections.enabled)
{
_changeViewTimer -= 0.02f;
return;
}
}
#endif

// Find out the reflection plane: position and normal in world space
Vector3 planePos = OceanRenderer.Instance.Root.position;
Vector3 planeNormal = Vector3.up;
Expand Down Expand Up @@ -423,17 +457,19 @@ private void OnDisable()
PreparedReflections.Remove(_camViewpoint.GetHashCode());
}

if (_camReflections)
{
_camReflections.targetTexture = null;
Helpers.Destroy(_camReflections.gameObject);
_camReflections = null;
}

// Cleanup all the objects we possibly have created
if (_reflectionTexture)
{
Helpers.Destroy(_reflectionTexture);
_reflectionTexture = null;
}
if (_camReflections)
{
Helpers.Destroy(_camReflections.gameObject);
_camReflections = null;
}
}
}

Expand Down

0 comments on commit b816684

Please sign in to comment.