From ef74f39cf46955df45d72be2a2e9d59b6e9dd3ab Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Tue, 22 Feb 2022 22:34:13 +0100 Subject: [PATCH] Splitted animation and data node inputs. --- src/AnimGraphResource.cc | 41 +++++++++++++++++++++++---------- src/AnimGraphResource.h | 37 +++++++++++++++++------------ tests/AnimGraphResourceTests.cc | 11 ++++----- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/AnimGraphResource.cc b/src/AnimGraphResource.cc index b7d56a9..1695d20 100644 --- a/src/AnimGraphResource.cc +++ b/src/AnimGraphResource.cc @@ -358,8 +358,8 @@ void AnimGraph::UpdateOrderedNodes() { m_eval_ordered_nodes.clear(); while (node_index_stack.size() > 0) { - std::vector& node_inputs = - m_node_inputs[node_index_stack.back()]; + std::vector& node_inputs = + m_node_anim_inputs[node_index_stack.back()]; node_index_stack.pop_back(); for (size_t i = 0, n = node_inputs.size(); i < n; i++) { @@ -392,9 +392,19 @@ void AnimGraph::MarkActiveNodes() { m_nodes[i]->m_state = AnimNodeEvalState::Deactivated; } - const std::vector graph_output_inputs = m_node_inputs[0]; - for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) { - AnimNode* node = m_eval_ordered_nodes[i]; + const std::vector graph_output_anim_inputs = m_node_anim_inputs[0]; + for (size_t i = 0, n = graph_output_anim_inputs.size(); i < n; i++) { + const NodeInput& graph_input = graph_output_anim_inputs[i]; + AnimNode* node = graph_input.m_node; + if (node != nullptr) { + node->m_state = AnimNodeEvalState::Activated; + } + } + + const std::vector graph_output_data_inputs = m_node_data_inputs[0]; + for (size_t i = 0, n = graph_output_data_inputs.size(); i < n; i++) { + const NodeInput& graph_input = graph_output_data_inputs[i]; + AnimNode* node = graph_input.m_node; if (node != nullptr) { node->m_state = AnimNodeEvalState::Activated; } @@ -404,13 +414,20 @@ void AnimGraph::MarkActiveNodes() { AnimNode* node = m_eval_ordered_nodes[i]; if (CheckIsNodeActive(node)) { int node_index = getAnimNodeIndex(node); - node->MarkActiveInputs(m_node_inputs[node_index]); + node->MarkActiveInputs(m_node_anim_inputs[node_index]); + + for (size_t j = 0, nj = m_node_data_inputs[node_index].size(); j < nj; j++) { + const NodeInput& data_input = m_node_data_inputs[node_index][j]; + if (data_input.m_node != nullptr) { + data_input.m_node->m_state = AnimNodeEvalState::Activated; + } + } } } } void AnimGraph::UpdateTime(float dt) { - const std::vector graph_output_inputs = m_node_inputs[0]; + const std::vector graph_output_inputs = m_node_anim_inputs[0]; for (size_t i = 0, n = graph_output_inputs.size(); i < n; i++) { AnimNode* node = m_eval_ordered_nodes[i]; if (node != nullptr) { @@ -425,15 +442,15 @@ void AnimGraph::UpdateTime(float dt) { } int node_index = getAnimNodeIndex(node); - const std::vector node_inputs = m_node_inputs[node_index]; + const std::vector node_anim_inputs = + m_node_anim_inputs[node_index]; float node_time_now = node->m_time_now; float node_time_last = node->m_time_last; - for (size_t i = 0, n = node_inputs.size(); i < n; i++) { - AnimNode* input_node = node_inputs[i].m_node; + for (size_t i = 0, n = node_anim_inputs.size(); i < n; i++) { + AnimNode* input_node = node_anim_inputs[i].m_node; if (input_node != nullptr - && input_node->m_state == AnimNodeEvalState::Activated - && node_inputs[i].m_type == SocketType::SocketTypeAnimation) { + && input_node->m_state == AnimNodeEvalState::Activated) { input_node->UpdateTime(node_time_last, node_time_now); } } diff --git a/src/AnimGraphResource.h b/src/AnimGraphResource.h index 6cdf6de..9d4e666 100644 --- a/src/AnimGraphResource.h +++ b/src/AnimGraphResource.h @@ -82,9 +82,8 @@ struct AnimNodeResource { }; -struct AnimNodeInput { +struct NodeInput { AnimNode* m_node; - SocketType m_type = SocketType::SocketTypeUndefined; std::string m_input_name; }; @@ -100,7 +99,7 @@ enum class AnimNodeEvalState { struct AnimNode { virtual ~AnimNode(){}; - virtual void MarkActiveInputs(const std::vector& inputs) { + virtual void MarkActiveInputs(const std::vector& inputs) { for (size_t i = 0, n = inputs.size(); i < n; i++) { AnimNode* input_node = inputs[i].m_node; if (input_node != nullptr) { @@ -337,7 +336,7 @@ struct Blend2Node : public AnimNode { bool m_sync_blend = false; - virtual void MarkActiveInputs(const std::vector& inputs) override { + virtual void MarkActiveInputs(const std::vector& inputs) override { for (size_t i = 0, n = inputs.size(); i < n; i++) { AnimNode* input_node = inputs[i].m_node; if (input_node == nullptr) { @@ -356,7 +355,7 @@ struct Blend2Node : public AnimNode { } } - virtual void UpdateTime(float dt, std::vector& inputs) { + virtual void UpdateTime(float dt, std::vector& inputs) { if (!m_sync_blend) { m_time_now = m_time_now + dt; } @@ -612,7 +611,8 @@ struct AnimGraph { std::vector m_nodes; std::vector m_eval_ordered_nodes; - std::vector > m_node_inputs; + std::vector > m_node_anim_inputs; + std::vector > m_node_data_inputs; NodeSocketAccessorBase* m_socket_accessor; char* m_input_buffer = nullptr; char* m_output_buffer = nullptr; @@ -636,9 +636,9 @@ struct AnimGraph { size_t node_index, const std::string& input_name) { assert(node_index < m_nodes.size()); - assert(node_index < m_node_inputs.size()); + assert(node_index < m_node_anim_inputs.size()); - std::vector& node_inputs = m_node_inputs[node_index]; + std::vector& node_inputs = m_node_anim_inputs[node_index]; for (size_t i = 0, n = node_inputs.size(); i < n; i++) { if (node_inputs[i].m_input_name == input_name) { return node_inputs[i].m_node; @@ -681,19 +681,26 @@ struct AnimGraph { result.m_nodes.push_back(node); assert(node_resource.m_socket_accessor != nullptr); - result.m_node_inputs.push_back(std::vector()); - std::vector& node_inputs = result.m_node_inputs.back(); + result.m_node_anim_inputs.push_back(std::vector()); + result.m_node_data_inputs.push_back(std::vector()); + std::vector& node_anim_inputs = result.m_node_anim_inputs.back(); + std::vector& node_data_inputs = result.m_node_data_inputs.back(); + for (int j = 0, n = node_resource.m_socket_accessor->m_inputs.size(); j < n; j++) { const Socket& input_socket = node_resource.m_socket_accessor->m_inputs[j]; - AnimNodeInput input; + + NodeInput input; input.m_node = nullptr; - input.m_type = input_socket.m_type; input.m_input_name = input_socket.m_name; - node_inputs.push_back(input); + if (input_socket.m_type == SocketType::SocketTypeAnimation) { + node_anim_inputs.push_back(input); + } else { + node_data_inputs.push_back(input); + } } } @@ -821,8 +828,8 @@ struct AnimGraph { size_t target_node_index = result.getAnimNodeIndex(target_node); - std::vector& node_inputs = - result.m_node_inputs[target_node_index]; + std::vector& node_inputs = + result.m_node_anim_inputs[target_node_index]; for (int j = 0, n = node_inputs.size(); j < n; j++) { if (node_inputs[j].m_input_name == target_socket->m_name) { node_inputs[j].m_node = source_node; diff --git a/tests/AnimGraphResourceTests.cc b/tests/AnimGraphResourceTests.cc index cdb8df6..e2f32a2 100644 --- a/tests/AnimGraphResourceTests.cc +++ b/tests/AnimGraphResourceTests.cc @@ -94,12 +94,11 @@ TEST_CASE("BasicGraph", "[AnimGraphResource]") { size_t anim_sampler_index1 = graph.getAnimNodeIndex(anim_sampler_instance1); size_t blend_index = graph.getAnimNodeIndex(blend2_instance); - CHECK(graph.m_node_inputs[anim_sampler_index0].size() == 0); - CHECK(graph.m_node_inputs[anim_sampler_index1].size() == 0); - CHECK(graph.m_node_inputs[blend_index].size() == 3); - CHECK(graph.m_node_inputs[blend_index][0].m_node == anim_sampler_instance0); - CHECK(graph.m_node_inputs[blend_index][1].m_node == anim_sampler_instance1); - CHECK(graph.m_node_inputs[blend_index][2].m_node == nullptr); + CHECK(graph.m_node_anim_inputs[anim_sampler_index0].size() == 0); + CHECK(graph.m_node_anim_inputs[anim_sampler_index1].size() == 0); + CHECK(graph.m_node_anim_inputs[blend_index].size() == 2); + CHECK(graph.m_node_anim_inputs[blend_index][0].m_node == anim_sampler_instance0); + CHECK(graph.m_node_anim_inputs[blend_index][1].m_node == anim_sampler_instance1); } TEST_CASE("InputAttributeConversion", "[AnimGraphResource]") {