From d58fb0c179f339cc4321c13efd7b22a64b86874a Mon Sep 17 00:00:00 2001 From: RELMYMathieu Date: Thu, 26 Nov 2026 23:44:55 +0100 Subject: [PATCH 1/7] fix: Scatterer camera atmosphere swap --- CHANGELOG.md | 4 ++ Source/ScattererScaledAtmosphere.cs | 90 +++++++++++++++++++++++++++++ Source/ScattererScaledCameraSwap.cs | 8 +++ 3 files changed, 102 insertions(+) create mode 100644 Source/ScattererScaledAtmosphere.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index cd43dcf..064cb2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Web API (`/api/v1`) - This allows you to make your own apps, pages and overlays on the same JRTI feeds as the default proposed interface, with an optional control key for other devices if need be. Pages from other websites can no longer change anything, as a security measure. See the API page of the docs for more information - Your own pages and styles (`PluginData/Apps/`, `PluginData/Web/`) - you can now make your own pages and styles, kept across updates +### Fixed + +- Atmosphere seen from orbit (Scatterer) - feeds no longer cut the atmosphere off along a line that follows where the main view looks, at least from what my testing has shown... Answers issue [#39](https://github.com/RELMYMathieu/JustReadTheInstructions/issues/39) + ## v3.3.0 - 2026-11-11 ### Added diff --git a/Source/ScattererScaledAtmosphere.cs b/Source/ScattererScaledAtmosphere.cs new file mode 100644 index 0000000..a16ec1f --- /dev/null +++ b/Source/ScattererScaledAtmosphere.cs @@ -0,0 +1,90 @@ +using System; +using System.Reflection; +using UnityEngine; + +namespace JustReadTheInstructions +{ + internal static class ScattererScaledAtmosphere + { + private static bool _resolved; + private static Type _skyNodeType; + private static FieldInfo _materialField; + private static FieldInfo _containerField; + private static FieldInfo _parentScaledTransformField; + private static FieldInfo _rtField; + private static MethodInfo _updateScreenBoundsMethod; + + private static readonly int CameraToWorldId = Shader.PropertyToID("CameraToWorld"); + private static readonly int CurrentVpId = Shader.PropertyToID("currentVP"); + private static readonly object[] BoundsArgs = new object[5]; + + public static void Bind(Camera feedCamera, Camera mainScaledCamera, Assembly scatterer) + { + if (feedCamera == null || mainScaledCamera == null || !Resolve(scatterer)) + return; + + try + { + foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) + BindSkyNode(skyNode, feedCamera); + } + catch (Exception ex) + { + Debug.LogError($"[JRTI-ScaledSwap]: Scaled atmosphere bind failed, disabled: {ex.Message}"); + _skyNodeType = null; + } + } + + private static void BindSkyNode(Component skyNode, Camera camera) + { + var material = _materialField.GetValue(skyNode) as Material; + var container = _containerField.GetValue(skyNode); + var parent = _parentScaledTransformField.GetValue(skyNode) as Transform; + if (material == null || container == null || parent == null) + return; + + var projection = camera.nonJitteredProjectionMatrix; + var view = camera.worldToCameraMatrix; + + var cameraToWorld = camera.cameraToWorldMatrix; + cameraToWorld.SetColumn(2, -cameraToWorld.GetColumn(2)); + material.SetMatrix(CameraToWorldId, cameraToWorld); + material.SetMatrix(CurrentVpId, GL.GetGPUProjectionMatrix(projection, false) * view); + + BoundsArgs[0] = camera; + BoundsArgs[1] = projection; + BoundsArgs[2] = view; + BoundsArgs[3] = parent.position; + BoundsArgs[4] = (float)_rtField.GetValue(skyNode) / ScaledSpace.ScaleFactor; + _updateScreenBoundsMethod.Invoke(container, BoundsArgs); + } + + private static bool Resolve(Assembly scatterer) + { + if (_resolved) + return _skyNodeType != null; + + _resolved = true; + var skyNodeType = scatterer?.GetType("Scatterer.SkyNode"); + if (skyNodeType == null) + return false; + + const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; + _materialField = skyNodeType.GetField("scaledDepthBufferScatteringMaterial", flags); + _containerField = skyNodeType.GetField("scaledDepthBufferScatteringContainer", flags); + _parentScaledTransformField = skyNodeType.GetField("parentScaledTransform", flags); + _rtField = skyNodeType.GetField("Rt", flags); + _updateScreenBoundsMethod = _containerField?.FieldType.GetMethod("UpdateScreenBounds", flags); + + if (_materialField == null || _parentScaledTransformField == null || _rtField == null || _updateScreenBoundsMethod == null) + { + Debug.Log("[JRTI-ScaledSwap]: Scatterer has no scaled depth-buffer atmosphere - nothing to bind"); + return false; + } + + _skyNodeType = skyNodeType; + Debug.Log("[JRTI-ScaledSwap]: Scaled atmosphere follows feed cameras"); + return true; + } + } +} diff --git a/Source/ScattererScaledCameraSwap.cs b/Source/ScattererScaledCameraSwap.cs index f674d16..26499c0 100644 --- a/Source/ScattererScaledCameraSwap.cs +++ b/Source/ScattererScaledCameraSwap.cs @@ -13,6 +13,7 @@ public class ScattererScaledCameraSwap : MonoBehaviour private static object _scattererInstance; private static FieldInfo _scaledCameraField; private static Camera _mainScaledCamera; + private static Assembly _scattererAssembly; private static readonly string[] _candidateFieldNames = { @@ -51,6 +52,7 @@ private static bool Initialize(Camera scaledCamera) if (assembly == null) return false; + _scattererAssembly = assembly; var scattererType = assembly.GetType("Scatterer.Scatterer"); if (scattererType == null) { @@ -123,6 +125,12 @@ void OnPreCull() _scaledCameraField.SetValue(_scattererInstance, _camera); } + void OnPreRender() + { + if (_initialized) + ScattererScaledAtmosphere.Bind(_camera, _mainScaledCamera, _scattererAssembly); + } + void OnPostRender() { if (!_initialized || !IsAlive(_scattererInstance) || _scaledCameraField == null) From 8cd4a47667c6e3b01a802244fd7482815105df84 Mon Sep 17 00:00:00 2001 From: github-actions Date: Thu, 26 Nov 2026 23:59:00 +0000 Subject: [PATCH 2/7] bump version to v3.3.0.1 --- CHANGELOG.md | 3 ++- .../JustReadTheInstructions.version | 2 +- GameData/JustReadTheInstructions/changelog.cfg | 16 ++++++++++++++++ Source/JustReadTheInstructions.csproj | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 064cb2d..fa52d88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ 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 +## v3.3.0.1 - 2026-11-26 ### Added @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Atmosphere seen from orbit (Scatterer) - feeds no longer cut the atmosphere off along a line that follows where the main view looks, at least from what my testing has shown... Answers issue [#39](https://github.com/RELMYMathieu/JustReadTheInstructions/issues/39) + ## v3.3.0 - 2026-11-11 ### Added diff --git a/GameData/JustReadTheInstructions/JustReadTheInstructions.version b/GameData/JustReadTheInstructions/JustReadTheInstructions.version index 10f8cee..38692c7 100644 --- a/GameData/JustReadTheInstructions/JustReadTheInstructions.version +++ b/GameData/JustReadTheInstructions/JustReadTheInstructions.version @@ -1,6 +1,6 @@ { "NAME": "Just Read The Instructions", - "VERSION": "3.3.0.0", + "VERSION": "3.3.0.1", "URL": "https://raw.githubusercontent.com/RELMYMathieu/JustReadTheInstructions/main/GameData/JustReadTheInstructions/JustReadTheInstructions.version", "DOWNLOAD": "https://github.com/RELMYMathieu/JustReadTheInstructions/releases/latest", "KSP_VERSION": "1.12.5", diff --git a/GameData/JustReadTheInstructions/changelog.cfg b/GameData/JustReadTheInstructions/changelog.cfg index 9ae1593..4a1f226 100644 --- a/GameData/JustReadTheInstructions/changelog.cfg +++ b/GameData/JustReadTheInstructions/changelog.cfg @@ -5,6 +5,22 @@ KERBALCHANGELOG license = MIT author = Mathieu Relmy + VERSION + { + version = 3.3.0.1 + CHANGE + { + change = Added + subchange = Web API (/api/v1) - This allows you to make your own apps, pages and overlays on the same JRTI feeds as the default proposed interface, with an optional control key for other devices if need be. Pages from other websites can no longer change anything, as a security measure. See the API page of the docs for more information + subchange = Your own pages and styles (PluginData/Apps/, PluginData/Web/) - you can now make your own pages and styles, kept across updates + } + CHANGE + { + change = Fixed + subchange = Atmosphere seen from orbit (Scatterer) - feeds no longer cut the atmosphere off along a line that follows where the main view looks, at least from what my testing has shown... Answers issue #39 + } + } + VERSION { version = 3.3.0 diff --git a/Source/JustReadTheInstructions.csproj b/Source/JustReadTheInstructions.csproj index dbc7a5d..033d7c4 100644 --- a/Source/JustReadTheInstructions.csproj +++ b/Source/JustReadTheInstructions.csproj @@ -2,7 +2,7 @@ net48 - 3.3.0.0 + 3.3.0.1 KSP Mod, inspired by OCISLY, as a spiritual successor. MIT $(MSBuildThisFileDirectory)../GameData/JustReadTheInstructions From a7d4b39774688bae1dfeaa9346ee151f7ff5203a Mon Sep 17 00:00:00 2001 From: RELMYMathieu Date: Fri, 27 Nov 2026 01:17:55 +0100 Subject: [PATCH 3/7] fix: bind the depth copy on pre-cull to avoid lighting flicker --- Source/ScattererScaledAtmosphere.cs | 6 +++--- Source/ScattererScaledCameraSwap.cs | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/ScattererScaledAtmosphere.cs b/Source/ScattererScaledAtmosphere.cs index a16ec1f..f18d0cc 100644 --- a/Source/ScattererScaledAtmosphere.cs +++ b/Source/ScattererScaledAtmosphere.cs @@ -18,15 +18,15 @@ internal static class ScattererScaledAtmosphere private static readonly int CurrentVpId = Shader.PropertyToID("currentVP"); private static readonly object[] BoundsArgs = new object[5]; - public static void Bind(Camera feedCamera, Camera mainScaledCamera, Assembly scatterer) + public static void Bind(Camera viewer, Camera mainScaledCamera, Assembly scatterer) { - if (feedCamera == null || mainScaledCamera == null || !Resolve(scatterer)) + if (viewer == null || mainScaledCamera == null || !Resolve(scatterer)) return; try { foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) - BindSkyNode(skyNode, feedCamera); + BindSkyNode(skyNode, viewer); } catch (Exception ex) { diff --git a/Source/ScattererScaledCameraSwap.cs b/Source/ScattererScaledCameraSwap.cs index 26499c0..c236532 100644 --- a/Source/ScattererScaledCameraSwap.cs +++ b/Source/ScattererScaledCameraSwap.cs @@ -123,6 +123,7 @@ void OnPreCull() return; _scaledCameraField.SetValue(_scattererInstance, _camera); + _camera.depthTextureMode |= _mainScaledCamera.depthTextureMode & DepthTextureMode.Depth; } void OnPreRender() @@ -136,8 +137,11 @@ void OnPostRender() if (!_initialized || !IsAlive(_scattererInstance) || _scaledCameraField == null) return; - if (_mainScaledCamera != null) - _scaledCameraField.SetValue(_scattererInstance, _mainScaledCamera); + if (_mainScaledCamera == null) + return; + + _scaledCameraField.SetValue(_scattererInstance, _mainScaledCamera); + ScattererScaledAtmosphere.Bind(_mainScaledCamera, _mainScaledCamera, _scattererAssembly); } } } From d72ea95601f465e5567a655518bf1ea4a9a03e2b Mon Sep 17 00:00:00 2001 From: RELMY Mathieu <60399897+RELMYMathieu@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:21:29 +0100 Subject: [PATCH 4/7] chore: bump CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa52d88..2989c77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ 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 + +- Fix the timing at which the JRTI cameras copy depth buffer to avoid lighting artifacts. + ## v3.3.0.1 - 2026-11-26 ### Added From 9d03f656f3bb4ee2735baf9d7e4cc66f5f2d7266 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 27 Nov 2026 00:22:46 +0000 Subject: [PATCH 5/7] bump version to v3.3.0.2 --- CHANGELOG.md | 3 ++- .../JustReadTheInstructions.version | 2 +- GameData/JustReadTheInstructions/changelog.cfg | 10 ++++++++++ Source/JustReadTheInstructions.csproj | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2989c77..48f4817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +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 +## v3.3.0.2 - 2026-11-27 ### Fixed - Fix the timing at which the JRTI cameras copy depth buffer to avoid lighting artifacts. + ## v3.3.0.1 - 2026-11-26 ### Added diff --git a/GameData/JustReadTheInstructions/JustReadTheInstructions.version b/GameData/JustReadTheInstructions/JustReadTheInstructions.version index 38692c7..239e239 100644 --- a/GameData/JustReadTheInstructions/JustReadTheInstructions.version +++ b/GameData/JustReadTheInstructions/JustReadTheInstructions.version @@ -1,6 +1,6 @@ { "NAME": "Just Read The Instructions", - "VERSION": "3.3.0.1", + "VERSION": "3.3.0.2", "URL": "https://raw.githubusercontent.com/RELMYMathieu/JustReadTheInstructions/main/GameData/JustReadTheInstructions/JustReadTheInstructions.version", "DOWNLOAD": "https://github.com/RELMYMathieu/JustReadTheInstructions/releases/latest", "KSP_VERSION": "1.12.5", diff --git a/GameData/JustReadTheInstructions/changelog.cfg b/GameData/JustReadTheInstructions/changelog.cfg index 4a1f226..3022202 100644 --- a/GameData/JustReadTheInstructions/changelog.cfg +++ b/GameData/JustReadTheInstructions/changelog.cfg @@ -5,6 +5,16 @@ KERBALCHANGELOG license = MIT author = Mathieu Relmy + VERSION + { + version = 3.3.0.2 + CHANGE + { + change = Fixed + subchange = Fix the timing at which the JRTI cameras copy depth buffer to avoid lighting artifacts. + } + } + VERSION { version = 3.3.0.1 diff --git a/Source/JustReadTheInstructions.csproj b/Source/JustReadTheInstructions.csproj index 033d7c4..a247568 100644 --- a/Source/JustReadTheInstructions.csproj +++ b/Source/JustReadTheInstructions.csproj @@ -2,7 +2,7 @@ net48 - 3.3.0.1 + 3.3.0.2 KSP Mod, inspired by OCISLY, as a spiritual successor. MIT $(MSBuildThisFileDirectory)../GameData/JustReadTheInstructions From ce2188bbb079c4cba45cf89863203e1117055ab9 Mon Sep 17 00:00:00 2001 From: RELMYMathieu Date: Fri, 27 Nov 2026 08:12:06 +0100 Subject: [PATCH 6/7] feat: deeper debug for eve atmo effects --- Source/EVEIntegration.cs | 14 ++++ Source/HullCameraManager.cs | 2 + Source/JRTISettingsGUI.cs | 14 ++++ Source/ScattererAtmosphereBinding.cs | 18 +++++ Source/ScattererIntegration.cs | 15 +++- Source/ScattererScaledAtmosphere.cs | 112 +++++++++++++++++++++++++-- Source/ScattererScaledCameraSwap.cs | 19 ++--- 7 files changed, 177 insertions(+), 17 deletions(-) create mode 100644 Source/ScattererAtmosphereBinding.cs diff --git a/Source/EVEIntegration.cs b/Source/EVEIntegration.cs index 6e1fca3..80e17c8 100644 --- a/Source/EVEIntegration.cs +++ b/Source/EVEIntegration.cs @@ -304,6 +304,20 @@ private static void RemoveComponentIfExists(Camera camera, Type componentType) UnityEngine.Object.Destroy(component); } + public static string GetLightDiagnosticInfo() + { + var info = "Light command buffers:\n"; + foreach (var light in UnityEngine.Object.FindObjectsOfType()) + { + var buffers = ((LightEvent[])Enum.GetValues(typeof(LightEvent))) + .SelectMany(e => light.GetCommandBuffers(e).Select(b => $"{e}:{b.name}")) + .ToArray(); + if (buffers.Length > 0) + info += $"- {light.name}: {string.Join(", ", buffers)}\n"; + } + return info; + } + public static string GetDiagnosticInfo(Camera camera) { if (!IsAvailable || camera == null) diff --git a/Source/HullCameraManager.cs b/Source/HullCameraManager.cs index db85dd3..d9daf11 100644 --- a/Source/HullCameraManager.cs +++ b/Source/HullCameraManager.cs @@ -424,6 +424,8 @@ public bool IsStreamOnly(MuMechModuleHullCamera hullCamera) public int GetOpenCameraCount() => _renderers.Count; + public IEnumerable GetCameraDiagnostics() => _renderers.Values.Select(r => r.GetDiagnosticInfo()); + public bool HasWindow(int stableId) => _windows.ContainsKey(stableId); public bool TryGetListener(int stableId, out Transform view, out Vessel vessel) diff --git a/Source/JRTISettingsGUI.cs b/Source/JRTISettingsGUI.cs index 769e1c2..60ff334 100644 --- a/Source/JRTISettingsGUI.cs +++ b/Source/JRTISettingsGUI.cs @@ -1,5 +1,6 @@ using KSP.UI.Screens; using System.Globalization; +using System.Linq; using UnityEngine; namespace JustReadTheInstructions @@ -572,6 +573,19 @@ private void PrintDiagnostics() if (ParallaxIntegration.IsAvailable) Debug.Log($"[JRTI-Diag]: Parallax scatters active: {ParallaxIntegration.HasActiveScatters()}"); Debug.Log($"[JRTI-Diag]: Open cameras: {HullCameraManager.Instance?.GetOpenCameraCount() ?? 0}"); + Debug.Log($"[JRTI-Diag]: {EVEIntegration.GetLightDiagnosticInfo()}"); + var mainNear = CameraLookup.FindActive("Camera 00"); + if (mainNear != null) + Debug.Log($"[JRTI-Diag]: Main near camera\n{EVEIntegration.GetDiagnosticInfo(mainNear)}\n{ScattererIntegration.GetDiagnosticInfo(mainNear)}"); + if (ScattererIntegration.IsAvailable) + { + var mainScaled = ScattererScaledCameraSwap.MainScaledCamera; + if (mainScaled != null) + Debug.Log($"[JRTI-Diag]: Main scaled camera\n{ScattererIntegration.GetDiagnosticInfo(mainScaled)}"); + Debug.Log($"[JRTI-Diag]: {ScattererScaledAtmosphere.GetDiagnosticInfo()}"); + } + foreach (var info in HullCameraManager.Instance?.GetCameraDiagnostics() ?? Enumerable.Empty()) + Debug.Log($"[JRTI-Diag]: {info}"); Debug.Log("[JRTI-Diag]: ======================="); } diff --git a/Source/ScattererAtmosphereBinding.cs b/Source/ScattererAtmosphereBinding.cs new file mode 100644 index 0000000..60707e5 --- /dev/null +++ b/Source/ScattererAtmosphereBinding.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace JustReadTheInstructions +{ + public class ScattererAtmosphereBinding : MonoBehaviour + { + private Camera _camera; + + void Awake() + { + _camera = GetComponent(); + } + + void OnPreRender() => ScattererScaledAtmosphere.Bind(_camera); + + void OnPostRender() => ScattererScaledAtmosphere.RestoreMain(); + } +} diff --git a/Source/ScattererIntegration.cs b/Source/ScattererIntegration.cs index 481897c..0ef96db 100644 --- a/Source/ScattererIntegration.cs +++ b/Source/ScattererIntegration.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Reflection; using UnityEngine; +using UnityEngine.Rendering; namespace JustReadTheInstructions { @@ -167,7 +168,19 @@ public static string GetDiagnosticInfo(Camera camera) return "Scatterer not available\n"; return $"Scatterer for {camera.name}:\n" + - $"- allowMSAA: {camera.allowMSAA}\n"; + $"- allowMSAA: {camera.allowMSAA}, allowHDR: {camera.allowHDR}\n" + + $"- depthTextureMode: {camera.depthTextureMode}, path: {camera.actualRenderingPath}, clear: {camera.clearFlags}, mask: {camera.cullingMask}\n" + + $"- command buffers: {DescribeCommandBuffers(camera)}\n" + + $"- components: {string.Join(", ", camera.GetComponents().Select(c => c.GetType().Name))}\n" + + ScattererScaledAtmosphere.DescribeQuadVisibility(camera); + } + + private static string DescribeCommandBuffers(Camera camera) + { + var buffers = ((CameraEvent[])Enum.GetValues(typeof(CameraEvent))) + .SelectMany(e => camera.GetCommandBuffers(e).Select(b => $"{e}:{b.name}")) + .ToArray(); + return buffers.Length == 0 ? "none" : string.Join(", ", buffers); } } } \ No newline at end of file diff --git a/Source/ScattererScaledAtmosphere.cs b/Source/ScattererScaledAtmosphere.cs index f18d0cc..a21cc0f 100644 --- a/Source/ScattererScaledAtmosphere.cs +++ b/Source/ScattererScaledAtmosphere.cs @@ -6,21 +6,32 @@ namespace JustReadTheInstructions { internal static class ScattererScaledAtmosphere { + private const string ReflectionProbeScaledCameraName = "ScattererReflectionProbeScaledSpaceCamera"; + private const float ReflectionProbeSearchInterval = 1f; + private static bool _resolved; private static Type _skyNodeType; private static FieldInfo _materialField; private static FieldInfo _containerField; private static FieldInfo _parentScaledTransformField; private static FieldInfo _rtField; + private static FieldInfo _inScaledSpaceField; private static MethodInfo _updateScreenBoundsMethod; + private static MethodInfo _enableScreenCopyMethod; + private static readonly object[] ScreenCopyArgs = new object[1]; + + private static GameObject _reflectionProbeScaledCamera; + private static float _nextReflectionProbeSearch; private static readonly int CameraToWorldId = Shader.PropertyToID("CameraToWorld"); private static readonly int CurrentVpId = Shader.PropertyToID("currentVP"); + private static readonly int ScreenBoundsId = Shader.PropertyToID("_scaledAtmosphereScreenBounds"); private static readonly object[] BoundsArgs = new object[5]; - public static void Bind(Camera viewer, Camera mainScaledCamera, Assembly scatterer) + public static void Bind(Camera viewer) { - if (viewer == null || mainScaledCamera == null || !Resolve(scatterer)) + var mainScaledCamera = ScattererScaledCameraSwap.MainScaledCamera; + if (viewer == null || mainScaledCamera == null || !Resolve()) return; try @@ -35,6 +46,90 @@ public static void Bind(Camera viewer, Camera mainScaledCamera, Assembly scatter } } + public static void RestoreMain() => Bind(ScattererScaledCameraSwap.MainScaledCamera); + + public static void EnableScreenCopy(Camera camera) + { + if (camera == null || !Resolve() || _enableScreenCopyMethod == null) + return; + + try + { + ScreenCopyArgs[0] = camera; + _enableScreenCopyMethod.Invoke(null, ScreenCopyArgs); + } + catch (Exception ex) + { + Debug.LogError($"[JRTI-ScaledSwap]: Screen copy failed, disabled: {ex.Message}"); + _enableScreenCopyMethod = null; + } + } + + public static void AttachToReflectionProbe() + { + if (_reflectionProbeScaledCamera != null || Time.unscaledTime < _nextReflectionProbeSearch) + return; + + _nextReflectionProbeSearch = Time.unscaledTime + ReflectionProbeSearchInterval; + _reflectionProbeScaledCamera = GameObject.Find(ReflectionProbeScaledCameraName); + if (_reflectionProbeScaledCamera == null || _reflectionProbeScaledCamera.GetComponent() != null) + return; + + _reflectionProbeScaledCamera.AddComponent(); + Debug.Log("[JRTI-ScaledSwap]: Scaled atmosphere follows the reflection probe camera"); + } + + public static string GetDiagnosticInfo() + { + var mainScaledCamera = ScattererScaledCameraSwap.MainScaledCamera; + if (mainScaledCamera == null || !Resolve()) + return "Scatterer scaled atmosphere: not bound\n"; + + var info = $"Scatterer scaled atmosphere (reflection probe camera {(_reflectionProbeScaledCamera != null ? "bound" : "not found")}):\n"; + foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) + { + var material = _materialField.GetValue(skyNode) as Material; + var container = _containerField.GetValue(skyNode); + info += $"- {skyNode.name}: inScaledSpace={_inScaledSpaceField?.GetValue(skyNode)}, " + + $"container={(container != null ? "yes" : "no")}, " + + $"bounds={(material != null ? material.GetVector(ScreenBoundsId).ToString("F3") : "no material")}\n"; + + var quad = FindQuad(skyNode); + info += quad == null + ? " quad: not found\n" + : $" quad: enabled={quad.enabled}, active={quad.gameObject.activeInHierarchy}, layer={quad.gameObject.layer}, " + + $"isVisible={quad.isVisible}, worldBounds={quad.bounds.center:F1}/{quad.bounds.extents:F1}, parentRot={quad.transform.parent.rotation.eulerAngles:F2}\n"; + } + return info; + } + + public static string DescribeQuadVisibility(Camera camera) + { + var mainScaledCamera = ScattererScaledCameraSwap.MainScaledCamera; + if (camera == null || mainScaledCamera == null || !Resolve()) + return ""; + + var info = ""; + var planes = GeometryUtility.CalculateFrustumPlanes(camera); + foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) + { + var quad = FindQuad(skyNode); + if (quad == null) + continue; + + int layer = quad.gameObject.layer; + info += $"- sees {skyNode.name} quad: inMask={(camera.cullingMask & (1 << layer)) != 0}, " + + $"layerCull={camera.layerCullDistances[layer]}, frustum={GeometryUtility.TestPlanesAABB(planes, quad.bounds)}\n"; + } + return info; + } + + private static Renderer FindQuad(Component skyNode) + { + var parent = _parentScaledTransformField.GetValue(skyNode) as Transform; + return parent?.Find("Scatterer scaled depth-buffer atmo")?.GetComponent(); + } + private static void BindSkyNode(Component skyNode, Camera camera) { var material = _materialField.GetValue(skyNode) as Material; @@ -46,7 +141,7 @@ private static void BindSkyNode(Component skyNode, Camera camera) var projection = camera.nonJitteredProjectionMatrix; var view = camera.worldToCameraMatrix; - var cameraToWorld = camera.cameraToWorldMatrix; + var cameraToWorld = view.inverse; cameraToWorld.SetColumn(2, -cameraToWorld.GetColumn(2)); material.SetMatrix(CameraToWorldId, cameraToWorld); material.SetMatrix(CurrentVpId, GL.GetGPUProjectionMatrix(projection, false) * view); @@ -59,13 +154,17 @@ private static void BindSkyNode(Component skyNode, Camera camera) _updateScreenBoundsMethod.Invoke(container, BoundsArgs); } - private static bool Resolve(Assembly scatterer) + private static bool Resolve() { if (_resolved) return _skyNodeType != null; + var scatterer = ScattererScaledCameraSwap.ScattererAssembly; + if (scatterer == null) + return false; + _resolved = true; - var skyNodeType = scatterer?.GetType("Scatterer.SkyNode"); + var skyNodeType = scatterer.GetType("Scatterer.SkyNode"); if (skyNodeType == null) return false; @@ -74,7 +173,10 @@ private static bool Resolve(Assembly scatterer) _containerField = skyNodeType.GetField("scaledDepthBufferScatteringContainer", flags); _parentScaledTransformField = skyNodeType.GetField("parentScaledTransform", flags); _rtField = skyNodeType.GetField("Rt", flags); + _inScaledSpaceField = skyNodeType.GetField("inScaledSpace", flags); _updateScreenBoundsMethod = _containerField?.FieldType.GetMethod("UpdateScreenBounds", flags); + _enableScreenCopyMethod = scatterer.GetType("Scatterer.ScreenCopyCommandBuffer") + ?.GetMethod("EnableScreenCopyForFrame", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(Camera) }, null); if (_materialField == null || _parentScaledTransformField == null || _rtField == null || _updateScreenBoundsMethod == null) { diff --git a/Source/ScattererScaledCameraSwap.cs b/Source/ScattererScaledCameraSwap.cs index c236532..43dd2f8 100644 --- a/Source/ScattererScaledCameraSwap.cs +++ b/Source/ScattererScaledCameraSwap.cs @@ -23,9 +23,13 @@ public class ScattererScaledCameraSwap : MonoBehaviour "mainScaledCamera", }; + internal static Camera MainScaledCamera => _initialized ? _mainScaledCamera : null; + internal static Assembly ScattererAssembly => _scattererAssembly; + void Awake() { _camera = GetComponent(); + gameObject.AddComponent(); } void OnEnable() @@ -124,12 +128,8 @@ void OnPreCull() _scaledCameraField.SetValue(_scattererInstance, _camera); _camera.depthTextureMode |= _mainScaledCamera.depthTextureMode & DepthTextureMode.Depth; - } - - void OnPreRender() - { - if (_initialized) - ScattererScaledAtmosphere.Bind(_camera, _mainScaledCamera, _scattererAssembly); + ScattererScaledAtmosphere.EnableScreenCopy(_camera); + ScattererScaledAtmosphere.AttachToReflectionProbe(); } void OnPostRender() @@ -137,11 +137,8 @@ void OnPostRender() if (!_initialized || !IsAlive(_scattererInstance) || _scaledCameraField == null) return; - if (_mainScaledCamera == null) - return; - - _scaledCameraField.SetValue(_scattererInstance, _mainScaledCamera); - ScattererScaledAtmosphere.Bind(_mainScaledCamera, _mainScaledCamera, _scattererAssembly); + if (_mainScaledCamera != null) + _scaledCameraField.SetValue(_scattererInstance, _mainScaledCamera); } } } From 091c3da60c88a0d5f58f980a42686ffa7d0eb8b2 Mon Sep 17 00:00:00 2001 From: RELMYMathieu Date: Fri, 27 Nov 2026 22:24:09 +0100 Subject: [PATCH 7/7] fix: hide unlit terrain on feeds after loading in orbit - add Deferred's PQS fade to the feed near camera, since Deferred only did it on Camera 00 - remove the issue 39 diagnostics and the depth-bit copy, since now it's all good :) - changelog updated to reflect everythin up till now: planet from orbit, hull lighting in orbit --- CHANGELOG.md | 7 ++++ Source/DeferredIntegration.cs | 28 +++++++++++++- Source/EVEIntegration.cs | 14 ------- Source/HullCameraManager.cs | 2 - Source/JRTISettingsGUI.cs | 14 ------- Source/ScattererIntegration.cs | 15 +------- Source/ScattererScaledAtmosphere.cs | 57 +---------------------------- Source/ScattererScaledCameraSwap.cs | 1 - 8 files changed, 36 insertions(+), 102 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48f4817..0935d0f 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 + +- Planet from orbit (Scatterer, Deferred) - camera feeds keep the atmosphere over the planet after loading a save in orbit, instead of showing improperly drawn atmosphere +- Hull lighting in orbit (Scatterer) - shiny hulls on camera feeds are no longer lit from where the main camera is looking + ## v3.3.0.2 - 2026-11-27 ### Fixed diff --git a/Source/DeferredIntegration.cs b/Source/DeferredIntegration.cs index ff735dd..31f1841 100644 --- a/Source/DeferredIntegration.cs +++ b/Source/DeferredIntegration.cs @@ -8,8 +8,11 @@ namespace JustReadTheInstructions { public static class DeferredIntegration { + private const int LocalCameraQueue = 15; + private static bool? _isAvailable; private static Type _forwardRenderingCompatibilityType; + private static Type _pqsFadeType; private static MethodInfo _initMethod; public static bool IsAvailable @@ -32,6 +35,7 @@ public static bool IsAvailable } _forwardRenderingCompatibilityType = deferredAssembly.GetType("Deferred.ForwardRenderingCompatibility"); + _pqsFadeType = deferredAssembly.GetType("Deferred.DeferredPQSFade"); if (_forwardRenderingCompatibilityType == null) { @@ -63,7 +67,7 @@ public static bool IsAvailable } } - public static void ApplyToCamera(Camera camera, int renderQueue = 15) + public static void ApplyToCamera(Camera camera, int renderQueue = LocalCameraQueue) { if (!IsAvailable || camera == null) return; @@ -72,6 +76,9 @@ public static void ApplyToCamera(Camera camera, int renderQueue = 15) { camera.renderingPath = RenderingPath.DeferredShading; + if (renderQueue == LocalCameraQueue) + AddPQSFade(camera); + var existingComponent = camera.gameObject.GetComponent(_forwardRenderingCompatibilityType); if (existingComponent != null) { @@ -102,6 +109,15 @@ public static void ApplyToCamera(Camera camera, int renderQueue = 15) } } + private static void AddPQSFade(Camera camera) + { + if (_pqsFadeType == null || camera.gameObject.GetComponent(_pqsFadeType) != null) + return; + + camera.gameObject.AddComponent(_pqsFadeType); + Debug.Log($"[JRTI-Deferred]: Added PQS fade to {camera.name}"); + } + public static void RemoveFromCamera(Camera camera) { if (camera == null) @@ -111,6 +127,13 @@ public static void RemoveFromCamera(Camera camera) { camera.renderingPath = RenderingPath.Forward; + if (_pqsFadeType != null) + { + var fade = camera.gameObject.GetComponent(_pqsFadeType); + if (fade != null) + UnityEngine.Object.Destroy(fade); + } + if (_forwardRenderingCompatibilityType != null) { var component = camera.gameObject.GetComponent(_forwardRenderingCompatibilityType); @@ -143,6 +166,9 @@ public static string GetDiagnosticInfo(Camera camera) : "- ForwardRenderingCompatibility: Missing\n"; } + if (_pqsFadeType != null) + info += $"- DeferredPQSFade: {(camera.GetComponent(_pqsFadeType) != null ? "Present" : "Missing")}\n"; + return info; } } diff --git a/Source/EVEIntegration.cs b/Source/EVEIntegration.cs index 80e17c8..6e1fca3 100644 --- a/Source/EVEIntegration.cs +++ b/Source/EVEIntegration.cs @@ -304,20 +304,6 @@ private static void RemoveComponentIfExists(Camera camera, Type componentType) UnityEngine.Object.Destroy(component); } - public static string GetLightDiagnosticInfo() - { - var info = "Light command buffers:\n"; - foreach (var light in UnityEngine.Object.FindObjectsOfType()) - { - var buffers = ((LightEvent[])Enum.GetValues(typeof(LightEvent))) - .SelectMany(e => light.GetCommandBuffers(e).Select(b => $"{e}:{b.name}")) - .ToArray(); - if (buffers.Length > 0) - info += $"- {light.name}: {string.Join(", ", buffers)}\n"; - } - return info; - } - public static string GetDiagnosticInfo(Camera camera) { if (!IsAvailable || camera == null) diff --git a/Source/HullCameraManager.cs b/Source/HullCameraManager.cs index d9daf11..db85dd3 100644 --- a/Source/HullCameraManager.cs +++ b/Source/HullCameraManager.cs @@ -424,8 +424,6 @@ public bool IsStreamOnly(MuMechModuleHullCamera hullCamera) public int GetOpenCameraCount() => _renderers.Count; - public IEnumerable GetCameraDiagnostics() => _renderers.Values.Select(r => r.GetDiagnosticInfo()); - public bool HasWindow(int stableId) => _windows.ContainsKey(stableId); public bool TryGetListener(int stableId, out Transform view, out Vessel vessel) diff --git a/Source/JRTISettingsGUI.cs b/Source/JRTISettingsGUI.cs index 60ff334..769e1c2 100644 --- a/Source/JRTISettingsGUI.cs +++ b/Source/JRTISettingsGUI.cs @@ -1,6 +1,5 @@ using KSP.UI.Screens; using System.Globalization; -using System.Linq; using UnityEngine; namespace JustReadTheInstructions @@ -573,19 +572,6 @@ private void PrintDiagnostics() if (ParallaxIntegration.IsAvailable) Debug.Log($"[JRTI-Diag]: Parallax scatters active: {ParallaxIntegration.HasActiveScatters()}"); Debug.Log($"[JRTI-Diag]: Open cameras: {HullCameraManager.Instance?.GetOpenCameraCount() ?? 0}"); - Debug.Log($"[JRTI-Diag]: {EVEIntegration.GetLightDiagnosticInfo()}"); - var mainNear = CameraLookup.FindActive("Camera 00"); - if (mainNear != null) - Debug.Log($"[JRTI-Diag]: Main near camera\n{EVEIntegration.GetDiagnosticInfo(mainNear)}\n{ScattererIntegration.GetDiagnosticInfo(mainNear)}"); - if (ScattererIntegration.IsAvailable) - { - var mainScaled = ScattererScaledCameraSwap.MainScaledCamera; - if (mainScaled != null) - Debug.Log($"[JRTI-Diag]: Main scaled camera\n{ScattererIntegration.GetDiagnosticInfo(mainScaled)}"); - Debug.Log($"[JRTI-Diag]: {ScattererScaledAtmosphere.GetDiagnosticInfo()}"); - } - foreach (var info in HullCameraManager.Instance?.GetCameraDiagnostics() ?? Enumerable.Empty()) - Debug.Log($"[JRTI-Diag]: {info}"); Debug.Log("[JRTI-Diag]: ======================="); } diff --git a/Source/ScattererIntegration.cs b/Source/ScattererIntegration.cs index 0ef96db..481897c 100644 --- a/Source/ScattererIntegration.cs +++ b/Source/ScattererIntegration.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Reflection; using UnityEngine; -using UnityEngine.Rendering; namespace JustReadTheInstructions { @@ -168,19 +167,7 @@ public static string GetDiagnosticInfo(Camera camera) return "Scatterer not available\n"; return $"Scatterer for {camera.name}:\n" + - $"- allowMSAA: {camera.allowMSAA}, allowHDR: {camera.allowHDR}\n" + - $"- depthTextureMode: {camera.depthTextureMode}, path: {camera.actualRenderingPath}, clear: {camera.clearFlags}, mask: {camera.cullingMask}\n" + - $"- command buffers: {DescribeCommandBuffers(camera)}\n" + - $"- components: {string.Join(", ", camera.GetComponents().Select(c => c.GetType().Name))}\n" + - ScattererScaledAtmosphere.DescribeQuadVisibility(camera); - } - - private static string DescribeCommandBuffers(Camera camera) - { - var buffers = ((CameraEvent[])Enum.GetValues(typeof(CameraEvent))) - .SelectMany(e => camera.GetCommandBuffers(e).Select(b => $"{e}:{b.name}")) - .ToArray(); - return buffers.Length == 0 ? "none" : string.Join(", ", buffers); + $"- allowMSAA: {camera.allowMSAA}\n"; } } } \ No newline at end of file diff --git a/Source/ScattererScaledAtmosphere.cs b/Source/ScattererScaledAtmosphere.cs index a21cc0f..900549c 100644 --- a/Source/ScattererScaledAtmosphere.cs +++ b/Source/ScattererScaledAtmosphere.cs @@ -15,7 +15,6 @@ internal static class ScattererScaledAtmosphere private static FieldInfo _containerField; private static FieldInfo _parentScaledTransformField; private static FieldInfo _rtField; - private static FieldInfo _inScaledSpaceField; private static MethodInfo _updateScreenBoundsMethod; private static MethodInfo _enableScreenCopyMethod; private static readonly object[] ScreenCopyArgs = new object[1]; @@ -24,9 +23,7 @@ internal static class ScattererScaledAtmosphere private static float _nextReflectionProbeSearch; private static readonly int CameraToWorldId = Shader.PropertyToID("CameraToWorld"); - private static readonly int CurrentVpId = Shader.PropertyToID("currentVP"); - private static readonly int ScreenBoundsId = Shader.PropertyToID("_scaledAtmosphereScreenBounds"); - private static readonly object[] BoundsArgs = new object[5]; + private static readonly int CurrentVpId = Shader.PropertyToID("currentVP"); private static readonly object[] BoundsArgs = new object[5]; public static void Bind(Camera viewer) { @@ -79,57 +76,6 @@ public static void AttachToReflectionProbe() Debug.Log("[JRTI-ScaledSwap]: Scaled atmosphere follows the reflection probe camera"); } - public static string GetDiagnosticInfo() - { - var mainScaledCamera = ScattererScaledCameraSwap.MainScaledCamera; - if (mainScaledCamera == null || !Resolve()) - return "Scatterer scaled atmosphere: not bound\n"; - - var info = $"Scatterer scaled atmosphere (reflection probe camera {(_reflectionProbeScaledCamera != null ? "bound" : "not found")}):\n"; - foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) - { - var material = _materialField.GetValue(skyNode) as Material; - var container = _containerField.GetValue(skyNode); - info += $"- {skyNode.name}: inScaledSpace={_inScaledSpaceField?.GetValue(skyNode)}, " + - $"container={(container != null ? "yes" : "no")}, " + - $"bounds={(material != null ? material.GetVector(ScreenBoundsId).ToString("F3") : "no material")}\n"; - - var quad = FindQuad(skyNode); - info += quad == null - ? " quad: not found\n" - : $" quad: enabled={quad.enabled}, active={quad.gameObject.activeInHierarchy}, layer={quad.gameObject.layer}, " + - $"isVisible={quad.isVisible}, worldBounds={quad.bounds.center:F1}/{quad.bounds.extents:F1}, parentRot={quad.transform.parent.rotation.eulerAngles:F2}\n"; - } - return info; - } - - public static string DescribeQuadVisibility(Camera camera) - { - var mainScaledCamera = ScattererScaledCameraSwap.MainScaledCamera; - if (camera == null || mainScaledCamera == null || !Resolve()) - return ""; - - var info = ""; - var planes = GeometryUtility.CalculateFrustumPlanes(camera); - foreach (var skyNode in mainScaledCamera.GetComponents(_skyNodeType)) - { - var quad = FindQuad(skyNode); - if (quad == null) - continue; - - int layer = quad.gameObject.layer; - info += $"- sees {skyNode.name} quad: inMask={(camera.cullingMask & (1 << layer)) != 0}, " + - $"layerCull={camera.layerCullDistances[layer]}, frustum={GeometryUtility.TestPlanesAABB(planes, quad.bounds)}\n"; - } - return info; - } - - private static Renderer FindQuad(Component skyNode) - { - var parent = _parentScaledTransformField.GetValue(skyNode) as Transform; - return parent?.Find("Scatterer scaled depth-buffer atmo")?.GetComponent(); - } - private static void BindSkyNode(Component skyNode, Camera camera) { var material = _materialField.GetValue(skyNode) as Material; @@ -173,7 +119,6 @@ private static bool Resolve() _containerField = skyNodeType.GetField("scaledDepthBufferScatteringContainer", flags); _parentScaledTransformField = skyNodeType.GetField("parentScaledTransform", flags); _rtField = skyNodeType.GetField("Rt", flags); - _inScaledSpaceField = skyNodeType.GetField("inScaledSpace", flags); _updateScreenBoundsMethod = _containerField?.FieldType.GetMethod("UpdateScreenBounds", flags); _enableScreenCopyMethod = scatterer.GetType("Scatterer.ScreenCopyCommandBuffer") ?.GetMethod("EnableScreenCopyForFrame", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(Camera) }, null); diff --git a/Source/ScattererScaledCameraSwap.cs b/Source/ScattererScaledCameraSwap.cs index 43dd2f8..55ff948 100644 --- a/Source/ScattererScaledCameraSwap.cs +++ b/Source/ScattererScaledCameraSwap.cs @@ -127,7 +127,6 @@ void OnPreCull() return; _scaledCameraField.SetValue(_scattererInstance, _camera); - _camera.depthTextureMode |= _mainScaledCamera.depthTextureMode & DepthTextureMode.Depth; ScattererScaledAtmosphere.EnableScreenCopy(_camera); ScattererScaledAtmosphere.AttachToReflectionProbe(); }