From 8fb39a1e5710ad5565187bd71825ec03b1de0783 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 16 Jul 2026 17:44:29 +0100 Subject: [PATCH] Unify field grid and temporal camera lowering --- docs/authority-map.md | 7 + .../org.gamecult.eve.unity-scene/README.md | 10 ++ .../Runtime/EveUnityPlayableWorldCameraRig.cs | 89 ++++++++++-- .../Runtime/EveUnitySceneSurfaceLowerer.cs | 29 +++- .../Fields/EveUnityFieldsParticleRenderer.cs | 118 +++------------- .../Fields/EveUnityFieldsViewportFrame.cs | 128 ++++++++++++++++++ .../EveUnityFieldsViewportFrame.cs.meta | 11 ++ .../Fields/EveUnityFieldsVolumeRenderer.cs | 49 +++++-- .../EveFieldsUnitySceneProjectionTests.cs | 49 ++++++- .../EveUnitySceneSurfaceLowererTests.cs | 27 +++- 10 files changed, 384 insertions(+), 133 deletions(-) create mode 100644 packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs create mode 100644 packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs.meta diff --git a/docs/authority-map.md b/docs/authority-map.md index 7c6d139..bc92db2 100644 --- a/docs/authority-map.md +++ b/docs/authority-map.md @@ -40,6 +40,13 @@ presentation counters, and receipt display state are projections. They are not world truth. Temporal targets reset when the volume program, node, or render size changes and can never become provider state. +Camera-relative field viewports have one generic resolver. The advertised +viewport frame owns its extent, spatial-cell scale, raster texels per cell, and +snap policy; fog volumes and stateless particle programs consume the identical +projected `EveFieldsViewport`. URP TAA state is likewise derived from the +world's neutral `temporal-reprojection.v1` camera contract and is restored on +release. + ## Forbidden Writers Unity transforms, input drivers, camera rigs, scene sinks, asset caches, and diff --git a/packages/org.gamecult.eve.unity-scene/README.md b/packages/org.gamecult.eve.unity-scene/README.md index 22359e7..c1c66a0 100644 --- a/packages/org.gamecult.eve.unity-scene/README.md +++ b/packages/org.gamecult.eve.unity-scene/README.md @@ -72,6 +72,16 @@ out of Eve's semantic surface while allowing a provider-owned shader to consume portable Eve Fields documents. Volume composition runs as a generic URP render pass before post-processing so the provider-advertised profile grades both the native scene and the provider volume through the same camera pipeline. + +Camera-relative field consumers share `viewportAnchor`, `span`, +`cellWorldSize`, `viewportSnapLayer`, and `viewportSnapTexels` as one viewport +frame contract. Volume and particle lowerers resolve that frame through the +same generic path, so a snapped stateless particle lattice cannot drift from +the field textures it samples. A `world.scene3d` may also request +`temporal-reprojection.v1`; EveUnity lowers its neutral history blend, jitter, +quality, and sharpening values to URP TAA and restores the previous camera +state when the world releases the camera. + Providers may advertise per-layer resolution scales, mipmap use, and filtering through `layerTargetDescriptors`; the Unity lowerer applies those native target requirements without assigning gameplay or product meaning to the layer keys. diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/EveUnityPlayableWorldCameraRig.cs b/packages/org.gamecult.eve.unity-scene/Runtime/EveUnityPlayableWorldCameraRig.cs index b0944e1..044b2c9 100644 --- a/packages/org.gamecult.eve.unity-scene/Runtime/EveUnityPlayableWorldCameraRig.cs +++ b/packages/org.gamecult.eve.unity-scene/Runtime/EveUnityPlayableWorldCameraRig.cs @@ -40,6 +40,8 @@ namespace GameCult.Eve.UnityScene private UniversalAdditionalCameraData? _postProcessCameraData; private bool _createdPostProcessCameraData; private bool _previousRenderPostProcessing; + private AntialiasingMode _previousAntialiasing; + private TemporalAA.Settings _previousTaaSettings; public EveUnityPlayableWorldClientHost? Host { @@ -307,7 +309,17 @@ namespace GameCult.Eve.UnityScene !IsFinite(world.KeyLightColorR) || !IsFinite(world.KeyLightColorG) || !IsFinite(world.KeyLightColorB) || - !IsFinite(world.KeyLightIntensity)) + !IsFinite(world.KeyLightIntensity) || + !IsFinite(world.TemporalHistoryBlend) || + !IsFinite(world.TemporalJitterScale) || + !IsFinite(world.TemporalSharpening) || + (!string.IsNullOrWhiteSpace(world.CameraReconstruction) && + !string.Equals(world.CameraReconstruction, "temporal-reprojection.v1", System.StringComparison.Ordinal)) || + (string.Equals(world.CameraReconstruction, "temporal-reprojection.v1", System.StringComparison.Ordinal) && + (world.TemporalHistoryBlend < 0f || world.TemporalHistoryBlend > 1f || + world.TemporalJitterScale < 0f || world.TemporalJitterScale > 1f || + world.TemporalSharpening < 0f || world.TemporalSharpening > 1f || + !IsTemporalQuality(world.TemporalQuality)))) return false; var assets = host.NativeAssetProvider; if (!string.IsNullOrWhiteSpace(world.SkyboxAssetRef)) @@ -453,12 +465,19 @@ namespace GameCult.Eve.UnityScene } if (environmentChanged) DynamicGI.UpdateEnvironment(); - ApplyPostProcess(camera, postProcessProfile); + ApplyCameraRendering(camera, postProcessProfile, world); } - private void ApplyPostProcess(Camera? camera, VolumeProfile? profile) + private void ApplyCameraRendering( + Camera? camera, + VolumeProfile? profile, + EveUnityPlayableWorldProjection world) { - if (profile == null) + var usesTemporalReprojection = string.Equals( + world.CameraReconstruction, + "temporal-reprojection.v1", + System.StringComparison.Ordinal); + if (profile == null && !usesTemporalReprojection) { RestorePostProcess(); return; @@ -474,19 +493,59 @@ namespace GameCult.Eve.UnityScene if (_postProcessCameraData == null) _postProcessCameraData = camera.gameObject.AddComponent(); _previousRenderPostProcessing = _postProcessCameraData.renderPostProcessing; + _previousAntialiasing = _postProcessCameraData.antialiasing; + _previousTaaSettings = _postProcessCameraData.taaSettings; } - _postProcessCameraData.renderPostProcessing = true; - if (_postProcessVolumeObject == null) + _postProcessCameraData.renderPostProcessing = profile != null || _previousRenderPostProcessing; + if (usesTemporalReprojection) { - _postProcessVolumeObject = new GameObject("Eve World Post Process"); - _postProcessVolumeObject.transform.SetParent(transform, worldPositionStays: false); - _postProcessVolume = _postProcessVolumeObject.AddComponent(); - _postProcessVolume.isGlobal = true; - _postProcessVolume.priority = 1000f; - _postProcessVolume.weight = 1f; + _postProcessCameraData.antialiasing = AntialiasingMode.TemporalAntiAliasing; + ref var settings = ref _postProcessCameraData.taaSettings; + settings = TemporalAA.Settings.Create(); + settings.quality = ParseTemporalQuality(world.TemporalQuality); + settings.baseBlendFactor = world.TemporalHistoryBlend; + settings.jitterScale = world.TemporalJitterScale; + settings.contrastAdaptiveSharpening = world.TemporalSharpening; + } + else + { + _postProcessCameraData.antialiasing = _previousAntialiasing; + _postProcessCameraData.taaSettings = _previousTaaSettings; + } + if (profile != null) + { + if (_postProcessVolumeObject == null) + { + _postProcessVolumeObject = new GameObject("Eve World Post Process"); + _postProcessVolumeObject.transform.SetParent(transform, worldPositionStays: false); + _postProcessVolume = _postProcessVolumeObject.AddComponent(); + _postProcessVolume.isGlobal = true; + _postProcessVolume.priority = 1000f; + _postProcessVolume.weight = 1f; + } + _postProcessVolume!.sharedProfile = profile; + _postProcessVolumeObject.SetActive(true); + } + else if (_postProcessVolumeObject != null) + { + _postProcessVolumeObject.SetActive(false); + } + } + + private static bool IsTemporalQuality(string quality) => + quality == "very-low" || quality == "low" || quality == "medium" || + quality == "high" || quality == "very-high"; + + private static TemporalAAQuality ParseTemporalQuality(string quality) + { + switch (quality) + { + case "very-low": return TemporalAAQuality.VeryLow; + case "low": return TemporalAAQuality.Low; + case "medium": return TemporalAAQuality.Medium; + case "very-high": return TemporalAAQuality.VeryHigh; + default: return TemporalAAQuality.High; } - _postProcessVolume!.sharedProfile = profile; - _postProcessVolumeObject.SetActive(true); } private void RestorePostProcess() @@ -503,6 +562,8 @@ namespace GameCult.Eve.UnityScene else { _postProcessCameraData.renderPostProcessing = _previousRenderPostProcessing; + _postProcessCameraData.antialiasing = _previousAntialiasing; + _postProcessCameraData.taaSettings = _previousTaaSettings; } } _postProcessCameraData = null; diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/EveUnitySceneSurfaceLowerer.cs b/packages/org.gamecult.eve.unity-scene/Runtime/EveUnitySceneSurfaceLowerer.cs index 726944c..bcc3637 100644 --- a/packages/org.gamecult.eve.unity-scene/Runtime/EveUnitySceneSurfaceLowerer.cs +++ b/packages/org.gamecult.eve.unity-scene/Runtime/EveUnitySceneSurfaceLowerer.cs @@ -156,7 +156,12 @@ namespace GameCult.Eve.UnityScene ParseFloat(worldRoot.GetProp("keyLightIntensity"), 0f), fieldVolumes, worldRoot.GetProp("cameraLookAt"), - fieldParticles); + fieldParticles, + worldRoot.GetProp("cameraReconstruction"), + worldRoot.GetProp("temporalQuality", "high"), + ParseFloat(worldRoot.GetProp("temporalHistoryBlend"), 0f), + ParseFloat(worldRoot.GetProp("temporalJitterScale"), 0f), + ParseFloat(worldRoot.GetProp("temporalSharpening"), 0f)); } private static EveUnityFieldVolumeProjection BuildFieldVolume(EveSurfaceComponent component) => @@ -411,7 +416,12 @@ namespace GameCult.Eve.UnityScene float keyLightIntensity = 0f, IReadOnlyList? fieldVolumes = null, string cameraLookAt = "", - IReadOnlyList? fieldParticles = null) + IReadOnlyList? fieldParticles = null, + string cameraReconstruction = "", + string temporalQuality = "high", + float temporalHistoryBlend = 0f, + float temporalJitterScale = 0f, + float temporalSharpening = 0f) { WorldRootId = worldRootId ?? ""; StatePointerId = statePointerId ?? ""; @@ -459,6 +469,11 @@ namespace GameCult.Eve.UnityScene FieldVolumes = fieldVolumes ?? Array.Empty(); FieldParticles = fieldParticles ?? Array.Empty(); CameraLookAt = cameraLookAt ?? ""; + CameraReconstruction = cameraReconstruction ?? ""; + TemporalQuality = temporalQuality ?? ""; + TemporalHistoryBlend = temporalHistoryBlend; + TemporalJitterScale = temporalJitterScale; + TemporalSharpening = temporalSharpening; } public string WorldRootId { get; } @@ -483,6 +498,16 @@ namespace GameCult.Eve.UnityScene public string CameraLookAt { get; } + public string CameraReconstruction { get; } + + public string TemporalQuality { get; } + + public float TemporalHistoryBlend { get; } + + public float TemporalJitterScale { get; } + + public float TemporalSharpening { get; } + public float CameraDistance { get; } public float CameraVerticalFieldOfViewDegrees { get; } diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsParticleRenderer.cs b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsParticleRenderer.cs index 6e6eb4e..34bfb2c 100644 --- a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsParticleRenderer.cs +++ b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsParticleRenderer.cs @@ -101,44 +101,28 @@ namespace GameCult.Eve.UnityScene.Fields var baseWidth = PositiveInt(field, "textureWidth", 512); var baseHeight = PositiveInt(field, "textureHeight", 512); - var viewportWidth = (float)(document.Viewport.MaxX - document.Viewport.MinX); - var viewportHeight = (float)(document.Viewport.MaxY - document.Viewport.MinY); - if (!float.IsFinite(viewportWidth) || !float.IsFinite(viewportHeight) || - viewportWidth <= 0f || viewportHeight <= 0f) - return false; - - var center = new Vector2( - (float)((document.Viewport.MinX + document.Viewport.MaxX) * 0.5), - (float)((document.Viewport.MinY + document.Viewport.MaxY) * 0.5)); - if (string.Equals(Prop(field, "viewportAnchor"), "active-camera.xz", StringComparison.Ordinal)) - { - var snapLayer = Prop(field, "viewportSnapLayer"); - var snapTarget = _targets.FirstOrDefault(target => - string.Equals(target.LayerKey, snapLayer, StringComparison.Ordinal)); - var snapWidth = snapTarget == null - ? 0 - : Mathf.Max(1, Mathf.RoundToInt(baseWidth * Mathf.Max(0.01f, snapTarget.WidthScale))); - var snapHeight = snapTarget == null - ? 0 - : Mathf.Max(1, Mathf.RoundToInt(baseHeight * Mathf.Max(0.01f, snapTarget.HeightScale))); - var snapTexels = PositiveInt(field, "viewportSnapTexels", 0); - var cellWorldSize = PositiveFloat(field, "cellWorldSize", 0f); - var span = PositiveInt(field, "span", 0); - if (!TryValidateSpatialLattice( - viewportWidth, - viewportHeight, - snapWidth, - snapHeight, - span, - cellWorldSize, - snapTexels)) return false; - center.x = SnapCameraCoordinate(camera.transform.position.x, viewportWidth, snapWidth, snapTexels); - center.y = SnapCameraCoordinate(camera.transform.position.z, viewportHeight, snapHeight, snapTexels); - } + var snapLayer = Prop(field, "viewportSnapLayer"); + var snapTarget = _targets.FirstOrDefault(target => + string.Equals(target.LayerKey, snapLayer, StringComparison.Ordinal)); + var snapWidth = snapTarget == null + ? 0 + : Mathf.Max(1, Mathf.RoundToInt(baseWidth * Mathf.Max(0.01f, snapTarget.WidthScale))); + var snapHeight = snapTarget == null + ? 0 + : Mathf.Max(1, Mathf.RoundToInt(baseHeight * Mathf.Max(0.01f, snapTarget.HeightScale))); + if (!EveUnityFieldsViewportFrame.TryResolve( + document, + field.Props, + new Vector2(camera.transform.position.x, camera.transform.position.z), + snapWidth, + snapHeight, + out var projected, + out var center)) return false; + var viewportWidth = (float)(projected.Viewport.MaxX - projected.Viewport.MinX); + var viewportHeight = (float)(projected.Viewport.MaxY - projected.Viewport.MinY); if (_rasterizedFrameId != document.FrameId || _rasterizedCenter != center) { - var projected = ProjectViewport(document, center, viewportWidth, viewportHeight); _layers.Render(projected, _targets, baseWidth, baseHeight); _rasterizedFrameId = document.FrameId; _rasterizedCenter = center; @@ -156,70 +140,6 @@ namespace GameCult.Eve.UnityScene.Fields return true; } - public static float SnapCameraCoordinate( - float coordinate, - float viewportWidth, - int snapTargetWidth, - float snapTexels) - { - if (!float.IsFinite(coordinate) || !float.IsFinite(viewportWidth) || - !float.IsFinite(snapTexels) || viewportWidth <= 0f || - snapTargetWidth <= 0 || snapTexels <= 0f) - throw new ArgumentOutOfRangeException(nameof(viewportWidth)); - var snapLength = viewportWidth * snapTexels / snapTargetWidth; - var cell = (int)(coordinate / snapLength); - return cell * snapLength; - } - - public static bool TryValidateSpatialLattice( - float viewportWidth, - float viewportHeight, - int gravityTextureWidth, - int gravityTextureHeight, - int span, - float cellWorldSize, - int gravityTexelsPerCell) - { - if (!float.IsFinite(viewportWidth) || !float.IsFinite(viewportHeight) || - !float.IsFinite(cellWorldSize) || viewportWidth <= 0f || viewportHeight <= 0f || - gravityTextureWidth <= 0 || gravityTextureHeight <= 0 || span <= 0 || - cellWorldSize <= 0f || gravityTexelsPerCell <= 0) - return false; - var expectedExtent = span * cellWorldSize; - var expectedCellFromX = viewportWidth * gravityTexelsPerCell / gravityTextureWidth; - var expectedCellFromY = viewportHeight * gravityTexelsPerCell / gravityTextureHeight; - const float tolerance = 0.0001f; - return Mathf.Abs(viewportWidth - expectedExtent) <= tolerance && - Mathf.Abs(viewportHeight - expectedExtent) <= tolerance && - Mathf.Abs(expectedCellFromX - cellWorldSize) <= tolerance && - Mathf.Abs(expectedCellFromY - cellWorldSize) <= tolerance; - } - - private static EveFieldsSplatsDocument ProjectViewport( - EveFieldsSplatsDocument source, - Vector2 center, - float width, - float height) => - new EveFieldsSplatsDocument - { - Schema = source.Schema, - FrameId = source.FrameId, - PublishedAtUtc = source.PublishedAtUtc, - SimulationTimeSeconds = source.SimulationTimeSeconds, - RunId = source.RunId, - ZoneIndex = source.ZoneIndex, - ZoneName = source.ZoneName, - Viewport = new EveFieldsViewport - { - MinX = center.x - width * 0.5, - MinY = center.y - height * 0.5, - MaxX = center.x + width * 0.5, - MaxY = center.y + height * 0.5 - }, - Layers = source.Layers, - Splats = source.Splats - }; - private bool EnsurePrograms(EveUnityFieldParticlesProjection field) { var provider = _host?.NativeAssetProvider; diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs new file mode 100644 index 0000000..608a02c --- /dev/null +++ b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using GameCult.Eve.PluginFields; +using UnityEngine; + +#nullable enable + +namespace GameCult.Eve.UnityScene.Fields +{ + public static class EveUnityFieldsViewportFrame + { + public static bool TryResolve( + EveFieldsSplatsDocument source, + IReadOnlyDictionary props, + Vector2 cameraPosition, + int snapTextureWidth, + int snapTextureHeight, + out EveFieldsSplatsDocument projected, + out Vector2 center) + { + projected = source; + center = default; + if (source == null || source.Viewport == null) return false; + + var width = (float)(source.Viewport.MaxX - source.Viewport.MinX); + var height = (float)(source.Viewport.MaxY - source.Viewport.MinY); + if (!float.IsFinite(width) || !float.IsFinite(height) || width <= 0f || height <= 0f) + return false; + + center = new Vector2( + (float)((source.Viewport.MinX + source.Viewport.MaxX) * 0.5), + (float)((source.Viewport.MinY + source.Viewport.MaxY) * 0.5)); + if (!string.Equals(Prop(props, "viewportAnchor"), "active-camera.xz", StringComparison.Ordinal)) + return true; + + var span = PositiveInt(props, "span"); + var cellWorldSize = PositiveFloat(props, "cellWorldSize"); + var snapTexels = PositiveInt(props, "viewportSnapTexels"); + if (!TryValidateSpatialLattice( + width, height, snapTextureWidth, snapTextureHeight, + span, cellWorldSize, snapTexels)) + return false; + + center.x = SnapCameraCoordinate(cameraPosition.x, width, snapTextureWidth, snapTexels); + center.y = SnapCameraCoordinate(cameraPosition.y, height, snapTextureHeight, snapTexels); + projected = Project(source, center, width, height); + return true; + } + + public static float SnapCameraCoordinate( + float coordinate, + float viewportWidth, + int snapTargetWidth, + float snapTexels) + { + if (!float.IsFinite(coordinate) || !float.IsFinite(viewportWidth) || + !float.IsFinite(snapTexels) || viewportWidth <= 0f || + snapTargetWidth <= 0 || snapTexels <= 0f) + throw new ArgumentOutOfRangeException(nameof(viewportWidth)); + var snapLength = viewportWidth * snapTexels / snapTargetWidth; + return (int)(coordinate / snapLength) * snapLength; + } + + public static bool TryValidateSpatialLattice( + float viewportWidth, + float viewportHeight, + int gravityTextureWidth, + int gravityTextureHeight, + int span, + float cellWorldSize, + int gravityTexelsPerCell) + { + if (!float.IsFinite(viewportWidth) || !float.IsFinite(viewportHeight) || + !float.IsFinite(cellWorldSize) || viewportWidth <= 0f || viewportHeight <= 0f || + gravityTextureWidth <= 0 || gravityTextureHeight <= 0 || span <= 0 || + cellWorldSize <= 0f || gravityTexelsPerCell <= 0) + return false; + var expectedExtent = span * cellWorldSize; + var expectedCellFromX = viewportWidth * gravityTexelsPerCell / gravityTextureWidth; + var expectedCellFromY = viewportHeight * gravityTexelsPerCell / gravityTextureHeight; + const float tolerance = 0.0001f; + return Mathf.Abs(viewportWidth - expectedExtent) <= tolerance && + Mathf.Abs(viewportHeight - expectedExtent) <= tolerance && + Mathf.Abs(expectedCellFromX - cellWorldSize) <= tolerance && + Mathf.Abs(expectedCellFromY - cellWorldSize) <= tolerance; + } + + private static EveFieldsSplatsDocument Project( + EveFieldsSplatsDocument source, + Vector2 center, + float width, + float height) => + new EveFieldsSplatsDocument + { + Schema = source.Schema, + FrameId = source.FrameId, + PublishedAtUtc = source.PublishedAtUtc, + SimulationTimeSeconds = source.SimulationTimeSeconds, + RunId = source.RunId, + ZoneIndex = source.ZoneIndex, + ZoneName = source.ZoneName, + Viewport = new EveFieldsViewport + { + MinX = center.x - width * 0.5, + MinY = center.y - height * 0.5, + MaxX = center.x + width * 0.5, + MaxY = center.y + height * 0.5 + }, + Layers = source.Layers, + Splats = source.Splats + }; + + private static string Prop(IReadOnlyDictionary props, string key) => + props != null && props.TryGetValue(key, out var value) ? value ?? "" : ""; + + private static int PositiveInt(IReadOnlyDictionary props, string key) => + int.TryParse(Prop(props, key), NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) && value > 0 + ? value + : 0; + + private static float PositiveFloat(IReadOnlyDictionary props, string key) => + float.TryParse(Prop(props, key), NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && + float.IsFinite(value) && value > 0f + ? value + : 0f; + } +} diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs.meta b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs.meta new file mode 100644 index 0000000..9ad030b --- /dev/null +++ b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsViewportFrame.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e9db43b5b96144a3a0df57a791b53547 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsVolumeRenderer.cs b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsVolumeRenderer.cs index a132030..f4e823a 100644 --- a/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsVolumeRenderer.cs +++ b/packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveUnityFieldsVolumeRenderer.cs @@ -30,11 +30,14 @@ namespace GameCult.Eve.UnityScene.Fields private Matrix4x4 _previousViewProjection; private EveUnityFieldsVolumePass? _renderPass; private string _activeNodeId = ""; + private long _rasterizedFrameId = -1; + private Vector2 _rasterizedCenter = new Vector2(float.NaN, float.NaN); public long PresentedFrameId { get; private set; } = -1; public int PresentedLayerCount => _targets.Count(target => target.TargetTexture != null); public long CompositeCount { get; private set; } public bool UsesTemporalHistory => HasTemporalProgram(); + public Vector2 LastGridCenter { get; private set; } public static RenderPassEvent CompositionRenderPassEvent => RenderPassEvent.BeforeRenderingPostProcessing; public void Bind(EveUnityPlayableWorldClientHost host, IEveUnityFieldsSplatsDocumentSource? source) @@ -67,20 +70,47 @@ namespace GameCult.Eve.UnityScene.Fields if (document == null || document.FrameId < PresentedFrameId) return; _document = document; PresentedFrameId = document.FrameId; - RenderLayers(document); } - private void RenderLayers(EveFieldsSplatsDocument document) + private bool TryRenderLayers( + EveFieldsSplatsDocument document, + Camera camera, + out EveFieldsSplatsDocument projected) { + projected = document; var field = ActiveField(); - if (field == null) return; + if (field == null) return false; EnsureLayerRenderer(); EnsureTargets(field); var width = PositiveInt(field, "textureWidth", 512); var height = PositiveInt(field, "textureHeight", 512); - _layers!.Render(document, _targets, width, height); - if (!EnsureMaterial(field)) return; - ApplyFieldBindings(field, document); + var snapLayer = Prop(field, "viewportSnapLayer"); + var snapTarget = _targets.FirstOrDefault(target => + string.Equals(target.LayerKey, snapLayer, StringComparison.Ordinal)); + var snapWidth = snapTarget == null + ? 0 + : Mathf.Max(1, Mathf.RoundToInt(width * Mathf.Max(0.01f, snapTarget.WidthScale))); + var snapHeight = snapTarget == null + ? 0 + : Mathf.Max(1, Mathf.RoundToInt(height * Mathf.Max(0.01f, snapTarget.HeightScale))); + if (!EveUnityFieldsViewportFrame.TryResolve( + document, + field.Props, + new Vector2(camera.transform.position.x, camera.transform.position.z), + snapWidth, + snapHeight, + out projected, + out var center)) return false; + if (_rasterizedFrameId != document.FrameId || _rasterizedCenter != center) + { + _layers!.Render(projected, _targets, width, height); + _rasterizedFrameId = document.FrameId; + _rasterizedCenter = center; + } + LastGridCenter = center; + if (!EnsureMaterial(field)) return false; + ApplyFieldBindings(field, projected); + return true; } private void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera) @@ -101,10 +131,8 @@ namespace GameCult.Eve.UnityScene.Fields if (_host == null || _document == null || camera == null || _host.ActiveCameraTransform != camera.transform) return; - if (PresentedLayerCount == 0) RenderLayers(_document); var field = ActiveField(); - if (field == null || !EnsureMaterial(field)) return; - ApplyFieldBindings(field, _document); + if (field == null || !TryRenderLayers(_document, camera, out _)) return; camera.depthTextureMode |= DepthTextureMode.Depth; EnsureVolumeTextures(camera, field); if (_raymarchTexture == null || !ApplyViewportTextureScaleBindings(field, camera)) return; @@ -369,6 +397,9 @@ namespace GameCult.Eve.UnityScene.Fields _programMetadata = null; _assetTexturesByPort.Clear(); _activeNodeId = ""; + _rasterizedFrameId = -1; + _rasterizedCenter = new Vector2(float.NaN, float.NaN); + LastGridCenter = default; PresentedFrameId = -1; CompositeCount = 0; ResetTemporalHistory(); diff --git a/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveFieldsUnitySceneProjectionTests.cs b/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveFieldsUnitySceneProjectionTests.cs index bcd1fa3..5f5f4e2 100644 --- a/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveFieldsUnitySceneProjectionTests.cs +++ b/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveFieldsUnitySceneProjectionTests.cs @@ -62,9 +62,9 @@ namespace GameCult.Eve.UnityScene.Tests [TestCase(11f, 6f)] [TestCase(-11f, -6f)] [TestCase(5.999f, 0f)] - public void ParticleRendererSnapsToAnIntegralGravityPixelMultiple(float coordinate, float expected) + public void FieldViewportFrameSnapsToAnIntegralGravityPixelMultiple(float coordinate, float expected) { - Assert.That(EveUnityFieldsParticleRenderer.SnapCameraCoordinate( + Assert.That(EveUnityFieldsViewportFrame.SnapCameraCoordinate( coordinate, 1536f, 2048, @@ -72,19 +72,56 @@ namespace GameCult.Eve.UnityScene.Tests } [Test] - public void ParticleRendererAcceptsAlignedGravityAndParticleLattices() + public void FieldViewportFrameAcceptsAlignedGravityAndParticleLattices() { - Assert.That(EveUnityFieldsParticleRenderer.TryValidateSpatialLattice( + Assert.That(EveUnityFieldsViewportFrame.TryValidateSpatialLattice( 1536f, 1536f, 2048, 2048, 256, 6f, 8), Is.True); } [Test] - public void ParticleRendererRejectsAliasedGravityAndParticleLattices() + public void FieldViewportFrameRejectsAliasedGravityAndParticleLattices() { - Assert.That(EveUnityFieldsParticleRenderer.TryValidateSpatialLattice( + Assert.That(EveUnityFieldsViewportFrame.TryValidateSpatialLattice( 2000f, 2000f, 2048, 2048, 256, 6f, 8), Is.False); } + [Test] + public void FogAndParticlesResolveTheSameSnappedWorldFrame() + { + var document = new EveFieldsSplatsDocument + { + FrameId = 7, + Viewport = new EveFieldsViewport + { + MinX = -768, + MinY = -768, + MaxX = 768, + MaxY = 768 + } + }; + var contract = new Dictionary(StringComparer.Ordinal) + { + ["viewportAnchor"] = "active-camera.xz", + ["span"] = "256", + ["cellWorldSize"] = "6", + ["viewportSnapTexels"] = "8" + }; + + Assert.That(EveUnityFieldsViewportFrame.TryResolve( + document, contract, new Vector2(11f, -11f), 2048, 2048, + out var fog, out var fogCenter), Is.True); + Assert.That(EveUnityFieldsViewportFrame.TryResolve( + document, contract, new Vector2(11f, -11f), 2048, 2048, + out var particles, out var particleCenter), Is.True); + + Assert.That(fogCenter, Is.EqualTo(new Vector2(6f, -6f))); + Assert.That(particleCenter, Is.EqualTo(fogCenter)); + Assert.That(particles.Viewport.MinX, Is.EqualTo(fog.Viewport.MinX)); + Assert.That(particles.Viewport.MinY, Is.EqualTo(fog.Viewport.MinY)); + Assert.That(particles.Viewport.MaxX, Is.EqualTo(fog.Viewport.MaxX)); + Assert.That(particles.Viewport.MaxY, Is.EqualTo(fog.Viewport.MaxY)); + } + [Test] public void ParticleRendererFailsClosedWithoutCompleteNativeAbi() { diff --git a/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveUnitySceneSurfaceLowererTests.cs b/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveUnitySceneSurfaceLowererTests.cs index 319e1d0..c716cd1 100644 --- a/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveUnitySceneSurfaceLowererTests.cs +++ b/packages/org.gamecult.eve.unity-scene/Tests/Editor/EveUnitySceneSurfaceLowererTests.cs @@ -1154,7 +1154,12 @@ namespace GameCult.Eve.UnityScene.Tests PlayableArpgDocument( skyboxAssetRef: "material.environment.skybox", reflectionAssetRef: "texture.environment.reflection", - postProcessProfileAssetRef: "profile.environment.flight"), + postProcessProfileAssetRef: "profile.environment.flight", + cameraReconstruction: "temporal-reprojection.v1", + temporalQuality: "high", + temporalHistoryBlend: "0.99", + temporalJitterScale: "0.1", + temporalSharpening: "0"), Advertisement("aetheria.daemon.game"), "cultmesh://aetheria/eve/surfaces/aetheria.daemon.game", 1), @@ -1205,7 +1210,13 @@ namespace GameCult.Eve.UnityScene.Tests Assert.That(RenderSettings.reflectionIntensity, Is.EqualTo(1f).Within(0.001f)); Assert.That(camera.clearFlags, Is.EqualTo(CameraClearFlags.Skybox)); Assert.That(camera.GetComponent(), Is.Not.Null); - Assert.That(camera.GetComponent().renderPostProcessing, Is.True); + var cameraData = camera.GetComponent(); + Assert.That(cameraData.renderPostProcessing, Is.True); + Assert.That(cameraData.antialiasing, Is.EqualTo(AntialiasingMode.TemporalAntiAliasing)); + Assert.That(cameraData.taaSettings.quality, Is.EqualTo(TemporalAAQuality.High)); + Assert.That(cameraData.taaSettings.baseBlendFactor, Is.EqualTo(0.99f).Within(0.0001f)); + Assert.That(cameraData.taaSettings.jitterScale, Is.EqualTo(0.1f).Within(0.0001f)); + Assert.That(cameraData.taaSettings.contrastAdaptiveSharpening, Is.Zero); var postProcessTransform = hostObject.transform.Find("Eve World Post Process"); Assert.That(postProcessTransform, Is.Not.Null); Assert.That(postProcessTransform.GetComponent().sharedProfile, Is.SameAs(postProcessProfile)); @@ -1894,7 +1905,12 @@ namespace GameCult.Eve.UnityScene.Tests string cameraPositionDamping = "5", string cameraNearClipPlane = "0.3", string cameraFarClipPlane = "4096", - string cameraLookAt = "") + string cameraLookAt = "", + string cameraReconstruction = "", + string temporalQuality = "high", + string temporalHistoryBlend = "0", + string temporalJitterScale = "0", + string temporalSharpening = "0") { var playableChildren = new List { @@ -2062,6 +2078,11 @@ namespace GameCult.Eve.UnityScene.Tests ["reflectionAssetRef"] = reflectionAssetRef, ["reflectionIntensity"] = "1", ["postProcessProfileAssetRef"] = postProcessProfileAssetRef, + ["cameraReconstruction"] = cameraReconstruction, + ["temporalQuality"] = temporalQuality, + ["temporalHistoryBlend"] = temporalHistoryBlend, + ["temporalJitterScale"] = temporalJitterScale, + ["temporalSharpening"] = temporalSharpening, ["keyLightDirection"] = "0.4,-1,0.25", ["keyLightColor"] = "1,0.95,0.9", ["keyLightIntensity"] = "0.75",