Created
August 25, 2024 20:01
-
-
Save Guilhem-Pech/196968fd5a6221686e87bc4ef2b687f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sandbox.UI.MapMonitor | |
{ | |
public partial class CameraRenderer : Panel | |
{ | |
// Public properties | |
public bool Static { get; set; } = false; | |
public bool FullUpdateSceneCamera { get; set; } = false; | |
public float LimitFrameRate { get; set; } = 30f; | |
public bool Fullbright | |
{ | |
get => _sceneCamera != null && _sceneCamera.AmbientLightColor == Color.Black; | |
set | |
{ | |
if ( _sceneCamera != null ) | |
{ | |
_sceneCamera.AmbientLightColor = new Color( 0.1f, 0.1f, 0.1f ); | |
} | |
} | |
} | |
public SceneCamera? SceneCamera => _sceneCamera; | |
public CameraComponent? Camera | |
{ | |
get => _camera; | |
set | |
{ | |
if(value != _camera) | |
{ | |
SetCamera(value); | |
} | |
} | |
} | |
// Private fields | |
private CameraComponent? _camera; | |
private SceneCamera? _sceneCamera; | |
private Texture _texture = Texture.Invalid; | |
private TimeSince _lastRender = 0; | |
// UI elements | |
private Panel VideoPanel { get; set; } = null!; | |
// Methods | |
protected override void OnAfterTreeRender( bool firstTime ) | |
{ | |
if ( firstTime ) | |
{ | |
VideoPanel.Style.SetBackgroundImage( Texture.White ); | |
CreateTexture(); | |
} | |
} | |
public override void Tick() | |
{ | |
if ( Camera == null ) | |
return; | |
if ( LimitFrameRate < 0 || _lastRender > 1f / LimitFrameRate ) | |
{ | |
RenderToTexture(); | |
_lastRender = 0; | |
} | |
} | |
private void RenderToTexture() | |
{ | |
if(_texture == Texture.Invalid) | |
{ | |
_texture = CreateTexture(); | |
VideoPanel.Style.SetBackgroundImage( _texture ); | |
} | |
if ( _sceneCamera != null && _texture != Texture.Invalid ) | |
{ | |
if ( FullUpdateSceneCamera && Camera.IsValid()) | |
{ | |
Camera.Enabled = true; | |
Camera?.UpdateSceneCamera( _sceneCamera ); | |
Camera!.Enabled = false; | |
} | |
else if ( !Static && _sceneCamera != null && Camera != null) | |
{ | |
_sceneCamera.Position = Camera.Transform.Position; | |
_sceneCamera.Rotation = Camera.Transform.Rotation; | |
} | |
Graphics.RenderToTexture(_sceneCamera, _texture); | |
} | |
_lastRender = 0; | |
} | |
private Texture CreateTexture() | |
{ | |
if(!VideoPanel.IsValid() || VideoPanel.Box.Rect.Size == Vector2.Zero) | |
return Texture.Invalid; | |
return Texture.CreateRenderTarget().WithSize( VideoPanel.Box.Rect.Size ).WithScreenFormat().WithScreenMultiSample().WithDynamicUsage().Create(); | |
} | |
private void SetCamera( CameraComponent? camera ) | |
{ | |
_camera = camera; | |
_sceneCamera ??= new SceneCamera(); | |
_camera?.UpdateSceneCamera( _sceneCamera ); | |
if(VideoPanel.IsValid()) | |
VideoPanel.Style.SetBackgroundImage( _texture ); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment