Files
sbox-public/engine/Sandbox.Engine/Resources/Textures/Generators/RadialGradientTextureGenerator.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

54 lines
1.5 KiB
C#

using System.Text.Json.Serialization;
using System.Threading;
namespace Sandbox.Resources;
[Title( "Gradient - Radial" )]
[Icon( "vignette" )]
[ClassName( "gradientradial" )]
[Expose]
public class RadialGradient : TextureGenerator
{
public Vector2Int Size { get; set; } = 128;
[Range( 0, 2 )]
public bool IsHdr { get; set; } = false;
[Range( 0, 2 )]
public float Scale { get; set; } = 1;
[Range( 0, 1 )]
public Vector2 Center { get; set; } = 0.5f;
[KeyProperty]
public Gradient Gradient { get; set; } = new Gradient( new Gradient.ColorFrame( 0.0f, Color.Cyan ), new Gradient.ColorFrame( 0.2f, Color.Red ), new Gradient.ColorFrame( 1.0f, Color.Yellow ) );
[Header( "Normal Map" ), Title( "Height to Normal" )]
public bool ConvertHeightToNormals { get; set; }
[ShowIf( "ConvertHeightToNormals", true )]
public float NormalScale { get; set; } = 1;
[Hide, JsonIgnore]
public override bool CacheToDisk => true;
protected override ValueTask<Texture> CreateTexture( Options options, CancellationToken ct )
{
var w = Size.x.Clamp( 1, 1024 * 4 );
var h = Size.y.Clamp( 1, 1024 * 4 );
var bitmap = new Bitmap( w, h, IsHdr && !ConvertHeightToNormals );
bitmap.Clear( Color.Transparent );
bitmap.SetRadialGradient( bitmap.Rect.Size * Center, Scale * bitmap.Width, Gradient );
bitmap.DrawRect( bitmap.Rect );
if ( ConvertHeightToNormals )
{
bitmap = bitmap.HeightmapToNormalMap( NormalScale );
}
return ValueTask.FromResult( bitmap.ToTexture() );
}
}