Skip to content

Commit

Permalink
Fix orbit_camera.script
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Feb 9, 2025
1 parent 728c2fa commit 9d258d1
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 7 deletions.
2 changes: 0 additions & 2 deletions material_screenspace/example/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ local w, h = window.get_size()
go.set("#model", "screen_size", vmath.vector4(w, h, 0, 0))
```

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.

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.
49 changes: 49 additions & 0 deletions material_screenspace/example/orbit_camera.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- The initial zoom level
go.property("zoom", 3)
-- The speed of the zoom
go.property("zoom_speed", 0.1)
-- The speed of the rotation
go.property("rotation_speed", 0.5)
-- The offset of the camera from the origin
go.property("offset", vmath.vector3(0, 0, 0))

function init(self)
-- Acquire input focus to receive input events
msg.post(".", "acquire_input_focus")

-- Initialize start values
self.yaw = go.get(".", "euler.y")
self.pitch = go.get(".", "euler.x")
self.zoom_offset = 0
self.current_yaw = self.yaw
self.current_pitch = self.pitch
self.current_zoom = self.zoom_offset
end

function update(self, dt)
-- Animate camera rotation and zoom
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)

-- Calculate rotation and position
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
local camera_rotation = camera_yaw * camera_pitch
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))

-- Set camera position and rotation
go.set_position(camera_position)
go.set_rotation(camera_rotation)
end

function on_input(self, action_id, action)
if action_id == hash("touch") and not action.pressed then
self.yaw = self.yaw - action.dx * self.rotation_speed
self.pitch = self.pitch + action.dy * self.rotation_speed
elseif action_id == hash("wheel_up") then
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
elseif action_id == hash("wheel_down") then
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
end
end
2 changes: 1 addition & 1 deletion material_screenspace/example/screenspace.collection
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ embedded_instances {
"}\n"
"components {\n"
" id: \"orbit_camera\"\n"
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
" component: \"/example/orbit_camera.script\"\n"
" properties {\n"
" id: \"offset\"\n"
" value: \"0.0, 0.25, 0.0\"\n"
Expand Down
2 changes: 0 additions & 2 deletions material_unlit/example/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ In industry-established terms, a material that is not affected by lighting is ca

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.

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.

The model used in this example is from Kenney's [Train Pack](https://kenney.nl/assets/train-kit), licensed under CC0.
49 changes: 49 additions & 0 deletions material_unlit/example/orbit_camera.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- The initial zoom level
go.property("zoom", 3)
-- The speed of the zoom
go.property("zoom_speed", 0.1)
-- The speed of the rotation
go.property("rotation_speed", 0.5)
-- The offset of the camera from the origin
go.property("offset", vmath.vector3(0, 0, 0))

function init(self)
-- Acquire input focus to receive input events
msg.post(".", "acquire_input_focus")

-- Initialize start values
self.yaw = go.get(".", "euler.y")
self.pitch = go.get(".", "euler.x")
self.zoom_offset = 0
self.current_yaw = self.yaw
self.current_pitch = self.pitch
self.current_zoom = self.zoom_offset
end

function update(self, dt)
-- Animate camera rotation and zoom
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)

-- Calculate rotation and position
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
local camera_rotation = camera_yaw * camera_pitch
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))

-- Set camera position and rotation
go.set_position(camera_position)
go.set_rotation(camera_rotation)
end

function on_input(self, action_id, action)
if action_id == hash("touch") and not action.pressed then
self.yaw = self.yaw - action.dx * self.rotation_speed
self.pitch = self.pitch + action.dy * self.rotation_speed
elseif action_id == hash("wheel_up") then
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
elseif action_id == hash("wheel_down") then
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
end
end
2 changes: 1 addition & 1 deletion material_unlit/example/unlit.collection
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ embedded_instances {
id: "camera"
data: "components {\n"
" id: \"orbit_camera\"\n"
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
" component: \"/example/orbit_camera.script\"\n"
" properties {\n"
" id: \"zoom\"\n"
" value: \"7.0\"\n"
Expand Down
2 changes: 1 addition & 1 deletion movement_look_rotation/example/look_rotation.collection
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ embedded_instances {
"}\n"
"components {\n"
" id: \"orbit_camera\"\n"
" component: \"/examples/render/orbit_camera/orbit_camera.script\"\n"
" component: \"/example/orbit_camera.script\"\n"
" properties {\n"
" id: \"zoom\"\n"
" value: \"6.0\"\n"
Expand Down
49 changes: 49 additions & 0 deletions movement_look_rotation/example/orbit_camera.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- The initial zoom level
go.property("zoom", 3)
-- The speed of the zoom
go.property("zoom_speed", 0.1)
-- The speed of the rotation
go.property("rotation_speed", 0.5)
-- The offset of the camera from the origin
go.property("offset", vmath.vector3(0, 0, 0))

function init(self)
-- Acquire input focus to receive input events
msg.post(".", "acquire_input_focus")

-- Initialize start values
self.yaw = go.get(".", "euler.y")
self.pitch = go.get(".", "euler.x")
self.zoom_offset = 0
self.current_yaw = self.yaw
self.current_pitch = self.pitch
self.current_zoom = self.zoom_offset
end

function update(self, dt)
-- Animate camera rotation and zoom
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)

-- Calculate rotation and position
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
local camera_rotation = camera_yaw * camera_pitch
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))

-- Set camera position and rotation
go.set_position(camera_position)
go.set_rotation(camera_rotation)
end

function on_input(self, action_id, action)
if action_id == hash("touch") and not action.pressed then
self.yaw = self.yaw - action.dx * self.rotation_speed
self.pitch = self.pitch + action.dy * self.rotation_speed
elseif action_id == hash("wheel_up") then
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
elseif action_id == hash("wheel_down") then
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
end
end
49 changes: 49 additions & 0 deletions orbit_camera.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- The initial zoom level
go.property("zoom", 3)
-- The speed of the zoom
go.property("zoom_speed", 0.1)
-- The speed of the rotation
go.property("rotation_speed", 0.5)
-- The offset of the camera from the origin
go.property("offset", vmath.vector3(0, 0, 0))

function init(self)
-- Acquire input focus to receive input events
msg.post(".", "acquire_input_focus")

-- Initialize start values
self.yaw = go.get(".", "euler.y")
self.pitch = go.get(".", "euler.x")
self.zoom_offset = 0
self.current_yaw = self.yaw
self.current_pitch = self.pitch
self.current_zoom = self.zoom_offset
end

function update(self, dt)
-- Animate camera rotation and zoom
self.current_yaw = vmath.lerp(0.15, self.current_yaw, self.yaw)
self.current_pitch = vmath.lerp(0.15, self.current_pitch, self.pitch)
self.current_zoom = vmath.lerp(0.15, self.current_zoom, self.zoom_offset)

-- Calculate rotation and position
local camera_yaw = vmath.quat_rotation_y(math.rad(self.current_yaw))
local camera_pitch = vmath.quat_rotation_x(math.rad(self.current_pitch))
local camera_rotation = camera_yaw * camera_pitch
local camera_position = self.offset + vmath.rotate(camera_rotation, vmath.vector3(0, 0, self.zoom + self.current_zoom))

-- Set camera position and rotation
go.set_position(camera_position)
go.set_rotation(camera_rotation)
end

function on_input(self, action_id, action)
if action_id == hash("touch") and not action.pressed then
self.yaw = self.yaw - action.dx * self.rotation_speed
self.pitch = self.pitch + action.dy * self.rotation_speed
elseif action_id == hash("wheel_up") then
self.zoom_offset = self.zoom_offset - self.zoom * self.zoom_speed
elseif action_id == hash("wheel_down") then
self.zoom_offset = self.zoom_offset + self.zoom * self.zoom_speed
end
end

0 comments on commit 9d258d1

Please sign in to comment.