Skip to content

Commit

Permalink
fix: animations inconsistency while fetching child_index 0 instead of…
Browse files Browse the repository at this point in the history
… actual gltf node (#433)

* fix: animations inconsistency while fetching child_index 0 instead of actual gltf node

* fix typo
  • Loading branch information
leanmendoza authored Jul 9, 2024
1 parent 5d7ec6c commit b81a27a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
8 changes: 4 additions & 4 deletions godot/src/decentraland_components/gltf_container.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand All @@ -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():
Expand Down
9 changes: 4 additions & 5 deletions godot/src/ui/components/map_shader/map.gdshader
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions godot/src/ui/components/map_shader/map_shader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
18 changes: 7 additions & 11 deletions lib/src/godot_classes/animator_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -368,7 +367,7 @@ impl MultipleAnimationController {
}

fn create_and_add_multiple_animation_controller(
mut gltf_node: Gd<Node>,
mut gltf_node: Gd<Node3D>,
) -> Option<Gd<MultipleAnimationController>> {
let anim_player = gltf_node.try_get_node_as::<AnimationPlayer>("AnimationPlayer")?;
let anim_list = anim_player.get_animation_list();
Expand All @@ -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
Expand All @@ -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<Node3D>, 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::<MultipleAnimationController>(MULTIPLE_ANIMATION_CONTROLLER_NAME)
if let Some(mut already_exist_node) = gltf_container_node
.try_get_node_as::<MultipleAnimationController>(MULTIPLE_ANIMATION_CONTROLLER_NAME)
{
already_exist_node.bind_mut().apply_anims(value);
return;
Expand All @@ -434,7 +429,8 @@ pub fn apply_anims(gltf_container_node: Gd<Node3D>, 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;
Expand Down
34 changes: 26 additions & 8 deletions lib/src/godot_classes/dcl_gltf_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,42 @@ pub struct DclGltfContainer {
base: Base<Node3D>,
}

fn get_animation_player(godot_entity_node: &Base<Node3D>) -> Option<Gd<AnimationPlayer>> {
godot_entity_node
.get_child(0)?
.try_get_node_as::<AnimationPlayer>("AnimationPlayer")
}

#[godot_api]
impl DclGltfContainer {
// This function
#[func]
pub fn get_gltf_resource(&self) -> Option<Gd<Node3D>> {
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::<Node3D>() {
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::<Node3D>();
let Some(animation_player) =
gltf_container_node.try_get_node_as::<AnimationPlayer>("AnimationPlayer")
else {
return;
};

let entity_id = SceneEntityId::from_i32(self.dcl_entity_id);

Expand Down
6 changes: 4 additions & 2 deletions lib/src/scene_runner/components/animator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b81a27a

Please sign in to comment.