From e69bdb93f889f48db6a55a6a21948dd664616e17 Mon Sep 17 00:00:00 2001 From: Ferlat Thibaud Date: Wed, 25 Mar 2026 01:28:18 +0100 Subject: [PATCH] Fix: cleanup and minor perf improvements --- .../Scripts/Core/Events/PlayerEvents.cs | 15 ++ .../Scripts/Core/Input/InputManager.cs | 144 ++++++++---------- .../Interaction/ObjectDescriptionTrigger.cs | 14 +- .../Scripts/Core/Utils/PortalConstants.cs | 2 +- .../Scripts/Dev/DebugTools/AudioSourceDemo.cs | 2 + .../Dev/DebugTools/DiapasonDebugger.cs | 2 + .../Dev/DebugTools/DiapasonPoseTweaker.cs | 2 + .../Dev/DebugTools/PortalAudioDebugger.cs | 2 + .../TestScripts/DiapasonEquipmentTester.cs | 2 + .../Dev/TestScripts/LanguageSwitcher.cs | 2 + .../Runtime/DetectiveBoardController.cs | 8 +- .../Dialogue/Runtime/DialogueFocusSystem.cs | 2 +- .../Dialogue/Runtime/DialogueManager.cs | 24 ++- .../Features/Dialogue/UI/BillBoardUI.cs | 6 +- .../Features/Dialogue/UI/DialogueUI.cs | 5 +- .../Runtime/DiapasonListeningHandlerV2.cs | 2 - .../Features/Items/Runtime/InspectableItem.cs | 5 - .../Features/Portals/Runtime/Portal.cs | 3 +- .../Portals/Runtime/PortalAudioProxy.cs | 4 +- .../Portals/Runtime/PortalRoomManager.cs | 14 +- .../Camera/Runtime/DynamicShadowDistance.cs | 6 +- 21 files changed, 130 insertions(+), 136 deletions(-) diff --git a/Assets/Unheimlich_POC/Scripts/Core/Events/PlayerEvents.cs b/Assets/Unheimlich_POC/Scripts/Core/Events/PlayerEvents.cs index c12dcc26..066f3540 100644 --- a/Assets/Unheimlich_POC/Scripts/Core/Events/PlayerEvents.cs +++ b/Assets/Unheimlich_POC/Scripts/Core/Events/PlayerEvents.cs @@ -8,6 +8,21 @@ namespace Unheimlich.Core /// public static class PlayerEvents { + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] + private static void ResetAll() + { + OnJump = null; + OnTeleport = null; + OnCameraShake = null; + OnDiapasonEquip = null; + OnDiapasonUnequip = null; + OnDiapasonTap = null; + OnEnterDialogue = null; + OnExitDialogue = null; + OnEnterListening = null; + OnExitListening = null; + } + // Movement events public static event System.Action OnJump; public static event System.Action OnTeleport; diff --git a/Assets/Unheimlich_POC/Scripts/Core/Input/InputManager.cs b/Assets/Unheimlich_POC/Scripts/Core/Input/InputManager.cs index e142a9a2..be317da1 100644 --- a/Assets/Unheimlich_POC/Scripts/Core/Input/InputManager.cs +++ b/Assets/Unheimlich_POC/Scripts/Core/Input/InputManager.cs @@ -14,10 +14,11 @@ namespace Unheimlich.Core.Input { public enum InputContext { - Normal, // All inputs enabled - Dialogue, // Only look and interact - Listening, // Only look and diapason - Inspection // Only look and interact (for item inspection) + Normal, // All inputs enabled + Dialogue, // Only look and interact + Listening, // Only look and diapason + Inspection, // Only look and interact (for item inspection) + DetectiveBoard // Mouse cursor free, movement disabled } [Header("Current State")] @@ -30,6 +31,14 @@ namespace Unheimlich.Core.Input public static Vector2 MoveInput { get; private set; } public static Vector2 LookInput { get; private set; } + // Cached InputAction references — populated once during initialization + private static UnityEngine.InputSystem.InputAction _cachedMoveAction; + private static UnityEngine.InputSystem.InputAction _cachedLookAction; + private static UnityEngine.InputSystem.InputAction _cachedInteractAction; + private static UnityEngine.InputSystem.InputAction _cachedDiapasonAction; + private static UnityEngine.InputSystem.InputAction _cachedFireAction; + private static UnityEngine.InputSystem.InputAction _cachedExitAction; + // Legacy compatibility public static bool IsInputEnabled => CurrentContext == InputContext.Normal; public static bool IsLookingEnabled => CurrentContext != InputContext.Dialogue; @@ -38,6 +47,7 @@ namespace Unheimlich.Core.Input { // Try to subscribe, will retry in Start if needed SubscribeAllInputs(); + CacheAllActions(); SetContext(InputContext.Normal); } @@ -56,8 +66,9 @@ namespace Unheimlich.Core.Input } - // Ensure we're subscribed + // Ensure we're subscribed and actions are cached SubscribeAllInputs(); + CacheAllActions(); } private void OnDestroy() @@ -79,6 +90,38 @@ namespace Unheimlich.Core.Input OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Exit, OnExit); } + /// + /// Caches all InputAction references from OctoCore once at startup to avoid + /// per-frame iteration over the Actions list. + /// + private void CacheAllActions() + { + var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; + if (asset == null) return; + + _cachedMoveAction = null; + _cachedLookAction = null; + _cachedInteractAction = null; + _cachedDiapasonAction = null; + _cachedFireAction = null; + _cachedExitAction = null; + + foreach (var actionInfo in asset) + { + if (actionInfo.Reference == null) continue; + + switch (actionInfo.Name) + { + case "Move": _cachedMoveAction = actionInfo.Reference.action; break; + case "Look": _cachedLookAction = actionInfo.Reference.action; break; + case "Interact": _cachedInteractAction = actionInfo.Reference.action; break; + case "Diapason": _cachedDiapasonAction = actionInfo.Reference.action; break; + case "Fire": _cachedFireAction = actionInfo.Reference.action; break; + case "Exit": _cachedExitAction = actionInfo.Reference.action; break; + } + } + } + private void UnsubscribeAllInputs() { if (OctoCore.OctoCore.Instance == null) return; @@ -126,6 +169,12 @@ namespace Unheimlich.Core.Input Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; break; + + case InputContext.DetectiveBoard: + // Mouse cursor free for board interaction, movement disabled + Cursor.lockState = CursorLockMode.None; + Cursor.visible = true; + break; } } @@ -180,83 +229,12 @@ namespace Unheimlich.Core.Input public static Vector2 GetMoveInput() => MoveInput; public static Vector2 GetLookInput() => LookInput; - // Action accessors for direct button checks - public static UnityEngine.InputSystem.InputAction GetMoveAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Move" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } - - public static UnityEngine.InputSystem.InputAction GetDiapasonAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Diapason" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } - - public static UnityEngine.InputSystem.InputAction GetInteractAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Interact" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } - - public static UnityEngine.InputSystem.InputAction GetFireAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Fire" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } - - public static UnityEngine.InputSystem.InputAction GetExitAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Exit" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } - - public static UnityEngine.InputSystem.InputAction GetLookAction() - { - var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions; - if (asset == null) return null; - - foreach (var actionInfo in asset) - { - if (actionInfo.Name == "Look" && actionInfo.Reference != null) - return actionInfo.Reference.action; - } - return null; - } + // Action accessors — return pre-cached references, no per-frame allocation + public static UnityEngine.InputSystem.InputAction GetMoveAction() => _cachedMoveAction; + public static UnityEngine.InputSystem.InputAction GetLookAction() => _cachedLookAction; + public static UnityEngine.InputSystem.InputAction GetInteractAction() => _cachedInteractAction; + public static UnityEngine.InputSystem.InputAction GetDiapasonAction() => _cachedDiapasonAction; + public static UnityEngine.InputSystem.InputAction GetFireAction() => _cachedFireAction; + public static UnityEngine.InputSystem.InputAction GetExitAction() => _cachedExitAction; } } diff --git a/Assets/Unheimlich_POC/Scripts/Core/Interaction/ObjectDescriptionTrigger.cs b/Assets/Unheimlich_POC/Scripts/Core/Interaction/ObjectDescriptionTrigger.cs index 0e0f21c7..f20547d5 100644 --- a/Assets/Unheimlich_POC/Scripts/Core/Interaction/ObjectDescriptionTrigger.cs +++ b/Assets/Unheimlich_POC/Scripts/Core/Interaction/ObjectDescriptionTrigger.cs @@ -88,6 +88,9 @@ namespace Unheimlich.Core.Interaction private void ResetSessionState() { + // With Domain Reload disabled, static fields and per-instance flags persist between + // Play Mode sessions. This method is called from OnEnable() to reset all transient + // animation state so merged objects start fresh each session without needing a full reload. if (_isActive) { foreach (var word in _spawnedWords) @@ -107,17 +110,6 @@ namespace Unheimlich.Core.Interaction _hasCompletedMergeOnce = false; // reset so MergeOnce objects start fresh each session _currentConvergenceRadius = 0f; } - - // Resets _hasCompletedMergeOnce at the very start of each Play Mode session so - // merged objects start fresh without requiring a full Domain Reload. - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] - private static void OnPlayModeReset() - { - // Finding all instances isn't possible from a static context with SubsystemRegistration. - // The reset is handled per-instance in OnEnable via _hasCompletedMergeOnce being - // re-initialized when the scene reloads. For Enter Play Mode without scene reload, - // ResetSessionState() is called from OnEnable and now correctly clears the flag. - } void Update() { diff --git a/Assets/Unheimlich_POC/Scripts/Core/Utils/PortalConstants.cs b/Assets/Unheimlich_POC/Scripts/Core/Utils/PortalConstants.cs index b3efde43..e7176954 100644 --- a/Assets/Unheimlich_POC/Scripts/Core/Utils/PortalConstants.cs +++ b/Assets/Unheimlich_POC/Scripts/Core/Utils/PortalConstants.cs @@ -21,6 +21,6 @@ namespace Unheimlich.Core.Utils public const float GAMEPAD_DEADZONE = 0.01f; public const int RENDER_TEXTURE_DEPTH = 24; - public const RenderTextureFormat RENDER_TEXTURE_FORMAT = RenderTextureFormat.ARGB32; + public const RenderTextureFormat RENDER_TEXTURE_FORMAT = RenderTextureFormat.DefaultHDR; } } diff --git a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/AudioSourceDemo.cs b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/AudioSourceDemo.cs index fb13f357..a31c1f9f 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/AudioSourceDemo.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/AudioSourceDemo.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using Unheimlich.Features.Player; @@ -125,3 +126,4 @@ namespace Unheimlich.Dev } } } +#endif diff --git a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonDebugger.cs b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonDebugger.cs index 59815ce9..e41ab665 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonDebugger.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonDebugger.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using Unheimlich.Features.Player; using Unheimlich.Features.Diapason; @@ -120,3 +121,4 @@ namespace Unheimlich.Dev } } } +#endif diff --git a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonPoseTweaker.cs b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonPoseTweaker.cs index aac445d3..56535b46 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonPoseTweaker.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/DiapasonPoseTweaker.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using Unheimlich.Features.OctoArms; @@ -179,3 +180,4 @@ public class DiapasonPoseTweaker : MonoBehaviour } } +#endif diff --git a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/PortalAudioDebugger.cs b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/PortalAudioDebugger.cs index bc94cabc..89511ad5 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/PortalAudioDebugger.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/DebugTools/PortalAudioDebugger.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using Unheimlich.Features.Portals; @@ -50,3 +51,4 @@ namespace Unheimlich.Dev } } } +#endif diff --git a/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/DiapasonEquipmentTester.cs b/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/DiapasonEquipmentTester.cs index 96609496..16ee9689 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/DiapasonEquipmentTester.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/DiapasonEquipmentTester.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using UnityEngine.InputSystem; using Unheimlich.Features.Diapason; @@ -45,3 +46,4 @@ public class DiapasonEquipmentTester : MonoBehaviour } } +#endif diff --git a/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/LanguageSwitcher.cs b/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/LanguageSwitcher.cs index 1c4c52f4..a2414e3d 100644 --- a/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/LanguageSwitcher.cs +++ b/Assets/Unheimlich_POC/Scripts/Dev/TestScripts/LanguageSwitcher.cs @@ -1,3 +1,4 @@ +#if UNITY_EDITOR || DEVELOPMENT_BUILD using UnityEngine; using OctoCore.Localization; using UnityEngine.InputSystem; @@ -63,3 +64,4 @@ namespace Unheimlich.Dev } } } +#endif \ No newline at end of file diff --git a/Assets/Unheimlich_POC/Scripts/Features/DetectiveBoard/Runtime/DetectiveBoardController.cs b/Assets/Unheimlich_POC/Scripts/Features/DetectiveBoard/Runtime/DetectiveBoardController.cs index d2bcbab5..e1b6ee7f 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/DetectiveBoard/Runtime/DetectiveBoardController.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/DetectiveBoard/Runtime/DetectiveBoardController.cs @@ -107,7 +107,7 @@ namespace Unheimlich.Features.DetectiveBoard arms.HideArms(3f); } - InputManager.SetContext(InputManager.InputContext.Listening); + InputManager.SetContext(InputManager.InputContext.DetectiveBoard); if (_cameraHeadBob != null) { @@ -119,9 +119,6 @@ namespace Unheimlich.Features.DetectiveBoard CrosshairController.Instance.Hide(); } - Cursor.lockState = CursorLockMode.None; - Cursor.visible = true; - var cameraPan = PlayerController.Instance.CameraPanController; var originalSettings = cameraPan.Settings; @@ -200,9 +197,6 @@ namespace Unheimlich.Features.DetectiveBoard { CrosshairController.Instance.Show(); } - - Cursor.lockState = CursorLockMode.Locked; - Cursor.visible = false; } private void RestoreHeadBob() diff --git a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueFocusSystem.cs b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueFocusSystem.cs index b04f47cf..b91f1ce7 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueFocusSystem.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueFocusSystem.cs @@ -63,7 +63,7 @@ namespace Unheimlich.Features.Dialogue /// public void Update() { - if (!_manager.IsInDialogue || !_manager.CurrentDialogueUI.AreChoicesVisible()) + if (!_manager.IsDialogueActive || !_manager.CurrentDialogueUI.AreChoicesVisible()) return; _raycastTimer += Time.deltaTime; diff --git a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueManager.cs b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueManager.cs index cddae1f9..1fef9173 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueManager.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/Runtime/DialogueManager.cs @@ -16,7 +16,7 @@ namespace Unheimlich.Features.Dialogue { [Header("State")] - public bool IsInDialogue = false; + [SerializeField] private bool IsInDialogue = false; [Header("Focus Settings")] [SerializeField] private LayerMask _choiceLayer; @@ -65,19 +65,7 @@ namespace Unheimlich.Features.Dialogue if (_focusSystem != null) { _mainCamera = Camera.main; - _focusSystem = new DialogueFocusSystem( - this, - _mainCamera, - PlayerController.Instance?.CameraEffects, - _choiceLayer, - _focusRange, - _focusTime, - _raycastInterval, - _recoveryDuration, - _focusRetentionTime, - _fallbackShakeIntensity - ); - _focusSystem.OnChoiceMade += (choice) => OnChoiceMade?.Invoke(choice); + InitFocusSystem(); // Also reset dialogue state in case we exited mid-dialogue IsInDialogue = false; @@ -89,7 +77,15 @@ namespace Unheimlich.Features.Dialogue private void Start() { _mainCamera = Camera.main; + InitFocusSystem(); + } + /// + /// Creates or recreates the DialogueFocusSystem with the current inspector settings. + /// Called from Start and OnEnabled (domain-reload-less re-entry). + /// + private void InitFocusSystem() + { _focusSystem = new DialogueFocusSystem( this, _mainCamera, diff --git a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/BillBoardUI.cs b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/BillBoardUI.cs index b8896b69..bc9b4071 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/BillBoardUI.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/BillBoardUI.cs @@ -10,14 +10,16 @@ namespace Unheimlich.Features.Dialogue.UI { private Transform _cameraTransform; - private void Start() + private void Awake() { - _cameraTransform = Camera.main.transform; + _cameraTransform = Camera.main != null ? Camera.main.transform : null; if (_cameraTransform == null) Debug.LogWarning($"BillboardUI on {gameObject.name}: Main camera not found!"); } + private void Start() { } + private void LateUpdate() { if (_cameraTransform != null) diff --git a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/DialogueUI.cs b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/DialogueUI.cs index ab78ad67..ba740516 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/DialogueUI.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Dialogue/UI/DialogueUI.cs @@ -48,11 +48,14 @@ namespace Unheimlich.Features.Dialogue.UI private int _fadeCanvasTweenId = -1; private int _rotationTweenId = -1; private List _floatingTweenIds = new List(); + private Camera _mainCamera; private static readonly Vector3 ViewportCenter = new Vector3(0.5f, 0.5f, 0f); private void Awake() { + _mainCamera = Camera.main; + if (_dialogueCanvasGroup == null && _dialoguePanel != null) { _dialogueCanvasGroup = _dialoguePanel.GetComponent(); @@ -366,7 +369,7 @@ namespace Unheimlich.Features.Dialogue.UI _sortTimer = 0f; - FloatingWordsSpawner.SortByDistance(_activeChoices, Camera.main); + FloatingWordsSpawner.SortByDistance(_activeChoices, _mainCamera); } } } \ No newline at end of file diff --git a/Assets/Unheimlich_POC/Scripts/Features/Diapason/Runtime/DiapasonListeningHandlerV2.cs b/Assets/Unheimlich_POC/Scripts/Features/Diapason/Runtime/DiapasonListeningHandlerV2.cs index c1a78c1f..1ed0c817 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Diapason/Runtime/DiapasonListeningHandlerV2.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Diapason/Runtime/DiapasonListeningHandlerV2.cs @@ -44,8 +44,6 @@ namespace Unheimlich.Features.Diapason _cameraHeadBob = GetComponentInChildren(); } - void OnDestroy() { } - void Update() { var diapasonAction = InputManager.GetDiapasonAction(); diff --git a/Assets/Unheimlich_POC/Scripts/Features/Items/Runtime/InspectableItem.cs b/Assets/Unheimlich_POC/Scripts/Features/Items/Runtime/InspectableItem.cs index 3893a48f..f594435a 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Items/Runtime/InspectableItem.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Items/Runtime/InspectableItem.cs @@ -101,11 +101,6 @@ namespace Unheimlich.Features.Items public float RotationSpeed => _rotationSpeed; - private void OnEnable() - { - LeanTween.init(); - } - private void OnDestroy() { LeanTween.cancel(gameObject); diff --git a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/Portal.cs b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/Portal.cs index 6297dfa6..07a4f01b 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/Portal.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/Portal.cs @@ -508,8 +508,7 @@ namespace Unheimlich.Features.Portals if (_viewTexture != null) _viewTexture.Release(); - // Use DefaultHDR for better shadow and lighting compatibility in URP - _viewTexture = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.DefaultHDR); + _viewTexture = new RenderTexture(Screen.width, Screen.height, PortalConstants.RENDER_TEXTURE_DEPTH, PortalConstants.RENDER_TEXTURE_FORMAT); _viewTexture.filterMode = FilterMode.Bilinear; _viewTexture.antiAliasing = 1; _viewTexture.Create(); diff --git a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalAudioProxy.cs b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalAudioProxy.cs index 2c5e0a89..9aaea038 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalAudioProxy.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalAudioProxy.cs @@ -31,6 +31,7 @@ namespace Unheimlich.Features.Portals private Portal _portal; private Dictionary _proxyMapping = new Dictionary(); private Dictionary _filterMapping = new Dictionary(); + private Camera _playerCamera; private GameObject _lastLoadedRoomInstance; private bool _needsRefresh; @@ -38,6 +39,7 @@ namespace Unheimlich.Features.Portals private void Awake() { _portal = GetComponent(); + _playerCamera = Camera.main; } private void OnEnable() @@ -123,7 +125,7 @@ namespace Unheimlich.Features.Portals /// private void UpdateProxies() { - Camera playerCam = Camera.main; + Camera playerCam = _playerCamera; if (playerCam == null) return; diff --git a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalRoomManager.cs b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalRoomManager.cs index baf82e74..2f42982e 100644 --- a/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalRoomManager.cs +++ b/Assets/Unheimlich_POC/Scripts/Features/Portals/Runtime/PortalRoomManager.cs @@ -32,7 +32,7 @@ namespace Unheimlich.Features.Portals /// /// Represents a room instance that has been loaded through a portal. - /// Caches room bounds for efficient distance calculations. + /// Caches room bounds and colliders for efficient distance/containment calculations. /// private class LoadedRoom { @@ -42,6 +42,7 @@ namespace Unheimlich.Features.Portals public Portal SourcePortal; public Bounds CachedBounds; public bool BoundsCalculated = false; + public Collider[] CachedColliders; } private void Start() @@ -184,7 +185,7 @@ namespace Unheimlich.Features.Portals /// /// Determines which room (if any) currently contains the player. - /// Uses collider bounds checks to detect room occupancy. + /// Uses cached collider bounds to detect room occupancy. /// private void UpdateCurrentPlayerRoom() { @@ -196,10 +197,12 @@ namespace Unheimlich.Features.Portals if (loadedRoom.RoomInstance == null) continue; - Collider[] roomColliders = loadedRoom.RoomInstance.GetComponentsInChildren(); + Collider[] roomColliders = loadedRoom.CachedColliders; + if (roomColliders == null) continue; + foreach (Collider col in roomColliders) { - if (col.bounds.Contains(_player.position)) + if (col != null && col.bounds.Contains(_player.position)) { _currentPlayerRoom = loadedRoom.RoomInstance; return; @@ -247,7 +250,8 @@ namespace Unheimlich.Features.Portals RoomInstance = roomInstance, RoomId = roomId, LoadPosition = portal.transform.position, - SourcePortal = portal + SourcePortal = portal, + CachedColliders = roomInstance.GetComponentsInChildren() }; _loadedRooms.Add(roomId, loadedRoom); diff --git a/Assets/Unheimlich_POC/Scripts/World/Camera/Runtime/DynamicShadowDistance.cs b/Assets/Unheimlich_POC/Scripts/World/Camera/Runtime/DynamicShadowDistance.cs index 1692ebb4..3fd2572f 100644 --- a/Assets/Unheimlich_POC/Scripts/World/Camera/Runtime/DynamicShadowDistance.cs +++ b/Assets/Unheimlich_POC/Scripts/World/Camera/Runtime/DynamicShadowDistance.cs @@ -26,9 +26,13 @@ namespace Unheimlich.World.CameraSystem private float _nextCheckTime; private Transform _playerTransform; + private void Awake() + { + _playerTransform = Camera.main != null ? Camera.main.transform : null; + } + private void Start() { - _playerTransform = Camera.main.transform; _currentShadowDistance = _baseShadowDistance; _targetShadowDistance = _baseShadowDistance; QualitySettings.shadowDistance = _baseShadowDistance;