Skip to content

Latest commit

 

History

History
731 lines (417 loc) · 37.7 KB

class_camera3d.rst

File metadata and controls

731 lines (417 loc) · 37.7 KB
github_url:hide

Camera3D

Inherits: :ref:`Node3D<class_Node3D>` < :ref:`Node<class_Node>` < :ref:`Object<class_Object>`

Inherited By: :ref:`XRCamera3D<class_XRCamera3D>`

Camera node, displays from a point of view.

.. rst-class:: classref-introduction-group

Description

Camera3D is a special node that displays what is visible from its current location. Cameras register themselves in the nearest :ref:`Viewport<class_Viewport>` node (when ascending the tree). Only one camera can be active per viewport. If no viewport is available ascending the tree, the camera will register in the global viewport. In other words, a camera just provides 3D display capabilities to a :ref:`Viewport<class_Viewport>`, and, without one, a scene registered in that :ref:`Viewport<class_Viewport>` (or higher viewports) can't be displayed.

.. rst-class:: classref-introduction-group

Tutorials

.. rst-class:: classref-reftable-group

Properties

:ref:`CameraAttributes<class_CameraAttributes>` :ref:`attributes<class_Camera3D_property_attributes>`  
:ref:`int<class_int>` :ref:`cull_mask<class_Camera3D_property_cull_mask>` 1048575
:ref:`bool<class_bool>` :ref:`current<class_Camera3D_property_current>` false
:ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` :ref:`doppler_tracking<class_Camera3D_property_doppler_tracking>` 0
:ref:`Environment<class_Environment>` :ref:`environment<class_Camera3D_property_environment>`  
:ref:`float<class_float>` :ref:`far<class_Camera3D_property_far>` 4000.0
:ref:`float<class_float>` :ref:`fov<class_Camera3D_property_fov>` 75.0
:ref:`Vector2<class_Vector2>` :ref:`frustum_offset<class_Camera3D_property_frustum_offset>` Vector2(0, 0)
:ref:`float<class_float>` :ref:`h_offset<class_Camera3D_property_h_offset>` 0.0
:ref:`KeepAspect<enum_Camera3D_KeepAspect>` :ref:`keep_aspect<class_Camera3D_property_keep_aspect>` 1
:ref:`float<class_float>` :ref:`near<class_Camera3D_property_near>` 0.05
:ref:`ProjectionType<enum_Camera3D_ProjectionType>` :ref:`projection<class_Camera3D_property_projection>` 0
:ref:`float<class_float>` :ref:`size<class_Camera3D_property_size>` 1.0
:ref:`float<class_float>` :ref:`v_offset<class_Camera3D_property_v_offset>` 0.0
.. rst-class:: classref-reftable-group

Methods

void :ref:`clear_current<class_Camera3D_method_clear_current>` ( :ref:`bool<class_bool>` enable_next=true )
:ref:`Projection<class_Projection>` :ref:`get_camera_projection<class_Camera3D_method_get_camera_projection>` ( ) |const|
:ref:`RID<class_RID>` :ref:`get_camera_rid<class_Camera3D_method_get_camera_rid>` ( ) |const|
:ref:`Transform3D<class_Transform3D>` :ref:`get_camera_transform<class_Camera3D_method_get_camera_transform>` ( ) |const|
:ref:`bool<class_bool>` :ref:`get_cull_mask_value<class_Camera3D_method_get_cull_mask_value>` ( :ref:`int<class_int>` layer_number ) |const|
:ref:`Plane[]<class_Plane>` :ref:`get_frustum<class_Camera3D_method_get_frustum>` ( ) |const|
:ref:`RID<class_RID>` :ref:`get_pyramid_shape_rid<class_Camera3D_method_get_pyramid_shape_rid>` ( )
:ref:`bool<class_bool>` :ref:`is_position_behind<class_Camera3D_method_is_position_behind>` ( :ref:`Vector3<class_Vector3>` world_point ) |const|
:ref:`bool<class_bool>` :ref:`is_position_in_frustum<class_Camera3D_method_is_position_in_frustum>` ( :ref:`Vector3<class_Vector3>` world_point ) |const|
void :ref:`make_current<class_Camera3D_method_make_current>` ( )
:ref:`Vector3<class_Vector3>` :ref:`project_local_ray_normal<class_Camera3D_method_project_local_ray_normal>` ( :ref:`Vector2<class_Vector2>` screen_point ) |const|
:ref:`Vector3<class_Vector3>` :ref:`project_position<class_Camera3D_method_project_position>` ( :ref:`Vector2<class_Vector2>` screen_point, :ref:`float<class_float>` z_depth ) |const|
:ref:`Vector3<class_Vector3>` :ref:`project_ray_normal<class_Camera3D_method_project_ray_normal>` ( :ref:`Vector2<class_Vector2>` screen_point ) |const|
:ref:`Vector3<class_Vector3>` :ref:`project_ray_origin<class_Camera3D_method_project_ray_origin>` ( :ref:`Vector2<class_Vector2>` screen_point ) |const|
void :ref:`set_cull_mask_value<class_Camera3D_method_set_cull_mask_value>` ( :ref:`int<class_int>` layer_number, :ref:`bool<class_bool>` value )
void :ref:`set_frustum<class_Camera3D_method_set_frustum>` ( :ref:`float<class_float>` size, :ref:`Vector2<class_Vector2>` offset, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )
void :ref:`set_orthogonal<class_Camera3D_method_set_orthogonal>` ( :ref:`float<class_float>` size, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )
void :ref:`set_perspective<class_Camera3D_method_set_perspective>` ( :ref:`float<class_float>` fov, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )
:ref:`Vector2<class_Vector2>` :ref:`unproject_position<class_Camera3D_method_unproject_position>` ( :ref:`Vector3<class_Vector3>` world_point ) |const|
.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Enumerations

.. rst-class:: classref-enumeration

enum ProjectionType:

.. rst-class:: classref-enumeration-constant

:ref:`ProjectionType<enum_Camera3D_ProjectionType>` PROJECTION_PERSPECTIVE = 0

Perspective projection. Objects on the screen becomes smaller when they are far away.

.. rst-class:: classref-enumeration-constant

:ref:`ProjectionType<enum_Camera3D_ProjectionType>` PROJECTION_ORTHOGONAL = 1

Orthogonal projection, also known as orthographic projection. Objects remain the same size on the screen no matter how far away they are.

.. rst-class:: classref-enumeration-constant

:ref:`ProjectionType<enum_Camera3D_ProjectionType>` PROJECTION_FRUSTUM = 2

Frustum projection. This mode allows adjusting :ref:`frustum_offset<class_Camera3D_property_frustum_offset>` to create "tilted frustum" effects.

.. rst-class:: classref-item-separator


.. rst-class:: classref-enumeration

enum KeepAspect:

.. rst-class:: classref-enumeration-constant

:ref:`KeepAspect<enum_Camera3D_KeepAspect>` KEEP_WIDTH = 0

Preserves the horizontal aspect ratio; also known as Vert- scaling. This is usually the best option for projects running in portrait mode, as taller aspect ratios will benefit from a wider vertical FOV.

.. rst-class:: classref-enumeration-constant

:ref:`KeepAspect<enum_Camera3D_KeepAspect>` KEEP_HEIGHT = 1

Preserves the vertical aspect ratio; also known as Hor+ scaling. This is usually the best option for projects running in landscape mode, as wider aspect ratios will automatically benefit from a wider horizontal FOV.

.. rst-class:: classref-item-separator


.. rst-class:: classref-enumeration

enum DopplerTracking:

.. rst-class:: classref-enumeration-constant

:ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` DOPPLER_TRACKING_DISABLED = 0

Disables Doppler effect simulation (default).

.. rst-class:: classref-enumeration-constant

:ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` DOPPLER_TRACKING_IDLE_STEP = 1

Simulate Doppler effect by tracking positions of objects that are changed in _process. Changes in the relative velocity of this camera compared to those objects affect how audio is perceived (changing the audio's :ref:`AudioStreamPlayer3D.pitch_scale<class_AudioStreamPlayer3D_property_pitch_scale>`).

.. rst-class:: classref-enumeration-constant

:ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` DOPPLER_TRACKING_PHYSICS_STEP = 2

Simulate Doppler effect by tracking positions of objects that are changed in _physics_process. Changes in the relative velocity of this camera compared to those objects affect how audio is perceived (changing the audio's :ref:`AudioStreamPlayer3D.pitch_scale<class_AudioStreamPlayer3D_property_pitch_scale>`).

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Property Descriptions

.. rst-class:: classref-property

:ref:`CameraAttributes<class_CameraAttributes>` attributes

.. rst-class:: classref-property-setget

The :ref:`CameraAttributes<class_CameraAttributes>` to use for this camera.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`int<class_int>` cull_mask = 1048575

.. rst-class:: classref-property-setget

The culling mask that describes which :ref:`VisualInstance3D.layers<class_VisualInstance3D_property_layers>` are rendered by this camera. By default, all 20 user-visible layers are rendered.

Note: Since the :ref:`cull_mask<class_Camera3D_property_cull_mask>` allows for 32 layers to be stored in total, there are an additional 12 layers that are only used internally by the engine and aren't exposed in the editor. Setting :ref:`cull_mask<class_Camera3D_property_cull_mask>` using a script allows you to toggle those reserved layers, which can be useful for editor plugins.

To adjust :ref:`cull_mask<class_Camera3D_property_cull_mask>` more easily using a script, use :ref:`get_cull_mask_value<class_Camera3D_method_get_cull_mask_value>` and :ref:`set_cull_mask_value<class_Camera3D_method_set_cull_mask_value>`.

Note: :ref:`VoxelGI<class_VoxelGI>`, SDFGI and :ref:`LightmapGI<class_LightmapGI>` will always take all layers into account to determine what contributes to global illumination. If this is an issue, set :ref:`GeometryInstance3D.gi_mode<class_GeometryInstance3D_property_gi_mode>` to :ref:`GeometryInstance3D.GI_MODE_DISABLED<class_GeometryInstance3D_constant_GI_MODE_DISABLED>` for meshes and :ref:`Light3D.light_bake_mode<class_Light3D_property_light_bake_mode>` to :ref:`Light3D.BAKE_DISABLED<class_Light3D_constant_BAKE_DISABLED>` for lights to exclude them from global illumination.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`bool<class_bool>` current = false

.. rst-class:: classref-property-setget

If true, the ancestor :ref:`Viewport<class_Viewport>` is currently using this camera.

If multiple cameras are in the scene, one will always be made current. For example, if two Camera3D nodes are present in the scene and only one is current, setting one camera's :ref:`current<class_Camera3D_property_current>` to false will cause the other camera to be made current.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` doppler_tracking = 0

.. rst-class:: classref-property-setget

If not :ref:`DOPPLER_TRACKING_DISABLED<class_Camera3D_constant_DOPPLER_TRACKING_DISABLED>`, this camera will simulate the Doppler effect for objects changed in particular _process methods. See :ref:`DopplerTracking<enum_Camera3D_DopplerTracking>` for possible values.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`Environment<class_Environment>` environment

.. rst-class:: classref-property-setget

The :ref:`Environment<class_Environment>` to use for this camera.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` far = 4000.0

.. rst-class:: classref-property-setget

The distance to the far culling boundary for this camera relative to its local Z axis. Higher values allow the camera to see further away, while decreasing :ref:`far<class_Camera3D_property_far>` can improve performance if it results in objects being partially or fully culled.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` fov = 75.0

.. rst-class:: classref-property-setget

The camera's field of view angle (in degrees). Only applicable in perspective mode. Since :ref:`keep_aspect<class_Camera3D_property_keep_aspect>` locks one axis, fov sets the other axis' field of view angle.

For reference, the default vertical field of view value (75.0) is equivalent to a horizontal FOV of:

  • ~91.31 degrees in a 4:3 viewport
  • ~101.67 degrees in a 16:10 viewport
  • ~107.51 degrees in a 16:9 viewport
  • ~121.63 degrees in a 21:9 viewport
.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`Vector2<class_Vector2>` frustum_offset = Vector2(0, 0)

.. rst-class:: classref-property-setget

The camera's frustum offset. This can be changed from the default to create "tilted frustum" effects such as Y-shearing.

Note: Only effective if :ref:`projection<class_Camera3D_property_projection>` is :ref:`PROJECTION_FRUSTUM<class_Camera3D_constant_PROJECTION_FRUSTUM>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` h_offset = 0.0

.. rst-class:: classref-property-setget

The horizontal (X) offset of the camera viewport.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`KeepAspect<enum_Camera3D_KeepAspect>` keep_aspect = 1

.. rst-class:: classref-property-setget

The axis to lock during :ref:`fov<class_Camera3D_property_fov>`/:ref:`size<class_Camera3D_property_size>` adjustments. Can be either :ref:`KEEP_WIDTH<class_Camera3D_constant_KEEP_WIDTH>` or :ref:`KEEP_HEIGHT<class_Camera3D_constant_KEEP_HEIGHT>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` near = 0.05

.. rst-class:: classref-property-setget

The distance to the near culling boundary for this camera relative to its local Z axis. Lower values allow the camera to see objects more up close to its origin, at the cost of lower precision across the entire range. Values lower than the default can lead to increased Z-fighting.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`ProjectionType<enum_Camera3D_ProjectionType>` projection = 0

.. rst-class:: classref-property-setget

The camera's projection mode. In :ref:`PROJECTION_PERSPECTIVE<class_Camera3D_constant_PROJECTION_PERSPECTIVE>` mode, objects' Z distance from the camera's local space scales their perceived size.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` size = 1.0

.. rst-class:: classref-property-setget

The camera's size in meters measured as the diameter of the width or height, depending on :ref:`keep_aspect<class_Camera3D_property_keep_aspect>`. Only applicable in orthogonal and frustum modes.

.. rst-class:: classref-item-separator


.. rst-class:: classref-property

:ref:`float<class_float>` v_offset = 0.0

.. rst-class:: classref-property-setget

The vertical (Y) offset of the camera viewport.

.. rst-class:: classref-section-separator


.. rst-class:: classref-descriptions-group

Method Descriptions

.. rst-class:: classref-method

void clear_current ( :ref:`bool<class_bool>` enable_next=true )

If this is the current camera, remove it from being current. If enable_next is true, request to make the next camera current, if any.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Projection<class_Projection>` get_camera_projection ( ) |const|

Returns the projection matrix that this camera uses to render to its associated viewport. The camera must be part of the scene tree to function.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`RID<class_RID>` get_camera_rid ( ) |const|

Returns the camera's RID from the :ref:`RenderingServer<class_RenderingServer>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Transform3D<class_Transform3D>` get_camera_transform ( ) |const|

Returns the transform of the camera plus the vertical (:ref:`v_offset<class_Camera3D_property_v_offset>`) and horizontal (:ref:`h_offset<class_Camera3D_property_h_offset>`) offsets; and any other adjustments made to the position and orientation of the camera by subclassed cameras such as :ref:`XRCamera3D<class_XRCamera3D>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` get_cull_mask_value ( :ref:`int<class_int>` layer_number ) |const|

Returns whether or not the specified layer of the :ref:`cull_mask<class_Camera3D_property_cull_mask>` is enabled, given a layer_number between 1 and 20.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Plane[]<class_Plane>` get_frustum ( ) |const|

Returns the camera's frustum planes in world space units as an array of :ref:`Plane<class_Plane>`s in the following order: near, far, left, top, right, bottom. Not to be confused with :ref:`frustum_offset<class_Camera3D_property_frustum_offset>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`RID<class_RID>` get_pyramid_shape_rid ( )

Returns the RID of a pyramid shape encompassing the camera's view frustum, ignoring the camera's near plane. The tip of the pyramid represents the position of the camera.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_position_behind ( :ref:`Vector3<class_Vector3>` world_point ) |const|

Returns true if the given position is behind the camera (the blue part of the linked diagram). See this diagram for an overview of position query methods.

Note: A position which returns false may still be outside the camera's field of view.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`bool<class_bool>` is_position_in_frustum ( :ref:`Vector3<class_Vector3>` world_point ) |const|

Returns true if the given position is inside the camera's frustum (the green part of the linked diagram). See this diagram for an overview of position query methods.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

void make_current ( )

Makes this camera the current camera for the :ref:`Viewport<class_Viewport>` (see class description). If the camera node is outside the scene tree, it will attempt to become current once it's added.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector3<class_Vector3>` project_local_ray_normal ( :ref:`Vector2<class_Vector2>` screen_point ) |const|

Returns a normal vector from the screen point location directed along the camera. Orthogonal cameras are normalized. Perspective cameras account for perspective, screen width/height, etc.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector3<class_Vector3>` project_position ( :ref:`Vector2<class_Vector2>` screen_point, :ref:`float<class_float>` z_depth ) |const|

Returns the 3D point in world space that maps to the given 2D coordinate in the :ref:`Viewport<class_Viewport>` rectangle on a plane that is the given z_depth distance into the scene away from the camera.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector3<class_Vector3>` project_ray_normal ( :ref:`Vector2<class_Vector2>` screen_point ) |const|

Returns a normal vector in world space, that is the result of projecting a point on the :ref:`Viewport<class_Viewport>` rectangle by the inverse camera projection. This is useful for casting rays in the form of (origin, normal) for object intersection or picking.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector3<class_Vector3>` project_ray_origin ( :ref:`Vector2<class_Vector2>` screen_point ) |const|

Returns a 3D position in world space, that is the result of projecting a point on the :ref:`Viewport<class_Viewport>` rectangle by the inverse camera projection. This is useful for casting rays in the form of (origin, normal) for object intersection or picking.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

void set_cull_mask_value ( :ref:`int<class_int>` layer_number, :ref:`bool<class_bool>` value )

Based on value, enables or disables the specified layer in the :ref:`cull_mask<class_Camera3D_property_cull_mask>`, given a layer_number between 1 and 20.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

void set_frustum ( :ref:`float<class_float>` size, :ref:`Vector2<class_Vector2>` offset, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )

Sets the camera projection to frustum mode (see :ref:`PROJECTION_FRUSTUM<class_Camera3D_constant_PROJECTION_FRUSTUM>`), by specifying a size, an offset, and the z_near and z_far clip planes in world space units. See also :ref:`frustum_offset<class_Camera3D_property_frustum_offset>`.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

void set_orthogonal ( :ref:`float<class_float>` size, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )

Sets the camera projection to orthogonal mode (see :ref:`PROJECTION_ORTHOGONAL<class_Camera3D_constant_PROJECTION_ORTHOGONAL>`), by specifying a size, and the z_near and z_far clip planes in world space units. (As a hint, 2D games often use this projection, with values specified in pixels.)

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

void set_perspective ( :ref:`float<class_float>` fov, :ref:`float<class_float>` z_near, :ref:`float<class_float>` z_far )

Sets the camera projection to perspective mode (see :ref:`PROJECTION_PERSPECTIVE<class_Camera3D_constant_PROJECTION_PERSPECTIVE>`), by specifying a fov (field of view) angle in degrees, and the z_near and z_far clip planes in world space units.

.. rst-class:: classref-item-separator


.. rst-class:: classref-method

:ref:`Vector2<class_Vector2>` unproject_position ( :ref:`Vector3<class_Vector3>` world_point ) |const|

Returns the 2D coordinate in the :ref:`Viewport<class_Viewport>` rectangle that maps to the given 3D point in world space.

Note: When using this to position GUI elements over a 3D viewport, use :ref:`is_position_behind<class_Camera3D_method_is_position_behind>` to prevent them from appearing if the 3D point is behind the camera:

# This code block is part of a script that inherits from Node3D.
# `control` is a reference to a node inheriting from Control.
control.visible = not get_viewport().get_camera_3d().is_position_behind(global_transform.origin)
control.position = get_viewport().get_camera_3d().unproject_position(global_transform.origin)