Skip to content

Commit 9d258d1

Browse files
committed
Fix orbit_camera.script
1 parent 728c2fa commit 9d258d1

File tree

9 files changed

+199
-7
lines changed

9 files changed

+199
-7
lines changed

material_screenspace/example/example.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ local w, h = window.get_size()
2121
go.set("#model", "screen_size", vmath.vector4(w, h, 0, 0))
2222
```
2323

24-
To activate a perspective camera and to have camera controls, we added the `orbit_camera.script` script from the [Orbit Camera (3D)](/examples/render/orbit_camera/orbit_camera/) example.
25-
2624
The shaders are written in GLSL 1.40, which is available from Defold 1.9.2. The model used in this example is from Kenney's [Prototype Pack](https://kenney.nl/assets/prototype-kit), licensed under CC0.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- The initial zoom level
2+
go.property("zoom", 3)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.1)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.5)
7+
-- The offset of the camera from the origin
8+
go.property("offset", vmath.vector3(0, 0, 0))
9+
10+
function init(self)
11+
-- Acquire input focus to receive input events
12+
msg.post(".", "acquire_input_focus")
13+
14+
-- Initialize start values
15+
self.yaw = go.get(".", "euler.y")
16+
self.pitch = go.get(".", "euler.x")
17+
self.zoom_offset = 0
18+
self.current_yaw = self.yaw
19+
self.current_pitch = self.pitch
20+
self.current_zoom = self.zoom_offset
21+
end
22+
23+
function update(self, dt)
24+
-- Animate camera rotation and zoom
25+
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
26+
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
27+
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)
28+
29+
-- Calculate rotation and position
30+
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
31+
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
32+
local camera_rotation = camera_yaw * camera_pitch
33+
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))
34+
35+
-- Set camera position and rotation
36+
go.set_position(camera_position)
37+
go.set_rotation(camera_rotation)
38+
end
39+
40+
function on_input(self, action_id, action)
41+
if action_id == hash("touch") and not action.pressed then
42+
self.yaw = self.yaw - action.dx * self.rotation_speed
43+
self.pitch = self.pitch + action.dy * self.rotation_speed
44+
elseif action_id == hash("wheel_up") then
45+
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
46+
elseif action_id == hash("wheel_down") then
47+
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
48+
end
49+
end

material_screenspace/example/screenspace.collection

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ embedded_instances {
88
"}\n"
99
"components {\n"
1010
" id: \"orbit_camera\"\n"
11-
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
11+
" component: \"/example/orbit_camera.script\"\n"
1212
" properties {\n"
1313
" id: \"offset\"\n"
1414
" value: \"0.0, 0.25, 0.0\"\n"

material_unlit/example/example.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ In industry-established terms, a material that is not affected by lighting is ca
1111

1212
This example contains a game object with a model that has an `unlit` material applied to it. The material is assigned custom vertex and fragment shaders. The shader is very simple and just transfers the texture color to the model. This is an excellent starting point for creating new materials and for creating effects that do not depend on lighting. The shaders are written in GLSL 1.40, which is available from Defold 1.9.2.
1313

14-
To activate a perspective camera and to have camera controls, we added the `orbit_camera.script` script from the [Orbit Camera (3D)](/examples/render/orbit_camera/orbit_camera/) example.
15-
1614
The model used in this example is from Kenney's [Train Pack](https://kenney.nl/assets/train-kit), licensed under CC0.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- The initial zoom level
2+
go.property("zoom", 3)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.1)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.5)
7+
-- The offset of the camera from the origin
8+
go.property("offset", vmath.vector3(0, 0, 0))
9+
10+
function init(self)
11+
-- Acquire input focus to receive input events
12+
msg.post(".", "acquire_input_focus")
13+
14+
-- Initialize start values
15+
self.yaw = go.get(".", "euler.y")
16+
self.pitch = go.get(".", "euler.x")
17+
self.zoom_offset = 0
18+
self.current_yaw = self.yaw
19+
self.current_pitch = self.pitch
20+
self.current_zoom = self.zoom_offset
21+
end
22+
23+
function update(self, dt)
24+
-- Animate camera rotation and zoom
25+
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
26+
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
27+
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)
28+
29+
-- Calculate rotation and position
30+
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
31+
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
32+
local camera_rotation = camera_yaw * camera_pitch
33+
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))
34+
35+
-- Set camera position and rotation
36+
go.set_position(camera_position)
37+
go.set_rotation(camera_rotation)
38+
end
39+
40+
function on_input(self, action_id, action)
41+
if action_id == hash("touch") and not action.pressed then
42+
self.yaw = self.yaw - action.dx * self.rotation_speed
43+
self.pitch = self.pitch + action.dy * self.rotation_speed
44+
elseif action_id == hash("wheel_up") then
45+
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
46+
elseif action_id == hash("wheel_down") then
47+
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
48+
end
49+
end

material_unlit/example/unlit.collection

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ embedded_instances {
2323
id: "camera"
2424
data: "components {\n"
2525
" id: \"orbit_camera\"\n"
26-
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
26+
" component: \"/example/orbit_camera.script\"\n"
2727
" properties {\n"
2828
" id: \"zoom\"\n"
2929
" value: \"7.0\"\n"

movement_look_rotation/example/look_rotation.collection

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ embedded_instances {
88
"}\n"
99
"components {\n"
1010
" id: \"orbit_camera\"\n"
11-
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
11+
" component: \"/example/orbit_camera.script\"\n"
1212
" properties {\n"
1313
" id: \"zoom\"\n"
1414
" value: \"6.0\"\n"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- The initial zoom level
2+
go.property("zoom", 3)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.1)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.5)
7+
-- The offset of the camera from the origin
8+
go.property("offset", vmath.vector3(0, 0, 0))
9+
10+
function init(self)
11+
-- Acquire input focus to receive input events
12+
msg.post(".", "acquire_input_focus")
13+
14+
-- Initialize start values
15+
self.yaw = go.get(".", "euler.y")
16+
self.pitch = go.get(".", "euler.x")
17+
self.zoom_offset = 0
18+
self.current_yaw = self.yaw
19+
self.current_pitch = self.pitch
20+
self.current_zoom = self.zoom_offset
21+
end
22+
23+
function update(self, dt)
24+
-- Animate camera rotation and zoom
25+
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
26+
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
27+
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)
28+
29+
-- Calculate rotation and position
30+
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
31+
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
32+
local camera_rotation = camera_yaw * camera_pitch
33+
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))
34+
35+
-- Set camera position and rotation
36+
go.set_position(camera_position)
37+
go.set_rotation(camera_rotation)
38+
end
39+
40+
function on_input(self, action_id, action)
41+
if action_id == hash("touch") and not action.pressed then
42+
self.yaw = self.yaw - action.dx * self.rotation_speed
43+
self.pitch = self.pitch + action.dy * self.rotation_speed
44+
elseif action_id == hash("wheel_up") then
45+
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
46+
elseif action_id == hash("wheel_down") then
47+
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
48+
end
49+
end

orbit_camera.script

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- The initial zoom level
2+
go.property("zoom", 3)
3+
-- The speed of the zoom
4+
go.property("zoom_speed", 0.1)
5+
-- The speed of the rotation
6+
go.property("rotation_speed", 0.5)
7+
-- The offset of the camera from the origin
8+
go.property("offset", vmath.vector3(0, 0, 0))
9+
10+
function init(self)
11+
-- Acquire input focus to receive input events
12+
msg.post(".", "acquire_input_focus")
13+
14+
-- Initialize start values
15+
self.yaw = go.get(".", "euler.y")
16+
self.pitch = go.get(".", "euler.x")
17+
self.zoom_offset = 0
18+
self.current_yaw = self.yaw
19+
self.current_pitch = self.pitch
20+
self.current_zoom = self.zoom_offset
21+
end
22+
23+
function update(self, dt)
24+
-- Animate camera rotation and zoom
25+
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
26+
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
27+
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)
28+
29+
-- Calculate rotation and position
30+
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
31+
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
32+
local camera_rotation = camera_yaw * camera_pitch
33+
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))
34+
35+
-- Set camera position and rotation
36+
go.set_position(camera_position)
37+
go.set_rotation(camera_rotation)
38+
end
39+
40+
function on_input(self, action_id, action)
41+
if action_id == hash("touch") and not action.pressed then
42+
self.yaw = self.yaw - action.dx * self.rotation_speed
43+
self.pitch = self.pitch + action.dy * self.rotation_speed
44+
elseif action_id == hash("wheel_up") then
45+
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
46+
elseif action_id == hash("wheel_down") then
47+
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
48+
end
49+
end

0 commit comments

Comments
 (0)