Files
sbox-public/engine/Sandbox.Engine/Resources/Compiling/TextureGenerator.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

67 lines
1.7 KiB
C#

using System.Threading;
namespace Sandbox.Resources;
public abstract class TextureGenerator : ResourceGenerator<Texture>
{
/// <summary>
/// Find an existing texture for this
/// </summary>
protected virtual ValueTask<Texture> CreateTexture( Options options, CancellationToken ct )
{
return default;
}
/// <summary>
/// Create a texture. Will replace a placeholder texture, which will turn into the generated texture later, if it's not immediately available.
/// </summary>
public sealed override Texture Create( Options options )
{
var tex = CreateTexture( options, default );
Texture output = default;
if ( !tex.IsCompletedSuccessfully )
{
// loading async
output = Texture.Create( 1, 1 ).WithData( new byte[4] { 0, 0, 0, 0 } ).Finish();
_ = output.ReplacementAsync( tex.AsTask() );
}
else
{
// finished immediately
output = tex.Result;
}
if ( output is null ) return default;
output.EmbeddedResource = CreateEmbeddedResource();
return output;
}
/// <summary>
/// Create a texture. Will wait until the texture is fully loaded and return when done.
/// </summary>
public sealed override async ValueTask<Texture> CreateAsync( Options options, CancellationToken token )
{
// Call it completely in a new thread
var output = await Task.Run( async () => await CreateTexture( options, token ) );
if ( output is null ) return default;
token.ThrowIfCancellationRequested();
output.EmbeddedResource = CreateEmbeddedResource();
return output;
}
public virtual EmbeddedResource? CreateEmbeddedResource()
{
return new EmbeddedResource
{
ResourceCompiler = "texture",
ResourceGenerator = DisplayInfo.For( this ).ClassName ?? GetType().FullName,
Data = Json.SerializeAsObject( this )
};
}
}