Skip to content

Commit ae3440b

Browse files
authored
Added support for camera viewport (#41)
* Added viewport settings for camera * Updated screen_to_world to take into account viewport * Added multi camera support
1 parent ce8d62d commit ae3440b

File tree

9 files changed

+242
-288
lines changed

9 files changed

+242
-288
lines changed

CHANGELOG.md

-69
This file was deleted.

README.md

+19-70
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Note that when using `go.animate()`, `go.get()` and `go.set()` you need to make
3030
* `go.set("mycamera#camerascript", "zoom")`
3131
* `go.get("mycamera#camerascript", "zoom")`
3232

33+
#### order (number)
34+
The order in which multiple cameras should be drawn, lower is drawn first.
35+
3336
#### projection (hash)
3437
The camera can be configured to support different kinds of orthographic projections. The default projection (aptly named `DEFAULT`) uses the same orthographic projection matrix as in the default render script (ie aspect ratio isn't maintained and content is stretched). Other projections are available out-of-the box:
3538

@@ -43,9 +46,6 @@ Additional custom projections can be added, see `camera.add_projector()` below.
4346
#### enabled (boolean)
4447
This controls if the camera is enabled by default or not. Send `enable` and `disable` messages to the script or use `go.set(id, "enable", true|false)` to toggle this value.
4548

46-
#### offset_gui (boolean)
47-
This controls if the gui should be offset during a screen shake or a camera recoil. This will send the camera offset to the render script using the `send_camera_offset` message.
48-
4949
#### follow (boolean)
5050
This controls if the camera should follow a target or not. See `camera.follow()` for details.
5151

@@ -73,6 +73,9 @@ The camera bounds. See `camera.bounds()` for details.
7373
#### deadzone_left (number), deadzone_right (number), deadzone_top (number), deadzone_bottom (number)
7474
The camera deadzone. See `camera.deadzone()` for details.
7575

76+
#### viewport_left (number), viewport_right (number), viewport_top (number), viewport_bottom (number)
77+
The camera viewport.
78+
7679

7780
## Render script integration
7881
In order for the Orthographic camera to function properly you need to integrate it in your render script. You can do this in a number of different ways:
@@ -81,81 +84,27 @@ In order for the Orthographic camera to function properly you need to integrate
8184
The Orthographic API comes with a ready to use render script in `orthographic/render/orthograpic.render_script`. Open `game.project` and make sure to reference `orthographic/render/orthograpic.render` in the `Render` field in the `Bootstrap` section.
8285

8386
### 2. Integrating in an existing render script
84-
While the camera is enabled it will send `set_view_projection` messages once per frame to the render script. The message is the same as that of the camera component, meaning that it contains `id`, `view` and `projection` values. Make sure that these values are handled and used properly in the render script.
85-
86-
#### 2.1. Simplified integration
87-
The Orthographic API provides a helper module to easily update the camera and set screen and world view and projection. Integrate it in your own render script like this:
88-
89-
local render_helper = require "orthographic.render.helper"
90-
91-
function init(self)
92-
...
93-
render_helper.init()
94-
...
95-
end
96-
97-
function update(self)
98-
render_helper.set_world_view_projection()
99-
-- draw world
100-
...
101-
102-
render_helper.set_screen_view_projection()
103-
-- draw screen
104-
...
105-
end
106-
107-
function on_message(self, message_id, message)
108-
render_helper.on_message(self, message_id, message)
109-
...
110-
end
111-
112-
NOTE: In order for this to work you need to make sure that the `Shared State` setting in the `Script` section of `game.project` is checked (defaults to checked)
113-
114-
#### 2.2. Manual integration
115-
If you prefer to manually setup the integration you need to make sure to handle the `set_view_projection` message:
116-
117-
function update(self)
118-
...
119-
render.set_view(self.view)
120-
render.set_projection(self.projection)
121-
-- draw using the view and projection
122-
...
123-
end
124-
125-
function on_message(self, message_id, message, sender)
126-
if message_id == hash("set_view_projection") then
127-
self.camera_id = message.id
128-
self.view = message.view
129-
self.projection = message.projection
130-
end
131-
end
132-
133-
An alternative approach is to ignore the `set_view_projection` message and directly read the view and projection from the camera in the render script:
87+
Get a list of active cameras and apply the camera viewport, view and projection before drawing:
13488

89+
```
13590
local camera = require "orthographic.camera"
13691
13792
function update(self)
13893
...
139-
local camera_id = id of your camera
140-
render.set_view(camera.get_view(camera_id))
141-
render.set_projection(camera.get_projection(camera_id))
142-
-- draw using the view and projection
143-
...
144-
end
94+
for _,camera_id in ipairs(camera.get_cameras()) do
95+
local viewport = camera.get_viewport(camera_id)
96+
local view = camera.get_view(camera_id)
97+
local projection = camera.get_projection(camera_id)
14598
146-
It is recommended to send the window width and height from the render script to the camera module. This is required if any of the projectors provided in `camera.lua` is used. It also allows custom projectors to get the current window size by calling `camera.get_window_size()`. Set the window size like this:
99+
render.set_viewport(viewport.x, viewport.y, viewport.z, viewport.w)
100+
render.set_view(view)
101+
render.set_projection(projection)
147102
148-
local camera = require "orthographic.camera"
149-
150-
function update(self)
151-
...
152-
local window_width = render.get_window_width()
153-
local window_height = render.get_window_height()
154-
camera.set_window_size(window_width, window_height)
155-
...
103+
-- draw using the viewport, view and projection
104+
...
105+
end
156106
end
157-
158-
NOTE: In order for this to work you need to make sure that the `Shared State` setting in the `Script` section of `game.project` is checked (defaults to checked)
107+
```
159108

160109
### Example render script
161110
The `orthographic/render` folder contains a render script that does the above mentioned integration of the Orthographic Camera API. Use it as it is or copy it into your project and make whatever modifications that you need.

example/allfeatures/allfeatures.collection

-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ instances {
2525
value: "FIXED_ZOOM"
2626
type: PROPERTY_TYPE_HASH
2727
}
28-
properties {
29-
id: "offset_gui"
30-
value: "true"
31-
type: PROPERTY_TYPE_BOOLEAN
32-
}
3328
properties {
3429
id: "follow"
3530
value: "true"

example/multicamera/scene1.collection

+48
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,54 @@ instances {
7272
z: 1.0
7373
}
7474
}
75+
instances {
76+
id: "camera2"
77+
prototype: "/orthographic/camera.go"
78+
position {
79+
x: 0.0
80+
y: 0.0
81+
z: 0.0
82+
}
83+
rotation {
84+
x: 0.0
85+
y: 0.0
86+
z: 0.0
87+
w: 1.0
88+
}
89+
component_properties {
90+
id: "script"
91+
properties {
92+
id: "order"
93+
value: "2.0"
94+
type: PROPERTY_TYPE_NUMBER
95+
}
96+
properties {
97+
id: "viewport_left"
98+
value: "20.0"
99+
type: PROPERTY_TYPE_NUMBER
100+
}
101+
properties {
102+
id: "viewport_bottom"
103+
value: "20.0"
104+
type: PROPERTY_TYPE_NUMBER
105+
}
106+
properties {
107+
id: "viewport_right"
108+
value: "340.0"
109+
type: PROPERTY_TYPE_NUMBER
110+
}
111+
properties {
112+
id: "viewport_top"
113+
value: "200.0"
114+
type: PROPERTY_TYPE_NUMBER
115+
}
116+
}
117+
scale3 {
118+
x: 1.0
119+
y: 1.0
120+
z: 1.0
121+
}
122+
}
75123
scale_along_z: 0
76124
embedded_instances {
77125
id: "map"

game.project

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[project]
22
title = Orthographic
33
version = 0.9
4-
dependencies = https://github.com/subsoap/defos/archive/v2.3.1.zip,https://github.com/britzl/deftest/archive/2.7.0.zip
4+
dependencies#0 = https://github.com/subsoap/defos/archive/v2.3.1.zip
5+
dependencies#1 = https://github.com/britzl/deftest/archive/2.7.0.zip
56

67
[bootstrap]
78
main_collection = /example/allfeatures/allfeatures.collectionc

0 commit comments

Comments
 (0)