diff --git a/CHANGELOG.md b/CHANGELOG.md index 57d57b8..2fcae7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > **Versioning note:** versions use the four-part `MAJOR.MINOR.PATCH.BUILD` form that KSP/Unity DLLs expect. The first three parts follow SemVer; the fourth part (`BUILD`) is repurposed here to mark pre-release / beta iterations of an upcoming version. +## Unreleased + +### Fixed + +- Sunflare (Scatterer) - camera feeds show the sun flare again after loading into flight, and it no longer disappears when the main camera looks away from the sun + + ## v3.4.0 - 2026-11-27 ### Added diff --git a/Source/HullCameraRenderer.cs b/Source/HullCameraRenderer.cs index 716711e..0bc98c0 100644 --- a/Source/HullCameraRenderer.cs +++ b/Source/HullCameraRenderer.cs @@ -140,6 +140,14 @@ private void CreateCameras() SetupFarPqsCamera(); SetupScaledCamera(); SetupGalaxyCamera(); + LinkScattererScaledCamera(); + } + + private void LinkScattererScaledCamera() + { + var swap = _cameras[NearCameraIndex]?.GetComponent(); + if (swap != null) + swap.ScaledCamera = _cameras[ScaledCameraIndex]; } public void RebuildCameras() diff --git a/Source/ScattererCameraSwap.cs b/Source/ScattererCameraSwap.cs index 27997da..f43cc61 100644 --- a/Source/ScattererCameraSwap.cs +++ b/Source/ScattererCameraSwap.cs @@ -9,6 +9,8 @@ public class ScattererCameraSwap : MonoBehaviour { private Camera _camera; + public Camera ScaledCamera { get; set; } + private static bool _initialized; private static object _scattererInstance; private static FieldInfo _nearCameraField; @@ -103,10 +105,14 @@ void OnPreCull() return; _nearCameraField.SetValue(_scattererInstance, _camera); + ScattererScaledCameraSwap.PointScattererAt(ScaledCamera); + ScattererIntegration.SyncHooks(_mainCamera, _camera); } void OnPostRender() { + ScattererScaledCameraSwap.RestoreMainCamera(); + if (!_initialized || !IsAlive(_scattererInstance) || _nearCameraField == null) return; diff --git a/Source/ScattererIntegration.cs b/Source/ScattererIntegration.cs index 481897c..3f10779 100644 --- a/Source/ScattererIntegration.cs +++ b/Source/ScattererIntegration.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using UnityEngine; @@ -7,8 +8,13 @@ namespace JustReadTheInstructions { public static class ScattererIntegration { + private const string MainDepthFlagField = "useDbufferOnCamera"; + private static bool? _isAvailable; - private static Type[] _scattererMonoBehaviourTypes; + private static Type[] _hookTypes; + private static readonly Dictionary _hookSources = new Dictionary(); + private static readonly List _sourceHooks = new List(); + private static readonly List _targetHooks = new List(); private static readonly string[] _hookTypeNames = { @@ -43,8 +49,8 @@ public static bool IsAvailable return false; } - _scattererMonoBehaviourTypes = loaded.assembly.GetTypes() - .Where(t => !t.IsAbstract && typeof(MonoBehaviour).IsAssignableFrom(t)) + _hookTypes = loaded.assembly.GetTypes() + .Where(t => !t.IsAbstract && typeof(MonoBehaviour).IsAssignableFrom(t) && IsClonableHook(t)) .ToArray(); _isAvailable = true; @@ -73,7 +79,7 @@ public static void ApplyToCamera(Camera camera) return; } - CopyCameraRenderingHooks(mainCam, camera); + SyncHooks(mainCam, camera); } public static void ApplyToScaledCamera(Camera camera) @@ -87,55 +93,74 @@ public static void ApplyToScaledCamera(Camera camera) return; } - CopyCameraRenderingHooks(scaledCam, camera); + SyncHooks(scaledCam, camera); } public static void RemoveFromCamera(Camera camera) { - if (!IsAvailable || camera == null || _scattererMonoBehaviourTypes == null) + if (!IsAvailable || camera == null || _hookTypes == null) return; - foreach (var hookType in _scattererMonoBehaviourTypes) + foreach (var hookType in _hookTypes) { - if (!IsClonableHook(hookType)) - continue; - - var hook = camera.gameObject.GetComponent(hookType); - if (hook != null) + camera.GetComponents(hookType, _targetHooks); + foreach (var hook in _targetHooks) + { + _hookSources.Remove(hook.GetInstanceID()); UnityEngine.Object.Destroy(hook); + } } } - private static void CopyCameraRenderingHooks(Camera source, Camera target) + public static void SyncHooks(Camera source, Camera target) { - if (_scattererMonoBehaviourTypes == null) + if (!JRTISettings.EnableScatterer || _hookTypes == null || source == null || target == null) return; - foreach (var hookType in _scattererMonoBehaviourTypes) + foreach (var hookType in _hookTypes) { - if (!IsClonableHook(hookType)) - continue; + source.GetComponents(hookType, _sourceHooks); + target.GetComponents(hookType, _targetHooks); - if (target.gameObject.GetComponent(hookType) != null) - continue; - - var sourceHook = source.gameObject.GetComponent(hookType); - if (sourceHook == null) - continue; - - try + for (int i = 0; i < _sourceHooks.Count; i++) { - var newHook = target.gameObject.AddComponent(hookType); - if (newHook != null) - { - CopyComponentFields(sourceHook, newHook); - Debug.Log($"[JRTI-Scatterer]: Added {hookType.Name} to {target.name}"); - } - } - catch (Exception ex) - { - Debug.LogWarning($"[JRTI-Scatterer]: Failed to add {hookType.Name}: {ex.Message}"); + var sourceHook = (Behaviour)_sourceHooks[i]; + var targetHook = i < _targetHooks.Count ? (Behaviour)_targetHooks[i] : AddHook(target, hookType); + if (targetHook != null) + Mirror(sourceHook, targetHook); } + + for (int i = _sourceHooks.Count; i < _targetHooks.Count; i++) + ((Behaviour)_targetHooks[i]).enabled = false; + } + } + + private static void Mirror(Behaviour sourceHook, Behaviour targetHook) + { + int targetId = targetHook.GetInstanceID(); + int sourceId = sourceHook.GetInstanceID(); + if (!_hookSources.TryGetValue(targetId, out var copiedFrom) || copiedFrom != sourceId) + { + CopyComponentFields(sourceHook, targetHook); + targetHook.GetType().GetField(MainDepthFlagField)?.SetValue(targetHook, 0f); + _hookSources[targetId] = sourceId; + } + + targetHook.enabled = sourceHook.enabled; + } + + private static Behaviour AddHook(Camera target, Type hookType) + { + try + { + var hook = target.gameObject.AddComponent(hookType) as Behaviour; + Debug.Log($"[JRTI-Scatterer]: Added {hookType.Name} to {target.name}"); + return hook; + } + catch (Exception ex) + { + Debug.LogWarning($"[JRTI-Scatterer]: Failed to add {hookType.Name}: {ex.Message}"); + return null; } } diff --git a/Source/ScattererScaledCameraSwap.cs b/Source/ScattererScaledCameraSwap.cs index 55ff948..bcba59e 100644 --- a/Source/ScattererScaledCameraSwap.cs +++ b/Source/ScattererScaledCameraSwap.cs @@ -121,17 +121,16 @@ private static bool IsAlive(object value) : unityObject != null; } - void OnPreCull() + internal static bool PointScattererAt(Camera camera) { - if (!EnsureInitialized() || _scaledCameraField == null) - return; + if (camera == null || !EnsureInitialized() || _scaledCameraField == null) + return false; - _scaledCameraField.SetValue(_scattererInstance, _camera); - ScattererScaledAtmosphere.EnableScreenCopy(_camera); - ScattererScaledAtmosphere.AttachToReflectionProbe(); + _scaledCameraField.SetValue(_scattererInstance, camera); + return true; } - void OnPostRender() + internal static void RestoreMainCamera() { if (!_initialized || !IsAlive(_scattererInstance) || _scaledCameraField == null) return; @@ -139,5 +138,20 @@ void OnPostRender() if (_mainScaledCamera != null) _scaledCameraField.SetValue(_scattererInstance, _mainScaledCamera); } + + void OnPreCull() + { + if (!PointScattererAt(_camera)) + return; + + ScattererIntegration.SyncHooks(_mainScaledCamera, _camera); + ScattererScaledAtmosphere.EnableScreenCopy(_camera); + ScattererScaledAtmosphere.AttachToReflectionProbe(); + } + + void OnPostRender() + { + RestoreMainCamera(); + } } }