Gravity grid-lowering: the drawn height is one Gravity Field pass over the zone's wells; gravity brushes and brush inputs deleted
Zone.WellReference owns each body's reference zero (GetHeight uses it). Gravity.cginc mirrors GravityField.Potential; Brushes/Gravity Field draws depth = -h on layer 9 from a StructuredBuffer that GravityLowering.Lower fills from the zone each frame; GravityLowering.CreateField makes the one quad. GravityFieldParityVerify renders it and compares every texel with -Zone.GetHeight (max error 0.0059 u), the gradient with GetForce (0.03 degrees) and the wiring. Deleted: the Gravity Well children of Planet, Gas Giant, Sun and PlanetOutpost prefabs, Wormhole 1's three gravity children, their overrides in Main Menu and TestScene, five wormhole/wave materials and two brush shaders, Planet.GravityWellDepth/Radius, BodyData keys 6-7, PlanetSettings.GravityDepth/GravityRadius (and the Settings.asset keys). Surface Displacement culls layer 9 only. Headless build and tests not yet run at this commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c4dfd905ff
commit
3a5b3b0bab
42 changed files with 504 additions and 1301 deletions
78
Assets/Scripts/Zone Display/GravityLowering.cs
Normal file
78
Assets/Scripts/Zone Display/GravityLowering.cs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// The renderer's lowering of the zone's potential (aetheria-gravity, cut grid-lowering). Zone.GetHeight owns the drawn
|
||||
/// height: this hands its inputs (each well's position, strength, core radius and reference, and the exaggeration) to
|
||||
/// Assets/Shaders/Gravity.cginc, which sums them per texel, and makes the one quad that carries the sum into every
|
||||
/// camera that culls layer 9. Nothing here decides a height.
|
||||
/// </summary>
|
||||
public static class GravityLowering
|
||||
{
|
||||
/// <summary>The layer the gravity cameras cull; nothing but the run-time field sits on it.</summary>
|
||||
public const int FieldLayer = 9;
|
||||
/// <summary>The quad's side in u: larger than any zone (10000) plus any view (4096); texels outside a camera cost nothing.</summary>
|
||||
public const float FieldExtent = 100000f;
|
||||
|
||||
// Mirrors Gravity.cginc's GravityWell: float2 position, strength, coreRadius, reference.
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
struct WellRecord
|
||||
{
|
||||
public Vector2 Position;
|
||||
public float Strength;
|
||||
public float CoreRadius;
|
||||
public float Reference;
|
||||
}
|
||||
|
||||
static readonly int Stride = Marshal.SizeOf<WellRecord>();
|
||||
static readonly int WellsId = Shader.PropertyToID("_GravityWells");
|
||||
static readonly int CountId = Shader.PropertyToID("_GravityWellCount");
|
||||
static readonly int ExaggerationId = Shader.PropertyToID("_HeightExaggeration");
|
||||
static WellRecord[] _records = new WellRecord[0];
|
||||
|
||||
/// <summary>
|
||||
/// Fills one record per body of the zone (a null zone has none) and sets the shader globals. The buffer is the
|
||||
/// caller's, (re)allocated to the body count (at least one, so the global is always bound).
|
||||
/// </summary>
|
||||
public static void Lower(Zone zone, ref ComputeBuffer wells)
|
||||
{
|
||||
var count = zone?.PlanetInstances.Count ?? 0;
|
||||
var capacity = Mathf.Max(count, 1);
|
||||
if (wells == null || wells.count != capacity)
|
||||
{
|
||||
wells?.Release();
|
||||
wells = new ComputeBuffer(capacity, Stride);
|
||||
}
|
||||
|
||||
if (_records.Length != capacity) _records = new WellRecord[capacity];
|
||||
var i = 0;
|
||||
if (zone != null)
|
||||
foreach (var body in zone.PlanetInstances.Values)
|
||||
_records[i++] = new WellRecord
|
||||
{
|
||||
Position = new Vector2(body.Orbit.Position.x, body.Orbit.Position.y),
|
||||
Strength = body.WellStrength,
|
||||
CoreRadius = body.CoreRadius,
|
||||
Reference = zone.WellReference(body)
|
||||
};
|
||||
wells.SetData(_records);
|
||||
|
||||
Shader.SetGlobalBuffer(WellsId, wells);
|
||||
Shader.SetGlobalInt(CountId, count);
|
||||
Shader.SetGlobalFloat(ExaggerationId, zone?.Settings.HeightExaggeration ?? 1f);
|
||||
}
|
||||
|
||||
/// <summary>The one quad of the field: flat in xz at y 0 on layer 9, drawn with the Gravity Field material.</summary>
|
||||
public static GameObject CreateField(Material material)
|
||||
{
|
||||
var field = new GameObject("Gravity Field") { layer = FieldLayer };
|
||||
field.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
|
||||
field.transform.localScale = FieldExtent * Vector3.one;
|
||||
field.AddComponent<MeshFilter>().sharedMesh = Resources.GetBuiltinResource<Mesh>("Quad.fbx");
|
||||
var renderer = field.AddComponent<MeshRenderer>();
|
||||
renderer.sharedMaterial = material;
|
||||
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
||||
renderer.receiveShadows = false;
|
||||
return field;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Zone Display/GravityLowering.cs.meta
Normal file
11
Assets/Scripts/Zone Display/GravityLowering.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 1a5e08474b514e509ed1477b3ac06e94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -9,7 +9,6 @@ using UnityEngine;
|
|||
public class PlanetObject : MonoBehaviour
|
||||
{
|
||||
public MeshRenderer Body;
|
||||
public MeshRenderer GravityWell;
|
||||
public MeshRenderer Icon;
|
||||
public CelestialBodyGenerator Generator;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,8 @@ public class ZoneRenderer : MonoBehaviour
|
|||
public MineInstance MinePrefab;
|
||||
public VapourCloudInstance CloudPrefab;
|
||||
public Material[] MapGravityMaterials;
|
||||
// The gravity field's material (Brushes/Gravity Field): GravityLowering makes the one quad that draws it.
|
||||
public Material GravityField;
|
||||
|
||||
[Header("Tour")] public bool Tour;
|
||||
public float TourSwitchTime = 5f;
|
||||
|
|
@ -77,7 +79,8 @@ public class ZoneRenderer : MonoBehaviour
|
|||
// is stored on AsteroidBelt any more.
|
||||
private Dictionary<CultRecordKey, float4[]> _beltTransforms = new Dictionary<CultRecordKey, float4[]>();
|
||||
private float _viewDistance;
|
||||
//private float _maxDepth;
|
||||
private GameObject _gravityField;
|
||||
private ComputeBuffer _gravityWells;
|
||||
private float _minimapDistance;
|
||||
|
||||
private int _tourIndex = -1;
|
||||
|
|
@ -195,6 +198,7 @@ public class ZoneRenderer : MonoBehaviour
|
|||
SectorBrushes.localScale = zone.Pack.Radius * 2 * Vector3.one;
|
||||
SlimeGravityCamera.orthographicSize = zone.Pack.Radius;
|
||||
SlimeRenderer.ZoneRadius = zone.Pack.Radius;
|
||||
if (_gravityField == null) _gravityField = GravityLowering.CreateField(GravityField);
|
||||
foreach (var p in zone.Planets)
|
||||
LoadPlanet(p.Key, p.Value);
|
||||
|
||||
|
|
@ -321,9 +325,17 @@ public class ZoneRenderer : MonoBehaviour
|
|||
WormholeInstances.Add(wormhole, (instance.gameObject, icon));
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
_gravityWells?.Release();
|
||||
_gravityWells = null;
|
||||
}
|
||||
|
||||
public void ClearZone()
|
||||
{
|
||||
if (Zone == null) return;
|
||||
_gravityWells?.Release();
|
||||
_gravityWells = null;
|
||||
foreach (var wormhole in WormholeInstances.Values)
|
||||
{
|
||||
Destroy(wormhole.gravity);
|
||||
|
|
@ -491,11 +503,6 @@ public class ZoneRenderer : MonoBehaviour
|
|||
|
||||
var planetInstance = Zone.PlanetInstances[key];
|
||||
planet.Body.transform.localScale = planetInstance.BodyRadius * Vector3.one;
|
||||
planet.GravityWell.transform.localScale = planetInstance.GravityWellRadius * Vector3.one;
|
||||
// var depth = planetInstance.GravityWellDepth;
|
||||
// if (depth > _maxDepth) _maxDepth = depth;
|
||||
planet.GravityWell.material.SetFloat("_Depth", planetInstance.GravityWellDepth);
|
||||
planet.Icon.transform.position = new Vector3(0, -planetInstance.GravityWellDepth, 0);
|
||||
planet.Icon.transform.localScale = Settings.IconSize.Evaluate(planetInstance.BodyData.Mass) * Vector3.one;
|
||||
|
||||
|
||||
|
|
@ -513,6 +520,7 @@ public class ZoneRenderer : MonoBehaviour
|
|||
void Update()
|
||||
{
|
||||
var maxDepth = 0f;
|
||||
GravityLowering.Lower(Zone, ref _gravityWells);
|
||||
foreach (var loot in _floatingItems.Values)
|
||||
{
|
||||
loot.ViewOrigin = PerspectiveEntity.Position;
|
||||
|
|
@ -576,6 +584,7 @@ public class ZoneRenderer : MonoBehaviour
|
|||
if (-height > maxDepth) maxDepth = -height;
|
||||
planet.Value.transform.position = new Vector3(p.x, 0, p.y);
|
||||
planet.Value.Body.transform.localPosition = new Vector3(0, height + planetInstance.BodyRadius * 2, 0);
|
||||
planet.Value.Icon.transform.localPosition = new Vector3(0, height, 0);
|
||||
if (planet.Value is GasGiantObject gasGiantObject)
|
||||
{
|
||||
if (!(planet.Value is SunObject))
|
||||
|
|
|
|||
Loading…
Reference in a new issue