From f1c36b4ccbdefbaaeaa15e0634765c95c4ff3478 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 12 Oct 2026 03:34:50 +0200 Subject: [PATCH] Vapour clouds are drawn as fog banks: one ZoneRenderer subscription, VapourCloudInstance presenter, DrawAhead for a free body ZoneRenderer loads and subscribes Zone.Clouds like mines and destroys a bank on removal; VapourCloudInstance places a Fog Patch brush at DrawAhead.Position(cloud.Body, Lead) and thins its _Depth with OpacityAt. The prefab and scene wiring follow in the next commit; this one compiles but CloudPrefab is unassigned. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/ServerShared/DrawAhead.cs | 4 ++ .../Zone Display/VapourCloudInstance.cs | 46 +++++++++++++++++++ Assets/Scripts/Zone Display/ZoneRenderer.cs | 22 +++++++++ tests/Aetheria.Shared.Tests/DrawAheadTests.cs | 29 ++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 Assets/Scripts/Zone Display/VapourCloudInstance.cs create mode 100644 tests/Aetheria.Shared.Tests/DrawAheadTests.cs diff --git a/Assets/Scripts/ServerShared/DrawAhead.cs b/Assets/Scripts/ServerShared/DrawAhead.cs index 13da8aae9..c15ce57e2 100644 --- a/Assets/Scripts/ServerShared/DrawAhead.cs +++ b/Assets/Scripts/ServerShared/DrawAhead.cs @@ -19,6 +19,10 @@ public static class DrawAhead ? entity.Position : entity.Position + float3(entity.Velocity.x, 0, entity.Velocity.y) * lead; + // A free body (a vapour cloud) drawn where the frame falls lead sim seconds after the last step, by the velocity + // that step moved it with: the launch velocity and the drift together. + public static float2 Position(KinematicBody body, float lead) => body.Position + body.TotalVelocity * lead; + // The ship's rotation turned about its own up axis by the rate its last step turned, for lead seconds. public static quaternion Rotation(Ship ship, float lead) => ship.WormholeAnimationInProgress ? ship.Rotation : Turned(ship.Rotation, ship.TurnRate * lead); diff --git a/Assets/Scripts/Zone Display/VapourCloudInstance.cs b/Assets/Scripts/Zone Display/VapourCloudInstance.cs new file mode 100644 index 000000000..74ec1abf5 --- /dev/null +++ b/Assets/Scripts/Zone Display/VapourCloudInstance.cs @@ -0,0 +1,46 @@ +using CultMath; +using CultMath.UnityBridge; +using UnityEngine; + +// How one vented vapour cloud looks: a fog-bank brush painted into the Patch layer, which the volumetric pass reads as +// patch thickness. Zone owns every cloud fact (existence, body, radius, opacity); this reads them and draws, and writes +// nothing back (rulings vapour-cloud-obscuration, unity-presents-only-r3). The brush's depth thins with the cloud's +// opacity, so the bank fades to nothing at the cloud's lifetime. +public class VapourCloudInstance : MonoBehaviour +{ + // The world height the scene's patch brushes sit at (Depth Micro), inside the Patch camera's clip range. + public float PatchHeight = -5f; + + private static readonly int Depth = Shader.PropertyToID("_Depth"); + + private Zone _zone; + private VapourCloud _cloud; + private MeshRenderer _renderer; + private MaterialPropertyBlock _block; + private float _authoredDepth; + + public void Bind(Zone zone, VapourCloud cloud) + { + _zone = zone; + _cloud = cloud; + _renderer = GetComponent(); + _block = new MaterialPropertyBlock(); + _authoredDepth = _renderer.sharedMaterial.GetFloat(Depth); + Place(); + } + + private void Update() => Place(); + + private void Place() + { + var p = DrawAhead.Position(_cloud.Body, ActionGameManager.Clock.Lead); + transform.position = new Vector3(p.x, PatchHeight, p.y); + transform.localScale = Vector3.one * (2f * _cloud.Radius); + + // A cloud with no opacity left is already gone from the sim; a zero Opacity would divide by zero. + var thinning = _cloud.Opacity > 0f ? _cloud.OpacityAt(_zone.Time) / _cloud.Opacity : 0f; + _renderer.GetPropertyBlock(_block); + _block.SetFloat(Depth, _authoredDepth * thinning); + _renderer.SetPropertyBlock(_block); + } +} diff --git a/Assets/Scripts/Zone Display/ZoneRenderer.cs b/Assets/Scripts/Zone Display/ZoneRenderer.cs index e969670db..d39e0d2ea 100644 --- a/Assets/Scripts/Zone Display/ZoneRenderer.cs +++ b/Assets/Scripts/Zone Display/ZoneRenderer.cs @@ -45,6 +45,7 @@ public class ZoneRenderer : MonoBehaviour public GridObject GearPickup; public GridObject WeaponPickup; public MineInstance MinePrefab; + public VapourCloudInstance CloudPrefab; public Material[] MapGravityMaterials; [Header("Tour")] public bool Tour; @@ -95,6 +96,7 @@ public class ZoneRenderer : MonoBehaviour public Dictionary WormholeInstances = new Dictionary(); private List _loot = new List(); private Dictionary _mines = new Dictionary(); + private Dictionary _clouds = new Dictionary(); public Zone Zone { get; private set; } public ItemManager ItemManager { get; set; } @@ -234,6 +236,15 @@ public class ZoneRenderer : MonoBehaviour instance.Blast(); })); + // Every vapour cloud, whoever vented it, is drawn through this one subscription. A cloud leaves Zone.Clouds + // when its opacity has reached nothing, so a removal destroys its fog bank with nothing left to show. + foreach (var cloud in zone.Clouds) LoadCloud(zone, cloud); + _zoneSubscriptions.Add(zone.Clouds.ObserveAdd().Subscribe(e => LoadCloud(zone, e.Value))); + _zoneSubscriptions.Add(zone.Clouds.ObserveRemove().Subscribe(e => + { + if (_clouds.Remove(e.Value, out var instance)) Destroy(instance.gameObject); + })); + if (zone.GalaxyZone != null) { foreach (var adjacentZone in zone.GalaxyZone.AdjacentZones) @@ -263,6 +274,13 @@ public class ZoneRenderer : MonoBehaviour _mines.Add(mine, instance); } + private void LoadCloud(Zone zone, VapourCloud cloud) + { + var instance = Instantiate(CloudPrefab); + instance.Bind(zone, cloud); + _clouds.Add(cloud, instance); + } + public void AddWormhole(Wormhole wormhole) { var instance = Instantiate(WormholePrefab); @@ -290,6 +308,10 @@ public class ZoneRenderer : MonoBehaviour if (instance) Destroy(instance.gameObject); _mines.Clear(); + foreach (var instance in _clouds.Values) + if (instance) Destroy(instance.gameObject); + _clouds.Clear(); + foreach(var gridObject in _loot) if(gridObject) Destroy(gridObject.gameObject); _loot.Clear(); diff --git a/tests/Aetheria.Shared.Tests/DrawAheadTests.cs b/tests/Aetheria.Shared.Tests/DrawAheadTests.cs new file mode 100644 index 000000000..208af3589 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/DrawAheadTests.cs @@ -0,0 +1,29 @@ +using Xunit; +using float2 = CultMath.float2; + +// DrawAhead draws a free body where the frame falls lead sim seconds after the last step, by the velocity that step +// moved it with. Expected values are worked from first principles: position plus launch velocity and drift, times lead. +public sealed class DrawAheadTests +{ + [Fact] + public void ABodyIsDrawnAheadByItsTotalVelocity() + { + var body = new KinematicBody { Position = new float2(10f, 20f), Velocity = new float2(3f, -1f), Drift = new float2(.5f, 2f) }; + + var drawn = DrawAhead.Position(body, .5f); + + Assert.Equal(10f + (3f + .5f) * .5f, drawn.x, 4); + Assert.Equal(20f + (-1f + 2f) * .5f, drawn.y, 4); + } + + [Fact] + public void ABodyDrawnWithNoLeadIsWhereTheSimPutIt() + { + var body = new KinematicBody { Position = new float2(10f, 20f), Velocity = new float2(3f, -1f), Drift = new float2(.5f, 2f) }; + + var drawn = DrawAhead.Position(body, 0f); + + Assert.Equal(10f, drawn.x, 4); + Assert.Equal(20f, drawn.y, 4); + } +}