trophy 0
May 2025 31 posts
Sandbox.Texture.CreateRenderTarget : api/Sandbox.Texture/CreateRenderTarget
trophy 1421
Jul 2022 1 post
Michael 19 days ago edited 19 days ago
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:
<ViewPort [email protected] />
trophy 115
Aug 2024 2 posts
MichaelI need yuou....
people
Log in to reply
You can't reply if you're not logged in. That would be crazy.