2026-07-12 12:33:36 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using GameCult.Eve.Surface ;
2026-07-13 18:01:12 +01:00
using GameCult.Mesh ;
2026-07-12 12:33:36 +01:00
#nullable enable
namespace GameCult.Eve.UnityScene
{
public interface IEveUnityEntitySoaViewDocumentSource
{
2026-07-13 18:01:12 +01:00
event Action < EveEntitySoaViewDocument , ICultMeshBodyReadLease > EntityViewAvailable ;
2026-07-12 12:33:36 +01:00
}
/// <summary>Projects portable entity SoA generations into the ordinary Unity scene sink.</summary>
public sealed class EveUnityEntitySoaPresenter
{
private readonly IEveUnityPlayableWorldSceneSink _sink ;
private readonly HashSet < string > _known = new HashSet < string > ( StringComparer . Ordinal ) ;
public EveUnityEntitySoaPresenter ( IEveUnityPlayableWorldSceneSink sink )
{
_sink = sink ? ? throw new ArgumentNullException ( nameof ( sink ) ) ;
}
2026-07-13 18:01:12 +01:00
public void Apply ( EveEntitySoaViewDocument document , ICultMeshBodyReadLease lease )
2026-07-12 12:33:36 +01:00
{
2026-07-13 18:01:12 +01:00
using var view = EveUnityEntitySoaView . Open ( document , lease ) ;
2026-07-13 21:18:10 +01:00
var presented = new List < EveUnityPresentedEntity > ( ) ;
2026-07-12 12:33:36 +01:00
var next = new HashSet < string > ( StringComparer . Ordinal ) ;
for ( var row = 0 ; row < view . EntityCount ; row + + )
{
if ( ! view . TryReadInt32 ( "entity.index" , row , out var entityIndex ) ) continue ;
if ( view . TryReadByte ( "render.visibility" , row , out var visible ) & & visible = = 0 ) continue ;
if ( ! view . TryReadVector3 ( "transform.position" , row , out var position ) ) continue ;
view . TryReadFloat ( "transform.rotation.radians" , row , out var rotationRadians ) ;
2026-07-13 21:18:10 +01:00
view . TryReadVector3 ( "transform.velocity" , row , out var velocity ) ;
view . TryReadFloat ( "physics.body.radius" , row , out var radius ) ;
2026-07-12 12:33:36 +01:00
view . TryReadFloat ( "render.scale" , row , out var scale ) ;
2026-07-13 21:18:10 +01:00
view . TryReadInt32 ( "render.lod" , row , out var lod ) ;
2026-07-12 12:33:36 +01:00
view . TryReadUInt32 ( "render.group.id" , row , out var groupId ) ;
var renderGroup = view . RenderGroups . FirstOrDefault ( group = > group . GroupId = = groupId ) ;
2026-07-13 18:01:12 +01:00
var identity = document . Identities . FirstOrDefault ( item = > item . Index = = entityIndex ) ;
var entityId = identity ? . EntityId ? ? $"entity:{entityIndex}" ;
2026-07-12 12:33:36 +01:00
next . Add ( entityId ) ;
2026-07-13 18:06:15 +01:00
var assetRef = string . IsNullOrWhiteSpace ( identity ? . AssetRef )
? renderGroup ? . MeshAssetRef ? ? ""
: identity ! . AssetRef ;
2026-07-19 05:56:09 +01:00
var scalarState = new Dictionary < string , float > ( StringComparer . Ordinal ) ;
foreach ( var semantic in view . FloatSemantics )
if ( view . TryReadFloat ( semantic , row , out var scalar ) )
scalarState [ semantic ] = scalar ;
2026-07-13 21:18:10 +01:00
var fact = new EveUnityPresentedEntity (
entityIndex ,
entityId ,
identity ? . EntityKind ? ? "entity" ,
string . IsNullOrWhiteSpace ( identity ? . Label ) ? entityId : identity ! . Label ,
identity ? . Faction ? ? "" ,
position ,
rotationRadians * 57.2957795f ,
velocity ,
radius ,
scale > 0f ? scale : renderGroup ? . DefaultScale ? ? 1f ,
groupId ,
lod ,
identity ? . Selectable ? ? true ,
identity ? . Controllable ? ? false ,
2026-07-19 05:56:09 +01:00
assetRef ,
scalarState ) ;
2026-07-13 21:18:10 +01:00
presented . Add ( fact ) ;
if ( _sink is IEveUnityEntityGenerationSink )
continue ;
2026-07-12 12:33:36 +01:00
_sink . UpsertEntity (
2026-07-13 21:18:10 +01:00
fact . ToPlayableWorldEntity ( ) ,
2026-07-12 12:33:36 +01:00
new EveUnityPlayableWorldAssetBinding (
assetRef ,
"entity" ,
string . IsNullOrWhiteSpace ( assetRef ) ? "unity-generated-placeholder" : "provider-asset-ref" ) ) ;
}
2026-07-13 21:18:10 +01:00
if ( _sink is IEveUnityEntityGenerationSink generationSink )
{
generationSink . ApplyGeneration ( new EveUnityPresentedEntityGeneration (
document . ViewId , document . ProducerEpoch , document . Sequence , presented ) ) ;
}
else
{
foreach ( var entityId in _known . Where ( entityId = > ! next . Contains ( entityId ) ) . ToArray ( ) )
_sink . RemoveEntity ( entityId ) ;
}
2026-07-12 12:33:36 +01:00
_known . Clear ( ) ;
foreach ( var entityId in next ) _known . Add ( entityId ) ;
}
}
}