init
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
/* Copyright (c) 2020-2023, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "animation.h"
|
||||
|
||||
#include "scene_graph/node.h"
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
Animation::Animation(const std::string &name) :
|
||||
Script{name}
|
||||
{
|
||||
}
|
||||
|
||||
Animation::Animation(const Animation &other) :
|
||||
channels{other.channels}
|
||||
{
|
||||
}
|
||||
|
||||
void Animation::add_channel(Node &node, const AnimationTarget &target, const AnimationSampler &sampler)
|
||||
{
|
||||
channels.push_back({node, target, sampler});
|
||||
}
|
||||
|
||||
void Animation::update(float delta_time)
|
||||
{
|
||||
current_time += delta_time;
|
||||
if (current_time > end_time)
|
||||
{
|
||||
current_time -= end_time;
|
||||
}
|
||||
|
||||
for (auto &channel : channels)
|
||||
{
|
||||
for (size_t i = 0; i < channel.sampler.inputs.size() - 1; ++i)
|
||||
{
|
||||
if ((current_time >= channel.sampler.inputs[i]) && (current_time <= channel.sampler.inputs[i + 1]))
|
||||
{
|
||||
float time = (current_time - channel.sampler.inputs[i]) / (channel.sampler.inputs[i + 1] - channel.sampler.inputs[i]);
|
||||
|
||||
auto &transform = channel.node.get_transform();
|
||||
|
||||
if (channel.sampler.type == AnimationType::Linear)
|
||||
{
|
||||
switch (channel.target)
|
||||
{
|
||||
case Translation:
|
||||
{
|
||||
transform.set_translation(glm::vec3(glm::mix(channel.sampler.outputs[i],
|
||||
channel.sampler.outputs[i + 1],
|
||||
time)));
|
||||
break;
|
||||
}
|
||||
case Rotation:
|
||||
{
|
||||
glm::quat q1;
|
||||
q1.x = channel.sampler.outputs[i].x;
|
||||
q1.y = channel.sampler.outputs[i].y;
|
||||
q1.z = channel.sampler.outputs[i].z;
|
||||
q1.w = channel.sampler.outputs[i].w;
|
||||
|
||||
glm::quat q2;
|
||||
q2.x = channel.sampler.outputs[i + 1].x;
|
||||
q2.y = channel.sampler.outputs[i + 1].y;
|
||||
q2.z = channel.sampler.outputs[i + 1].z;
|
||||
q2.w = channel.sampler.outputs[i + 1].w;
|
||||
|
||||
transform.set_rotation(glm::normalize(glm::slerp(q1, q2, time)));
|
||||
break;
|
||||
}
|
||||
|
||||
case Scale:
|
||||
{
|
||||
transform.set_scale(glm::vec3(glm::mix(channel.sampler.outputs[i],
|
||||
channel.sampler.outputs[i + 1],
|
||||
time)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (channel.sampler.type == AnimationType::Step)
|
||||
{
|
||||
switch (channel.target)
|
||||
{
|
||||
case Translation:
|
||||
{
|
||||
transform.set_translation(glm::vec3(channel.sampler.outputs[i]));
|
||||
break;
|
||||
}
|
||||
case Rotation:
|
||||
{
|
||||
glm::quat q1;
|
||||
q1.x = channel.sampler.outputs[i].x;
|
||||
q1.y = channel.sampler.outputs[i].y;
|
||||
q1.z = channel.sampler.outputs[i].z;
|
||||
q1.w = channel.sampler.outputs[i].w;
|
||||
|
||||
transform.set_rotation(glm::normalize(q1));
|
||||
break;
|
||||
}
|
||||
|
||||
case Scale:
|
||||
{
|
||||
transform.set_scale(glm::vec3(channel.sampler.outputs[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (channel.sampler.type == AnimationType::CubicSpline)
|
||||
{
|
||||
float delta = channel.sampler.inputs[i + 1] - channel.sampler.inputs[i];
|
||||
|
||||
glm::vec4 p0 = channel.sampler.outputs[i * 3 + 1]; // Starting point
|
||||
glm::vec4 p1 = channel.sampler.outputs[(i + 1) * 3 + 1]; // Ending point
|
||||
|
||||
glm::vec4 m0 = delta * channel.sampler.outputs[i * 3 + 2]; // Delta time * out tangent
|
||||
glm::vec4 m1 = delta * channel.sampler.outputs[(i + 1) * 3 + 0]; // Delta time * in tangent of next point
|
||||
|
||||
// This equation is taken from the GLTF 2.0 specification Appendix C (https://github.com/KhronosGroup/glTF/tree/main/specification/2.0#appendix-c-spline-interpolation)
|
||||
glm::vec4 result = (2.0f * glm::pow(time, 3.0f) - 3.0f * glm::pow(time, 2.0f) + 1.0f) * p0 + (glm::pow(time, 3.0f) - 2.0f * glm::pow(time, 2.0f) + time) * m0 + (-2.0f * glm::pow(time, 3.0f) + 3.0f * glm::pow(time, 2.0f)) * p1 + (glm::pow(time, 3.0f) - glm::pow(time, 2.0f)) * m1;
|
||||
|
||||
auto &transform = channel.node.get_transform();
|
||||
|
||||
switch (channel.target)
|
||||
{
|
||||
case Translation:
|
||||
{
|
||||
transform.set_translation(glm::vec3(result));
|
||||
break;
|
||||
}
|
||||
case Rotation:
|
||||
{
|
||||
glm::quat q1;
|
||||
q1.x = result.x;
|
||||
q1.y = result.y;
|
||||
q1.z = result.z;
|
||||
q1.w = result.w;
|
||||
|
||||
transform.set_rotation(glm::normalize(q1));
|
||||
break;
|
||||
}
|
||||
|
||||
case Scale:
|
||||
{
|
||||
transform.set_scale(glm::vec3(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Animation::update_times(float new_start_time, float new_end_time)
|
||||
{
|
||||
if (new_start_time < start_time)
|
||||
{
|
||||
start_time = new_start_time;
|
||||
}
|
||||
if (new_end_time > end_time)
|
||||
{
|
||||
end_time = new_end_time;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
@@ -0,0 +1,88 @@
|
||||
/* Copyright (c) 2020-2021, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
#include "scene_graph/components/transform.h"
|
||||
#include "scene_graph/script.h"
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
enum AnimationType
|
||||
{
|
||||
Linear,
|
||||
Step,
|
||||
CubicSpline
|
||||
};
|
||||
|
||||
enum AnimationTarget
|
||||
{
|
||||
Translation,
|
||||
Rotation,
|
||||
Scale
|
||||
};
|
||||
|
||||
struct AnimationSampler
|
||||
{
|
||||
AnimationType type{Linear};
|
||||
|
||||
std::vector<float> inputs{};
|
||||
|
||||
std::vector<glm::vec4> outputs{};
|
||||
};
|
||||
|
||||
struct AnimationChannel
|
||||
{
|
||||
Node &node;
|
||||
|
||||
AnimationTarget target;
|
||||
|
||||
AnimationSampler sampler;
|
||||
};
|
||||
|
||||
class Animation : public Script
|
||||
{
|
||||
public:
|
||||
Animation(const std::string &name = "");
|
||||
|
||||
Animation(const Animation &);
|
||||
|
||||
virtual void update(float delta_time) override;
|
||||
|
||||
void update_times(float start_time, float end_time);
|
||||
|
||||
void add_channel(Node &node, const AnimationTarget &target, const AnimationSampler &sampler);
|
||||
|
||||
private:
|
||||
std::vector<AnimationChannel> channels;
|
||||
|
||||
float current_time{0.0f};
|
||||
|
||||
float start_time{std::numeric_limits<float>::max()};
|
||||
|
||||
float end_time{std::numeric_limits<float>::min()};
|
||||
};
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
@@ -0,0 +1,241 @@
|
||||
/* Copyright (c) 2019-2024, Arm Limited and Contributors
|
||||
* Copyright (c) 2020-2024, Andrew Cox, Huawei Technologies Research & Development (UK) Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "free_camera.h"
|
||||
|
||||
#include "common/error.h"
|
||||
|
||||
#include "common/glm_common.h"
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
#include "scene_graph/components/perspective_camera.h"
|
||||
#include "scene_graph/components/transform.h"
|
||||
#include "scene_graph/node.h"
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
const float FreeCamera::TOUCH_DOWN_MOVE_FORWARD_WAIT_TIME = 2.0f;
|
||||
|
||||
const float FreeCamera::ROTATION_MOVE_WEIGHT = 0.1f;
|
||||
|
||||
const float FreeCamera::KEY_ROTATION_MOVE_WEIGHT = 0.5f;
|
||||
|
||||
const float FreeCamera::TRANSLATION_MOVE_WEIGHT = 3.0f;
|
||||
|
||||
const float FreeCamera::TRANSLATION_MOVE_STEP = 50.0f;
|
||||
|
||||
const uint32_t FreeCamera::TRANSLATION_MOVE_SPEED = 4;
|
||||
|
||||
FreeCamera::FreeCamera(Node &node) :
|
||||
NodeScript{node, "FreeCamera"}
|
||||
{}
|
||||
|
||||
void FreeCamera::update(float delta_time)
|
||||
{
|
||||
glm::vec3 delta_translation(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 delta_rotation(0.0f, 0.0f, 0.0f);
|
||||
|
||||
float mul_translation = speed_multiplier;
|
||||
|
||||
if (key_pressed[KeyCode::W])
|
||||
{
|
||||
delta_translation.z -= TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::S])
|
||||
{
|
||||
delta_translation.z += TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::A])
|
||||
{
|
||||
delta_translation.x -= TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::D])
|
||||
{
|
||||
delta_translation.x += TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::Q])
|
||||
{
|
||||
delta_translation.y -= TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::E])
|
||||
{
|
||||
delta_translation.y += TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
if (key_pressed[KeyCode::LeftControl])
|
||||
{
|
||||
mul_translation *= (1.0f * TRANSLATION_MOVE_SPEED);
|
||||
}
|
||||
if (key_pressed[KeyCode::LeftShift])
|
||||
{
|
||||
mul_translation *= (1.0f / TRANSLATION_MOVE_SPEED);
|
||||
}
|
||||
|
||||
if (key_pressed[KeyCode::I])
|
||||
{
|
||||
delta_rotation.x += KEY_ROTATION_MOVE_WEIGHT;
|
||||
}
|
||||
if (key_pressed[KeyCode::K])
|
||||
{
|
||||
delta_rotation.x -= KEY_ROTATION_MOVE_WEIGHT;
|
||||
}
|
||||
if (key_pressed[KeyCode::J])
|
||||
{
|
||||
delta_rotation.y += KEY_ROTATION_MOVE_WEIGHT;
|
||||
}
|
||||
if (key_pressed[KeyCode::L])
|
||||
{
|
||||
delta_rotation.y -= KEY_ROTATION_MOVE_WEIGHT;
|
||||
}
|
||||
|
||||
if (mouse_button_pressed[MouseButton::Left] && mouse_button_pressed[MouseButton::Right])
|
||||
{
|
||||
delta_rotation.z += TRANSLATION_MOVE_WEIGHT * mouse_move_delta.x;
|
||||
}
|
||||
else if (mouse_button_pressed[MouseButton::Right])
|
||||
{
|
||||
delta_rotation.x -= ROTATION_MOVE_WEIGHT * mouse_move_delta.y;
|
||||
delta_rotation.y -= ROTATION_MOVE_WEIGHT * mouse_move_delta.x;
|
||||
}
|
||||
else if (mouse_button_pressed[MouseButton::Left])
|
||||
{
|
||||
delta_translation.x += TRANSLATION_MOVE_WEIGHT * mouse_move_delta.x;
|
||||
delta_translation.y += TRANSLATION_MOVE_WEIGHT * -mouse_move_delta.y;
|
||||
}
|
||||
|
||||
if (touch_pointer_pressed[0])
|
||||
{
|
||||
delta_rotation.x -= ROTATION_MOVE_WEIGHT * touch_move_delta.y;
|
||||
delta_rotation.y -= ROTATION_MOVE_WEIGHT * touch_move_delta.x;
|
||||
|
||||
if (touch_pointer_time > TOUCH_DOWN_MOVE_FORWARD_WAIT_TIME)
|
||||
{
|
||||
delta_translation.z -= TRANSLATION_MOVE_STEP;
|
||||
}
|
||||
else
|
||||
{
|
||||
touch_pointer_time += delta_time;
|
||||
}
|
||||
}
|
||||
|
||||
delta_translation *= mul_translation * delta_time;
|
||||
delta_rotation *= delta_time;
|
||||
|
||||
// Only re-calculate the transform if it's changed
|
||||
if (delta_rotation != glm::vec3(0.0f, 0.0f, 0.0f) || delta_translation != glm::vec3(0.0f, 0.0f, 0.0f))
|
||||
{
|
||||
auto &transform = get_node().get_component<Transform>();
|
||||
|
||||
glm::quat qx = glm::angleAxis(delta_rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
glm::quat qy = glm::angleAxis(delta_rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
glm::quat orientation = glm::normalize(qy * transform.get_rotation() * qx);
|
||||
|
||||
transform.set_translation(transform.get_translation() + delta_translation * glm::conjugate(orientation));
|
||||
transform.set_rotation(orientation);
|
||||
}
|
||||
|
||||
mouse_move_delta = {};
|
||||
touch_move_delta = {};
|
||||
}
|
||||
|
||||
void FreeCamera::input_event(const InputEvent &input_event)
|
||||
{
|
||||
if (input_event.get_source() == EventSource::Keyboard)
|
||||
{
|
||||
const auto &key_event = static_cast<const KeyInputEvent &>(input_event);
|
||||
|
||||
if (key_event.get_action() == KeyAction::Down ||
|
||||
key_event.get_action() == KeyAction::Repeat)
|
||||
{
|
||||
key_pressed[key_event.get_code()] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
key_pressed[key_event.get_code()] = false;
|
||||
}
|
||||
}
|
||||
else if (input_event.get_source() == EventSource::Mouse)
|
||||
{
|
||||
const auto &mouse_button = static_cast<const MouseButtonInputEvent &>(input_event);
|
||||
|
||||
glm::vec2 mouse_pos{std::floor(mouse_button.get_pos_x()), std::floor(mouse_button.get_pos_y())};
|
||||
|
||||
if (mouse_button.get_action() == MouseAction::Down)
|
||||
{
|
||||
mouse_button_pressed[mouse_button.get_button()] = true;
|
||||
}
|
||||
|
||||
if (mouse_button.get_action() == MouseAction::Up)
|
||||
{
|
||||
mouse_button_pressed[mouse_button.get_button()] = false;
|
||||
}
|
||||
|
||||
if (mouse_button.get_action() == MouseAction::Move)
|
||||
{
|
||||
mouse_move_delta = mouse_pos - mouse_last_pos;
|
||||
|
||||
mouse_last_pos = mouse_pos;
|
||||
}
|
||||
}
|
||||
else if (input_event.get_source() == EventSource::Touchscreen)
|
||||
{
|
||||
const auto &touch_event = static_cast<const TouchInputEvent &>(input_event);
|
||||
|
||||
glm::vec2 touch_pos{std::floor(touch_event.get_pos_x()), std::floor(touch_event.get_pos_y())};
|
||||
|
||||
if (touch_event.get_action() == TouchAction::Down)
|
||||
{
|
||||
touch_pointer_pressed[touch_event.get_pointer_id()] = true;
|
||||
|
||||
touch_last_pos = touch_pos;
|
||||
}
|
||||
|
||||
if (touch_event.get_action() == TouchAction::Up)
|
||||
{
|
||||
touch_pointer_pressed[touch_event.get_pointer_id()] = false;
|
||||
|
||||
touch_pointer_time = 0.0f;
|
||||
}
|
||||
|
||||
if (touch_event.get_action() == TouchAction::Move && touch_event.get_pointer_id() == 0)
|
||||
{
|
||||
touch_move_delta = touch_pos - touch_last_pos;
|
||||
|
||||
touch_last_pos = touch_pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FreeCamera::resize(uint32_t width, uint32_t height)
|
||||
{
|
||||
auto &camera_node = get_node();
|
||||
|
||||
if (camera_node.has_component<Camera>())
|
||||
{
|
||||
if (auto camera = dynamic_cast<PerspectiveCamera *>(&camera_node.get_component<Camera>()))
|
||||
{
|
||||
camera->set_aspect_ratio(static_cast<float>(width) / height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
@@ -0,0 +1,81 @@
|
||||
/* Copyright (c) 2019-2024, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "common/error.h"
|
||||
|
||||
#include "common/glm_common.h"
|
||||
|
||||
#include "scene_graph/script.h"
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
class FreeCamera : public NodeScript
|
||||
{
|
||||
public:
|
||||
static const float TOUCH_DOWN_MOVE_FORWARD_WAIT_TIME;
|
||||
|
||||
static const float ROTATION_MOVE_WEIGHT;
|
||||
|
||||
static const float KEY_ROTATION_MOVE_WEIGHT;
|
||||
|
||||
static const float TRANSLATION_MOVE_WEIGHT;
|
||||
|
||||
static const float TRANSLATION_MOVE_STEP;
|
||||
|
||||
static const uint32_t TRANSLATION_MOVE_SPEED;
|
||||
|
||||
FreeCamera(Node &node);
|
||||
|
||||
virtual ~FreeCamera() = default;
|
||||
|
||||
virtual void update(float delta_time) override;
|
||||
|
||||
virtual void input_event(const InputEvent &input_event) override;
|
||||
|
||||
virtual void resize(uint32_t width, uint32_t height) override;
|
||||
|
||||
private:
|
||||
float speed_multiplier{3.0f};
|
||||
|
||||
glm::vec2 mouse_move_delta{0.0f};
|
||||
|
||||
glm::vec2 mouse_last_pos{0.0f};
|
||||
|
||||
glm::vec2 touch_move_delta{0.0f};
|
||||
|
||||
glm::vec2 touch_last_pos{0.0f};
|
||||
|
||||
float touch_pointer_time{0.0f};
|
||||
|
||||
std::unordered_map<KeyCode, bool> key_pressed;
|
||||
|
||||
std::unordered_map<MouseButton, bool> mouse_button_pressed;
|
||||
|
||||
std::unordered_map<int32_t, bool> touch_pointer_pressed;
|
||||
};
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
@@ -0,0 +1,58 @@
|
||||
/* Copyright (c) 2019-2024, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "node_animation.h"
|
||||
|
||||
#include "common/error.h"
|
||||
|
||||
#include "common/glm_common.h"
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
#include "scene_graph/components/perspective_camera.h"
|
||||
#include "scene_graph/components/transform.h"
|
||||
#include "scene_graph/node.h"
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
NodeAnimation::NodeAnimation(Node &node, TransformAnimFn animation_fn) :
|
||||
NodeScript{node, ""},
|
||||
animation_fn{animation_fn}
|
||||
{
|
||||
}
|
||||
|
||||
void NodeAnimation::update(float delta_time)
|
||||
{
|
||||
if (animation_fn)
|
||||
{
|
||||
animation_fn(get_node().get_component<Transform>(), delta_time);
|
||||
}
|
||||
}
|
||||
|
||||
void NodeAnimation::set_animation(TransformAnimFn handle)
|
||||
{
|
||||
animation_fn = handle;
|
||||
}
|
||||
|
||||
void NodeAnimation::clear_animation()
|
||||
{
|
||||
animation_fn = {};
|
||||
}
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
@@ -0,0 +1,56 @@
|
||||
/* Copyright (c) 2019-2021, Arm Limited and Contributors
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
#include "scene_graph/components/transform.h"
|
||||
#include "scene_graph/script.h"
|
||||
|
||||
/**
|
||||
* @param std::shared_ptr<vkb::sg::Transform> The transform to animate
|
||||
* @param float The delta time of the frame to scale animations
|
||||
*/
|
||||
using TransformAnimFn = std::function<void(vkb::sg::Transform &, float)>;
|
||||
|
||||
namespace vkb
|
||||
{
|
||||
namespace sg
|
||||
{
|
||||
class NodeAnimation : public NodeScript
|
||||
{
|
||||
public:
|
||||
NodeAnimation(Node &node, TransformAnimFn animation_fn);
|
||||
|
||||
virtual ~NodeAnimation() = default;
|
||||
|
||||
virtual void update(float delta_time) override;
|
||||
|
||||
void set_animation(TransformAnimFn handle);
|
||||
|
||||
void clear_animation();
|
||||
|
||||
private:
|
||||
TransformAnimFn animation_fn{};
|
||||
};
|
||||
} // namespace sg
|
||||
} // namespace vkb
|
||||
Reference in New Issue
Block a user