Skip to content

Camera Extensions

Andrzej Kebab edited this page Feb 4, 2024 · 4 revisions

UtilityLibrary.Unity.Runtime

Viewport

GetViewportExtentsWithMargin

Calculates and returns viewport extents with an optional margin. Useful for calculating a frustum for culling. It calculates and returns the viewport extents based on the camera's field of view and aspect ratio. An optional margin can be applied to the extents.

Camera myCamera = GetComponent<Camera>();
Vector2 viewportExtents = myCamera.GetViewportExtentsWithMargin(new Vector2(0.5f, 0.5f));

Parameters:

  • camera: The camera object this method extends.
  • viewportMargin (Optional): Margin to be applied to viewport extents. Default is 0.2, 0.2.

Returns:

  • Vector2: Viewport extents as a Vector2 after applying the margin.

Coordinate Conversion

ScreenToWorldPointIgnoreDeep

Converts a screen point to a world point, ignoring the depth component.

Vector3 worldPosition = camera.ScreenToWorldPointIgnoreDeep(new Vector3(screenX, screenY, 0f));

Parameters:

  • {Vector3} position: The screen position to convert.

Returns:

  • {Vector3}: The converted world position.

Meter / Pixel Calculation

MetersPerPixel

Calculates the size of a meter in pixels for an orthographic camera.

float meterPerPixel = orthographicCamera.MetersPerPixel();

Returns:

  • {float}: The size of a meter in pixels.

PixelsPerMeter

Calculates the size of a pixel in meters for an orthographic camera.

float pixelPerMeter = orthographicCamera.PixelsPerMeter();

Returns:

  • {float}: The size of a pixel in meters.

Capture

Capture

Captures the current view of the camera as a Texture2D.

Texture2D capturedTexture = camera.Capture();

Parameters:

  • No parameters.

Returns:

  • {Texture2D}: The captured view as a Texture2D.

Capture (with Rect)

Captures a portion of the current view of the camera as a Texture2D.

Texture2D capturedTexture = camera.Capture(new Rect(0, 0, captureWidth, captureHeight));

Parameters:

  • {Rect} rect: The portion of the view to capture.

Returns:

  • {Texture2D}: The captured view as a Texture2D.

Size Conversion

ScreenToWorldSize

Converts a size in pixels to a size in world units.

float worldSize = camera.ScreenToWorldSize(pixelSize, clipPlaneDistance);

Parameters:

  • {float} pixelSize: The size in pixels to convert.
  • {float} clipPlane: The distance from the camera at which the conversion should be performed.

Returns:

  • {float}: The converted size in world units.

WorldToScreenSize

Converts a size in world units to a size in pixels.

float pixelSize = camera.WorldToScreenSize(worldSize, clipPlaneDistance);

Parameters:

  • {float} worldSize: The size in world units to convert.
  • {float} clipPlane: The distance from the camera at which the conversion should be performed.

Returns:

  • {float}: The converted size in pixels.

Clip Plane Calculation

GetClipPlane

Calculates the clip plane for the camera at a given point.

Vector4 clipPlane = camera.GetClipPlane(point, normal);

Parameters:

  • {Vector3} point: The point at which to calculate the clip plane.
  • {Vector3} normal: The normal of the clip plane.

Returns:

  • {Vector4}: The calculated clip plane.

Z Buffer

GetZBufferParams

Gets the parameters of the Z buffer for the camera.

Vector4 zBufferParams = camera.GetZBufferParams();

Returns:

  • {Vector4}: The parameters of the Z buffer.

Clone this wiki locally