2026-07-15 17:12:11 +01:00
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.Linq ;
using GameCult.Eve.PluginFields ;
using UnityEngine ;
using UnityEngine.Rendering ;
2026-07-16 00:37:33 +01:00
using UnityEngine.Rendering.RenderGraphModule ;
using UnityEngine.Rendering.Universal ;
2026-07-15 17:12:11 +01:00
#nullable enable
namespace GameCult.Eve.UnityScene.Fields
{
2026-07-18 16:36:28 +01:00
public sealed class EveUnityFieldsVolumeRenderer : MonoBehaviour , IEveUnityRenderPassSource
2026-07-15 17:12:11 +01:00
{
private EveUnityPlayableWorldClientHost ? _host ;
private IEveUnityFieldsSplatsDocumentSource ? _source ;
private EveFieldsSplatsDocument ? _document ;
private EveFieldsSplatLayerRenderer ? _layers ;
private readonly List < EveFieldsSplatLayerTarget > _targets = new List < EveFieldsSplatLayerTarget > ( ) ;
private Material ? _material ;
private Shader ? _sourceShader ;
2026-07-15 19:59:37 +01:00
private IReadOnlyDictionary < string , string > ? _programMetadata ;
2026-07-15 21:47:50 +01:00
private readonly Dictionary < string , Texture > _assetTexturesByPort =
new Dictionary < string , Texture > ( StringComparer . Ordinal ) ;
2026-07-18 16:57:36 +01:00
private readonly Dictionary < int , CameraRenderState > _cameraRenderStates =
new Dictionary < int , CameraRenderState > ( ) ;
2026-07-16 00:37:33 +01:00
private EveUnityFieldsVolumePass ? _renderPass ;
2026-07-15 17:12:11 +01:00
private string _activeNodeId = "" ;
2026-07-16 17:44:29 +01:00
private long _rasterizedFrameId = - 1 ;
private Vector2 _rasterizedCenter = new Vector2 ( float . NaN , float . NaN ) ;
2026-07-15 17:12:11 +01:00
public long PresentedFrameId { get ; private set ; } = - 1 ;
public int PresentedLayerCount = > _targets . Count ( target = > target . TargetTexture ! = null ) ;
2026-07-15 17:31:02 +01:00
public long CompositeCount { get ; private set ; }
2026-07-15 20:59:30 +01:00
public bool UsesTemporalHistory = > HasTemporalProgram ( ) ;
2026-07-16 17:44:29 +01:00
public Vector2 LastGridCenter { get ; private set ; }
2026-07-16 00:37:33 +01:00
public static RenderPassEvent CompositionRenderPassEvent = > RenderPassEvent . BeforeRenderingPostProcessing ;
2026-07-18 16:36:28 +01:00
int IEveUnityRenderPassSource . RenderOrder = > 100 ;
2026-07-15 17:12:11 +01:00
public void Bind ( EveUnityPlayableWorldClientHost host , IEveUnityFieldsSplatsDocumentSource ? source )
{
if ( host = = null ) throw new ArgumentNullException ( nameof ( host ) ) ;
if ( _source ! = null ) _source . FieldsSplatsAvailable - = OnFieldsSplatsAvailable ;
_host = host ;
_source = source ;
if ( _source ! = null ) _source . FieldsSplatsAvailable + = OnFieldsSplatsAvailable ;
enabled = _source ! = null ;
}
2026-07-18 16:36:28 +01:00
private void OnEnable ( ) = > EveUnityRenderPassRegistry . Register ( this ) ;
2026-07-15 17:12:11 +01:00
private void OnDisable ( )
{
2026-07-18 16:36:28 +01:00
EveUnityRenderPassRegistry . Unregister ( this ) ;
2026-07-15 17:12:11 +01:00
ReleaseRenderState ( ) ;
}
private void OnDestroy ( )
{
if ( _source ! = null ) _source . FieldsSplatsAvailable - = OnFieldsSplatsAvailable ;
_source = null ;
_host = null ;
}
private void OnFieldsSplatsAvailable ( EveFieldsSplatsDocument document )
{
if ( document = = null | | document . FrameId < PresentedFrameId ) return ;
_document = document ;
PresentedFrameId = document . FrameId ;
}
2026-07-16 17:44:29 +01:00
private bool TryRenderLayers (
EveFieldsSplatsDocument document ,
Camera camera ,
out EveFieldsSplatsDocument projected )
2026-07-15 17:12:11 +01:00
{
2026-07-16 17:44:29 +01:00
projected = document ;
2026-07-15 17:12:11 +01:00
var field = ActiveField ( ) ;
2026-07-16 17:44:29 +01:00
if ( field = = null ) return false ;
2026-07-15 17:12:11 +01:00
EnsureLayerRenderer ( ) ;
EnsureTargets ( field ) ;
var width = PositiveInt ( field , "textureWidth" , 512 ) ;
var height = PositiveInt ( field , "textureHeight" , 512 ) ;
2026-07-16 17:44:29 +01:00
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 ;
2026-07-15 17:12:11 +01:00
}
2026-07-18 16:36:28 +01:00
bool IEveUnityRenderPassSource . TryEnqueuePass ( Camera camera , ScriptableRenderer renderer )
2026-07-16 00:37:33 +01:00
{
if ( _host = = null | | _document = = null | | camera = = null | |
2026-07-18 16:57:36 +01:00
! CanRenderCamera ( _host , camera ) )
2026-07-18 16:36:28 +01:00
return false ;
2026-07-16 00:37:33 +01:00
_renderPass ? ? = new EveUnityFieldsVolumePass ( this ) ;
2026-07-18 16:36:28 +01:00
renderer . EnqueuePass ( _renderPass ) ;
return true ;
2026-07-16 00:37:33 +01:00
}
2026-07-18 16:57:36 +01:00
internal static bool CanRenderCamera ( EveUnityPlayableWorldClientHost host , Camera camera ) = >
camera . cameraType = = CameraType . SceneView | | host . ActiveCameraTransform = = camera . transform ;
2026-07-16 00:37:33 +01:00
private void RenderVolume (
UnsafeCommandBuffer commands ,
Camera camera ,
TextureHandle colorTarget ,
TextureHandle depthTarget )
2026-07-15 17:12:11 +01:00
{
if ( _host = = null | | _document = = null | | camera = = null | |
2026-07-18 16:57:36 +01:00
! CanRenderCamera ( _host , camera ) )
2026-07-15 17:12:11 +01:00
return ;
var field = ActiveField ( ) ;
2026-07-16 17:44:29 +01:00
if ( field = = null | | ! TryRenderLayers ( _document , camera , out _ ) ) return ;
2026-07-15 17:12:11 +01:00
camera . depthTextureMode | = DepthTextureMode . Depth ;
2026-07-18 16:57:36 +01:00
var state = CameraState ( camera ) ;
EnsureVolumeTextures ( camera , field , state ) ;
if ( state . RaymarchTexture = = null | | ! ApplyViewportTextureScaleBindings ( field , camera ) ) return ;
ApplyQuality ( field , state . HistoryTextureIndex < 0 ) ;
2026-07-15 17:12:11 +01:00
var gpuProjection = GL . GetGPUProjectionMatrix ( camera . projectionMatrix , true ) ;
2026-07-16 21:41:18 +01:00
var nonRenderTargetProjection = GL . GetGPUProjectionMatrix ( camera . projectionMatrix , false ) ;
2026-07-15 20:59:30 +01:00
var viewProjection = gpuProjection * camera . worldToCameraMatrix ;
SetMatrixPort ( "cameraInverseViewProjection" , viewProjection . inverse ) ;
2026-07-16 19:25:57 +01:00
SetMatrixPort ( "cameraToWorld" , CameraTransformToWorld ( camera ) ) ;
2026-07-15 19:59:37 +01:00
SetVectorPort ( "cameraProjectionExtents" , ProjectionExtents ( camera ) ) ;
SetFloatPort ( "raymarchOffset" , Halton ( Time . frameCount & 1023 , 3 ) ) ;
2026-07-15 17:12:11 +01:00
2026-07-15 19:59:37 +01:00
var raymarchPass = ProgramPass ( "raymarch" ) ;
var compositePass = ProgramPass ( "composite" ) ;
2026-07-15 20:59:30 +01:00
var temporalPass = ProgramPass ( "temporal" ) ;
var useTemporalHistory = temporalPass > = 0 ;
var historyWriteIndex = - 1 ;
2026-07-18 16:57:36 +01:00
RenderTexture finalCloudTexture = state . RaymarchTexture ;
2026-07-15 20:59:30 +01:00
if ( useTemporalHistory )
{
2026-07-18 16:57:36 +01:00
historyWriteIndex = state . HistoryTextureIndex < 0 ? 0 : ( state . HistoryTextureIndex + 1 ) % state . HistoryTextures . Length ;
var historyReadTexture = state . HistoryTextureIndex < 0
? state . RaymarchTexture
: state . HistoryTextures [ state . HistoryTextureIndex ] ;
finalCloudTexture = state . HistoryTextures [ historyWriteIndex ] ! ;
SetTexturePort ( "currentSample" , state . RaymarchTexture ) ;
2026-07-15 20:59:30 +01:00
SetTexturePort ( "history" , historyReadTexture ! ) ;
2026-07-16 20:04:19 +01:00
SetMatrixPort (
"previousViewProjection" ,
2026-07-18 16:57:36 +01:00
state . HistoryTextureIndex < 0
2026-07-16 20:04:19 +01:00
? viewProjection
: ResolvePreviousViewProjection (
_programMetadata ,
2026-07-18 16:57:36 +01:00
state . PreviousViewProjection ,
2026-07-16 20:04:19 +01:00
gpuProjection ,
2026-07-16 21:41:18 +01:00
nonRenderTargetProjection ,
2026-07-18 16:57:36 +01:00
state . PreviousView ) ) ;
SetFloatPort ( "resetHistory" , state . HistoryTextureIndex < 0 ? 1f : 0f ) ;
2026-07-15 20:59:30 +01:00
}
SetTexturePort ( "cloud" , finalCloudTexture ) ;
2026-07-15 17:12:11 +01:00
2026-07-18 16:57:36 +01:00
commands . SetRenderTarget ( state . RaymarchTexture ) ;
commands . SetViewport ( new Rect ( 0f , 0f , state . RaymarchTexture . width , state . RaymarchTexture . height ) ) ;
2026-07-16 00:37:33 +01:00
commands . ClearRenderTarget ( false , true , Color . clear ) ;
commands . DrawProcedural ( Matrix4x4 . identity , _material ! , raymarchPass , MeshTopology . Triangles , 3 , 1 ) ;
2026-07-15 20:59:30 +01:00
if ( useTemporalHistory )
{
2026-07-16 00:37:33 +01:00
commands . SetRenderTarget ( finalCloudTexture ) ;
commands . SetViewport ( new Rect ( 0f , 0f , finalCloudTexture . width , finalCloudTexture . height ) ) ;
commands . ClearRenderTarget ( false , true , Color . clear ) ;
commands . DrawProcedural ( Matrix4x4 . identity , _material ! , temporalPass , MeshTopology . Triangles , 3 , 1 ) ;
2026-07-15 20:59:30 +01:00
}
2026-07-16 00:37:33 +01:00
if ( depthTarget . IsValid ( ) )
commands . SetRenderTarget ( colorTarget , depthTarget ) ;
else
commands . SetRenderTarget ( colorTarget ) ;
commands . SetViewport ( camera . pixelRect ) ;
commands . DrawProcedural ( Matrix4x4 . identity , _material ! , compositePass , MeshTopology . Triangles , 3 , 1 ) ;
2026-07-15 20:59:30 +01:00
if ( useTemporalHistory )
{
2026-07-18 16:57:36 +01:00
state . HistoryTextureIndex = historyWriteIndex ;
state . PreviousViewProjection = viewProjection ;
state . PreviousView = camera . worldToCameraMatrix ;
2026-07-15 20:59:30 +01:00
}
2026-07-15 17:31:02 +01:00
CompositeCount + + ;
2026-07-15 17:12:11 +01:00
}
private EveUnityFieldVolumeProjection ? ActiveField ( ) = >
_host ? . ActiveWorld ? . FieldVolumes . FirstOrDefault ( field = >
string . Equals ( field . DocumentSchema , EveFieldsSchemas . Splats , StringComparison . Ordinal ) ) ;
private bool EnsureMaterial ( EveUnityFieldVolumeProjection field )
{
var provider = _host ? . NativeAssetProvider ;
if ( provider = = null | | string . IsNullOrWhiteSpace ( field . MaterialAssetRef ) ) return false ;
var shader = provider . ResolveAsset (
new EveUnityPlayableWorldAssetBinding ( field . MaterialAssetRef , "" , "provider-asset-ref" ) ,
typeof ( Shader ) ) as Shader ;
if ( shader = = null | | ! shader . isSupported ) return false ;
2026-07-15 19:59:37 +01:00
var shaderBinding = new EveUnityPlayableWorldAssetBinding ( field . MaterialAssetRef , "" , "provider-asset-ref" ) ;
if ( provider is not IEveUnityNativeAssetMetadataProvider metadataProvider | |
! metadataProvider . TryResolveAssetMetadata ( shaderBinding , out var metadata ) )
return false ;
_programMetadata = metadata ;
2026-07-15 20:59:30 +01:00
if ( ! TryValidateProgramMetadata ( metadata , out _ ) )
2026-07-15 19:59:37 +01:00
return false ;
2026-07-15 17:12:11 +01:00
if ( _material ! = null & & ReferenceEquals ( shader , _sourceShader ) & &
string . Equals ( _activeNodeId , field . NodeId , StringComparison . Ordinal ) ) return true ;
if ( _material ! = null ) Destroy ( _material ) ;
_sourceShader = shader ;
_activeNodeId = field . NodeId ;
_material = new Material ( shader ) { hideFlags = HideFlags . DontSave } ;
2026-07-15 21:47:50 +01:00
_assetTexturesByPort . Clear ( ) ;
2026-07-15 20:59:30 +01:00
ResetTemporalHistory ( ) ;
2026-07-15 19:59:37 +01:00
foreach ( var parameter in ParseBindings ( Prop ( field , "floatParameters" ) ) )
2026-07-15 17:12:11 +01:00
{
2026-07-15 19:59:37 +01:00
if ( TryFloat ( parameter . Value , out var value ) ) SetFloatPort ( parameter . Key , value ) ;
2026-07-15 17:12:11 +01:00
}
2026-07-15 19:59:37 +01:00
foreach ( var parameter in ParseBindings ( Prop ( field , "vectorParameters" ) ) )
if ( TryVector ( parameter . Value , out var vector ) ) SetVectorPort ( parameter . Key , vector ) ;
2026-07-15 17:12:11 +01:00
foreach ( var binding in ParseBindings ( Prop ( field , "assetTextureBindings" ) ) )
{
var texture = provider . ResolveAsset (
new EveUnityPlayableWorldAssetBinding ( binding . Key , "" , "provider-asset-ref" ) ,
typeof ( Texture ) ) as Texture ;
2026-07-15 21:47:50 +01:00
if ( texture ! = null )
{
_assetTexturesByPort [ binding . Value ] = texture ;
SetTexturePort ( binding . Value , texture ) ;
}
2026-07-15 19:59:37 +01:00
}
foreach ( var feature in Prop ( field , "features" ) . Split ( ';' ) )
{
var logicalFeature = feature . Trim ( ) ;
if ( ! string . IsNullOrWhiteSpace ( logicalFeature ) & &
TryProgramValue ( $"unity.volume.feature.{logicalFeature}.keyword" , out var featureKeyword ) & &
! string . IsNullOrWhiteSpace ( featureKeyword ) )
_material . EnableKeyword ( featureKeyword ) ;
2026-07-15 17:12:11 +01:00
}
return true ;
}
2026-07-16 20:13:29 +01:00
private void ApplyQuality ( EveUnityFieldVolumeProjection field , bool bootstrap )
{
if ( _material = = null | | _programMetadata = = null ) return ;
foreach ( var binding in _programMetadata )
{
if ( binding . Key . StartsWith ( "unity.volume.quality." , StringComparison . Ordinal ) & &
binding . Key . EndsWith ( ".keyword" , StringComparison . Ordinal ) & &
! string . IsNullOrWhiteSpace ( binding . Value ) )
_material . DisableKeyword ( binding . Value ) ;
}
var quality = ResolveQuality ( _programMetadata , Prop ( field , "quality" ) , bootstrap ) ;
if ( TryProgramValue ( $"unity.volume.quality.{quality}.keyword" , out var keyword ) & &
! string . IsNullOrWhiteSpace ( keyword ) )
_material . EnableKeyword ( keyword ) ;
}
public static string ResolveQuality (
IReadOnlyDictionary < string , string > ? metadata ,
string steadyQuality ,
bool bootstrap )
{
if ( bootstrap & & metadata ! = null & &
metadata . TryGetValue ( "unity.volume.quality.bootstrap" , out var bootstrapQuality ) & &
! string . IsNullOrWhiteSpace ( bootstrapQuality ) )
return bootstrapQuality . Trim ( ) . ToLowerInvariant ( ) ;
return ( steadyQuality ? ? "" ) . Trim ( ) . ToLowerInvariant ( ) ;
}
2026-07-15 17:12:11 +01:00
private void ApplyFieldBindings ( EveUnityFieldVolumeProjection field , EveFieldsSplatsDocument document )
{
if ( _material = = null ) return ;
var bindings = ParseBindings ( Prop ( field , "layerBindings" ) ) ;
foreach ( var binding in bindings )
{
var target = _targets . FirstOrDefault ( value = > string . Equals ( value . LayerKey , binding . Key , StringComparison . Ordinal ) ) ;
2026-07-15 19:59:37 +01:00
if ( target ? . TargetTexture ! = null ) SetTexturePort ( binding . Value , target . TargetTexture ) ;
2026-07-15 17:12:11 +01:00
}
2026-07-15 19:59:37 +01:00
var viewport = document . Viewport ;
var width = viewport . MaxX - viewport . MinX ;
var height = viewport . MaxY - viewport . MinY ;
SetVectorPort ( "viewportTransform" , new Vector4 (
( float ) ( ( viewport . MinX + viewport . MaxX ) * 0.5 ) ,
( float ) ( ( viewport . MinY + viewport . MaxY ) * 0.5 ) ,
( float ) width ,
( float ) height ) ) ;
2026-07-16 01:06:07 +01:00
foreach ( var binding in ParseBindings ( Prop ( field , "documentFloatBindings" ) ) )
{
if ( TryEvaluateDocumentFloatBinding (
document ,
binding . Key ,
binding . Value ,
out var port ,
out var value ) )
SetFloatPort ( port , value ) ;
}
}
public static bool TryEvaluateDocumentFloatBinding (
EveFieldsSplatsDocument ? document ,
string source ,
string descriptor ,
out string port ,
out float value )
{
port = "" ;
value = 0f ;
if ( document = = null ) return false ;
var values = ( descriptor ? ? "" ) . Split ( ',' ) ;
if ( values . Length ! = 3 | |
string . IsNullOrWhiteSpace ( values [ 0 ] ) | |
! TryFloat ( values [ 1 ] . Trim ( ) , out var scale ) | |
! TryFloat ( values [ 2 ] . Trim ( ) , out var offset ) )
return false ;
double sourceValue ;
switch ( ( source ? ? "" ) . Trim ( ) )
{
case "simulationTimeSeconds" :
sourceValue = document . SimulationTimeSeconds ;
break ;
default :
return false ;
}
port = values [ 0 ] . Trim ( ) ;
value = ( float ) ( sourceValue * scale + offset ) ;
return float . IsFinite ( value ) ;
2026-07-15 17:12:11 +01:00
}
private void EnsureTargets ( EveUnityFieldVolumeProjection field )
{
var bindings = ParseBindings ( Prop ( field , "layerBindings" ) ) ;
2026-07-16 00:53:35 +01:00
var descriptors = ParseBindings ( Prop ( field , "layerTargetDescriptors" ) ) ;
2026-07-15 17:12:11 +01:00
if ( string . Equals ( _activeNodeId , field . NodeId , StringComparison . Ordinal ) & &
2026-07-16 00:53:35 +01:00
_targets . Count = = bindings . Count & &
_targets . All ( target = > bindings . ContainsKey ( target . LayerKey ) ) )
{
foreach ( var target in _targets )
ApplyLayerTargetDescriptor ( target , descriptors ) ;
return ;
}
2026-07-15 17:12:11 +01:00
_targets . Clear ( ) ;
foreach ( var binding in bindings )
2026-07-16 00:53:35 +01:00
{
var target = new EveFieldsSplatLayerTarget { LayerKey = binding . Key } ;
ApplyLayerTargetDescriptor ( target , descriptors ) ;
_targets . Add ( target ) ;
}
}
private static void ApplyLayerTargetDescriptor (
EveFieldsSplatLayerTarget target ,
IReadOnlyDictionary < string , string > descriptors )
{
target . WidthScale = 1f ;
target . HeightScale = 1f ;
target . UseMipMaps = false ;
target . FilterMode = FilterMode . Bilinear ;
if ( descriptors . TryGetValue ( target . LayerKey , out var descriptor ) )
TryApplyLayerTargetDescriptor ( target , descriptor ) ;
}
public static bool TryApplyLayerTargetDescriptor ( EveFieldsSplatLayerTarget ? target , string descriptor )
{
if ( target = = null ) return false ;
var values = ( descriptor ? ? "" ) . Split ( ',' ) ;
if ( values . Length ! = 4 | |
! TryFloat ( values [ 0 ] . Trim ( ) , out var widthScale ) | | widthScale < = 0 | |
! TryFloat ( values [ 1 ] . Trim ( ) , out var heightScale ) | | heightScale < = 0 | |
! bool . TryParse ( values [ 2 ] . Trim ( ) , out var useMipMaps ) | |
! Enum . TryParse ( values [ 3 ] . Trim ( ) , true , out FilterMode filterMode ) )
return false ;
target . WidthScale = widthScale ;
target . HeightScale = heightScale ;
target . UseMipMaps = useMipMaps ;
target . FilterMode = filterMode ;
return true ;
2026-07-15 17:12:11 +01:00
}
private void EnsureLayerRenderer ( )
{
if ( _layers ! = null ) return ;
_layers = GetComponent < EveFieldsSplatLayerRenderer > ( ) ;
if ( _layers = = null ) _layers = gameObject . AddComponent < EveFieldsSplatLayerRenderer > ( ) ;
}
2026-07-18 16:57:36 +01:00
private CameraRenderState CameraState ( Camera camera )
{
var cameraId = camera . GetInstanceID ( ) ;
if ( _cameraRenderStates . TryGetValue ( cameraId , out var state ) ) return state ;
state = new CameraRenderState ( ) ;
_cameraRenderStates . Add ( cameraId , state ) ;
return state ;
}
private void EnsureVolumeTextures (
Camera camera ,
EveUnityFieldVolumeProjection field ,
CameraRenderState state )
2026-07-15 17:12:11 +01:00
{
var downsample = Mathf . Clamp ( NonNegativeInt ( field , "downsample" , 1 ) , 0 , 3 ) ;
var width = Mathf . Max ( 1 , camera . pixelWidth > > downsample ) ;
var height = Mathf . Max ( 1 , camera . pixelHeight > > downsample ) ;
2026-07-18 16:57:36 +01:00
var resetHistory = EnsureRenderTexture (
ref state . RaymarchTexture ,
width ,
height ,
$"Eve Fields Volume Raymarch {camera.GetInstanceID()}" ) ;
2026-07-15 20:59:30 +01:00
if ( HasTemporalProgram ( ) )
2026-07-15 17:12:11 +01:00
{
2026-07-18 16:57:36 +01:00
resetHistory | = EnsureRenderTexture ( ref state . HistoryTextures [ 0 ] , width , height , "Eve Fields Volume History A" ) ;
resetHistory | = EnsureRenderTexture ( ref state . HistoryTextures [ 1 ] , width , height , "Eve Fields Volume History B" ) ;
2026-07-15 20:59:30 +01:00
}
else
{
2026-07-18 16:57:36 +01:00
ReleaseRenderTexture ( ref state . HistoryTextures [ 0 ] ) ;
ReleaseRenderTexture ( ref state . HistoryTextures [ 1 ] ) ;
2026-07-15 20:59:30 +01:00
resetHistory = true ;
}
2026-07-18 16:57:36 +01:00
if ( resetHistory ) ResetTemporalHistory ( state ) ;
2026-07-15 17:12:11 +01:00
}
private void ReleaseRenderState ( )
{
2026-07-18 16:57:36 +01:00
foreach ( var state in _cameraRenderStates . Values )
{
ReleaseRenderTexture ( ref state . RaymarchTexture ) ;
ReleaseRenderTexture ( ref state . HistoryTextures [ 0 ] ) ;
ReleaseRenderTexture ( ref state . HistoryTextures [ 1 ] ) ;
}
_cameraRenderStates . Clear ( ) ;
2026-07-15 17:12:11 +01:00
if ( _material ! = null ) { Destroy ( _material ) ; _material = null ; }
_sourceShader = null ;
2026-07-15 19:59:37 +01:00
_programMetadata = null ;
2026-07-15 21:47:50 +01:00
_assetTexturesByPort . Clear ( ) ;
2026-07-15 17:12:11 +01:00
_activeNodeId = "" ;
2026-07-16 17:44:29 +01:00
_rasterizedFrameId = - 1 ;
_rasterizedCenter = new Vector2 ( float . NaN , float . NaN ) ;
LastGridCenter = default ;
2026-07-15 17:12:11 +01:00
PresentedFrameId = - 1 ;
2026-07-15 17:31:02 +01:00
CompositeCount = 0 ;
2026-07-15 20:59:30 +01:00
ResetTemporalHistory ( ) ;
}
2026-07-16 00:37:33 +01:00
private sealed class EveUnityFieldsVolumePass : ScriptableRenderPass
{
private readonly EveUnityFieldsVolumeRenderer _owner ;
private sealed class PassData
{
internal Camera Camera = null ! ;
internal EveUnityFieldsVolumeRenderer Owner = null ! ;
internal TextureHandle ColorTarget ;
internal TextureHandle DepthTarget ;
}
internal EveUnityFieldsVolumePass ( EveUnityFieldsVolumeRenderer owner )
{
_owner = owner ;
renderPassEvent = CompositionRenderPassEvent ;
ConfigureInput ( ScriptableRenderPassInput . Depth ) ;
}
public override void RecordRenderGraph ( RenderGraph renderGraph , ContextContainer frameData )
{
var cameraData = frameData . Get < UniversalCameraData > ( ) ;
if ( cameraData . camera = = null | | _owner . _host = = null | |
2026-07-18 16:57:36 +01:00
! CanRenderCamera ( _owner . _host , cameraData . camera ) )
2026-07-16 00:37:33 +01:00
return ;
var resourceData = frameData . Get < UniversalResourceData > ( ) ;
var colorTarget = resourceData . activeColorTexture ;
if ( ! colorTarget . IsValid ( ) ) return ;
using var builder = renderGraph . AddUnsafePass < PassData > ( "Eve Fields Volume" , out var passData ) ;
passData . Camera = cameraData . camera ;
passData . Owner = _owner ;
passData . ColorTarget = colorTarget ;
passData . DepthTarget = resourceData . activeDepthTexture ;
2026-07-18 16:36:28 +01:00
builder . UseTexture ( passData . ColorTarget , AccessFlags . Write ) ;
2026-07-16 00:37:33 +01:00
if ( passData . DepthTarget . IsValid ( ) )
builder . UseTexture ( passData . DepthTarget , AccessFlags . Read ) ;
builder . AllowPassCulling ( false ) ;
builder . SetRenderFunc ( static ( PassData data , UnsafeGraphContext context ) = >
data . Owner . RenderVolume ( context . cmd , data . Camera , data . ColorTarget , data . DepthTarget ) ) ;
}
}
2026-07-15 20:59:30 +01:00
private void ResetTemporalHistory ( )
{
2026-07-18 16:57:36 +01:00
foreach ( var state in _cameraRenderStates . Values ) ResetTemporalHistory ( state ) ;
}
private static void ResetTemporalHistory ( CameraRenderState state )
{
state . HistoryTextureIndex = - 1 ;
state . PreviousViewProjection = Matrix4x4 . identity ;
state . PreviousView = Matrix4x4 . identity ;
}
private sealed class CameraRenderState
{
internal RenderTexture ? RaymarchTexture ;
internal readonly RenderTexture ? [ ] HistoryTextures = new RenderTexture ? [ 2 ] ;
internal int HistoryTextureIndex = - 1 ;
internal Matrix4x4 PreviousViewProjection = Matrix4x4 . identity ;
internal Matrix4x4 PreviousView = Matrix4x4 . identity ;
2026-07-16 20:04:19 +01:00
}
public static Matrix4x4 ResolvePreviousViewProjection (
IReadOnlyDictionary < string , string > ? metadata ,
Matrix4x4 previousViewProjection ,
Matrix4x4 currentGpuProjection ,
2026-07-16 21:41:18 +01:00
Matrix4x4 currentNonRenderTargetProjection ,
2026-07-16 20:04:19 +01:00
Matrix4x4 previousView )
{
2026-07-16 21:41:18 +01:00
if ( metadata = = null | |
! metadata . TryGetValue ( "unity.volume.matrixSemantic.previousViewProjection" , out var semantic ) )
return previousViewProjection ;
if ( string . Equals ( semantic , "current-projection.previous-view.v1" , StringComparison . Ordinal ) )
return currentGpuProjection * previousView ;
if ( string . Equals ( semantic , "non-render-target-projection.previous-view.v1" , StringComparison . Ordinal ) )
return currentNonRenderTargetProjection * previousView ;
return previousViewProjection ;
2026-07-15 20:59:30 +01:00
}
private bool HasTemporalProgram ( ) = > ProgramPass ( "temporal" ) > = 0 ;
2026-07-15 21:47:50 +01:00
private bool ApplyViewportTextureScaleBindings ( EveUnityFieldVolumeProjection field , Camera camera )
{
foreach ( var binding in ParseBindings ( Prop ( field , "viewportTextureScaleBindings" ) ) )
{
if ( string . IsNullOrWhiteSpace ( ProgramPort ( "vector" , binding . Key ) ) | |
string . IsNullOrWhiteSpace ( ProgramPort ( "texture" , binding . Value ) ) | |
! _assetTexturesByPort . TryGetValue ( binding . Value , out var texture ) | |
! TryComputeViewportTextureScale ( camera . pixelWidth , camera . pixelHeight , texture , out var scale ) )
return false ;
SetVectorPort ( binding . Key , scale ) ;
}
return true ;
}
public static bool TryComputeViewportTextureScale (
int viewportWidth ,
int viewportHeight ,
Texture ? texture ,
out Vector4 scale )
{
scale = default ;
if ( texture = = null | | texture . width < = 0 | | texture . height < = 0 ) return false ;
scale = new Vector4 (
( float ) Math . Max ( 1 , viewportWidth ) / texture . width ,
( float ) Math . Max ( 1 , viewportHeight ) / texture . height ,
0f ,
0f ) ;
return true ;
}
2026-07-15 20:59:30 +01:00
private static bool EnsureRenderTexture ( ref RenderTexture ? texture , int width , int height , string name )
{
if ( texture ! = null & & texture . width = = width & & texture . height = = height ) return false ;
ReleaseRenderTexture ( ref texture ) ;
texture = new RenderTexture ( width , height , 0 , RenderTextureFormat . ARGBFloat )
{
name = name ,
filterMode = FilterMode . Bilinear ,
wrapMode = TextureWrapMode . Clamp
} ;
texture . Create ( ) ;
return true ;
}
private static void ReleaseRenderTexture ( ref RenderTexture ? texture )
{
if ( texture = = null ) return ;
texture . Release ( ) ;
Destroy ( texture ) ;
texture = null ;
}
public static bool TryValidateProgramMetadata (
IReadOnlyDictionary < string , string > ? metadata ,
out string error )
{
error = "" ;
if ( metadata = = null ) return Fail ( "Native volume program metadata is missing." , out error ) ;
if ( ! ValidPass ( metadata , "raymarch" ) ) return Fail ( "Native volume raymarch pass is missing." , out error ) ;
if ( ! ValidPass ( metadata , "composite" ) ) return Fail ( "Native volume composite pass is missing." , out error ) ;
if ( ! ValidPort ( metadata , "texture" , "cloud" ) | |
! ValidPort ( metadata , "matrix" , "cameraInverseViewProjection" ) | |
! ValidPort ( metadata , "vector" , "cameraProjectionExtents" ) | |
! ValidPort ( metadata , "vector" , "viewportTransform" ) | |
! ValidPort ( metadata , "float" , "raymarchOffset" ) )
return Fail ( "Native volume program is missing a required raymarch or composite port." , out error ) ;
if ( ! metadata . ContainsKey ( "unity.volume.pass.temporal" ) ) return true ;
if ( ! ValidPass ( metadata , "temporal" ) ) return Fail ( "Native volume temporal pass is invalid." , out error ) ;
if ( ! ValidPort ( metadata , "texture" , "currentSample" ) | |
! ValidPort ( metadata , "texture" , "history" ) | |
! ValidPort ( metadata , "matrix" , "previousViewProjection" ) | |
! ValidPort ( metadata , "float" , "resetHistory" ) )
return Fail ( "Native volume temporal pass is missing a required history port." , out error ) ;
2026-07-16 20:04:19 +01:00
if ( metadata . TryGetValue ( "unity.volume.matrixSemantic.previousViewProjection" , out var semantic ) & &
! string . Equals ( semantic , "previous-view-projection.v1" , StringComparison . Ordinal ) & &
2026-07-16 21:41:18 +01:00
! string . Equals ( semantic , "current-projection.previous-view.v1" , StringComparison . Ordinal ) & &
! string . Equals ( semantic , "non-render-target-projection.previous-view.v1" , StringComparison . Ordinal ) )
2026-07-16 20:04:19 +01:00
return Fail ( "Native volume temporal pass has an unsupported previous-view-projection semantic." , out error ) ;
2026-07-16 20:13:29 +01:00
if ( metadata . TryGetValue ( "unity.volume.quality.bootstrap" , out var bootstrapQuality ) & &
( ! metadata . TryGetValue (
$"unity.volume.quality.{bootstrapQuality.Trim().ToLowerInvariant()}.keyword" ,
out var bootstrapKeyword ) | | string . IsNullOrWhiteSpace ( bootstrapKeyword ) ) )
return Fail ( "Native volume temporal pass has no keyword for its bootstrap quality." , out error ) ;
2026-07-15 20:59:30 +01:00
return true ;
}
private static bool ValidPass ( IReadOnlyDictionary < string , string > metadata , string pass ) = >
metadata . TryGetValue ( $"unity.volume.pass.{pass}" , out var value ) & &
int . TryParse ( value , NumberStyles . Integer , CultureInfo . InvariantCulture , out var parsed ) & & parsed > = 0 ;
private static bool ValidPort ( IReadOnlyDictionary < string , string > metadata , string kind , string port ) = >
metadata . TryGetValue ( $"unity.volume.{kind}Port.{port}" , out var value ) & &
! string . IsNullOrWhiteSpace ( value ) ;
private static bool Fail ( string message , out string error )
{
error = message ;
return false ;
2026-07-15 17:12:11 +01:00
}
2026-07-15 19:59:37 +01:00
private void SetMatrixPort ( string port , Matrix4x4 value )
2026-07-15 17:12:11 +01:00
{
2026-07-15 19:59:37 +01:00
var property = ProgramPort ( "matrix" , port ) ;
2026-07-15 17:12:11 +01:00
if ( ! string . IsNullOrWhiteSpace ( property ) ) _material ! . SetMatrix ( property , value ) ;
}
2026-07-15 19:59:37 +01:00
private void SetVectorPort ( string port , Vector4 value )
2026-07-15 17:12:11 +01:00
{
2026-07-15 19:59:37 +01:00
var property = ProgramPort ( "vector" , port ) ;
2026-07-15 17:12:11 +01:00
if ( ! string . IsNullOrWhiteSpace ( property ) ) _material ! . SetVector ( property , value ) ;
}
2026-07-15 19:59:37 +01:00
private void SetFloatPort ( string port , float value )
2026-07-15 17:12:11 +01:00
{
2026-07-15 19:59:37 +01:00
var property = ProgramPort ( "float" , port ) ;
2026-07-15 17:12:11 +01:00
if ( ! string . IsNullOrWhiteSpace ( property ) ) _material ! . SetFloat ( property , value ) ;
}
2026-07-15 19:59:37 +01:00
private void SetTexturePort ( string port , Texture value )
{
var property = ProgramPort ( "texture" , port ) ;
if ( ! string . IsNullOrWhiteSpace ( property ) ) _material ! . SetTexture ( property , value ) ;
}
private string ProgramPort ( string kind , string port ) = >
TryProgramValue ( $"unity.volume.{kind}Port.{port}" , out var value ) ? value : "" ;
private int ProgramPass ( string pass ) = >
TryProgramValue ( $"unity.volume.pass.{pass}" , out var value ) & &
int . TryParse ( value , NumberStyles . Integer , CultureInfo . InvariantCulture , out var parsed ) & & parsed > = 0
? parsed
: - 1 ;
private bool TryProgramValue ( string key , out string value )
{
value = "" ;
return _programMetadata ! = null & & _programMetadata . TryGetValue ( key , out value ! ) ;
}
2026-07-15 17:12:11 +01:00
private static Vector4 ProjectionExtents ( Camera camera )
{
var y = camera . orthographic ? camera . orthographicSize : Mathf . Tan ( 0.5f * Mathf . Deg2Rad * camera . fieldOfView ) ;
return new Vector4 ( y * camera . aspect , y , 0f , 0f ) ;
}
2026-07-16 19:25:57 +01:00
private static Matrix4x4 CameraTransformToWorld ( Camera camera ) = >
camera . transform . localToWorldMatrix ;
2026-07-15 17:12:11 +01:00
private static float Halton ( int index , int radix )
{
var result = 0f ;
var fraction = 1f / radix ;
while ( index > 0 ) { result + = ( index % radix ) * fraction ; index / = radix ; fraction / = radix ; }
return result ;
}
private static Dictionary < string , string > ParseBindings ( string value )
{
var result = new Dictionary < string , string > ( StringComparer . Ordinal ) ;
foreach ( var pair in ( value ? ? "" ) . Split ( ';' ) )
{
var separator = pair . IndexOf ( '=' ) ;
if ( separator < = 0 | | separator > = pair . Length - 1 ) continue ;
result [ pair . Substring ( 0 , separator ) . Trim ( ) ] = pair . Substring ( separator + 1 ) . Trim ( ) ;
}
return result ;
}
private static string Prop ( EveUnityFieldVolumeProjection field , string key ) = >
field . Props . TryGetValue ( key , out var value ) ? value ? ? "" : "" ;
private static int PositiveInt ( EveUnityFieldVolumeProjection field , string key , int fallback ) = >
int . TryParse ( Prop ( field , key ) , NumberStyles . Integer , CultureInfo . InvariantCulture , out var value ) & & value > 0 ? value : fallback ;
private static int NonNegativeInt ( EveUnityFieldVolumeProjection field , string key , int fallback ) = >
int . TryParse ( Prop ( field , key ) , NumberStyles . Integer , CultureInfo . InvariantCulture , out var value ) & & value > = 0 ? value : fallback ;
private static bool TryFloat ( string value , out float result ) = >
float . TryParse ( value , NumberStyles . Float , CultureInfo . InvariantCulture , out result ) & & ! float . IsNaN ( result ) & & ! float . IsInfinity ( result ) ;
private static bool TryVector ( string value , out Vector4 result )
{
result = default ;
var values = ( value ? ? "" ) . Split ( ',' ) ;
if ( values . Length < 2 | | values . Length > 4 ) return false ;
var parsed = new float [ 4 ] ;
for ( var index = 0 ; index < values . Length ; index + + )
if ( ! TryFloat ( values [ index ] , out parsed [ index ] ) ) return false ;
result = new Vector4 ( parsed [ 0 ] , parsed [ 1 ] , parsed [ 2 ] , parsed [ 3 ] ) ;
return true ;
}
}
}