From b81a27ae524db3b65446425f20f052a9c8e75d49 Mon Sep 17 00:00:00 2001 From: Lean Mendoza Date: Mon, 8 Jul 2024 22:44:33 -0300 Subject: [PATCH] fix: animations inconsistency while fetching child_index 0 instead of actual gltf node (#433) * fix: animations inconsistency while fetching child_index 0 instead of actual gltf node * fix typo --- .../decentraland_components/gltf_container.gd | 8 ++--- .../src/ui/components/map_shader/map.gdshader | 9 +++-- .../ui/components/map_shader/map_shader.gd | 1 + lib/src/godot_classes/animator_controller.rs | 18 ++++------ lib/src/godot_classes/dcl_gltf_container.rs | 34 ++++++++++++++----- lib/src/scene_runner/components/animator.rs | 6 ++-- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/godot/src/decentraland_components/gltf_container.gd b/godot/src/decentraland_components/gltf_container.gd index a2c94b1e..58cb2da4 100644 --- a/godot/src/decentraland_components/gltf_container.gd +++ b/godot/src/decentraland_components/gltf_container.gd @@ -121,6 +121,7 @@ func update_mask_colliders(node_to_inspect: Node): func change_gltf(new_gltf, visible_meshes_collision_mask, invisible_meshes_collision_mask): + var gltf_node = self.get_gltf_resource() if self.dcl_gltf_src != new_gltf: dcl_gltf_loading_state = GltfContainerLoadingState.LOADING timer.start() @@ -129,8 +130,7 @@ func change_gltf(new_gltf, visible_meshes_collision_mask, invisible_meshes_colli dcl_visible_cmask = visible_meshes_collision_mask dcl_invisible_cmask = invisible_meshes_collision_mask - if get_child_count() > 0: - var gltf_node = get_child(0) + if gltf_node != null: remove_child(gltf_node) gltf_node.queue_free() @@ -143,11 +143,11 @@ func change_gltf(new_gltf, visible_meshes_collision_mask, invisible_meshes_colli visible_meshes_collision_mask != dcl_visible_cmask or invisible_meshes_collision_mask != dcl_invisible_cmask ) - and get_child_count() > 0 + and gltf_node != null ): dcl_visible_cmask = visible_meshes_collision_mask dcl_invisible_cmask = invisible_meshes_collision_mask - update_mask_colliders(get_child(0)) + update_mask_colliders(gltf_node) func _on_timer_timeout(): diff --git a/godot/src/ui/components/map_shader/map.gdshader b/godot/src/ui/components/map_shader/map.gdshader index 6c3f4ce8..86698ba0 100644 --- a/godot/src/ui/components/map_shader/map.gdshader +++ b/godot/src/ui/components/map_shader/map.gdshader @@ -36,7 +36,6 @@ void fragment() { bool topMask = (flagsR & 0x8) > 0; bool leftMask = (flagsR & 0x10) > 0; - bool topLeftMask = (flagsR & 0x20) > 0; vec3 parcel_color; if (flagsG == 32) { @@ -54,17 +53,17 @@ void fragment() { bool borderLeft = false; bool borderTop = false; - if (!topMask && !leftMask) { + if (topMask == false && leftMask == false) { borderLeft = true; borderTop = true; - } else if (topMask && leftMask && topLeftMask) { + } else if (topMask && leftMask) { borderLeft = false; borderTop = false; } else { - if (!topMask) { + if (topMask == false) { borderTop = true; } - if (!leftMask) { + if (leftMask == false) { borderLeft = true; } } diff --git a/godot/src/ui/components/map_shader/map_shader.gd b/godot/src/ui/components/map_shader/map_shader.gd index 49a001ff..96c127eb 100644 --- a/godot/src/ui/components/map_shader/map_shader.gd +++ b/godot/src/ui/components/map_shader/map_shader.gd @@ -114,6 +114,7 @@ func set_used_parcels(used_parcel, emtpy_parcels): if to_delete > 0: for i in range(to_delete): + # TODO: warn: check if get_child(0) is properly set var child = color_rect_map.get_child(0) color_rect_map.remove_child(child) child.queue_free() diff --git a/lib/src/godot_classes/animator_controller.rs b/lib/src/godot_classes/animator_controller.rs index a25eea0a..88d70cda 100644 --- a/lib/src/godot_classes/animator_controller.rs +++ b/lib/src/godot_classes/animator_controller.rs @@ -5,8 +5,7 @@ use godot::{ builtin::{meta::ToGodot, StringName}, engine::{ AnimationNodeAdd2, AnimationNodeAnimation, AnimationNodeBlend2, AnimationNodeBlendTree, - AnimationNodeTimeScale, AnimationPlayer, AnimationTree, IAnimationTree, Node, Node3D, - NodeExt, + AnimationNodeTimeScale, AnimationPlayer, AnimationTree, IAnimationTree, Node3D, NodeExt, }, obj::{Base, Gd}, }; @@ -368,7 +367,7 @@ impl MultipleAnimationController { } fn create_and_add_multiple_animation_controller( - mut gltf_node: Gd, + mut gltf_node: Gd, ) -> Option> { let anim_player = gltf_node.try_get_node_as::("AnimationPlayer")?; let anim_list = anim_player.get_animation_list(); @@ -390,7 +389,6 @@ fn create_and_add_multiple_animation_controller( .collect(), ); anim_builder.set_name(MULTIPLE_ANIMATION_CONTROLLER_NAME.into()); - anim_builder.set_animation_player("../AnimationPlayer".into()); if !anim_player.has_animation(DUMMY_ANIMATION_NAME.into()) { anim_player @@ -400,17 +398,14 @@ fn create_and_add_multiple_animation_controller( } gltf_node.add_child(anim_builder.clone().upcast()); + anim_builder.set_animation_player("../AnimationPlayer".into()); Some(anim_builder) } pub fn apply_anims(gltf_container_node: Gd, value: &PbAnimator) { - let Some(gltf_node) = gltf_container_node.get_child(0) else { - return; - }; - - if let Some(mut already_exist_node) = - gltf_node.try_get_node_as::(MULTIPLE_ANIMATION_CONTROLLER_NAME) + if let Some(mut already_exist_node) = gltf_container_node + .try_get_node_as::(MULTIPLE_ANIMATION_CONTROLLER_NAME) { already_exist_node.bind_mut().apply_anims(value); return; @@ -434,7 +429,8 @@ pub fn apply_anims(gltf_container_node: Gd, value: &PbAnimator) { // For handling multiple animations, we need to create a new MultipleAnimationController if need_multiple_animation { - let Some(mut new_blend_builder) = create_and_add_multiple_animation_controller(gltf_node) + let Some(mut new_blend_builder) = + create_and_add_multiple_animation_controller(gltf_container_node) else { // No animations available return; diff --git a/lib/src/godot_classes/dcl_gltf_container.rs b/lib/src/godot_classes/dcl_gltf_container.rs index 23798dd4..ee61969c 100644 --- a/lib/src/godot_classes/dcl_gltf_container.rs +++ b/lib/src/godot_classes/dcl_gltf_container.rs @@ -94,24 +94,42 @@ pub struct DclGltfContainer { base: Base, } -fn get_animation_player(godot_entity_node: &Base) -> Option> { - godot_entity_node - .get_child(0)? - .try_get_node_as::("AnimationPlayer") -} - #[godot_api] impl DclGltfContainer { + // This function + #[func] + pub fn get_gltf_resource(&self) -> Option> { + let child_count = self.base.get_child_count(); + if child_count == 0 { + return None; + } + + for i in 0..child_count { + if let Some(child) = self.base.get_child(i) { + if let Ok(node) = child.try_cast::() { + return Some(node); + } + } + } + + None + } + #[func] fn check_animations(&mut self) { if self.dcl_gltf_loading_state != GltfContainerLoadingState::Finished { return; } - let Some(animation_player) = get_animation_player(&self.base) else { + + let Some(gltf_container_node) = self.get_gltf_resource() else { return; }; - let gltf_container_node = self.base.clone().cast::(); + let Some(animation_player) = + gltf_container_node.try_get_node_as::("AnimationPlayer") + else { + return; + }; let entity_id = SceneEntityId::from_i32(self.dcl_entity_id); diff --git a/lib/src/scene_runner/components/animator.rs b/lib/src/scene_runner/components/animator.rs index e443ef75..ffcfb79a 100644 --- a/lib/src/scene_runner/components/animator.rs +++ b/lib/src/scene_runner/components/animator.rs @@ -34,7 +34,9 @@ pub fn update_animator(scene: &mut Scene, crdt_state: &mut SceneCrdtState) { let entry = new_value.unwrap(); let (godot_entity_node, _node_3d) = godot_dcl_scene.ensure_node_3d(entity); - let Some(gltf_container_node) = get_gltf_container(godot_entity_node) else { + let Some(gltf_container_node) = + get_gltf_container(godot_entity_node).and_then(|v| v.bind().get_gltf_resource()) + else { let value = entry.value.clone(); if let Some(value) = value { scene.dup_animator.insert(*entity, value); @@ -45,7 +47,7 @@ pub fn update_animator(scene: &mut Scene, crdt_state: &mut SceneCrdtState) { }; let value = entry.value.clone().unwrap_or_default(); - apply_anims(gltf_container_node.upcast(), &value); + apply_anims(gltf_container_node, &value); if entry.value.is_none() { scene.dup_animator.remove(entity);