menu_bookDocumentation

s&box CameraComponent: Screen/World Conversion, RenderToTexture, Priority, Viewport, and Oblique Projection

calendar_today May 4, 2026 schedule ~1 min read person patrickjr verified 50

s&box CameraComponent: Screen/World Conversion, RenderToTexture, and Priority

CameraComponent wraps a SceneCamera and provides the main rendering configuration for a s&box scene. Every scene needs at least one.

Screen/World Coordinate Conversion

CSHARP
var camera = Scene.Camera; // or Components.Get<CameraComponent>()

// World position → screen pixel position
var screenPos = camera.PointToScreenPixels( worldPosition );

// World position → screen pixel position (with behind-camera detection)
var screenPos = camera.PointToScreenPixels( worldPosition, out bool isBehind );

// World position → normalized screen position [0..1]
var normalPos = camera.PointToScreenNormal( worldPosition );

// Screen pixel → world ray (for mouse picking)
var ray = camera.ScreenPixelToRay( new Vector2( mouseX, mouseY ) );

// Normalized screen position → world ray
var ray = camera.ScreenNormalToRay( new Vector3( 0.5f, 0.5f, 0 ) ); // center of screen

// Screen pixel → world position on near plane
var worldPos = camera.ScreenToWorld( new Vector2( mouseX, mouseY ) );

// BBox → screen rect (for UI overlays)
var rect = camera.BBoxToScreenPixels( worldBounds, out bool isBehind );

Camera Priority

When multiple cameras exist, the one with the highest Priority renders on top. IsMainCamera = true marks the primary camera.

CSHARP
camera.Priority = 1;        // default
camera.IsMainCamera = true; // marks as main camera

Render Tags

CSHARP
// Only render objects with these tags
camera.RenderTags.Add( "player" );

// Exclude objects with these tags
camera.RenderExcludeTags.Add( "editor_only" );

Rendering to a Texture

CSHARP
// Assign a render target — camera renders to this texture every frame
camera.RenderTarget = Texture.CreateRenderTarget().WithSize( 512, 512 ).Create();

// Or render on-demand
camera.RenderToTexture( renderTarget );

Viewport

CSHARP
// Full screen (default)
camera.Viewport = new Vector4( 0, 0, 1, 1 );

// Bottom-right quarter of screen
camera.Viewport = new Vector4( 0.5f, 0.5f, 0.5f, 0.5f );

Oblique Projection

For portal rendering or water reflections, override the projection matrix:

CSHARP
camera.CustomProjectionMatrix = camera.CalculateObliqueMatrix( clipPlane );

Custom Size

Override the render resolution without changing the viewport:

CSHARP
camera.CustomSize = new Vector2( 1920, 1080 );
Was this helpful?