@@ -1232,11 +1232,15 @@ static FreeCamBehaviour()
12321232 public FreeCamBehaviour ( IntPtr ptr ) : base ( ptr ) { }
12331233#endif
12341234 private Action onBeforeRenderAction ;
1235- private Vector3 cachedPosition ;
1236- private Quaternion cachedRotation ;
12371235 private CamPaths cachedCamPathsPanel ;
12381236 private bool ? hasHDRPComponent = null ;
12391237 private float cachedAspectRatio = - 1f ;
1238+
1239+ // Cached HDRP reflection objects
1240+ private Type cachedCameraPositionSettingsType = null ;
1241+ private MethodInfo cachedApplySettingsMethod = null ;
1242+ private FieldInfo cachedPositionField = null ;
1243+ private FieldInfo cachedRotationField = null ;
12401244
12411245 internal void Update ( )
12421246 {
@@ -1279,7 +1283,7 @@ internal void Update()
12791283 }
12801284
12811285 UpdateRelativeMatrix ( ) ;
1282- UpdateRealCamera ( ) ;
1286+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
12831287
12841288 FreeCamPanel . connector ? . ExecuteCameraCommand ( FreeCamPanel . GetFreecam ( ) ) ;
12851289
@@ -1309,25 +1313,25 @@ private bool IsInputFieldInFocus()
13091313 private void OnPreCull ( )
13101314 {
13111315 UpdateRelativeMatrix ( ) ;
1312- UpdateRealCamera ( ) ;
1316+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
13131317 }
13141318
13151319 private void OnPreRender ( )
13161320 {
13171321 UpdateRelativeMatrix ( ) ;
1318- UpdateRealCamera ( ) ;
1322+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
13191323 }
13201324
13211325 private void OnCameraPostRender ( )
13221326 {
13231327 UpdateRelativeMatrix ( ) ;
1324- //RestoreRealCameraTransform ();
1328+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
13251329 }
13261330
13271331 private void LateUpdate ( )
13281332 {
13291333 UpdateRelativeMatrix ( ) ;
1330- //RestoreRealCameraTransform ();
1334+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
13311335 }
13321336
13331337 internal void UpdateRelativeMatrix ( ) {
@@ -1577,7 +1581,7 @@ internal void ProcessInput(Transform transform){
15771581 // The following code forces the freecamcam to update before rendering a frame
15781582 protected virtual void Awake ( )
15791583 {
1580- onBeforeRenderAction = ( ) => { UpdateRelativeMatrix ( ) ; UpdateRealCamera ( ) ; } ;
1584+ onBeforeRenderAction = ( ) => { UpdateRelativeMatrix ( ) ; MaybeUpdateHDRPCameraPositionSettings ( ) ; } ;
15811585 }
15821586
15831587 protected virtual void OnEnable ( )
@@ -1728,41 +1732,65 @@ protected virtual void OnDisable()
17281732 private void OnBeforeEvent ( object arg1 , Camera [ ] arg2 )
17291733 {
17301734 UpdateRelativeMatrix ( ) ;
1731- UpdateRealCamera ( ) ;
1735+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
17321736 }
17331737
17341738 private void OnAfterEvent ( object arg1 , Camera [ ] arg2 )
17351739 {
17361740 UpdateRelativeMatrix ( ) ;
1737- //RestoreRealCameraTransform ();
1741+ MaybeUpdateHDRPCameraPositionSettings ( ) ;
17381742 }
17391743
1740- // HDRP matrix override ignores us moving the camera unfortunately, so we try to copy over the position of the camera matrix overrider.
1741- protected void UpdateRealCamera ( ) {
1742- if ( FreeCamPanel . cameraMatrixOverrider != null ) {
1743- if ( ! hasHDRPComponent . HasValue )
1744+ internal void MaybeUpdateHDRPCameraPositionSettings ( )
1745+ {
1746+ if ( ! hasHDRPComponent . HasValue )
1747+ {
1748+ hasHDRPComponent = false ;
1749+
1750+ Type cameraSettingsUtilitiesType = ReflectionUtility . GetTypeByName ( "UnityEngine.Rendering.HighDefinition.CameraSettingsUtilities" ) ;
1751+ if ( cameraSettingsUtilitiesType == null )
1752+ return ;
1753+
1754+ cachedCameraPositionSettingsType = ReflectionUtility . GetTypeByName ( "UnityEngine.Rendering.HighDefinition.CameraPositionSettings" ) ;
1755+ if ( cachedCameraPositionSettingsType == null )
1756+ return ;
1757+
1758+ cachedApplySettingsMethod = cameraSettingsUtilitiesType . GetMethod ( "ApplySettings" , BindingFlags . Public | BindingFlags . Static , null , new Type [ ] { typeof ( Camera ) , cachedCameraPositionSettingsType } , null ) ;
1759+ if ( cachedApplySettingsMethod == null )
1760+ return ;
1761+
1762+ cachedPositionField = cachedCameraPositionSettingsType . GetField ( "position" , BindingFlags . Public | BindingFlags . Instance ) ;
1763+ cachedRotationField = cachedCameraPositionSettingsType . GetField ( "rotation" , BindingFlags . Public | BindingFlags . Instance ) ;
1764+
1765+ if ( cachedPositionField != null && cachedRotationField != null )
17441766 {
1745- var hdrpComponent = FreeCamPanel . GetComponentByName ( FreeCamPanel . ourCamera . gameObject , "UnityEngine.Rendering.HighDefinition.HDAdditionalCameraData" ) ;
1746- hasHDRPComponent = hdrpComponent != null ;
1767+ hasHDRPComponent = true ;
17471768 }
1769+ }
17481770
1749- if ( hasHDRPComponent == true )
1750- {
1751- cachedPosition = FreeCamPanel . ourCamera . transform . position ;
1752- FreeCamPanel . ourCamera . transform . position = FreeCamPanel . cameraMatrixOverrider . transform . position ;
1753- // We also try to update the rotation in case there are game shaders that use the real camera rotation
1754- cachedRotation = FreeCamPanel . ourCamera . transform . rotation ;
1755- FreeCamPanel . ourCamera . transform . rotation = FreeCamPanel . cameraMatrixOverrider . transform . rotation ;
1756- }
1771+ if ( hasHDRPComponent == true )
1772+ {
1773+ UpdateHDRPCameraPositionSettings ( ) ;
17571774 }
17581775 }
17591776
1760- protected void RestoreRealCameraTransform ( ) {
1761- if ( FreeCamPanel . cameraMatrixOverrider != null ) {
1762- FreeCamPanel . ourCamera . transform . position = cachedPosition ;
1763- FreeCamPanel . ourCamera . transform . rotation = cachedRotation ;
1777+ // HDRP handles position and rotation internally, so we need to replace these within HDRP systems
1778+ internal void UpdateHDRPCameraPositionSettings ( )
1779+ {
1780+ try
1781+ {
1782+ object cameraPositionSettings = Activator . CreateInstance ( cachedCameraPositionSettingsType ) ;
1783+
1784+ cachedPositionField . SetValue ( cameraPositionSettings , FreeCamPanel . cameraMatrixOverrider . transform . position ) ;
1785+ cachedRotationField . SetValue ( cameraPositionSettings , FreeCamPanel . cameraMatrixOverrider . transform . rotation ) ;
1786+
1787+ cachedApplySettingsMethod . Invoke ( null , new object [ ] { FreeCamPanel . ourCamera , cameraPositionSettings } ) ;
1788+ }
1789+ catch ( Exception ex )
1790+ {
1791+ ExplorerCore . LogWarning ( $ "Failed to apply HDRP camera position settings: { ex . Message } \n { ex . StackTrace } ") ;
1792+ hasHDRPComponent = false ;
17641793 }
1765- UpdateRealCamera ( ) ;
17661794 }
17671795 }
17681796
0 commit comments