Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skinned colliders not work #311

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions godot/src/decentraland_components/avatar_attach.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends PlayerColliderFilter
@export var user_id: String = "":
set(value):
if user_id != value:
_player_node = null
_player_avatar_node = null
user_id = value

# AAPT_POSITION = 0;
Expand All @@ -12,6 +12,8 @@ extends PlayerColliderFilter
# AAPT_RIGHT_HAND = 3;
var attach_point: int = -1

var _player_avatar_node: Avatar


func init():
self.init_player_collider_filter()
Expand All @@ -26,25 +28,25 @@ func _process(_delta):
if p == null:
return

if _player_node == null:
if _player_avatar_node == null:
look_up_player()
if _player_node == null:
if _player_avatar_node == null:
return

match attach_point:
0:
p.global_transform = _player_node.global_transform
p.global_transform = _player_avatar_node.global_transform
1:
p.global_transform = _player_node.label_3d_name.global_transform
p.global_transform = _player_avatar_node.label_3d_name.global_transform
2:
p.global_transform = (
_player_node.body_shape_skeleton_3d.global_transform
* _player_node.left_hand_position
_player_avatar_node.body_shape_skeleton_3d.global_transform
* _player_avatar_node.left_hand_position
)
3:
p.global_transform = (
_player_node.body_shape_skeleton_3d.global_transform
* _player_node.right_hand_position
_player_avatar_node.body_shape_skeleton_3d.global_transform
* _player_avatar_node.right_hand_position
)
_:
p.transform = Transform3D.IDENTITY
Expand All @@ -56,6 +58,6 @@ func look_up_player():
# default to current player
var look_up_player_user_id := user_id if not user_id.is_empty() else primary_player_user_id
if primary_player_user_id == look_up_player_user_id:
_player_node = get_node("/root/explorer/world/Player/Avatar")
_player_avatar_node = get_node("/root/explorer/world/Player/Avatar")
else:
_player_node = Global.avatars.get_avatar_by_address(user_id)
_player_avatar_node = Global.avatars.get_avatar_by_address(user_id)
6 changes: 3 additions & 3 deletions godot/src/decentraland_components/player_collider_filter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const TIME_TO_RETURN_BACK_LAYER = 0.5

var _computed_colliders: Array[Node]
var _main_scene_tree: SceneTree
var _player_node: Node3D


# This function MUST be called manually
Expand Down Expand Up @@ -49,8 +48,9 @@ func _async_apply_after_physics(check_computed_colliders: Array[Node]):
await _main_scene_tree.create_timer(TIME_TO_RETURN_BACK_LAYER).timeout

for old_collider in check_computed_colliders:
if not _is_filter_still_needed(old_collider):
old_collider.collision_layer = old_collider.get_meta("dcl_col")
if old_collider != null:
if not _is_filter_still_needed(old_collider):
old_collider.collision_layer = old_collider.get_meta("dcl_col")


# Corner case when is moved from one collider filtered to another
Expand Down
19 changes: 13 additions & 6 deletions godot/src/ui/components/loading_screen/loading_screen.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,27 @@ vertical_alignment = 1

[node name="LoadingBar" type="Control" parent="VBox_Loading/VBox_Header"]
layout_mode = 2
size_flags_vertical = 3

[node name="ColorRect_LoadingProgress_BG" type="ColorRect" parent="VBox_Loading/VBox_Header/LoadingBar"]
material = SubResource("ShaderMaterial_t4bn8")
custom_minimum_size = Vector2(0, 6)
layout_mode = 2
offset_top = -6.0
offset_right = 1280.0
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

[node name="ColorRect_LoadingProgress" type="ColorRect" parent="VBox_Loading/VBox_Header/LoadingBar/ColorRect_LoadingProgress_BG"]
unique_name_in_owner = true
custom_minimum_size = Vector2(0, 6)
layout_mode = 2
offset_right = 1280.0
offset_bottom = 6.0
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 1)

[node name="ColorRect_Background" type="ColorRect" parent="VBox_Loading"]
Expand Down
87 changes: 47 additions & 40 deletions rust/decentraland-godot-lib/src/content/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use godot::{
engine::{
animation::TrackType, global::Error, node::ProcessMode, AnimatableBody3D, Animation,
AnimationLibrary, AnimationPlayer, CollisionShape3D, ConcavePolygonShape3D, GltfDocument,
GltfState, MeshInstance3D, Node, Node3D, NodeExt, StaticBody3D,
GltfState, MeshInstance3D, Node, Node3D, NodeExt, Skeleton3D, StaticBody3D,
},
obj::{Gd, InstanceId},
};
Expand Down Expand Up @@ -147,7 +147,7 @@ pub async fn load_gltf_scene_content(
ctx: ContentProviderContext,
) -> Result<Option<Variant>, anyhow::Error> {
let (node, _thread_safe_check) = internal_load_gltf(file_path, content_mapping, ctx).await?;
create_colliders(node.clone().upcast());
create_colliders(node.clone().upcast(), false);
Ok(Some(node.to_variant()))
}

Expand Down Expand Up @@ -281,7 +281,7 @@ fn get_collider(mesh_instance: &Gd<MeshInstance3D>) -> Option<Gd<StaticBody3D>>
None
}

fn create_colliders(node_to_inspect: Gd<Node>) {
fn create_colliders(node_to_inspect: Gd<Node>, inside_skeleton: bool) {
for child in node_to_inspect.get_children().iter_shared() {
if let Ok(mut mesh_instance_3d) = child.clone().try_cast::<MeshInstance3D>() {
let invisible_mesh = mesh_instance_3d
Expand All @@ -294,43 +294,49 @@ fn create_colliders(node_to_inspect: Gd<Node>) {
mesh_instance_3d.set_visible(false);
}

let mut static_body_3d = get_collider(&mesh_instance_3d);
if static_body_3d.is_none() {
mesh_instance_3d.create_trimesh_collision();
static_body_3d = get_collider(&mesh_instance_3d);
}
if !inside_skeleton {
let mut static_body_3d = get_collider(&mesh_instance_3d);
if static_body_3d.is_none() {
mesh_instance_3d.create_trimesh_collision();
static_body_3d = get_collider(&mesh_instance_3d);
}

if let Some(mut static_body_3d) = static_body_3d {
if let Some(mut parent) = static_body_3d.get_parent() {
let mut new_animatable = AnimatableBody3D::new_alloc();
new_animatable.set_sync_to_physics(false);
new_animatable.set_process_mode(ProcessMode::PROCESS_MODE_DISABLED);
new_animatable.set_meta("dcl_col".into(), 0.to_variant());
new_animatable.set_meta("invisible_mesh".into(), invisible_mesh.to_variant());
new_animatable.set_collision_layer(0);
new_animatable.set_collision_mask(0);
new_animatable.set_name(GString::from(format!(
"{}_colgen",
mesh_instance_3d.get_name()
)));

parent.add_child(new_animatable.clone().upcast());
parent.remove_child(static_body_3d.clone().upcast());

for body_child in static_body_3d
.get_children_ex()
.include_internal(true)
.done()
.iter_shared()
{
static_body_3d.remove_child(body_child.clone());
new_animatable.add_child(body_child.clone());
if let Ok(collision_shape_3d) = body_child.try_cast::<CollisionShape3D>() {
if let Some(shape) = collision_shape_3d.get_shape() {
if let Ok(mut concave_polygon_shape_3d) =
shape.try_cast::<ConcavePolygonShape3D>()
{
concave_polygon_shape_3d.set_backface_collision_enabled(true);
if let Some(mut static_body_3d) = static_body_3d {
if let Some(mut parent) = static_body_3d.get_parent() {
let mut new_animatable = AnimatableBody3D::new_alloc();
new_animatable.set_sync_to_physics(false);
new_animatable.set_process_mode(ProcessMode::PROCESS_MODE_DISABLED);
new_animatable.set_meta("dcl_col".into(), 0.to_variant());
new_animatable
.set_meta("invisible_mesh".into(), invisible_mesh.to_variant());
new_animatable.set_collision_layer(0);
new_animatable.set_collision_mask(0);
new_animatable.set_name(GString::from(format!(
"{}_colgen",
mesh_instance_3d.get_name()
)));

parent.add_child(new_animatable.clone().upcast());
parent.remove_child(static_body_3d.clone().upcast());

for body_child in static_body_3d
.get_children_ex()
.include_internal(true)
.done()
.iter_shared()
{
static_body_3d.remove_child(body_child.clone());
new_animatable.add_child(body_child.clone());
if let Ok(collision_shape_3d) =
body_child.try_cast::<CollisionShape3D>()
{
if let Some(shape) = collision_shape_3d.get_shape() {
if let Ok(mut concave_polygon_shape_3d) =
shape.try_cast::<ConcavePolygonShape3D>()
{
concave_polygon_shape_3d
.set_backface_collision_enabled(true);
}
}
}
}
Expand All @@ -339,7 +345,8 @@ fn create_colliders(node_to_inspect: Gd<Node>) {
}
}

create_colliders(child);
let is_skeleton = child.clone().try_cast::<Skeleton3D>().is_ok();
create_colliders(child, inside_skeleton | is_skeleton);
}
}

Expand Down
14 changes: 12 additions & 2 deletions rust/decentraland-godot-lib/src/dcl/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,16 @@ fn op_require(
}

#[op(v8)]
fn op_log(state: Rc<RefCell<OpState>>, message: String, immediate: bool) {
fn op_log(state: Rc<RefCell<OpState>>, mut message: String, immediate: bool) {
if !is_scene_log_enabled() {
return;
}

if message.len() > 8192 {
tracing::warn!("log message too long, truncating");
message = message[..8192].to_string();
}

if immediate {
tracing::info!("{}", message);
}
Expand All @@ -429,11 +434,16 @@ fn op_log(state: Rc<RefCell<OpState>>, message: String, immediate: bool) {
}

#[op(v8)]
fn op_error(state: Rc<RefCell<OpState>>, message: String, immediate: bool) {
fn op_error(state: Rc<RefCell<OpState>>, mut message: String, immediate: bool) {
if !is_scene_log_enabled() {
return;
}

if message.len() > 8192 {
tracing::warn!("log message too long, truncating");
message = message[..8192].to_string();
}

if immediate {
tracing::error!("{}", message);
}
Expand Down
Loading