mirror of
https://github.com/Facepunch/sbox-public.git
synced 2025-12-23 22:48:07 -05:00
Add screenshot_highres con command (#3546)
This commit is contained in:
@@ -212,12 +212,21 @@ public static partial class Game
|
||||
/// <summary>
|
||||
/// Capture a screenshot. Saves it in Steam.
|
||||
/// </summary>
|
||||
[ConCmd( "screenshot" )]
|
||||
[ConCmd( "screenshot", Help = "Take a screenshot and save it to your Steam screenshots" )]
|
||||
public static void TakeScreenshot()
|
||||
{
|
||||
ScreenshotService.RequestCapture();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Capture a high resolution screenshot using the active scene camera.
|
||||
/// </summary>
|
||||
[ConCmd( "screenshot_highres", Help = "Take a high resolution screenshot you specify the width and height" )]
|
||||
public static void TakeHighResScreenshot( int width, int height )
|
||||
{
|
||||
ScreenshotService.TakeHighResScreenshot( Application.GetActiveScene(), width, height );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This has to be in Game.dll so the codegen will get generated for it
|
||||
/// </summary>
|
||||
|
||||
@@ -80,4 +80,92 @@ internal static class ScreenshotService
|
||||
Log.Error( $"Error capturing screenshot: {ex.Message}" );
|
||||
}
|
||||
}
|
||||
|
||||
public static void TakeHighResScreenshot( Scene scene, int width, int height )
|
||||
{
|
||||
if ( !scene.IsValid() )
|
||||
{
|
||||
Log.Warning( "No valid scene available for high-res screenshot." );
|
||||
return;
|
||||
}
|
||||
|
||||
const int MaxDimension = 16384;
|
||||
|
||||
if ( width <= 0 || height <= 0 )
|
||||
{
|
||||
Log.Warning( "screenshot_highres requires width and height greater than zero." );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( width > MaxDimension || height > MaxDimension )
|
||||
{
|
||||
Log.Warning( $"screenshot_highres maximum dimension is {MaxDimension}px." );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( scene.Camera is not { } camera || !camera.IsValid() )
|
||||
{
|
||||
Log.Warning( "Active scene does not have a main camera to capture from." );
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap captureBitmap = null;
|
||||
RenderTarget renderTarget = null;
|
||||
var previousCustomSize = camera.CustomSize;
|
||||
|
||||
try
|
||||
{
|
||||
camera.CustomSize = new Vector2( width, height );
|
||||
|
||||
renderTarget = RenderTarget.GetTemporary( width, height, ImageFormat.Default, ImageFormat.Default, MultisampleAmount.Multisample16x, 1, "HighResScreenshot" );
|
||||
if ( renderTarget is null )
|
||||
{
|
||||
Log.Warning( "Failed to create render target for high-res screenshot." );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !camera.RenderToTexture( renderTarget.ColorTarget ) )
|
||||
{
|
||||
Log.Warning( "Camera failed to render to texture for high-res screenshot." );
|
||||
return;
|
||||
}
|
||||
|
||||
captureBitmap = renderTarget.ColorTarget.GetBitmap( 0 );
|
||||
if ( captureBitmap is null || !captureBitmap.IsValid )
|
||||
{
|
||||
Log.Warning( "Failed to read pixels from render target for high-res screenshot." );
|
||||
return;
|
||||
}
|
||||
|
||||
var filePath = ScreenCaptureUtility.GenerateScreenshotFilename( "png" );
|
||||
var directory = Path.GetDirectoryName( filePath );
|
||||
if ( !string.IsNullOrEmpty( directory ) )
|
||||
{
|
||||
Directory.CreateDirectory( directory );
|
||||
}
|
||||
|
||||
var pngData = captureBitmap.ToPng();
|
||||
|
||||
File.WriteAllBytes( filePath, pngData );
|
||||
|
||||
Log.Info( $"High-res screenshot saved to: {filePath} ({width}x{height})" );
|
||||
}
|
||||
catch ( Exception ex )
|
||||
{
|
||||
Log.Error( $"Failed to capture high-res screenshot: {ex.Message}" );
|
||||
}
|
||||
finally
|
||||
{
|
||||
camera.CustomSize = previousCustomSize;
|
||||
|
||||
renderTarget?.Dispose();
|
||||
|
||||
captureBitmap?.Dispose();
|
||||
|
||||
if ( camera.IsValid() )
|
||||
{
|
||||
camera.InitializeRendering();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -574,4 +574,13 @@ public static class EditorScene
|
||||
Log.Warning( "Failed to paste, invalid JSON." );
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Capture a high resolution screenshot using the active scene camera.
|
||||
/// </summary>
|
||||
[ConCmd( "screenshot_highres", Help = "Take a high resolution screenshot you specify the width and height" )]
|
||||
public static void TakeHighResScreenshot( int width, int height )
|
||||
{
|
||||
ScreenshotService.TakeHighResScreenshot( SceneEditorSession.Active.Scene, width, height );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user