|
@@ -7,7 +7,7 @@ brief: This manual describes the functionality of the Defold camera component.
|
|
|
|
|
|
A camera in Defold is a component that changes the viewport and projection of the game world. Out of the box, Defold ships with a built in render script that renders the game with no need of a camera component but you can easily replace the built in script with one that renders the game to your liking.
|
|
A camera in Defold is a component that changes the viewport and projection of the game world. Out of the box, Defold ships with a built in render script that renders the game with no need of a camera component but you can easily replace the built in script with one that renders the game to your liking.
|
|
|
|
|
|
-The camera component defines a bare bones perspective camera that provides a view and projection matrix to the render script. If you need advanced features like chasing, zooming, shake etc you will need to implement it. There are a few library camera solutions that implements common camera features. They are available from the Defold community assets portal:
|
|
|
|
|
|
+The camera component defines a bare bones perspective or orthographic camera that provides a view and projection matrix to the render script. If you need advanced features like chasing, zooming, shake etc you will need to implement it. There are a few library camera solutions that implements common camera features. They are available from the Defold community assets portal:
|
|
|
|
|
|
- [Rendercam](https://www.defold.com/community/projects/84064/) by Ross Grams.
|
|
- [Rendercam](https://www.defold.com/community/projects/84064/) by Ross Grams.
|
|
- [Ortographic camera](https://www.defold.com/community/projects/76573/) by Björn Ritzl.
|
|
- [Ortographic camera](https://www.defold.com/community/projects/76573/) by Björn Ritzl.
|
|
@@ -26,10 +26,10 @@ Id
|
|
: The id of the component
|
|
: The id of the component
|
|
|
|
|
|
Aspect Ratio
|
|
Aspect Ratio
|
|
-: The ratio between the frustum width and height. 1.0 means that you assume a quadratic view. 1.33 is good for a 4:3 view like 1024x768. 1.78 is good for a 16:9 view. This setting is ignored if *Auto Aspect Ratio* is set.
|
|
|
|
|
|
+: (**Perspective camera only**) - The ratio between the frustum width and height. 1.0 means that you assume a quadratic view. 1.33 is good for a 4:3 view like 1024x768. 1.78 is good for a 16:9 view. This setting is ignored if *Auto Aspect Ratio* is set.
|
|
|
|
|
|
Fov
|
|
Fov
|
|
-: The *vertical* camera field of view expressed in _radians_. The wider the field of view, the more the camera will see. Note that the current default value (45) is misleading. For a 45 degree field of view, change the value to 0.785 ($\pi / 4$).
|
|
|
|
|
|
+: (**Perspective camera only**) - The *vertical* camera field of view expressed in _radians_. The wider the field of view, the more the camera will see. Note that the current default value (45) is misleading. For a 45 degree field of view, change the value to 0.785 ($\pi / 4$).
|
|
|
|
|
|
Near Z
|
|
Near Z
|
|
: The Z-value of the near clipping plane.
|
|
: The Z-value of the near clipping plane.
|
|
@@ -38,7 +38,7 @@ Far Z
|
|
: The Z-value of the far clipping plane.
|
|
: The Z-value of the far clipping plane.
|
|
|
|
|
|
Auto Aspect Ratio
|
|
Auto Aspect Ratio
|
|
-: Set this to let the camera automatically calculate the aspect ratio.
|
|
|
|
|
|
+: (**Perspective camera only**) - Set this to let the camera automatically calculate the aspect ratio.
|
|
|
|
|
|
## Using the camera
|
|
## Using the camera
|
|
|
|
|
|
@@ -53,13 +53,6 @@ Each frame, the camera component that currently has camera focus will send a `"s
|
|
```lua
|
|
```lua
|
|
-- example.render_script
|
|
-- example.render_script
|
|
--
|
|
--
|
|
-function update(self)
|
|
|
|
- ...
|
|
|
|
- render.set_view(self.view)
|
|
|
|
- render.set_projection(self.projection)
|
|
|
|
- ...
|
|
|
|
-end
|
|
|
|
-
|
|
|
|
function on_message(self, message_id, message)
|
|
function on_message(self, message_id, message)
|
|
if message_id == hash("set_view_projection") then
|
|
if message_id == hash("set_view_projection") then
|
|
self.view = message.view -- [1]
|
|
self.view = message.view -- [1]
|
|
@@ -71,10 +64,11 @@ end
|
|
|
|
|
|
## Projections
|
|
## Projections
|
|
|
|
|
|
-The camera component currently supplies the render script with a perspective projection. This is well suited for 3D games. For 2D games, it is often desirable to render the scene with *orthographic projection*. This means that the view of the camera is no longer dictated by a frustum, but by a box. Orthographic projection is unrealistic in that it does not alter the size of objects based on their distance. An object 1000 units away will render at the same size as an object right in front of the camera.
|
|
|
|
|
|
+The camera component supplies the render script with a perspective projection. This is well suited for 3D games. For 2D games, it is often desirable to render the scene with *orthographic projection*. This means that the view of the camera is no longer dictated by a frustum, but by a box. Orthographic projection is unrealistic in that it does not alter the size of objects based on their distance. An object 1000 units away will render at the same size as an object right in front of the camera.
|
|
|
|
|
|
{srcset="images/camera/[email protected] 2x"}
|
|
{srcset="images/camera/[email protected] 2x"}
|
|
|
|
|
|
|
|
+### Orthographic projection
|
|
To use orthographic projection you can ignore the projection matrix sent by the camera component and instead provide one yourself in the render script, just like the default renderscript does:
|
|
To use orthographic projection you can ignore the projection matrix sent by the camera component and instead provide one yourself in the render script, just like the default renderscript does:
|
|
|
|
|
|
```lua
|
|
```lua
|
|
@@ -98,3 +92,42 @@ end
|
|
```
|
|
```
|
|
1. Set up an orthographic projection based on the width and height of the game window. The center of view is the camera's position. Note that the default render script sets the lower left corner of the view at the camera's position.
|
|
1. Set up an orthographic projection based on the width and height of the game window. The center of view is the camera's position. Note that the default render script sets the lower left corner of the view at the camera's position.
|
|
2. Only care about camera view since the projection is done separately.
|
|
2. Only care about camera view since the projection is done separately.
|
|
|
|
+
|
|
|
|
+The default render script is initially set up to use the view matrix provided by the camera and a stretch projection. You can change the orthographic projection type by sending a message to the render script:
|
|
|
|
+
|
|
|
|
+```lua
|
|
|
|
+msg.post("@render:", "use_fixed_fit_projection", { near = -1, far = 1 })
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+Learn more about the render script and how to change which type of orthographic projection to use in the [Render manual](/manuals/render).
|
|
|
|
+
|
|
|
|
+### Perspective projection
|
|
|
|
+To use perspective projection you must use both the view and projection provided by the camera:
|
|
|
|
+
|
|
|
|
+```lua
|
|
|
|
+-- example_perspective.render_script
|
|
|
|
+--
|
|
|
|
+function update(self)
|
|
|
|
+ ...
|
|
|
|
+ render.set_view(self.view) -- [1]
|
|
|
|
+ render.set_projection(self.projection)
|
|
|
|
+ ...
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+function on_message(self, message_id, message)
|
|
|
|
+ if message_id == hash("set_view_projection") then
|
|
|
|
+ self.view = message.view -- [2]
|
|
|
|
+ self.projection = message.projection
|
|
|
|
+ end
|
|
|
|
+end
|
|
|
|
+```
|
|
|
|
+1. Use both the view and projection matrix provided by the camera
|
|
|
|
+2. The message posted from the camera component includes a view matrix and a projection matrix.
|
|
|
|
+
|
|
|
|
+The default render script is initially set up to use an orthographic projection. You can change to the render script to use the perspective projection provided by a camera by sending a message to the render script:
|
|
|
|
+
|
|
|
|
+```lua
|
|
|
|
+msg.post("@render:", "use_camera_projection")
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+Learn more about the render script in the [Render manual](/manuals/render).
|