Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/facepunch/sbox-public/llms.txt

Use this file to discover all available pages before exploring further.

CameraComponent is s&box’s primary rendering component. Attach it to any GameObject to define a viewpoint — it controls projection, clipping planes, background color, post-processing, and HUD rendering. Every scene needs at least one active camera. This page covers all properties and methods available on CameraComponent.

Adding a camera

Add CameraComponent to a GameObject in the editor or create it in code.
var go = new GameObject( "Main Camera" );
var cam = go.Components.Create<CameraComponent>();
cam.FieldOfView = 90;
cam.ZNear = 10;
cam.ZFar = 8000;

Properties

Projection

FieldOfView
float
default:"60"
The horizontal (or vertical) field of view in degrees. Valid range is 1–179. Has no effect when Orthographic is true.
FovAxis
Axis
default:"Horizontal"
Controls which screen axis FieldOfView is measured along. Axis.Horizontal fits the view within the horizontal extent of the screen; Axis.Vertical fits it vertically.
Orthographic
bool
default:"false"
When true, the camera uses orthographic projection instead of perspective. FieldOfView and FovAxis have no effect.
OrthographicHeight
float
default:"1204"
The world-space height of the orthographic view frustum. Only relevant when Orthographic is true.
ZNear
float
default:"10"
Near clip plane distance. Anything closer than this distance is not rendered. Keep this value at 5 or above — values below 1 cause z-fighting artifacts.
ZFar
float
default:"10000"
Far clip plane distance. Anything beyond this distance is not rendered. Sensible values are 1 000–30 000; you can push further by raising ZNear proportionally.

Camera priority and viewport

IsMainCamera
bool
default:"true"
Marks this camera as the main game camera. The scene exposes the highest-priority main camera via Scene.Camera.
Priority
int
default:"1"
Determines render order when multiple cameras are active. Higher values render on top. Range is 1–16.
Viewport
Vector4
default:"(0, 0, 1, 1)"
Normalized screen rectangle (x, y, width, height) in the 0–1 range. Use this to create split-screen or picture-in-picture effects.
ScreenRect
Rect
Read-only. The viewport rectangle in screen pixels, derived from Viewport and the current screen size (or CustomSize).

Appearance

BackgroundColor
Color
default:"#557685"
The solid color used to fill the background when no sky is present and ClearFlags includes ClearFlags.Color.
ClearFlags
ClearFlags
default:"Color | Stencil | Depth"
Controls which buffers are cleared before rendering. Defaults to clearing color, stencil, and depth.
RenderTags
TagSet
Only objects whose tags match at least one tag in this set are rendered. Leave empty to render all objects.
RenderExcludeTags
TagSet
Objects whose tags match any tag in this set are excluded from rendering.

Render targets

RenderTarget
Texture
When set to a valid render-target texture, the camera outputs to that texture instead of the screen. Create one with Texture.CreateRenderTarget().
RenderTexture
RenderTextureAsset
An asset-based alternative to RenderTarget. Mipmaps are regenerated automatically after rendering when the texture has more than one mip level.
CustomSize
Vector2?
Optional override for the camera’s output size. When null, the camera uses the screen size or the render target’s size.

Post-processing

EnablePostProcessing
bool
default:"true"
Enables or disables post-processing for this camera. Disabling skips bloom, auto-exposure, volumetric fog, and all other post-process volumes.
PostProcessAnchor
GameObject
When set, post-process volumes are sampled from this object’s position instead of the camera’s position. Useful for third-person cameras.

Advanced projection

CustomProjectionMatrix
Matrix?
Optional override for the camera’s projection matrix. Set to null to use the standard perspective or orthographic matrix.
ProjectionMatrix
Matrix
Read-only. The current projection matrix, either the custom one or the computed standard matrix.
TargetEye
StereoTargetEye
default:"None"
The HMD eye this camera targets. Use StereoTargetEye.None for the main monitor (companion window).

HUD and overlay

CameraComponent exposes two HudPainter accessors for immediate-mode drawing directly into the camera’s output.
Hud
HudPainter
A painter that draws before post-processing. Use this for in-world overlays that should be affected by post-process effects.
Overlay
HudPainter
A painter that draws on top of everything, including the UI. Useful for debug visualizations and always-visible HUD elements.
Both painters are reset at the start of every frame, so you must re-draw each tick.
// Draw a crosshair before post-processing
protected override void OnUpdate()
{
    var center = Screen.Size / 2;
    Camera.Hud.DrawCircle( center, 4, Color.White );
}
Hud and Overlay are component-level properties — access them on your CameraComponent reference, not statically.

Methods

Screen-space utilities

PointToScreenNormal( Vector3 worldPosition )
Vector2
Converts a world-space position to normalized screen coordinates (0–1 range).
PointToScreenPixels( Vector3 worldPosition )
Vector2
Converts a world-space position to screen pixel coordinates.
PointToScreenPixels( Vector3 worldPosition, out bool isBehind )
Vector2
Like the overload above, but also outputs isBehindtrue when the point is behind the camera.
BBoxToScreenPixels( BBox bounds, out bool isBehind )
Rect
Computes the screen-space bounding rectangle that fully contains the given world-space BBox. Sets isBehind when all corners are behind the camera or off-screen.
ScreenPixelToRay( Vector2 pixelPosition )
Ray
Returns a world-space ray from the camera through the given screen pixel. Use this for mouse picking.
ScreenNormalToRay( Vector3 normalPosition )
Ray
Like ScreenPixelToRay but accepts normalized screen coordinates.
ScreenToWorld( Vector2 screen )
Vector3
Converts a screen position to a world-space point on the near frustum plane.

Frustum

GetFrustum()
Frustum
Returns the view frustum for the current viewport.
GetFrustum( Rect screenRect )
Frustum
Returns the view frustum for a custom pixel rectangle within the viewport.
GetFrustum( Rect screenRect, Vector3 screenSize )
Frustum
Returns the view frustum with a custom screen-size reference. Pass Vector3.One to use normalized coordinates.

Rendering to a texture

RenderToTexture( Texture target, in ViewSetup config = default )
bool
Renders the scene from this camera’s viewpoint into target. Returns false if target is null or invalid. Use a texture created with Texture.CreateRenderTarget().
RenderToBitmap( Bitmap targetBitmap )
void
Renders the scene into a Bitmap. The bitmap must be at least 2×2 pixels.

Projection helpers

CalculateObliqueMatrix( Plane clipPlane )
Matrix
Computes a projection matrix with an oblique near clip plane defined in world space. Useful for portal or mirror rendering.
UpdateSceneCamera( SceneCamera camera, bool includeTags = true )
void
Pushes all settings from this component onto a SceneCamera instance. Called automatically each frame; useful when you manage your own SceneCamera.

Usage examples

var ray = Camera.ScreenPixelToRay( Mouse.Position );
var result = Scene.Trace.Ray( ray, 5000 ).Run();

if ( result.Hit )
{
    Log.Info( $"Hit: {result.GameObject?.Name}" );
}

Rendering guide

Overview of s&box’s rendering pipeline and how cameras fit in.

UI panels

Building HUD panels with ScreenPanel and Razor UI.

Light components

DirectionalLight, PointLight, SpotLight, and environment probes.

Shaders guide

Writing custom shaders and reading render attributes from cameras.

Build docs developers (and LLMs) love