mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-04 09:43:37 -04:00
* 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
60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using System.Text.Json.Serialization;
|
|
using System.Threading;
|
|
|
|
namespace Sandbox.Resources;
|
|
|
|
|
|
[Title( "Gradient - Linear" )]
|
|
[Icon( "gradient" )]
|
|
[ClassName( "gradientlinear" )]
|
|
public class LinearGradient : TextureGenerator
|
|
{
|
|
public Vector2Int Size { get; set; } = 128;
|
|
|
|
[Range( 0, 2 )]
|
|
public bool IsHdr { get; set; } = false;
|
|
|
|
[Range( 0, 360 )]
|
|
public float Angle { get; set; } = 0;
|
|
|
|
[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.White ), new Gradient.ColorFrame( 1, Color.Black ) );
|
|
|
|
[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 )
|
|
{
|
|
using var bitmap = new Bitmap( Size.x, Size.y, IsHdr && !ConvertHeightToNormals );
|
|
bitmap.Clear( Color.Transparent );
|
|
|
|
var hw = bitmap.Rect.Size.Length * 0.5f * Scale.Clamp( 0.0001f, 1000f );
|
|
var dir = Vector2.FromDegrees( Angle ) * hw;
|
|
var center = Center * bitmap.Rect.Size;
|
|
|
|
bitmap.SetLinearGradient( center - dir, center + dir, Gradient );
|
|
bitmap.DrawRect( bitmap.Rect );
|
|
|
|
if ( ConvertHeightToNormals )
|
|
{
|
|
using var normalMap = bitmap.HeightmapToNormalMap( NormalScale );
|
|
ValueTask.FromResult( normalMap.ToTexture() );
|
|
}
|
|
|
|
return ValueTask.FromResult( bitmap.ToTexture() );
|
|
}
|
|
}
|
|
|