diff --git a/material_screenspace/example/example.md b/material_screenspace/example/example.md index fcddf35..c7ea988 100644 --- a/material_screenspace/example/example.md +++ b/material_screenspace/example/example.md @@ -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. diff --git a/material_screenspace/example/orbit_camera.script b/material_screenspace/example/orbit_camera.script new file mode 100644 index 0000000..9683366 --- /dev/null +++ b/material_screenspace/example/orbit_camera.script @@ -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 diff --git a/material_screenspace/example/screenspace.collection b/material_screenspace/example/screenspace.collection index 0ab4bdf..85f4d5a 100644 --- a/material_screenspace/example/screenspace.collection +++ b/material_screenspace/example/screenspace.collection @@ -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" diff --git a/material_unlit/example/example.md b/material_unlit/example/example.md index 520077b..3897b48 100644 --- a/material_unlit/example/example.md +++ b/material_unlit/example/example.md @@ -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. diff --git a/material_unlit/example/orbit_camera.script b/material_unlit/example/orbit_camera.script new file mode 100644 index 0000000..9683366 --- /dev/null +++ b/material_unlit/example/orbit_camera.script @@ -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 diff --git a/material_unlit/example/unlit.collection b/material_unlit/example/unlit.collection index 6f41a92..f55efe5 100644 --- a/material_unlit/example/unlit.collection +++ b/material_unlit/example/unlit.collection @@ -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" diff --git a/movement_look_rotation/example/look_rotation.collection b/movement_look_rotation/example/look_rotation.collection index 246cb79..87bf878 100644 --- a/movement_look_rotation/example/look_rotation.collection +++ b/movement_look_rotation/example/look_rotation.collection @@ -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" diff --git a/movement_look_rotation/example/orbit_camera.script b/movement_look_rotation/example/orbit_camera.script new file mode 100644 index 0000000..9683366 --- /dev/null +++ b/movement_look_rotation/example/orbit_camera.script @@ -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 diff --git a/orbit_camera.script b/orbit_camera.script new file mode 100644 index 0000000..9683366 --- /dev/null +++ b/orbit_camera.script @@ -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