using MoonTools.ECS;
using Nerfed.Runtime.Scene.Streaming;
using System;
namespace Nerfed.Runtime.Resources;
///
/// A typical rendering preparation system that natively resolves and requests
/// asynchronous background loading for its own required assets, removing the
/// need for a monolithic generic AssetStreaming manager.
///
public class SampleRenderSystem : MoonTools.ECS.System
{
private readonly Filter _meshVisualsFilter;
public SampleRenderSystem(World world) : base(world) {
_meshVisualsFilter = FilterBuilder
.Include()
// Always ignore chunk entities technically "unloading" from RAM
.Exclude()
.Build();
}
public override void Update(TimeSpan delta) {
foreach(Entity entity in _meshVisualsFilter.Entities) {
SampleMeshVisualComponent visualComp = Get(entity);
// 1. Resolve State
ResourceState vertState = ResourceManager.GetState(visualComp.VertexShader.AssetId);
ResourceState fragState = ResourceManager.GetState(visualComp.FragmentShader.AssetId);
// 2. Asynchronously request assets if they don't exist in memory yet
if(vertState == ResourceState.Unloaded) {
ResourceManager.Retain(visualComp.VertexShader.AssetId, "Unknown/Path");
}
if(fragState == ResourceState.Unloaded) {
ResourceManager.Retain(visualComp.FragmentShader.AssetId, "Unknown/Path");
}
// 3. Prevent rendering logic unless ALL strictly required assets are fully mapped
bool isReadyToDraw = vertState == ResourceState.Loaded && fragState == ResourceState.Loaded;
if(isReadyToDraw) {
// At this exact point, you can safely assume:
// 1) The background loading threads are 100% finished processing these shaders.
// 2) The GraphicsDevice can safely extract the native handle.
// e.g. GraphicsDevice.BindShader(visualComp.VertexShader.AssetId);
// e.g. GraphicsDevice.DrawPolygons(...);
}
}
}
}