From 7eb86f2fbc8e75f76473dd6a0869611cf5c0cfb6 Mon Sep 17 00:00:00 2001 From: marynate Date: Tue, 13 May 2014 13:24:26 +0800 Subject: [PATCH] Add 'mesh/skeleton' property to MeshInstance to decouple mesh->skeleton from child parent constraint --- scene/3d/mesh_instance.cpp | 35 +++++++++++++++++++++++++++++++++-- scene/3d/mesh_instance.h | 7 ++++++- scene/3d/visual_instance.cpp | 4 +++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/scene/3d/mesh_instance.cpp b/scene/3d/mesh_instance.cpp index 6387f5fdbc..40981d468e 100644 --- a/scene/3d/mesh_instance.cpp +++ b/scene/3d/mesh_instance.cpp @@ -113,6 +113,27 @@ Ref MeshInstance::get_mesh() const { return mesh; } +void MeshInstance::_resolve_skeleton_path(){ + + if (skeleton_path.is_empty()) + return; + + Skeleton *skeleton=get_node(skeleton_path)?get_node(skeleton_path)->cast_to():NULL; + if (skeleton) + VisualServer::get_singleton()->instance_attach_skeleton( get_instance(), skeleton->get_skeleton() ); +} + +void MeshInstance::set_skeleton_path(const NodePath &p_skeleton) { + + skeleton_path = p_skeleton; + if (!is_inside_scene()) + return; + _resolve_skeleton_path(); +} + +NodePath MeshInstance::get_skeleton_path() { + return skeleton_path; +} AABB MeshInstance::get_aabb() const { @@ -192,22 +213,32 @@ void MeshInstance::create_convex_collision() { } +void MeshInstance::_notification(int p_what) { + + if (p_what==NOTIFICATION_ENTER_SCENE) { + _resolve_skeleton_path(); + } +} + + void MeshInstance::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_mesh","mesh:Mesh"),&MeshInstance::set_mesh); ObjectTypeDB::bind_method(_MD("get_mesh:Mesh"),&MeshInstance::get_mesh); + ObjectTypeDB::bind_method(_MD("set_skeleton_path","skeleton_path:NodePath"),&MeshInstance::set_skeleton_path); + ObjectTypeDB::bind_method(_MD("get_skeleton_path:NodePath"),&MeshInstance::get_skeleton_path); ObjectTypeDB::bind_method(_MD("get_aabb"),&MeshInstance::get_aabb); ObjectTypeDB::bind_method(_MD("create_trimesh_collision"),&MeshInstance::create_trimesh_collision); ObjectTypeDB::set_method_flags("MeshInstance","create_trimesh_collision",METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR); ObjectTypeDB::bind_method(_MD("create_convex_collision"),&MeshInstance::create_convex_collision); ObjectTypeDB::set_method_flags("MeshInstance","create_convex_collision",METHOD_FLAGS_DEFAULT|METHOD_FLAG_EDITOR); ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "mesh/mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh" ), _SCS("set_mesh"), _SCS("get_mesh")); - - + ADD_PROPERTY( PropertyInfo (Variant::NODE_PATH, "mesh/skeleton"), _SCS("set_skeleton_path"), _SCS("get_skeleton_path")); } MeshInstance::MeshInstance() { + skeleton_path=NodePath(".."); } diff --git a/scene/3d/mesh_instance.h b/scene/3d/mesh_instance.h index 0e07109502..1b97564b4e 100644 --- a/scene/3d/mesh_instance.h +++ b/scene/3d/mesh_instance.h @@ -40,6 +40,7 @@ class MeshInstance : public GeometryInstance { OBJ_TYPE( MeshInstance, GeometryInstance ); Ref mesh; + NodePath skeleton_path; struct MorphTrack { @@ -50,6 +51,7 @@ class MeshInstance : public GeometryInstance { Map morph_tracks; + void _resolve_skeleton_path(); protected: @@ -57,12 +59,15 @@ protected: bool _get(const StringName& p_name,Variant &r_ret) const; void _get_property_list( List *p_list) const; - + void _notification(int p_what); static void _bind_methods(); public: void set_mesh(const Ref& p_mesh); Ref get_mesh() const; + + void set_skeleton_path(const NodePath& p_skeleton); + NodePath get_skeleton_path(); Node* create_trimesh_collision_node(); void create_trimesh_collision(); diff --git a/scene/3d/visual_instance.cpp b/scene/3d/visual_instance.cpp index 9419996187..96f16ab8c8 100644 --- a/scene/3d/visual_instance.cpp +++ b/scene/3d/visual_instance.cpp @@ -65,10 +65,12 @@ void VisualInstance::_notification(int p_what) { VisualServer::get_singleton()->instance_set_room(instance,room->get_instance()); } - // CHECK SKELETON + // CHECK SKELETON => moving skeleton attaching logic to MeshInstance + /* Skeleton *skeleton=get_parent()?get_parent()->cast_to():NULL; if (skeleton) VisualServer::get_singleton()->instance_attach_skeleton( instance, skeleton->get_skeleton() ); + */ VisualServer::get_singleton()->instance_set_scenario( instance, get_world()->get_scenario() );