Begins creation of a render target. Finish by calling Sandbox.TextureBuilder.Create(System.String,System.Boolean,System.ReadOnlySpan{System.Byte},System.Int32).
Here's a simple implementation of a viewport using this, inheriting from Image:
using Sandbox;
using Sandbox.UI;
/// <summary>
/// A box that will display what a Camera sees.
/// </summary>
public partial class ViewPort : Image
{
/// <summary>The camera that will render to our texture.</summary>
public CameraComponent Camera { get; set; }
/// <summary>The image format that the texture will use.</summary>
public ImageFormat TextureFormat { get; set; } = ImageFormat.RGBA8888;
protected override void OnAfterTreeRender( bool firstTime )
{
Vector2 textureSize = (Box.Rect.Size.Length != 0) ? Box.Rect.Size : new( 200, 200 );
Camera.RenderTarget = Texture.CreateRenderTarget( "camerapanel", TextureFormat, textureSize, Camera.RenderTarget );
Texture = Camera.RenderTarget;
}
protected override int BuildHash() => Box.Rect.Size.LengthSquared.FloorToInt();
}
You can then implement it inside a Razor PanelComponent like this:
using Sandbox; using Sandbox.UI; /// <summary> /// A box that will display what a Camera sees. /// </summary> public partial class ViewPort : Image { /// <summary>The camera that will render to our texture.</summary> public CameraComponent Camera { get; set; } /// <summary>The image format that the texture will use.</summary> public ImageFormat TextureFormat { get; set; } = ImageFormat.RGBA8888; protected override void OnAfterTreeRender( bool firstTime ) { Vector2 textureSize = (Box.Rect.Size.Length != 0) ? Box.Rect.Size : new( 200, 200 ); Camera.RenderTarget = Texture.CreateRenderTarget( "camerapanel", TextureFormat, textureSize, Camera.RenderTarget ); Texture = Camera.RenderTarget; } protected override int BuildHash() => Box.Rect.Size.LengthSquared.FloorToInt(); }
PanelComponentlike this: