Files
sbox-public/engine/Sandbox.Engine/Resources/Textures/Generators/RadialGradientTextureGenerator.cs
Lorenz Junglas 236b95e0ed Disposable diagnostic (#3693)
* Enable CA2000 in editorconfig

* Remove unused CaptureStdOut class

* Add missing dispose calls in Sandbox.Tools

* Add missing dispose calls in tests

* Add missing dispose calls in launchers

* Add missing dispose calls in Topten.RichtTextKit

* Add missing dispose calls in Engine

* Add missing dispose calls in SboxBuild

* Add nullchecks to a few dispose calls

* Fix more missing disposal calls and leaks

* Disable CA2000 for Textures

* Fix disposing too early in ImageFileTextureGenerator

* Fix disposing codec, ownership is transferred to animation

* Add missing using in ByteStream benchmark
2026-01-12 21:59:15 +01:00

55 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 );
using 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 )
{
using var normalMap = bitmap.HeightmapToNormalMap( NormalScale );
return ValueTask.FromResult( normalMap.ToTexture() );
}
return ValueTask.FromResult( bitmap.ToTexture() );
}
}