Skip to content

Repository files navigation

kOS Stock Camera Addon


Description

This project is an addon for kOS, which is a mod for the game Kerbal Space Program. It provides scriptable access to KSP camera controls from KerboScript.

The addon exposes both stock-camera helpers and an optional free-camera mode:

  • FLIGHTCAMERA controls KSP's normal stock flight camera behavior, including camera mode, target, distance, heading, pitch, position, and FOV.
  • MAPCAMERA controls map-view camera behavior.
  • INTERNALCAMERA controls IVA/internal camera behavior.
  • FREECAMERA temporarily takes ownership of KSP's FlightCamera so scripts can set an exact camera pose, including position, facing, roll, anchor behavior, and vessel-relative camera frames.
  • LIGHT creates a camera-mounted spot light that follows the active gameplay camera, including stock flight camera, free camera, map camera, and IVA when a compatible render camera is available.

The "stock" designation remains important. This addon works with KSP's existing camera systems. Other mods that replace or heavily modify camera behavior may conflict with this functionality.

Quick start

Existing stock flight-camera usage:

set cam toaddons:camera:flightcamera.
set cam:mode to"FREE".
set cam:fov to45.
set cam:distanceto40.
set cam:headingto90.
set cam:pitch to10.

Free-camera usage:

set cam toaddons:camera:freecamera.
set cam:enabledtotrue.
set cam:fov to35.
set cam:anchor toship.
cam:setpose(V(0, 12, -35), heading(90, 10, 0)).
wait 5.
set cam:enabledtofalse.

FLIGHTCAMERA and FREECAMERA are intentionally separate. Use FLIGHTCAMERA for normal stock camera control. Use FREECAMERA when a script needs full-pose control of the flight camera. Avoid driving both from one script at the same time.

Camera light usage:

set light toaddons:camera:light.
set light:enabledtotrue.
set light:intensity to1.5.
set light:rangeto80.
set light:angle to45.

Structures

CAMERA

The root addon is available as:

addons:camera

Suffixes:

SuffixTypeGet/SetDescription
FLIGHTCAMERA / FLIGHTFlightCameraGetReturns the object which allows control of the stock camera in the flight scene.
MAPCAMERA / MAPMapCameraGetReturns the object which allows control of the camera in map view.
INTERNALCAMERA / INTERNALInternalCameraGetReturns the object which allows control of the IVA/internal camera.
FREECAMERA / FREEFreeCameraGetReturns the object which allows temporary full-pose control of the flight camera.
CAMERALIGHT / LIGHTCameraLightGetReturns the object which controls a camera-mounted spot light.

FLIGHTCAMERA

FLIGHTCAMERA controls KSP's normal flight camera. It does not replace the stock camera update model; it changes the same fields KSP normally uses.

SuffixTypeGet/SetDescription
MODE
CAMERAMODE
StringGet/SetReturns or changes the selected camera mode. Valid options are "AUTO", "CHASE", "FREE", "LOCKED", and "ORBITAL".
FOV
CAMERAFOV
ScalarGet/SetReturns or sets the field of view for the flight camera.
PITCH
CAMERAPITCH
ScalarGet/SetReturns or sets the pitch component of the camera position rotation. The actual direction depends on the frame of reference of the current camera mode.
HEADING
HDG
CAMERAHDG
ScalarGet/SetReturns or sets the yaw component of the camera position rotation. The actual direction depends on the frame of reference of the current camera mode.
DISTANCE
CAMERADISTANCE
ScalarGet/SetReturns or sets the distance component of the camera position, the magnitude applied to the rotation defined by pitch and heading.
POSITION
CAMERAPOSITION
VectorGet/SetReturns or sets the camera's position using a CPU-vessel-centered vector. The pitch, heading, and distance components are automatically calculated from the vector. Changing the camera target does not change the reference origin; always set this using vectors based on the CPU vessel.
TARGETPart or VesselGet/SetReturns or sets the vessel or part that the camera is pointing at. This is the same as KSP's "Aim here" feature.
TARGETPOSVectorGetDebugging value.
PIVOTPOSVectorGetDebugging value.
POSITIONUPDATERUserDelegateGet/SetA delegate automatically called once per tick to update the camera position. Initially this returns a DONOTHING delegate. Set it back to DONOTHING to stop automatic position updates.

MAPCAMERA

MAPCAMERA controls the map-view camera.

SuffixTypeGet/SetDescription
SETFILTER(string, boolean)FunctionSets whether objects of a given type should be visible in map mode. See FILTERNAMES for valid names.
GETFILTER(string)FunctionReturns whether objects of the specified type are visible. See FILTERNAMES for valid names.
COMMNETMODEStringGet/SetGets or sets the current commnet display mode. See COMMNETNAMES for valid modes.
PITCH
CAMERAPITCH
ScalarGet/SetGets or sets the pitch angle of the camera, relative to the ecliptic plane.
HDG
HEADING
CAMERAHDG
ScalarGet/SetGets or sets the camera heading/yaw.
DISTANCE
CAMERADISTANCE
ScalarGet/SetReturns the camera distance from the camera pivot, in meters. Map view may enforce limits on this value.
POSITION
CAMERAPOSITION
VectorGet/SetGets or sets the position of the camera in SHIP-RAW coordinates. The camera will always face toward the pivot point.
TARGETVessel, Body, or NodeGet/SetGets or sets the pivot object.
FILTERNAMESListGetReturns the valid filter names for use with SETFILTER and GETFILTER.
COMMNETNAMESListGetReturns the valid commnet display mode names for use with COMMNETMODE.

INTERNALCAMERA

INTERNALCAMERA controls the IVA/internal camera when IVA is active.

SuffixTypeGet/SetDescription
PITCH
CAMERAPITCH
ScalarGet/SetGets or sets the IVA camera pitch, clamped to the limits of the active internal camera.
ROT
ROTATION
CAMERAROTATION
ScalarGet/SetGets or sets the IVA camera rotation, clamped to the limits of the active internal camera.
FOV
CAMERAFOV
ScalarGet/SetGets or sets the IVA camera field of view.
ACTIVEKERBALCrewMemberGet/SetGets or sets the active IVA Kerbal. The Kerbal must be on the active vessel.
ACTIVEBooleanGetTrue when the current camera mode is IVA.

FREECAMERA

FREECAMERA is for scripted full-pose camera control in the flight scene. Unlike FLIGHTCAMERA, it temporarily takes ownership of KSP's FlightCamera while enabled, then restores the stock camera when disabled or when KSP changes to an incompatible camera context.

Access:

set cam toaddons:camera:freecamera.

Suffixes:

SuffixTypeGet/SetDescription
ENABLEDBooleanGet/SetEnables or disables free-camera control. Disabling restores the stock flight camera.
ACTIVEBooleanGetTrue when FREECAMERA currently owns the flight camera.
AVAILABLEBooleanGetTrue when the current scene has a usable FlightCamera.
STATUSStringGetHuman-readable status/debug text.
FOVScalarGet/SetCamera field of view in degrees. Restored to the stock FOV when freecam is disabled.
POSITIONVectorGet/SetCamera position in SHIP-RAW coordinates.
DISTANCEScalarGet/SetDistance from the CPU vessel's CoM. Gets the same value as cam:position:mag; setting scales the current POSITION direction to the requested distance.
FACINGDirection or VectorGet/SetCamera facing. Gets a kOS Direction; accepts a Direction or non-zero Vector look direction when set. Vector assignment uses the camera's body-up vector as the effective up direction.
HEADING
HDG
ScalarGet/SetLocal-horizon heading in degrees. 0 is north and 90 is east.
PITCHScalarGet/SetLocal-horizon pitch in degrees. Positive looks upward.
ROLLScalarGet/SetRoll around the camera's forward axis, in degrees.
ANCHORVessel, Part, Body, or StringGet/SetAnchor target. Gets the current anchor object; accepts a vessel, part, body, or string shorthand such as "SHIP"/"VESSEL" or "BODY".
ANCHORFRAMEStringGet/SetShip anchor frame. Valid values are "RAW" and "FACING".
SETPOSE(position, facing)FunctionSets POSITION and FACING together. The facing argument may be a Direction or non-zero Vector look direction. Vector facing uses the body-up vector at the new camera position.
LOOKAT(targetPosition)FunctionPoints the camera at a SHIP-RAW target position without moving it. Uses the camera's body-up vector as the effective up direction.
MOVE(delta)FunctionAdds a SHIP-RAW vector delta to POSITION. Equivalent to set cam:position to cam:position + delta.
COPYFROMSTOCK()FunctionCopies the current stock flight camera pose and FOV into freecam state.
RESET()FunctionResets freecam to the saved stock pose when available.

FREECAMERA anchor behavior

ANCHOR controls how the desired camera pose is preserved over time. It accepts actual kOS target objects as well as string shorthands:

set cam:anchor toship. // vessel anchorset cam:anchor toship:rootpart. // part anchorset cam:anchor tobody. // body anchor, when body is a BodyTargetset cam:anchor to"SHIP". // shorthand for the CPU/active vesselset cam:anchor to"BODY". // shorthand for the CPU/active vessel's body

When anchored to a vessel, the camera follows the vessel center of mass. With ANCHORFRAME = "RAW", it keeps a fixed raw-axis offset from the vessel. With ANCHORFRAME = "FACING", it stores both position and facing relative to the vessel's current facing.

When anchored to a part, the camera position and facing are stored in the part's local transform space. This is useful for root-part, cockpit, wing, or other attached cameras because the camera stays fixed relative to the selected part even when the vessel center of mass shifts due to fuel burn, staging, docking, or cargo changes. If the part is decoupled or undocked into a new loaded vessel, the camera continues following that same part. If the part becomes unavailable because it is destroyed or its vessel unloads, freecam preserves the current visual pose and falls back to a vessel anchor using the part's vessel when available, otherwise the CPU vessel. ANCHORFRAME does not change part-anchor behavior.

When anchored to a body, the camera position is stored in the celestial body's local transform space. This is useful for runway, launchpad, flyby, or terrain-fixed shots. BODY anchors also apply a narrow render-time correction for the active flight camera, re-resolving the body-local pose immediately before rendering to reduce jitter from late floating-origin/Krakensbane corrections.

Changing ANCHOR preserves the current visual/world camera pose, then rebuilds the internal representation for the new anchor target.

ANCHORFRAME = "RAW" is the default vessel-anchor behavior:

set cam:anchor toship.
set cam:anchorframe to"RAW".
set cam:positiontoV(...).

ANCHORFRAME = "FACING" is useful for low-lag onboard, chase, or wing cameras anchored to a vessel:

set cam:anchor toship.
cam:setpose(ship:facing:starvector*2+ship:facing:vector*-1+ship:facing:topvector*0.1,
ship:facing).
set cam:anchorframe to"FACING".

For a camera fixed to a specific part, set the pose and anchor to the part:

cam:setpose(ship:facing:starvector*2+ship:facing:vector*-1+ship:facing:topvector*0.1,
ship:facing).
set cam:anchor toship:rootpart.

FREECAMERA facing behavior

You can use local-horizon heading, pitch, and roll:

set cam:headingto90.
set cam:pitch to15.
set cam:roll to0.

Or assign a kOS Direction, or a non-zero look-direction Vector, directly:

set cam:facingtoship:facing.
set cam:facingtoheading(90, 15, 0).
set cam:facingtoship:facing:vector.

When FACING is set from a vector, the vector is treated as a look direction, not a target position. The camera's effective up direction is the body-up vector at the camera position, equivalent to:

set cam:facingtolookdirup(<vector>, cam:position-body:position).

Use LOOKAT(targetPosition) for point-target behavior. LOOKAT interprets its argument as a SHIP-RAW point, not a look direction:

cam:lookat(target:position).

LOOKAT changes facing only; it does not move the camera or change the current anchor settings.

SETPOSE(position, facing) is equivalent to setting POSITION and FACING, but applies both values as one pose update. The facing argument may be a Direction or a non-zero Vector look direction. When the facing argument is a vector, its effective up direction is calculated at the new position:

cam:setpose(V(0, 12, -35), heading(90, 10, 0)).

Use SETPOSE when changing both position and facing in the same script step.

MOVE(delta) is a shorthand for adding a SHIP-RAW vector to the current camera position:

cam:move(ship:facing:vector*-5).

FREECAMERA scene and IVA behavior

FREECAMERA controls KSP's existing FlightCamera; it does not create a separate Unity camera stack.

When KSP changes away from the normal flight camera, freecam releases control. Map-view transitions may suspend and later resume freecam behavior. IVA/internal camera mode is treated differently: selecting IVA disables freecam and defers FlightCamera restore/reparent work until KSP is safely back in flight camera mode. This avoids corrupting the first IVA camera frame.

The controller normally applies pose updates from LateUpdate. BODY anchors also re-apply their body-local pose from Camera.onPreCull, but only for the active flight camera while freecam is active in Flight camera mode. This targeted render-time correction is intended to keep stationary/body-fixed cameras stable when KSP performs late floating-origin or Krakensbane corrections.

LIGHT

LIGHT controls a Unity spot light that is kept just behind the active gameplay camera and pointed the same way as the camera. It is intended as a scriptable camera-mounted flashlight/fill light for night shots or dark interiors.

Access:

set light toaddons:camera:light.

Suffixes:

SuffixTypeGet/SetDescription
ENABLEDBooleanGet/SetEnables or disables the camera light.
ACTIVEBooleanGetTrue when the Unity light exists and is currently enabled for a render camera.
AVAILABLEBooleanGetTrue when the addon can find a likely gameplay camera to follow.
STATUSStringGetHuman-readable status/debug text.
INTENSITYScalarGet/SetUnity light intensity. Must be zero or greater. Default is 1.
RANGE
FALLOFF
ScalarGet/SetUnity light range in meters; this is the falloff distance. Must be greater than zero. Default is 50.
ANGLE
FOV
ScalarGet/SetSpot-light outer cone angle in degrees. Must be greater than 0 and less than 180. Default is 45.
DISTANCEScalarGet/SetDistance behind the camera where the light origin is placed. Must be zero or greater. Default is 0.25.
SHADOWS
SHADOW
BooleanGet/SetEnables or disables Unity soft shadows for the light. Default is false.
COLOR
COLOUR
RGBAGet/SetkOS color structure. Use values like RGB(1, 0.92, 0.82) or WHITE. Alpha is ignored.
RED
R
ScalarGet/SetRed color channel from 0 to 1. Default is 1.
GREEN
G
ScalarGet/SetGreen color channel from 0 to 1. Default is 0.92.
BLUE
B
ScalarGet/SetBlue color channel from 0 to 1. Default is 0.82.

The default color is slightly warm white: RGB(1, 0.92, 0.82). Printing addons:camera:light shows a one-line summary of the current light settings.

Example:

set light toaddons:camera:light.
set light:enabledtotrue.
set light:intensity to2.
set light:rangeto100.
set light:angle to35.
set light:distanceto0.5.
set light:color toRGB(1, 0.92, 0.82).
set light:shadows tofalse.

Building

You must have an IDE or compiler capable of building Visual Studio solutions (.sln files). For the sake of simplicity, this repository assumes that it will be located next to the kOS repository, in the same parent directory. All references match those of kOS, and use relative paths pointing to the files in the kOS repository.

About

No description, website, or topics provided.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages