From c809e0d5880bec33a9d6cb1e1ae7e2b61a195b8d Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 15 Dec 2023 08:47:36 +0100 Subject: [PATCH] Removed the Skeleton3D module. --- modules/skeleton_3d/.gitignore | 7 - modules/skeleton_3d/SCsub | 31 - modules/skeleton_3d/config.py | 35 - .../doc_classes/BoneAttachment.xml | 83 - .../skeleton_3d/doc_classes/PhysicalBone.xml | 72 - modules/skeleton_3d/doc_classes/Skeleton.xml | 451 ----- .../skeleton_3d/doc_classes/SkeletonIK.xml | 92 - .../doc_classes/SkeletonModification3D.xml | 79 - .../SkeletonModification3DCCDIK.xml | 138 -- .../SkeletonModification3DFABRIK.xml | 163 -- .../SkeletonModification3DJiggle.xml | 201 -- .../SkeletonModification3DLookAt.xml | 66 - .../SkeletonModification3DStackHolder.xml | 29 - .../SkeletonModification3DTwoBoneIK.xml | 193 -- .../SkeletonModificationStack3D.xml | 90 - modules/skeleton_3d/doc_classes/Skin.xml | 75 - .../skeleton_3d/doc_classes/SkinReference.xml | 23 - .../editor/physical_bone_editor_plugin.cpp | 139 -- .../editor/physical_bone_editor_plugin.h | 21 - .../editor/physical_bone_plugin.cpp | 119 -- .../skeleton_3d/editor/physical_bone_plugin.h | 84 - .../editor/skeleton_editor_plugin.cpp | 1677 ----------------- .../editor/skeleton_editor_plugin.h | 315 ---- .../editor/skeleton_ik_editor_plugin.cpp | 102 - .../editor/skeleton_ik_editor_plugin.h | 66 - .../icons/icon_editor_bone_handle.svg | 1 - .../skeleton_3d/icons/icon_physical_bone.svg | 1 - modules/skeleton_3d/icons/icon_skeleton.svg | 1 - .../skeleton_3d/icons/icon_skeleton_i_k.svg | 1 - .../skeleton_3d/icons/icon_tool_bone_move.svg | 1 - .../skeleton_3d/icons/icon_tool_bone_rest.svg | 1 - .../icons/icon_tool_bone_rotate.svg | 1 - .../icons/icon_tool_bone_scale.svg | 1 - .../icons/icon_tool_bone_select.svg | 1 - modules/skeleton_3d/nodes/bone_attachment.cpp | 416 ---- modules/skeleton_3d/nodes/bone_attachment.h | 100 - modules/skeleton_3d/nodes/physical_bone.cpp | 1242 ------------ modules/skeleton_3d/nodes/physical_bone.h | 321 ---- modules/skeleton_3d/nodes/skeleton.cpp | 1475 --------------- modules/skeleton_3d/nodes/skeleton.h | 314 --- modules/skeleton_3d/nodes/skeleton_ik.cpp | 595 ------ modules/skeleton_3d/nodes/skeleton_ik.h | 215 --- modules/skeleton_3d/register_types.cpp | 59 - modules/skeleton_3d/register_types.h | 10 - .../resources/skeleton_modification_3d.cpp | 169 -- .../resources/skeleton_modification_3d.h | 79 - .../skeleton_modification_3d_ccdik.cpp | 479 ----- .../skeleton_modification_3d_ccdik.h | 125 -- .../skeleton_modification_3d_fabrik.cpp | 638 ------- .../skeleton_modification_3d_fabrik.h | 135 -- .../skeleton_modification_3d_jiggle.cpp | 582 ------ .../skeleton_modification_3d_jiggle.h | 148 -- .../skeleton_modification_3d_lookat.cpp | 269 --- .../skeleton_modification_3d_lookat.h | 88 - .../skeleton_modification_3d_stackholder.cpp | 103 - .../skeleton_modification_3d_stackholder.h | 58 - .../skeleton_modification_3d_twoboneik.cpp | 639 ------- .../skeleton_modification_3d_twoboneik.h | 120 -- .../skeleton_modification_stack_3d.cpp | 230 --- .../skeleton_modification_stack_3d.h | 92 - modules/skeleton_3d/resources/skin.cpp | 157 -- modules/skeleton_3d/resources/skin.h | 93 - 62 files changed, 13311 deletions(-) delete mode 100644 modules/skeleton_3d/.gitignore delete mode 100644 modules/skeleton_3d/SCsub delete mode 100644 modules/skeleton_3d/config.py delete mode 100644 modules/skeleton_3d/doc_classes/BoneAttachment.xml delete mode 100644 modules/skeleton_3d/doc_classes/PhysicalBone.xml delete mode 100644 modules/skeleton_3d/doc_classes/Skeleton.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonIK.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3D.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DCCDIK.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DFABRIK.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DJiggle.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DLookAt.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DStackHolder.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModification3DTwoBoneIK.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkeletonModificationStack3D.xml delete mode 100644 modules/skeleton_3d/doc_classes/Skin.xml delete mode 100644 modules/skeleton_3d/doc_classes/SkinReference.xml delete mode 100644 modules/skeleton_3d/editor/physical_bone_editor_plugin.cpp delete mode 100644 modules/skeleton_3d/editor/physical_bone_editor_plugin.h delete mode 100644 modules/skeleton_3d/editor/physical_bone_plugin.cpp delete mode 100644 modules/skeleton_3d/editor/physical_bone_plugin.h delete mode 100644 modules/skeleton_3d/editor/skeleton_editor_plugin.cpp delete mode 100644 modules/skeleton_3d/editor/skeleton_editor_plugin.h delete mode 100644 modules/skeleton_3d/editor/skeleton_ik_editor_plugin.cpp delete mode 100644 modules/skeleton_3d/editor/skeleton_ik_editor_plugin.h delete mode 100644 modules/skeleton_3d/icons/icon_editor_bone_handle.svg delete mode 100644 modules/skeleton_3d/icons/icon_physical_bone.svg delete mode 100644 modules/skeleton_3d/icons/icon_skeleton.svg delete mode 100644 modules/skeleton_3d/icons/icon_skeleton_i_k.svg delete mode 100644 modules/skeleton_3d/icons/icon_tool_bone_move.svg delete mode 100644 modules/skeleton_3d/icons/icon_tool_bone_rest.svg delete mode 100644 modules/skeleton_3d/icons/icon_tool_bone_rotate.svg delete mode 100644 modules/skeleton_3d/icons/icon_tool_bone_scale.svg delete mode 100644 modules/skeleton_3d/icons/icon_tool_bone_select.svg delete mode 100644 modules/skeleton_3d/nodes/bone_attachment.cpp delete mode 100644 modules/skeleton_3d/nodes/bone_attachment.h delete mode 100644 modules/skeleton_3d/nodes/physical_bone.cpp delete mode 100644 modules/skeleton_3d/nodes/physical_bone.h delete mode 100644 modules/skeleton_3d/nodes/skeleton.cpp delete mode 100644 modules/skeleton_3d/nodes/skeleton.h delete mode 100644 modules/skeleton_3d/nodes/skeleton_ik.cpp delete mode 100644 modules/skeleton_3d/nodes/skeleton_ik.h delete mode 100644 modules/skeleton_3d/register_types.cpp delete mode 100644 modules/skeleton_3d/register_types.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_lookat.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_lookat.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.h delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_stack_3d.cpp delete mode 100644 modules/skeleton_3d/resources/skeleton_modification_stack_3d.h delete mode 100644 modules/skeleton_3d/resources/skin.cpp delete mode 100644 modules/skeleton_3d/resources/skin.h diff --git a/modules/skeleton_3d/.gitignore b/modules/skeleton_3d/.gitignore deleted file mode 100644 index e795fbe..0000000 --- a/modules/skeleton_3d/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.import -*.d -*.o -*.meta -*.obj -*.pyc -*.bc diff --git a/modules/skeleton_3d/SCsub b/modules/skeleton_3d/SCsub deleted file mode 100644 index 3939eda..0000000 --- a/modules/skeleton_3d/SCsub +++ /dev/null @@ -1,31 +0,0 @@ -import os - -Import('env') - -module_env = env.Clone() - -module_env.add_source_files(env.modules_sources,"register_types.cpp") - -module_env.add_source_files(env.modules_sources,"nodes/skeleton.cpp") -module_env.add_source_files(env.modules_sources,"resources/skin.cpp") - -module_env.add_source_files(env.modules_sources,"nodes/physical_bone.cpp") - -module_env.add_source_files(env.modules_sources,"nodes/bone_attachment.cpp") - -module_env.add_source_files(env.modules_sources,"nodes/skeleton_ik.cpp") - -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_ccdik.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_fabrik.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_jiggle.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_lookat.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_stackholder.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_3d_twoboneik.cpp") -module_env.add_source_files(env.modules_sources,"resources/skeleton_modification_stack_3d.cpp") - -if env["tools"]: - env.add_source_files(env.modules_sources, "editor/physical_bone_editor_plugin.cpp") - env.add_source_files(env.modules_sources, "editor/skeleton_editor_plugin.cpp") - env.add_source_files(env.modules_sources, "editor/skeleton_ik_editor_plugin.cpp") - env.add_source_files(env.modules_sources, "editor/physical_bone_plugin.cpp") diff --git a/modules/skeleton_3d/config.py b/modules/skeleton_3d/config.py deleted file mode 100644 index 3bbfab8..0000000 --- a/modules/skeleton_3d/config.py +++ /dev/null @@ -1,35 +0,0 @@ - - -def can_build(env, platform): - return True - - -def configure(env): - pass - - -def get_doc_classes(): - return [ - "Skeleton", - "Skin", - "SkinReference", - - "BoneAttachment", - - "PhysicalBone", - - "SkeletonIK", - - "SkeletonModificationStack3D", - - "SkeletonModification3D", - "SkeletonModification3DCCDIK", - "SkeletonModification3DFABRIK", - "SkeletonModification3DJiggle", - "SkeletonModification3DLookAt", - "SkeletonModification3DStackHolder", - "SkeletonModification3DTwoBoneIK", - ] - -def get_doc_path(): - return "doc_classes" diff --git a/modules/skeleton_3d/doc_classes/BoneAttachment.xml b/modules/skeleton_3d/doc_classes/BoneAttachment.xml deleted file mode 100644 index a33b556..0000000 --- a/modules/skeleton_3d/doc_classes/BoneAttachment.xml +++ /dev/null @@ -1,83 +0,0 @@ - - - - A node that will attach to a bone. - - - This node will allow you to select a bone for this node to attach to. The BoneAttachment3D node can copy the transform of the select bone, or can override the transform of the selected bone. - The BoneAttachment3D node must either be a child of a [Skeleton] node or be given an external [Skeleton] to use in order to function properly. - - - - - - - - Returns the [NodePath] to the external [Skeleton3D] node, if one has been set. - - - - - - Returns the override mode for the BoneAttachment3D node. - - - - - - Returns whether the BoneAttachment3D node is overriding the bone pose of the bone it's attached to. - - - - - - Returns whether the BoneAttachment3D node is using an external [Skeleton3D] rather than attempting to use its parent node as the [Skeleton3D]. - - - - - - - A function that is called automatically when the [Skeleton3D] the BoneAttachment3D node is using has a bone that has changed its pose. This function is where the BoneAttachment3D node updates its position so it is correctly bound when it is [i]not[/i] set to override the bone pose. - - - - - - - Sets the [NodePath] to the external skeleton that the BoneAttachment3D node should use. The external [Skeleton3D] node is only used when [code]use_external_skeleton[/code] is set to [code]true[/code]. - - - - - - - Sets the override mode for the BoneAttachment3D node. The override mode defines which of the bone poses the BoneAttachment3D node will override. - - - - - - - Sets whether the BoneAttachment3D node will override the bone pose of the bone it is attached to. When set to [code]true[/code], the BoneAttachment3D node can change the pose of the bone. - - - - - - - Sets whether the BoneAttachment3D node will use an extenral [Skeleton3D] node rather than attenpting to use its parent node as the [Skeleton3D]. When set to [code]true[/code], the BoneAttachment3D node will use the external [Skeleton3D] node set in [code]set_external_skeleton[/code]. - - - - - - The index of the attached bone. - - - The name of the attached bone. - - - - - diff --git a/modules/skeleton_3d/doc_classes/PhysicalBone.xml b/modules/skeleton_3d/doc_classes/PhysicalBone.xml deleted file mode 100644 index 54de002..0000000 --- a/modules/skeleton_3d/doc_classes/PhysicalBone.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - - - - [b]Warning:[/b] With a non-uniform scale this node will probably not function as expected. Please make sure to keep its scale uniform (i.e. the same on all axes), and change the size(s) of its collision shape(s) instead. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/modules/skeleton_3d/doc_classes/Skeleton.xml b/modules/skeleton_3d/doc_classes/Skeleton.xml deleted file mode 100644 index ce3f1bc..0000000 --- a/modules/skeleton_3d/doc_classes/Skeleton.xml +++ /dev/null @@ -1,451 +0,0 @@ - - - - Skeleton for characters and animated objects. - - - Skeleton provides a hierarchical interface for managing bones, including pose, rest and animation (see [Animation]). It can also use ragdoll physics. - The overall transform of a bone with respect to the skeleton is determined by the following hierarchical order: rest pose, custom pose and pose. - Note that "global pose" below refers to the overall transform of the bone with respect to skeleton, so it not the actual global/world transform of the bone. - - - https://godotengine.org/asset-library/asset/523 - https://godotengine.org/asset-library/asset/678 - - - - - - - Adds a bone, with name [code]name[/code]. [method get_bone_count] will become the bone index. - - - - - - - - - - - - - Clear all the bones in this skeleton. - - - - - - Removes the global pose override on all bones in the skeleton. - - - - - - Removes the local pose override on all bones in the skeleton. - - - - - - - - - - - - - Executes all the modifications on the [SkeletonModificationStack3D], if the Skeleton3D has one assigned. - - - - - - - Returns the bone index that matches [code]name[/code] as its name. - - - - - - Force updates the bone transforms/poses for all bones in the skeleton. - - - - - - - - - - - - Force updates the bone transform for the bone at [code]bone_idx[/code] and all of its children. - - - - - - - Returns an array containing the bone indexes of all the children node of the passed in bone, [code]bone_idx[/code]. - - - - - - Returns the amount of bones in the skeleton. - - - - - - - Returns the overall transform of the specified bone, with respect to the skeleton. Being relative to the skeleton frame, this is not the actual "global" transform of the bone. - - - - - - - Returns the overall transform of the specified bone, with respect to the skeleton, but without any global pose overrides. Being relative to the skeleton frame, this is not the actual "global" transform of the bone. - - - - - - - Returns the global pose override transform for [code]bone_idx[/code]. - - - - - - - Returns the global rest transform for [code]bone_idx[/code]. - - - - - - - Returns the local pose override transform for [code]bone_idx[/code]. - - - - - - - Returns the name of the bone at index [code]index[/code]. - - - - - - - Returns the bone index which is the parent of the bone at [code]bone_idx[/code]. If -1, then bone has no parent. - [b]Note:[/b] The parent bone returned will always be less than [code]bone_idx[/code]. - - - - - - - Returns the pose transform of the specified bone. Pose is applied on top of the custom pose, which is applied on top the rest pose. - - - - - - - - - - - - - - - - - - - - - - - - - Returns the rest transform for a bone [code]bone_idx[/code]. - - - - - - Returns the modification stack attached to this skeleton, if one exists. - - - - - - Returns an array with all of the bones that are parentless. Another way to look at this is that it returns the indexes of all the bones that are not dependent or modified by other bones in the Skeleton. - - - - - - - - Takes the passed-in global pose and converts it to local pose transform. - This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform in [method set_bone_local_pose_override]. - - - - - - - Takes the passed-in global pose and converts it to a world transform. - This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform usable with a node's transform, like [member Spatial.global_transform] for example. - - - - - - - - Rotates the given [Basis] so that the forward axis of the Basis is facing in the forward direction of the bone at [code]bone_idx[/code]. - This is helper function to make using [method Transform.looking_at] easier with bone poses. - - - - - - - - - - - - Returns whether the bone pose for the bone at [code]bone_idx[/code] is enabled. - - - - - - - - Converts the passed-in local pose to a global pose relative to the inputted bone, [code]bone_idx[/code]. - This could be used to convert [method get_bone_pose] for use with the [method set_bone_global_pose_override] function. - - - - - - Returns all bones in the skeleton to their rest poses. - - - - - - - Adds a collision exception to the physical bone. - Works just like the [RigidBody3D] node. - - - - - - - Removes a collision exception to the physical bone. - Works just like the [RigidBody3D] node. - - - - - - - Tells the [PhysicalBone3D] nodes in the Skeleton to start simulating and reacting to the physics world. - Optionally, a list of bone names can be passed-in, allowing only the passed-in bones to be simulated. - - - - - - Tells the [PhysicalBone3D] nodes in the Skeleton to stop simulating. - - - - - - - Binds the given Skin to the Skeleton. - - - - - - - - - - - - - - Removes the passed in child bone index, [code]child_bone_idx[/code], from the passed-in bone, [code]bone_idx[/code], if it exists. - [b]Note:[/b] This does not remove the child bone, but instead it removes the connection it has to the parent bone. - - - - - - - - - - - - - - - Disables the pose for the bone at [code]bone_idx[/code] if [code]false[/code], enables the bone pose if [code]true[/code]. - - - - - - - - - - Sets the global pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code]. - [code]amount[/code] is the interpolation strength that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain. - [b]Note:[/b] The pose transform needs to be a global pose! Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Spatial], to a global pose. - - - - - - - - - - Sets the local pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code]. - [code]amount[/code] is the interpolation strength that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain. - [b]Note:[/b] The pose transform needs to be a local pose! Use [method global_pose_to_local_pose] to convert a global pose to a local pose. - - - - - - - - - - - - - - - Sets the bone index [code]parent_idx[/code] as the parent of the bone at [code]bone_idx[/code]. If -1, then bone has no parent. - [b]Note:[/b] [code]parent_idx[/code] must be less than [code]bone_idx[/code]. - - - - - - - - Sets the pose transform for bone [code]bone_idx[/code]. Prefer set_bone_pose_position, set_bone_pose_rotation, or set_bone_pose_scale instead. - [b]Note:[/b] The pose transform needs to be in bone space. Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Spatial], to bone space. - - - - - - - - - - - - - - - - - - - - - - Sets the pose transform for bone [code]bone_idx[/code]. - [b]Note:[/b] The pose transform needs to be in bone space. Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Spatial], to bone space. - - - - - - - - Sets the rest transform for bone [code]bone_idx[/code]. - - - - - - - Sets the modification stack for this skeleton to the passed-in modification stack, [code]modification_stack[/code]. - - - - - - - Unparents the bone at [code]bone_idx[/code] and sets its rest position to that of it's parent prior to being reset. - - - - - - - Takes the passed-in global transform and converts it to a global pose. - This can be used to easily convert a global transform from [member Spatial.global_transform] to a global pose usable with [method set_bone_global_pose_override], for example. - - - - - - - - - - - - - - - - - - - This signal is emitted when one of the bones in the Skeleton3D node have changed their pose. This is used to inform nodes that rely on bone positions that one of the bones in the Skeleton3D have changed their transform/pose. - - - - - - - - - - - - - - - - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonIK.xml b/modules/skeleton_3d/doc_classes/SkeletonIK.xml deleted file mode 100644 index 252b0dd..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonIK.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - - SkeletonIK is used to place the end bone of a [Skeleton] bone chain at a certain point in 3D by rotating all bones in the chain accordingly. - - - SkeletonIK is used to place the end bone of a [Skeleton] bone chain at a certain point in 3D by rotating all bones in the chain accordingly. A typical scenario for IK in games is to place a characters feet on the ground or a characters hands on a currently hold object. SkeletonIK uses FabrikInverseKinematic internally to solve the bone chain and applies the results to the [Skeleton] [code]bones_global_pose_override[/code] property for all affected bones in the chain. If fully applied this overwrites any bone transform from [Animation]s or bone custom poses set by users. The applied amount can be controlled with the [code]interpolation[/code] property. - [codeblock] - # Apply IK effect automatically on every new frame (not the current) - skeleton_ik_node.start() - - # Apply IK effect only on the current frame - skeleton_ik_node.start(true) - - # Stop IK effect and reset bones_global_pose_override on Skeleton - skeleton_ik_node.stop() - - # Apply full IK effect - skeleton_ik_node.set_interpolation(1.0) - - # Apply half IK effect - skeleton_ik_node.set_interpolation(0.5) - - # Apply zero IK effect (a value at or below 0.01 also removes bones_global_pose_override on Skeleton) - skeleton_ik_node.set_interpolation(0.0) - [/codeblock] - - - https://godotengine.org/asset-library/asset/523 - - - - - - Returns the parent [Skeleton] Node that was present when SkeletonIK entered the [SceneTree]. Returns null if the parent node was not a [Skeleton] Node when SkeletonIK entered the [SceneTree]. - - - - - - Returns [code]true[/code] if SkeletonIK is applying IK effects on continues frames to the [Skeleton] bones. Returns [code]false[/code] if SkeletonIK is stopped or [method start] was used with the [code]one_time[/code] parameter set to [code]true[/code]. - - - - - - - Starts applying IK effects on each frame to the [Skeleton] bones but will only take effect starting on the next frame. If [code]one_time[/code] is [code]true[/code], this will take effect immediately but also reset on the next frame. - - - - - - Stops applying IK effects on each frame to the [Skeleton] bones and also calls [method Skeleton.clear_bones_global_pose_override] to remove existing overrides on all bones. - - - - - - Interpolation value for how much the IK results are applied to the current skeleton bone chain. A value of [code]1.0[/code] will overwrite all skeleton bone transforms completely while a value of [code]0.0[/code] will visually disable the SkeletonIK. A value at or below [code]0.01[/code] also calls [method Skeleton.clear_bones_global_pose_override]. - - - Secondary target position (first is [member target] property or [member target_node]) for the IK chain. Use magnet position (pole target) to control the bending of the IK chain. Only works if the bone chain has more than 2 bones. The middle chain bone position will be linearly interpolated with the magnet position. - - - Number of iteration loops used by the IK solver to produce more accurate (and elegant) bone chain results. - - - The minimum distance between bone and goal target. If the distance is below this value, the IK solver stops further iterations. - - - If [code]true[/code] overwrites the rotation of the tip bone with the rotation of the [member target] (or [member target_node] if defined). - - - The name of the current root bone, the first bone in the IK chain. - - - First target of the IK chain where the tip bone is placed and, if [member override_tip_basis] is [code]true[/code], how the tip bone is rotated. If a [member target_node] path is available the nodes transform is used instead and this property is ignored. - - - Target node [NodePath] for the IK chain. If available, the node's current [Transform] is used instead of the [member target] property. - - - The name of the current tip bone, the last bone in the IK chain placed at the [member target] transform (or [member target_node] if defined). - - - If [code]true[/code], instructs the IK solver to consider the secondary magnet target (pole target) when calculating the bone chain. Use the magnet position (pole target) to control the bending of the IK chain. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3D.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3D.xml deleted file mode 100644 index a33c744..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3D.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - A resource that operates on bones in a [Skeleton3D]. - - - This resource provides an interface that can be expanded so code that operates on bones in a [Skeleton3D] can be mixed and matched together to create complex interactions. - This is used to provide Godot with a flexible and powerful Inverse Kinematics solution that can be adapted for many different uses. - - - - - - - - - - - - - - - Sets up the modification so it can be executed. This function should be called automatically by the [SkeletonModificationStack3D] containing this modification. - If you need to initialize a modification before use, this is the place to do it! - - - - - - - - - - Takes a angle and clamps it so it is within the passed-in [code]min[/code] and [code]max[/code] range. [code]invert[/code] will inversely clamp the angle, clamping it to the range outside of the given bounds. - - - - - - - - - - - - Returns whether this modification has been successfully setup or not. - - - - - - Returns the [SkeletonModificationStack3D] that this modification is bound to. Through the modification stack, you can access the Skeleton3D the modification is operating on. - - - - - - - Manually allows you to set the setup state of the modification. This function should only rarely be used, as the [SkeletonModificationStack3D] the modification is bound to should handle setting the modification up. - - - - - - - - - - - - When true, the modification's [method _execute] function will be called by the [SkeletonModificationStack3D]. - - - The execution mode for the modification. This tells the modification stack when to execute the modification. Some modifications have settings that are only available in certain execution modes. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DCCDIK.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DCCDIK.xml deleted file mode 100644 index 975c72a..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DCCDIK.xml +++ /dev/null @@ -1,138 +0,0 @@ - - - - A modification that uses CCDIK to manipulate a series of bones to reach a target. - - - This [SkeletonModification3D] uses an algorithm called Cyclic Coordinate Descent Inverse Kinematics, or CCDIK, to manipulate a chain of bones in a Skeleton so it reaches a defined target. - CCDIK works by rotating a set of bones, typically called a "bone chain", on a single axis. Each bone is rotated to face the target from the tip (by default), which over a chain of bones allow it to rotate properly to reach the target. Because the bones only rotate on a single axis, CCDIK [i]can[/i] look more robotic than other IK solvers. - [b]Note:[/b] The CCDIK modifier has [code]ccdik_joints[/code], which are the data objects that hold the data for each joint in the CCDIK chain. This is different from a bone! CCDIK joints hold the data needed for each bone in the bone chain used by CCDIK. - CCDIK also fully supports angle constraints, allowing for more control over how a solution is met. - - - - - - - - - Returns the bone index of the bone assigned to the CCDIK joint at [code]joint_idx[/code]. - - - - - - - Returns the name of the bone that is assigned to the CCDIK joint at [code]joint_idx[/code]. - - - - - - - Returns the integer representing the joint axis of the CCDIK joint at [code]joint_idx[/code]. - - - - - - - Returns the maximum angle constraint for the joint at [code]joint_idx[/code]. [b]Note:[/b] This angle is in degrees! - - - - - - - Returns the minimum angle constraint for the joint at [code]joint_idx[/code]. [b]Note:[/b] This angle is in degrees! - - - - - - - Returns whether the CCDIK joint at [code]joint_idx[/code] uses an inverted joint constraint. See [method set_ccdik_joint_constraint_invert] for details. - - - - - - - Enables angle constraints to the CCDIK joint at [code]joint_idx[/code]. - - - - - - - - Sets the bone index, [code]bone_index[/code], of the CCDIK joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_name[/code] of the CCDIK joint based on data provided by the linked skeleton. - - - - - - - - Sets the bone name, [code]bone_name[/code], of the CCDIK joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_index[/code] of the CCDIK joint based on data provided by the linked skeleton. - - - - - - - - Sets the joint axis of the CCDIK joint at [code]joint_idx[/code] to the passed-in joint axis, [code]axis[/code]. - - - - - - - - Sets the maximum angle constraint for the joint at [code]joint_idx[/code]. [b]Note:[/b] This angle must be in radians! - - - - - - - - Sets the minimum angle constraint for the joint at [code]joint_idx[/code]. [b]Note:[/b] This angle must be in radians! - - - - - - - - Sets whether the CCDIK joint at [code]joint_idx[/code] uses an inverted joint constraint. - An inverted joint constraint only constraints the CCDIK joint to the angles [i]outside of[/i] the inputted minimum and maximum angles. For this reason, it is referred to as an inverted joint constraint, as it constraints the joint to the outside of the inputted values. - - - - - - - - Sets whether joint constraints are enabled for the CCDIK joint at [code]joint_idx[/code]. - - - - - - The amount of CCDIK joints in the CCDIK modification. - - - When true, the CCDIK algorithm will perform a higher quality solve that returns more natural results. A high quality solve requires more computation power to solve though, and therefore can be disabled to save performance. - - - The NodePath to the node that is the target for the CCDIK modification. This node is what the CCDIK chain will attempt to rotate the bone chain to. - - - The end position of the CCDIK chain. Typically, this should be a child of a [BoneAttachment3D] node attached to the final bone in the CCDIK chain, where the child node is offset so it is at the end of the final bone. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DFABRIK.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DFABRIK.xml deleted file mode 100644 index 89c1112..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DFABRIK.xml +++ /dev/null @@ -1,163 +0,0 @@ - - - - A modification that uses FABRIK to manipulate a series of bones to reach a target. - - - This [SkeletonModification3D] uses an algorithm called Forward And Backward Reaching Inverse Kinematics, or FABRIK, to rotate a bone chain so that it reaches a target. - FABRIK works by knowing the positions and lengths of a series of bones, typically called a "bone chain". It first starts by running a forward pass, which places the final bone at the target's position. Then all other bones are moved towards the tip bone, so they stay at the defined bone length away. Then a backwards pass is performed, where the root/first bone in the FABRIK chain is placed back at the origin. then all other bones are moved so they stay at the defined bone length away. This positions the bone chain so that it reaches the target when possible, but all of the bones stay the correct length away from each other. - Because of how FABRIK works, it often gives more natural results than those seen in [SkeletonModification3DCCDIK], though FABRIK currently does not support joint constraints. - [b]Note:[/b] The FABRIK modifier has [code]fabrik_joints[/code], which are the data objects that hold the data for each joint in the FABRIK chain. This is different from a bone! FABRIK joints hold the data needed for each bone in the bone chain used by FABRIK. - To help control how the FABRIK joints move, a magnet vector can be passed, which can nudge the bones in a certain direction prior to solving, giving a level of control over the final result. - - - - - - - - - Will attempt to automatically calculate the length of the bone assigned to the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns a boolean that indicates whether this modification will attempt to autocalculate the length of the bone assigned to the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns the bone index of the bone assigned to the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns the name of the bone that is assigned to the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns the length of the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns the magnet vector of the FABRIK joint at [code]joint_idx[/code]. - - - - - - - Returns the [Spatial]-based node placed at the tip of the FABRIK joint at [code]joint_idx[/code], if one has been set. - - - - - - - Returns a boolean indicating whether the FABRIK joint uses the target's [Basis] for its rotation. - [b]Note:[/b] This option is only available for the final bone in the FABRIK chain, with this setting being ignored for all other bones. - - - - - - - Sets the [Spatial]-based node that will be used as the tip of the FABRIK joint at [code]joint_idx[/code]. - - - - - - - - When [code]true[/code], this modification will attempt to automatically calculate the length of the bone for the FABRIK joint at [code]joint_idx[/code]. It does this by either using the tip node assigned, if there is one assigned, or the distance the of the bone's children, if the bone has any. If the bone has no children and no tip node is assigned, then the modification [b]cannot[/b] autocalculate the joint's length. In this case, the joint length should be entered manually or a tip node assigned. - - - - - - - - Sets the bone index, [code]bone_index[/code], of the FABRIK joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_name[/code] of the FABRIK joint based on data provided by the [Skeleton3D]. - - - - - - - - Sets the bone name, [code]bone_name[/code], of the FABRIK joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_index[/code] of the FABRIK joint based on data provided by the [Skeleton3D]. - - - - - - - - Sets the joint length, [code]length[/code], of the FABRIK joint at [code]joint_idx[/code]. - - - - - - - - Sets the magenet position to [code]magnet_position[/code] for the joint at [code]joint_idx[/code]. The magnet position is used to nudge the joint in that direction when solving, which gives some control over how that joint will bend when being solved. - - - - - - - - Sets the nodepath of the FARIK joint at [code]joint_idx[/code] to [code]tip_node[/code]. The tip node is used to calculate the length of the FABRIK joint when set to automatically calculate joint length. - [b]Note:[/b] The tip node should generally be a child node of a [BoneAttachment3D] node attached to the bone that this FABRIK joint operates on, with the child node being offset so it is at the end of the bone. - - - - - - - - Sets whether the FABRIK joint at [code]joint_idx[/code] uses the target's [Basis] for its rotation. - [b]Note:[/b] This option is only available for the final bone in the FABRIK chain, with this setting being ignored for all other bones. - - - - - - - - Sets whether the tip node should be used when autocalculating the joint length for the FABRIK joint at [code]joint_idx[/code]. This will only work if there is a node assigned to the tip nodepath for this joint. - - - - - - The number of times FABRIK will try to solve each time the [code]execute[/code] function is called. Setting this value to a lower number will be result in better performance, but this can also result in harsher movements and slower solves. - - - The minimum distance the target has to be from the tip of the final bone in the bone chain. Setting this value to a higher number allows for greater performance, but less accurate solves. - - - The amount of FABRIK joints in the FABRIK modification. - - - The NodePath to the node that is the target for the FABRIK modification. This node is what the FABRIK chain will attempt to rotate the bone chain to. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DJiggle.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DJiggle.xml deleted file mode 100644 index ab5e0b1..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DJiggle.xml +++ /dev/null @@ -1,201 +0,0 @@ - - - - A modification that jiggles bones as they move towards a target. - - - This modification moves a series of bones, typically called a bone chain, towards a target. What makes this modification special is that it calculates the velocity and acceleration for each bone in the bone chain, and runs a very light physics-like calculation using the inputted values. This allows the bones to overshoot the target and "jiggle" around. It can be configured to act more like a spring, or sway around like cloth might. - This modification is useful for adding additional motion to things like hair, the edges of clothing, and more. It has several settings to that allow control over how the joint moves when the target moves. - [b]Note:[/b] The Jiggle modifier has [code]jiggle_joints[/code], which are the data objects that hold the data for each joint in the Jiggle chain. This is different from a bone! Jiggle joints hold the data needed for each bone in the bone chain used by the Jiggle modification. - - - - - - - - Returns the collision mask that the Jiggle modifier will take into account when performing physics calculations. - - - - - - - Returns the bone index of the bone assigned to the Jiggle joint at [code]joint_idx[/code]. - - - - - - - Returns the name of the bone that is assigned to the Jiggle joint at [code]joint_idx[/code]. - - - - - - - Returns the amount of dampening of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - Returns a [Vector3] representign the amount of gravity the Jiggle joint at [code]joint_idx[/code] is influenced by. - - - - - - - Returns the amount of mass of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - Returns a boolean that indicates whether the joint at [code]joint_idx[/code] is overriding the default jiggle joint data defined in the modification. - - - - - - - Returns the amount of roll/twist applied to the bone that the Jiggle joint is applied to. - - - - - - - Returns the stiffness of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - Returns a boolean that indicates whether the joint at [code]joint_idx[/code] is using gravity or not. - - - - - - Returns whether the Jiggle modifier is taking physics colliders into account when solving. - - - - - - - Sets the collision mask that the Jiggle modifier takes into account when performing physics calculations. - - - - - - - - Sets the bone index, [code]bone_index[/code], of the Jiggle joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_name[/code] of the Jiggle joint based on data provided by the [Skeleton3D]. - - - - - - - - Sets the bone name, [code]bone_name[/code], of the Jiggle joint at [code]joint_idx[/code]. When possible, this will also update the [code]bone_index[/code] of the Jiggle joint based on data provided by the [Skeleton3D]. - - - - - - - - Sets the amount of dampening of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - - Sets the gravity vector of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - - Sets the of mass of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - - Sets whether the Jiggle joint at [code]joint_idx[/code] should override the default Jiggle joint settings. Setting this to true will make the joint use its own settings rather than the default ones attached to the modification. - - - - - - - - Sets the amount of roll/twist on the bone the Jiggle joint is attached to. - - - - - - - - Sets the of stiffness of the Jiggle joint at [code]joint_idx[/code]. - - - - - - - - Sets whether the Jiggle joint at [code]joint_idx[/code] should use gravity. - - - - - - - When [code]true[/code], the Jiggle modifier will use raycasting to prevent the Jiggle joints from rotating themselves into collision objects when solving. - - - - - - The default amount of dampening applied to the Jiggle joints, if they are not overridden. Higher values lead to more of the calculated velocity being applied. - - - The default amount of gravity applied to the Jiggle joints, if they are not overridden. - - - The amount of Jiggle joints in the Jiggle modification. - - - The default amount of mass assigned to the Jiggle joints, if they are not overridden. Higher values lead to faster movements and more overshooting. - - - The default amount of stiffness assigned to the Jiggle joints, if they are not overridden. Higher values act more like springs, quickly moving into the correct position. - - - The NodePath to the node that is the target for the Jiggle modification. This node is what the Jiggle chain will attempt to rotate the bone chain to. - - - Whether the gravity vector, [member gravity], should be applied to the Jiggle joints, assuming they are not overriding the default settings. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DLookAt.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DLookAt.xml deleted file mode 100644 index 6c24bd1..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DLookAt.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - - A modification that rotates a bone to look at a target. - - - This [SkeletonModification3D] rotates a bone to look a target. This is extremely helpful for moving character's heads to look at the player, rotating a turret to look at a target, or any other case where you want to make a bone rotate towards something quickly and easily. - - - - - - - - Returns the amount of extra rotation that is applied to the bone after the LookAt modification executes. - - - - - - Returns the plane that the LookAt modification is limiting rotation to. - - - - - - Returns whether the LookAt modification is limiting rotation to a single plane in 3D space. - - - - - - - Sets the amount of extra rotation to be applied after the LookAt modification executes. This allows you to adjust the finished result. - - - - - - - - - - - - - When [code]true[/code], the LookAt modification will limit its rotation to a single plane in 3D space. The plane used can be configured using the [code]set_lock_rotation_plane[/code] function. - - - - - - The bone index of the bone that should be operated on by this modification. - When possible, this will also update the [member bone_name] based on data provided by the [Skeleton3D]. - - - The name of the bone that should be operated on by this modification. - When possible, this will also update the [member bone_index] based on data provided by the [Skeleton3D]. - - - The NodePath to the node that is the target for the modification. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DStackHolder.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DStackHolder.xml deleted file mode 100644 index 0b3a6ea..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DStackHolder.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - A modification that holds and executes a [SkeletonModificationStack3D]. - - - This [SkeletonModification3D] holds a reference to a [SkeletonModificationStack3D], allowing you to use multiple modification stacks on a single [Skeleton3D]. - [b]Note:[/b] The modifications in the held [SkeletonModificationStack3D] will only be executed if their execution mode matches the execution mode of the SkeletonModification3DStackHolder. - - - - - - - - Returns the [SkeletonModificationStack3D] that this modification is holding. - - - - - - - Sets the [SkeletonModificationStack3D] that this modification is holding. This modification stack will then be executed when this modification is executed. - - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModification3DTwoBoneIK.xml b/modules/skeleton_3d/doc_classes/SkeletonModification3DTwoBoneIK.xml deleted file mode 100644 index 1008cdb..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModification3DTwoBoneIK.xml +++ /dev/null @@ -1,193 +0,0 @@ - - - - A modification that moves two bones to reach the target. - - - This [SkeletonModification3D] uses an algorithm typically called TwoBoneIK. This algorithm works by leveraging the law of cosigns and the lengths of the bones to figure out what rotation the bones currently have, and what rotation they need to make a complete triangle, where the first bone, the second bone, and the target form the three vertices of the triangle. Because the algorithm works by making a triangle, it can only operate on two bones. - TwoBoneIK is great for arms, legs, and really any joints that can be represented by just two bones that bend to reach a target. This solver is more lightweight than [SkeletonModification3DFABRIK], but gives similar, natural looking results. - A [Spatial]-based node can be used to define the pole, or bend direction, allowing control over which direction the joint takes when bending to reach the target when the target is within reach. - - - - - - - - Returns whether the TwoBoneIK modification will attempt to autocalculate the lengths of the two bones. - - - - - - Returns the bone index of the first bone in the TwoBoneIK modification. - - - - - - Returns the name of the first bone in the TwoBoneIK modification. - - - - - - Returns the length of the first bone in the TwoBoneIK modification. - - - - - - Returns the amount of roll/twist applied to the first bone in the TwoBoneIK modification. - - - - - - Returns the bone index of the second bone in the TwoBoneIK modification. - - - - - - Returns the name of the second bone in the TwoBoneIK modification. - - - - - - Returns the length of the second bone in the TwoBoneIK modification. - - - - - - Returns the amount of roll/twist applied to the second bone in the TwoBoneIK modification. - - - - - - Returns the node that is being used as the pole node for the TwoBoneIK modification, if a pole node has been set. - - - - - - Returns the node that is being used to calculate the tip position of the second bone in the TwoBoneIK modification, if a tip node has been set. - - - - - - Returns whether the TwoBoneIK modification will attempt to use the pole node to figure out which direction to bend, if a pole node has been set. - - - - - - Returns whether the TwoBoneIK modification will attempt to use the tip node to figure out the length and position of the tip of the second bone. - - - - - - - If true, the TwoBoneIK modification will attempt to autocalculate the lengths of the bones being used. The first bone will be calculated by using the distance from the origin of the first bone to the origin of the second bone. - The second bone will be calculated either using the tip node if that setting is enabled, or by using the distances of the second bone's children. If the tip node is not enabled and the bone has no children, then the length cannot be autocalculated. In this case, the length will either have to be manually inputted or a tip node used to calculate the length. - - - - - - - Sets the bone index, [code]bone_index[/code], of the first bone. When possible, this will also update the [code]bone_name[/code] of the first bone based on data provided by the [Skeleton3D]. - - - - - - - Sets the bone name, [code]bone_name[/code], of the first bone. When possible, this will also update the [code]bone_index[/code] of the first bone based on data provided by the [Skeleton3D]. - - - - - - - Sets the length of the first bone in the TwoBoneIK modification. - - - - - - - Sets the amount of roll/twist applied to the first bone in the TwoBoneIK modification. - - - - - - - Sets the bone index, [code]bone_index[/code], of the second bone. When possible, this will also update the [code]bone_name[/code] of the second bone based on data provided by the [Skeleton3D]. - - - - - - - Sets the bone name, [code]bone_name[/code], of the second bone. When possible, this will also update the [code]bone_index[/code] of the second bone based on data provided by the [Skeleton3D]. - - - - - - - Sets the length of the second bone in the TwoBoneIK modification. - - - - - - - Sets the amount of roll/twist applied to the second bone in the TwoBoneIK modification. - - - - - - - Sets the node to be used as the for the pole of the TwoBoneIK. When a node is set and the modification is set to use the pole node, the TwoBoneIK modification will bend the nodes in the direction towards this node when the bones need to bend. - - - - - - - Sets the node to be used as the tip for the second bone. This is used to calculate the length and position of the end of the second bone in the TwoBoneIK modification. - [b]Note:[/b] The tip node should generally be a child node of a [BoneAttachment3D] node attached to the second bone, with the child node being offset so it is at the end of the bone. - - - - - - - When [code]true[/code], the TwoBoneIK modification will bend the bones towards the pole node, if one has been set. This gives control over the direction the TwoBoneIK solver will bend, which is helpful for joints like elbows that only bend in certain directions. - - - - - - - When [code]true[/code], the TwoBoneIK modification will use the tip node to calculate the distance and position of the end/tip of the second bone. This is the most stable solution for knowing the tip position and length of the second bone. - - - - - - The NodePath to the node that is the target for the TwoBoneIK modification. This node is what the modification will attempt to rotate the bones to reach. - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkeletonModificationStack3D.xml b/modules/skeleton_3d/doc_classes/SkeletonModificationStack3D.xml deleted file mode 100644 index ac4638b..0000000 --- a/modules/skeleton_3d/doc_classes/SkeletonModificationStack3D.xml +++ /dev/null @@ -1,90 +0,0 @@ - - - - A resource that holds a stack of [SkeletonModification3D]s. - - - This resource is used by the Skeleton and holds a stack of [SkeletonModification3D]s. The SkeletonModificationStack3D controls the order of the modifications, which controls how they are applied. Modification order is especially important for full-body IK setups, as you need to execute the modifications in the correct order to get the desired results. For example, you want to execute a modification on the spine [i]before[/i] the arms on a humanoid skeleton. - Additionally, the SkeletonModificationStack3D also controls how strongly the modifications are applied to the [Skeleton3D] node. - - - - - - - - - Adds the passed-in [SkeletonModification3D] to the stack. - - - - - - - Deletes the [SkeletonModification3D] at the index position [code]mod_idx[/code], if it exists. - - - - - - - Enables all [SkeletonModification3D]s in the stack. - - - - - - - - Executes all of the [SkeletonModification3D]s in the stack that use the same execution mode as the passed-in [code]execution_mode[/code], starting from index [code]0[/code] to [member modification_count]. - [b]Note:[/b] The order of the modifications can matter depending on the modifications. For example, modifications on a spine should operate before modifications on the arms in order to get proper results. - - - - - - Returns a boolean that indicates whether the modification stack is setup and can execute. - - - - - - - Returns the [SkeletonModification3D] at the passed-in index, [code]mod_idx[/code]. - - - - - - Returns the [Skeleton3D] node that the SkeletonModificationStack3D is bound to. - - - - - - - - Sets the modification at [code]mod_idx[/code] to the passed-in modification, [code]modification[/code]. - - - - - - Sets up the modification stack so it can execute. This function should be called by [Skeleton3D] and shouldn't be called unless you know what you are doing. - - - - - - When true, the modification's in the stack will be called. This is handled automatically through the [Skeleton3D] node. - - - The amount of modifications in the stack. - - - The interpolation strength of the modifications in stack. A value of [code]0[/code] will make it where the modifications are not applied, a strength of [code]0.5[/code] will be half applied, and a strength of [code]1[/code] will allow the modifications to be fully applied and override the skeleton bone poses. - - - - - diff --git a/modules/skeleton_3d/doc_classes/Skin.xml b/modules/skeleton_3d/doc_classes/Skin.xml deleted file mode 100644 index a18f71f..0000000 --- a/modules/skeleton_3d/doc_classes/Skin.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/modules/skeleton_3d/doc_classes/SkinReference.xml b/modules/skeleton_3d/doc_classes/SkinReference.xml deleted file mode 100644 index f2ddc23..0000000 --- a/modules/skeleton_3d/doc_classes/SkinReference.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/modules/skeleton_3d/editor/physical_bone_editor_plugin.cpp b/modules/skeleton_3d/editor/physical_bone_editor_plugin.cpp deleted file mode 100644 index ac285cd..0000000 --- a/modules/skeleton_3d/editor/physical_bone_editor_plugin.cpp +++ /dev/null @@ -1,139 +0,0 @@ - -#include "physical_bone_editor_plugin.h" - -#include "editor/editor_settings.h" - -#include "../nodes/skeleton.h" -#include "../resources/skin.h" -#include "../nodes/physical_bone.h" - -PhysicalBoneSpatialGizmoPlugin::PhysicalBoneSpatialGizmoPlugin() { - create_material("joint_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint", Color(0.5, 0.8, 1))); -} - -bool PhysicalBoneSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) { - return Object::cast_to(p_spatial) != nullptr; -} - -String PhysicalBoneSpatialGizmoPlugin::get_gizmo_name() const { - return "PhysicalBones"; -} - -int PhysicalBoneSpatialGizmoPlugin::get_priority() const { - return -1; -} - -void PhysicalBoneSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) { - p_gizmo->clear(); - - PhysicalBone *physical_bone = Object::cast_to(p_gizmo->get_spatial_node()); - - if (!physical_bone) { - return; - } - - Skeleton *sk(physical_bone->find_skeleton_parent()); - if (!sk) { - return; - } - - PhysicalBone *pb(sk->get_physical_bone(physical_bone->get_bone_id())); - if (!pb) { - return; - } - - PhysicalBone *pbp(sk->get_physical_bone_parent(physical_bone->get_bone_id())); - if (!pbp) { - return; - } - - Vector points; - - switch (physical_bone->get_joint_type()) { - case PhysicalBone::JOINT_TYPE_PIN: { - JointSpatialGizmoPlugin::CreatePinJointGizmo(physical_bone->get_joint_offset(), points); - } break; - case PhysicalBone::JOINT_TYPE_CONE: { - const PhysicalBone::ConeJointData *cjd(static_cast(physical_bone->get_joint_data())); - JointSpatialGizmoPlugin::CreateConeTwistJointGizmo( - physical_bone->get_joint_offset(), - physical_bone->get_global_transform() * physical_bone->get_joint_offset(), - pb->get_global_transform(), - pbp->get_global_transform(), - cjd->swing_span, - cjd->twist_span, - &points, - &points); - } break; - case PhysicalBone::JOINT_TYPE_HINGE: { - const PhysicalBone::HingeJointData *hjd(static_cast(physical_bone->get_joint_data())); - JointSpatialGizmoPlugin::CreateHingeJointGizmo( - physical_bone->get_joint_offset(), - physical_bone->get_global_transform() * physical_bone->get_joint_offset(), - pb->get_global_transform(), - pbp->get_global_transform(), - hjd->angular_limit_lower, - hjd->angular_limit_upper, - hjd->angular_limit_enabled, - points, - &points, - &points); - } break; - case PhysicalBone::JOINT_TYPE_SLIDER: { - const PhysicalBone::SliderJointData *sjd(static_cast(physical_bone->get_joint_data())); - JointSpatialGizmoPlugin::CreateSliderJointGizmo( - physical_bone->get_joint_offset(), - physical_bone->get_global_transform() * physical_bone->get_joint_offset(), - pb->get_global_transform(), - pbp->get_global_transform(), - sjd->angular_limit_lower, - sjd->angular_limit_upper, - sjd->linear_limit_lower, - sjd->linear_limit_upper, - points, - &points, - &points); - } break; - case PhysicalBone::JOINT_TYPE_6DOF: { - const PhysicalBone::SixDOFJointData *sdofjd(static_cast(physical_bone->get_joint_data())); - JointSpatialGizmoPlugin::CreateGeneric6DOFJointGizmo( - physical_bone->get_joint_offset(), - - physical_bone->get_global_transform() * physical_bone->get_joint_offset(), - pb->get_global_transform(), - pbp->get_global_transform(), - - sdofjd->axis_data[0].angular_limit_lower, - sdofjd->axis_data[0].angular_limit_upper, - sdofjd->axis_data[0].linear_limit_lower, - sdofjd->axis_data[0].linear_limit_upper, - sdofjd->axis_data[0].angular_limit_enabled, - sdofjd->axis_data[0].linear_limit_enabled, - - sdofjd->axis_data[1].angular_limit_lower, - sdofjd->axis_data[1].angular_limit_upper, - sdofjd->axis_data[1].linear_limit_lower, - sdofjd->axis_data[1].linear_limit_upper, - sdofjd->axis_data[1].angular_limit_enabled, - sdofjd->axis_data[1].linear_limit_enabled, - - sdofjd->axis_data[2].angular_limit_lower, - sdofjd->axis_data[2].angular_limit_upper, - sdofjd->axis_data[2].linear_limit_lower, - sdofjd->axis_data[2].linear_limit_upper, - sdofjd->axis_data[2].angular_limit_enabled, - sdofjd->axis_data[2].linear_limit_enabled, - - points, - &points, - &points); - } break; - default: - return; - } - - Ref material = get_material("joint_material", p_gizmo); - - p_gizmo->add_collision_segments(points); - p_gizmo->add_lines(points, material); -} diff --git a/modules/skeleton_3d/editor/physical_bone_editor_plugin.h b/modules/skeleton_3d/editor/physical_bone_editor_plugin.h deleted file mode 100644 index e65e46c..0000000 --- a/modules/skeleton_3d/editor/physical_bone_editor_plugin.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef PHYSICAL_BONE_EDITOR_PLUGIN_H -#define PHYSICAL_BONE_EDITOR_PLUGIN_H - -#include "core/string/ustring.h" - -#include "editor/spatial_editor_gizmos.h" - -class PhysicalBoneSpatialGizmoPlugin : public EditorSpatialGizmoPlugin { - GDCLASS(PhysicalBoneSpatialGizmoPlugin, EditorSpatialGizmoPlugin); - -public: - bool has_gizmo(Spatial *p_spatial); - String get_gizmo_name() const; - int get_priority() const; - void redraw(EditorSpatialGizmo *p_gizmo); - - PhysicalBoneSpatialGizmoPlugin(); -}; - - -#endif diff --git a/modules/skeleton_3d/editor/physical_bone_plugin.cpp b/modules/skeleton_3d/editor/physical_bone_plugin.cpp deleted file mode 100644 index a2159f0..0000000 --- a/modules/skeleton_3d/editor/physical_bone_plugin.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/*************************************************************************/ -/* physical_bone_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "physical_bone_plugin.h" - -#include "editor/plugins/spatial_editor_plugin.h" -#include "scene/3d/physics_body.h" -#include "../nodes/physical_bone.h" -#include "core/object/class_db.h" -#include "core/error/error_macros.h" -#include "core/os/memory.h" -#include "scene/gui/box_container.h" -#include "scene/main/control.h" -#include "scene/gui/separator.h" -#include "scene/gui/tool_button.h" - -class EditorNode; - -void PhysicalBoneEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_on_toggle_button_transform_joint", "is_pressed"), &PhysicalBoneEditor::_on_toggle_button_transform_joint); -} - -void PhysicalBoneEditor::_on_toggle_button_transform_joint(bool p_is_pressed) { - _set_move_joint(); -} - -void PhysicalBoneEditor::_set_move_joint() { - if (selected) { - selected->_set_gizmo_move_joint(button_transform_joint->is_pressed()); - } -} - -PhysicalBoneEditor::PhysicalBoneEditor(EditorNode *p_editor) : - editor(p_editor), - selected(nullptr) { - spatial_editor_hb = memnew(HBoxContainer); - spatial_editor_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); - spatial_editor_hb->set_alignment(BoxContainer::ALIGN_BEGIN); - SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb); - - spatial_editor_hb->add_child(memnew(VSeparator)); - - button_transform_joint = memnew(ToolButton); - spatial_editor_hb->add_child(button_transform_joint); - - button_transform_joint->set_text(TTR("Move Joint")); - button_transform_joint->set_icon(SpatialEditor::get_singleton()->get_theme_icon("PhysicalBone", "EditorIcons")); - button_transform_joint->set_toggle_mode(true); - button_transform_joint->connect("toggled", this, "_on_toggle_button_transform_joint"); - - hide(); -} - -PhysicalBoneEditor::~PhysicalBoneEditor() {} - -void PhysicalBoneEditor::set_selected(PhysicalBone *p_pb) { - button_transform_joint->set_pressed(false); - - _set_move_joint(); - selected = p_pb; - _set_move_joint(); -} - -void PhysicalBoneEditor::hide() { - spatial_editor_hb->hide(); -} - -void PhysicalBoneEditor::show() { - spatial_editor_hb->show(); -} - -PhysicalBonePlugin::PhysicalBonePlugin(EditorNode *p_editor) : - editor(p_editor), - selected(nullptr), - physical_bone_editor(editor) {} - -void PhysicalBonePlugin::make_visible(bool p_visible) { - if (p_visible) { - physical_bone_editor.show(); - } else { - physical_bone_editor.hide(); - physical_bone_editor.set_selected(nullptr); - selected = nullptr; - } -} - -void PhysicalBonePlugin::edit(Object *p_node) { - selected = static_cast(p_node); // Trust it - ERR_FAIL_COND(!selected); - - physical_bone_editor.set_selected(selected); -} diff --git a/modules/skeleton_3d/editor/physical_bone_plugin.h b/modules/skeleton_3d/editor/physical_bone_plugin.h deleted file mode 100644 index b19d3a0..0000000 --- a/modules/skeleton_3d/editor/physical_bone_plugin.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef PHYSICAL_BONE_PLUGIN_H -#define PHYSICAL_BONE_PLUGIN_H -/*************************************************************************/ -/* physical_bone_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/object/object.h" -#include "core/string/ustring.h" -#include "editor/editor_plugin.h" - -class PhysicalBone; -class EditorNode; -class HBoxContainer; -class ToolButton; - -class PhysicalBoneEditor : public Object { - GDCLASS(PhysicalBoneEditor, Object); - - EditorNode *editor; - HBoxContainer *spatial_editor_hb; - ToolButton *button_transform_joint; - - PhysicalBone *selected; - -protected: - static void _bind_methods(); - -private: - void _on_toggle_button_transform_joint(bool p_is_pressed); - void _set_move_joint(); - -public: - PhysicalBoneEditor(EditorNode *p_editor); - ~PhysicalBoneEditor(); - - void set_selected(PhysicalBone *p_pb); - - void hide(); - void show(); -}; - -class PhysicalBonePlugin : public EditorPlugin { - GDCLASS(PhysicalBonePlugin, EditorPlugin); - - EditorNode *editor; - PhysicalBone *selected; - PhysicalBoneEditor physical_bone_editor; - -public: - virtual String get_name() const { return "PhysicalBone"; } - virtual bool handles(Object *p_object) const { return p_object->is_class("PhysicalBone"); } - virtual void make_visible(bool p_visible); - virtual void edit(Object *p_node); - - PhysicalBonePlugin(EditorNode *p_editor); -}; - -#endif diff --git a/modules/skeleton_3d/editor/skeleton_editor_plugin.cpp b/modules/skeleton_3d/editor/skeleton_editor_plugin.cpp deleted file mode 100644 index a28eb55..0000000 --- a/modules/skeleton_3d/editor/skeleton_editor_plugin.cpp +++ /dev/null @@ -1,1677 +0,0 @@ -/*************************************************************************/ -/* skeleton_editor_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_editor_plugin.h" - -#include "../nodes/physical_bone.h" -#include "../resources/skin.h" -#include "core/io/resource_saver.h" -#include "core/os/keyboard.h" -#include "editor/animation_track_editor.h" -#include "editor/editor_file_dialog.h" -#include "editor/editor_inspector.h" -#include "editor/editor_properties.h" -#include "editor/editor_scale.h" -#include "editor/editor_settings.h" -#include "editor/editor_spin_slider.h" -#include "editor/plugins/animation_player_editor_plugin.h" -#include "editor/plugins/spatial_editor_plugin.h" -#include "scene/3d/collision_shape.h" -#include "scene/3d/mesh_instance.h" -#include "scene/3d/physics_body.h" -#include "scene/3d/physics_joint.h" -#include "scene/gui/check_box.h" -#include "scene/gui/grid_container.h" -#include "scene/gui/label.h" -#include "scene/gui/popup_menu.h" -#include "scene/gui/separator.h" -#include "scene/gui/texture_rect.h" -#include "scene/gui/tree.h" -#include "scene/main/viewport.h" -#include "scene/resources/shapes/capsule_shape.h" -#include "scene/resources/shapes/sphere_shape.h" -#include "scene/resources/mesh/surface_tool.h" - -#include "physical_bone_editor_plugin.h" - -#define DISTANCE_DEFAULT 4 - -#define GIZMO_ARROW_SIZE 0.35 -#define GIZMO_RING_HALF_WIDTH 0.1 -//#define GIZMO_SCALE_DEFAULT 0.15 -#define GIZMO_PLANE_SIZE 0.2 -#define GIZMO_PLANE_DST 0.3 -#define GIZMO_CIRCLE_SIZE 1.1 -#define GIZMO_SCALE_OFFSET (GIZMO_CIRCLE_SIZE + 0.3) -#define GIZMO_ARROW_OFFSET (GIZMO_CIRCLE_SIZE + 0.3) - -#define ZOOM_FREELOOK_MIN 0.01 -#define ZOOM_FREELOOK_MULTIPLIER 1.08 -#define ZOOM_FREELOOK_INDICATOR_DELAY_S 1.5 - -#ifdef REAL_T_IS_DOUBLE -#define ZOOM_FREELOOK_MAX 1'000'000'000'000 -#else -#define ZOOM_FREELOOK_MAX 10'000 -#endif - -#define MIN_Z 0.01 -#define MAX_Z 1000000.0 - -#define MIN_FOV 0.01 -#define MAX_FOV 179 - -void BoneTransformEditor::create_editors() { - const Color section_color = get_theme_color("prop_subsection", "Editor"); - - section = memnew(EditorInspectorSection); - section->setup("trf_properties", label, this, section_color, true); - section->unfold(); - add_child(section); - - enabled_checkbox = memnew(EditorPropertyCheck()); - enabled_checkbox->set_label("Pose Enabled"); - enabled_checkbox->set_selectable(false); - enabled_checkbox->connect("property_changed", this, "_value_changed"); - section->get_vbox()->add_child(enabled_checkbox); - - // Position property. - position_property = memnew(EditorPropertyVector3()); - position_property->setup(-10000, 10000, 0.001f, true); - position_property->set_label("Position"); - position_property->set_selectable(false); - position_property->connect("property_changed", this, "_value_changed"); - position_property->connect("property_keyed", this, "_property_keyed"); - section->get_vbox()->add_child(position_property); - - // Rotation property - rotation_property = memnew(EditorPropertyQuaternion()); - rotation_property->setup(-10000, 10000, 0.001f, true); - rotation_property->set_label("Rotation"); - rotation_property->set_selectable(false); - rotation_property->connect("property_changed", this, "_value_changed"); - rotation_property->connect("property_keyed", this, "_property_keyed"); - section->get_vbox()->add_child(rotation_property); - - // Scale property - scale_property = memnew(EditorPropertyVector3()); - scale_property->setup(-10000, 10000, 0.001f, true); - scale_property->set_label("Scale"); - scale_property->set_selectable(false); - scale_property->connect("property_changed", this, "_value_changed_vector3"); - scale_property->connect("property_keyed", this, "_property_keyed"); - section->get_vbox()->add_child(scale_property); - - // Transform/Matrix section - rest_section = memnew(EditorInspectorSection); - rest_section->setup("trf_properties_transform", "Rest", this, section_color, true); - section->get_vbox()->add_child(rest_section); - - // Transform/Matrix property - rest_matrix = memnew(EditorPropertyTransform()); - rest_matrix->setup(-10000, 10000, 0.001f, true); - rest_matrix->set_label("Transform"); - rest_matrix->set_selectable(false); - rest_section->get_vbox()->add_child(rest_matrix); -} - -void BoneTransformEditor::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - create_editors(); - } - } -} - -void BoneTransformEditor::_value_changed(const String &p_path, const Variant &p_value, const String &p_name, bool changing) { - if (updating) { - return; - } - - if (skeleton) { - undo_redo->create_action(TTR("Set Bone Transform"), UndoRedo::MERGE_ENDS); - undo_redo->add_undo_property(skeleton, p_path, skeleton->get(p_path)); - undo_redo->add_do_property(skeleton, p_path, p_value); - undo_redo->commit_action(); - } -} - -void BoneTransformEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_value_changed"), &BoneTransformEditor::_value_changed); - ClassDB::bind_method(D_METHOD("_property_keyed"), &BoneTransformEditor::_property_keyed); - //ClassDB::bind_method(D_METHOD("update_joint_tree"), &BoneTransformEditor::update_joint_tree); -} - -BoneTransformEditor::BoneTransformEditor(Skeleton *p_skeleton) { - skeleton = p_skeleton; - enabled_checkbox = nullptr; - toggle_enabled = false; - updating = false; - undo_redo = EditorNode::get_undo_redo(); -} - -void BoneTransformEditor::set_keyable(const bool p_keyable) { - position_property->set_keying(p_keyable); - rotation_property->set_keying(p_keyable); - scale_property->set_keying(p_keyable); -} - -void BoneTransformEditor::set_target(const String &p_prop) { - enabled_checkbox->set_object_and_property(skeleton, p_prop + "enabled"); - enabled_checkbox->update_property(); - - position_property->set_object_and_property(skeleton, p_prop + "position"); - position_property->update_property(); - - rotation_property->set_object_and_property(skeleton, p_prop + "rotation"); - rotation_property->update_property(); - - scale_property->set_object_and_property(skeleton, p_prop + "scale"); - scale_property->update_property(); - - rest_matrix->set_object_and_property(skeleton, p_prop + "rest"); - rest_matrix->update_property(); -} - -void BoneTransformEditor::_property_keyed(const String &p_path, bool p_advance) { - AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor(); - Vector split = p_path.split("/"); - if (split.size() == 3 && split[0] == "bones") { - int bone_idx = split[1].to_int(); - if (split[2] == "position") { - te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_POSITION_3D, skeleton->get(p_path)); - } - if (split[2] == "rotation") { - te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_ROTATION_3D, skeleton->get(p_path)); - } - if (split[2] == "scale") { - te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_SCALE_3D, skeleton->get(p_path)); - } - } -} - -void BoneTransformEditor::_update_properties() { - /* - if (!skeleton) { - return; - } - - int selected = SkeletonEditor::get_singleton()->get_selected_bone(); - - List props; - skeleton->get_property_list(&props); - for (const PropertyInfo &E : props) { - PackedStringArray spr = E.name.split("/"); - if (spr.size() == 3 && spr[0] == "bones") { - if (spr[1].to_int() == selected) { - if (spr[2] == "enabled") { - enabled_checkbox->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY); - enabled_checkbox->update_property(); - enabled_checkbox->update(); - } - if (spr[2] == "position") { - position_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY); - position_property->update_property(); - position_property->update(); - } - if (spr[2] == "rotation") { - rotation_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY); - rotation_property->update_property(); - rotation_property->update(); - } - if (spr[2] == "scale") { - scale_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY); - scale_property->update_property(); - scale_property->update(); - } - if (spr[2] == "rest") { - rest_matrix->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY); - rest_matrix->update_property(); - rest_matrix->update(); - } - } - } - } - */ -} - -SkeletonEditor *SkeletonEditor::singleton = nullptr; - -void SkeletonEditor::set_keyable(const bool p_keyable) { - keyable = p_keyable; - - if (p_keyable) { - animation_hb->show(); - } else { - animation_hb->hide(); - } -}; - -void SkeletonEditor::set_bone_options_enabled(const bool p_bone_options_enabled) { - skeleton_options->get_popup()->set_item_disabled(SKELETON_OPTION_INIT_SELECTED_POSES, !p_bone_options_enabled); - skeleton_options->get_popup()->set_item_disabled(SKELETON_OPTION_SELECTED_POSES_TO_RESTS, !p_bone_options_enabled); -}; - -void SkeletonEditor::_on_click_skeleton_option(int p_skeleton_option) { - if (!skeleton) { - return; - } - - switch (p_skeleton_option) { - case SKELETON_OPTION_INIT_ALL_POSES: { - init_pose(true); - break; - } - case SKELETON_OPTION_INIT_SELECTED_POSES: { - init_pose(false); - break; - } - case SKELETON_OPTION_ALL_POSES_TO_RESTS: { - pose_to_rest(true); - break; - } - case SKELETON_OPTION_SELECTED_POSES_TO_RESTS: { - pose_to_rest(false); - break; - } - case SKELETON_OPTION_CREATE_PHYSICAL_SKELETON: { - create_physical_skeleton(); - break; - } - case SKELETON_OPTION_ADD_BONE: { - add_bone(); - } break; - case SKELETON_OPTION_REMOVE_BONE: { - remove_bone(); - } break; - case SKELETON_OPTION_RENAME_BONE: { - rename_bone(); - } break; - } -} - -void SkeletonEditor::init_pose(const bool p_all_bones) { - if (!skeleton) { - return; - } - - const int bone_len = skeleton->get_bone_count(); - if (!bone_len) { - return; - } - UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); - ur->create_action(TTR("Set Bone Transform"), UndoRedo::MERGE_ENDS); - - if (p_all_bones) { - for (int i = 0; i < bone_len; i++) { - Transform rest = skeleton->get_bone_rest(i); - ur->add_do_method(skeleton, "set_bone_pose_position", i, rest.origin); - ur->add_do_method(skeleton, "set_bone_pose_rotation", i, rest.basis.get_rotation_quaternion()); - ur->add_do_method(skeleton, "set_bone_pose_scale", i, rest.basis.get_scale()); - ur->add_undo_method(skeleton, "set_bone_pose_position", i, skeleton->get_bone_pose_position(i)); - ur->add_undo_method(skeleton, "set_bone_pose_rotation", i, skeleton->get_bone_pose_rotation(i)); - ur->add_undo_method(skeleton, "set_bone_pose_scale", i, skeleton->get_bone_pose_scale(i)); - } - } else { - // Todo: Do method with multiple bone selection. - if (selected_bone == -1) { - ur->commit_action(); - return; - } - Transform rest = skeleton->get_bone_rest(selected_bone); - ur->add_do_method(skeleton, "set_bone_pose_position", selected_bone, rest.origin); - ur->add_do_method(skeleton, "set_bone_pose_rotation", selected_bone, rest.basis.get_rotation_quaternion()); - ur->add_do_method(skeleton, "set_bone_pose_scale", selected_bone, rest.basis.get_scale()); - ur->add_undo_method(skeleton, "set_bone_pose_position", selected_bone, skeleton->get_bone_pose_position(selected_bone)); - ur->add_undo_method(skeleton, "set_bone_pose_rotation", selected_bone, skeleton->get_bone_pose_rotation(selected_bone)); - ur->add_undo_method(skeleton, "set_bone_pose_scale", selected_bone, skeleton->get_bone_pose_scale(selected_bone)); - } - - ur->commit_action(); -} - -void SkeletonEditor::insert_keys(const bool p_all_bones) { - if (!skeleton) { - return; - } - - bool pos_enabled = key_loc_button->is_pressed(); - bool rot_enabled = key_rot_button->is_pressed(); - bool scl_enabled = key_scale_button->is_pressed(); - - int bone_len = skeleton->get_bone_count(); - Node *root = EditorNode::get_singleton()->get_tree()->get_root(); - String path = root->get_path_to(skeleton); - - AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor(); - for (int i = 0; i < bone_len; i++) { - const String name = skeleton->get_bone_name(i); - - if (name.empty()) { - continue; - } - - if (pos_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_POSITION_3D))) { - te->insert_transform_key(skeleton, name, Animation::TYPE_POSITION_3D, skeleton->get_bone_pose_position(i)); - } - if (rot_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_ROTATION_3D))) { - te->insert_transform_key(skeleton, name, Animation::TYPE_ROTATION_3D, skeleton->get_bone_pose_rotation(i)); - } - if (scl_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_SCALE_3D))) { - te->insert_transform_key(skeleton, name, Animation::TYPE_SCALE_3D, skeleton->get_bone_pose_scale(i)); - } - } - - //te->commit_insert_queue(); -} - -void SkeletonEditor::pose_to_rest(const bool p_all_bones) { - if (!skeleton) { - return; - } - - const int bone_len = skeleton->get_bone_count(); - if (!bone_len) { - return; - } - - UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); - - ur->create_action(TTR("Set Bone Rest"), UndoRedo::MERGE_ENDS); - - if (p_all_bones) { - for (int i = 0; i < bone_len; i++) { - ur->add_do_method(skeleton, "set_bone_rest", i, skeleton->get_bone_pose(i)); - ur->add_undo_method(skeleton, "set_bone_rest", i, skeleton->get_bone_rest(i)); - } - } else { - // Todo: Do method with multiple bone selection. - if (selected_bone == -1) { - ur->commit_action(); - return; - } - ur->add_do_method(skeleton, "set_bone_rest", selected_bone, skeleton->get_bone_pose(selected_bone)); - ur->add_undo_method(skeleton, "set_bone_rest", selected_bone, skeleton->get_bone_rest(selected_bone)); - } - ur->commit_action(); -} - -void SkeletonEditor::create_physical_skeleton() { - UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); - ERR_FAIL_COND(!get_tree()); - Node *owner = get_tree()->get_edited_scene_root(); - - const int bc = skeleton->get_bone_count(); - - if (!bc) { - return; - } - - Vector bones_infos; - bones_infos.resize(bc); - - for (int bone_id = 0; bc > bone_id; ++bone_id) { - const int parent = skeleton->get_bone_parent(bone_id); - - if (parent < 0) { - bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id); - - } else { - const int parent_parent = skeleton->get_bone_parent(parent); - - bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id); - - /// create physical bone on parent - if (!bones_infos[parent].physical_bone) { - bones_infos.write[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos); - - ur->create_action(TTR("Create physical bones")); - ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone); - ur->add_do_reference(bones_infos[parent].physical_bone); - ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone); - ur->commit_action(); - - bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent)); - bones_infos[parent].physical_bone->set_owner(owner); - bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner - - /// Create joint between parent of parent - if (-1 != parent_parent) { - bones_infos[parent].physical_bone->set_joint_type(PhysicalBone::JOINT_TYPE_PIN); - } - } - } - } -} - -PhysicalBone *SkeletonEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector &bones_infos) { - const Transform child_rest = skeleton->get_bone_rest(bone_child_id); - - real_t half_height(child_rest.origin.length() * 0.5); - real_t radius(half_height * 0.2); - - CapsuleShape *bone_shape_capsule = memnew(CapsuleShape); - //bone_shape_capsule->set_height((half_height - radius) * 2); - bone_shape_capsule->set_height(half_height * 2); - bone_shape_capsule->set_radius(radius); - - CollisionShape *bone_shape = memnew(CollisionShape); - bone_shape->set_shape(bone_shape_capsule); - - Transform capsule_transform; - bone_shape->set_transform(capsule_transform); - - Vector3 up = Vector3(0, 1, 0); - if (up.cross(child_rest.origin).is_equal_approx(Vector3())) { - up = Vector3(0, 0, 1); - } - - Transform body_transform; - body_transform.set_look_at(Vector3(0, 0, 0), child_rest.origin, up); - body_transform.origin = body_transform.basis.xform(Vector3(0, 0, -half_height)); - - Transform joint_transform; - joint_transform.origin = Vector3(0, 0, half_height); - - PhysicalBone *physical_bone = memnew(PhysicalBone); - physical_bone->add_child(bone_shape); - physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id)); - physical_bone->set_body_offset(body_transform); - physical_bone->set_joint_offset(joint_transform); - return physical_bone; -} - -Variant SkeletonEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { - TreeItem *selected = joint_tree->get_selected(); - - if (!selected) { - return Variant(); - } - - Ref icon = selected->get_icon(0); - - VBoxContainer *vb = memnew(VBoxContainer); - HBoxContainer *hb = memnew(HBoxContainer); - TextureRect *tf = memnew(TextureRect); - tf->set_texture(icon); - tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED); - hb->add_child(tf); - Label *label = memnew(Label(selected->get_text(0))); - hb->add_child(label); - vb->add_child(hb); - hb->set_modulate(Color(1, 1, 1, 1)); - - set_drag_preview(vb); - Dictionary drag_data; - drag_data["type"] = "nodes"; - drag_data["node"] = selected; - - return drag_data; -} - -bool SkeletonEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { - TreeItem *target = joint_tree->get_item_at_position(p_point); - if (!target) { - return false; - } - - const String path = target->get_metadata(0); - if (!path.begins_with("bones/")) { - return false; - } - - TreeItem *selected = Object::cast_to(Dictionary(p_data)["node"]); - if (target == selected) - return false; - - const String path2 = target->get_metadata(0); - if (!path2.begins_with("bones/")) { - return false; - } - - return true; -} - -void SkeletonEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { - if (!can_drop_data_fw(p_point, p_data, p_from)) { - return; - } - - TreeItem *target = joint_tree->get_item_at_position(p_point); - TreeItem *selected = Object::cast_to(Dictionary(p_data)["node"]); - - const BoneId target_boneidx = String(target->get_metadata(0)).get_slicec('/', 1).to_int(); - const BoneId selected_boneidx = String(selected->get_metadata(0)).get_slicec('/', 1).to_int(); - - move_skeleton_bone(skeleton->get_path(), selected_boneidx, target_boneidx); -} - -void SkeletonEditor::move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx) { - Node *node = get_node_or_null(p_skeleton_path); - Skeleton *skeleton = Object::cast_to(node); - ERR_FAIL_NULL(skeleton); - UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); - ur->create_action(TTR("Set Bone Parentage")); - - // If the target is a child of ourselves, we move only *us* and not our children - if (skeleton->is_bone_parent_of(p_target_boneidx, p_selected_boneidx)) { - const BoneId parent_idx = skeleton->get_bone_parent(p_selected_boneidx); - const int bone_count = skeleton->get_bone_count(); - for (BoneId i = 0; i < bone_count; ++i) { - if (skeleton->get_bone_parent(i) == p_selected_boneidx) { - ur->add_undo_method(skeleton, "set_bone_parent", i, skeleton->get_bone_parent(i)); - ur->add_do_method(skeleton, "set_bone_parent", i, parent_idx); - skeleton->set_bone_parent(i, parent_idx); - } - } - } - - ur->add_undo_method(skeleton, "set_bone_parent", p_selected_boneidx, skeleton->get_bone_parent(p_selected_boneidx)); - ur->add_do_method(skeleton, "set_bone_parent", p_selected_boneidx, p_target_boneidx); - skeleton->set_bone_parent(p_selected_boneidx, p_target_boneidx); - - ur->commit_action(); - - update_joint_tree(); -} - -void SkeletonEditor::_joint_tree_selection_changed() { - TreeItem *selected = joint_tree->get_selected(); - - if (selected) { - const String path = selected->get_metadata(0); - - if (path.begins_with("bones/")) { - const int b_idx = path.get_slicec('/', 1).to_int(); - const String bone_path = "bones/" + itos(b_idx) + "/"; - - pose_editor->set_target(bone_path); - pose_editor->set_keyable(keyable); - - selected_bone = b_idx; - } - } - - pose_editor->set_visible(selected); - - set_bone_options_enabled(selected); - _update_properties(); - _update_gizmo_visible(); -} - -void SkeletonEditor::_joint_tree_rmb_select(const Vector2 &p_pos) { - //skeleton->set_selected_bone(-1); - //_update_spatial_transform_gizmo(); -} - -void SkeletonEditor::_update_properties() { - if (pose_editor) { - pose_editor->_update_properties(); - } - - SpatialEditor::get_singleton()->update_transform_gizmo(); -} - -void SkeletonEditor::update_joint_tree() { - skeleton->force_update_all_bone_transforms(); - - joint_tree->clear(); - - if (!skeleton) { - return; - } - - TreeItem *root = joint_tree->create_item(); - - RBMap items; - - items.insert(-1, root); - - Ref bone_icon = get_theme_icon("Bone", "EditorIcons"); - - Vector bones_to_process = skeleton->get_parentless_bones(); - while (bones_to_process.size() > 0) { - int current_bone_idx = bones_to_process[0]; - bones_to_process.erase(current_bone_idx); - - const int parent_idx = skeleton->get_bone_parent(current_bone_idx); - TreeItem *parent_item = items.find(parent_idx)->get(); - - TreeItem *joint_item = joint_tree->create_item(parent_item); - items.insert(current_bone_idx, joint_item); - - joint_item->set_text(0, skeleton->get_bone_name(current_bone_idx)); - joint_item->set_icon(0, bone_icon); - joint_item->set_selectable(0, true); - joint_item->set_metadata(0, "bones/" + itos(current_bone_idx)); - - // Add the bone's children to the list of bones to be processed. - Vector current_bone_child_bones = skeleton->get_bone_children(current_bone_idx); - int child_bone_size = current_bone_child_bones.size(); - for (int i = 0; i < child_bone_size; i++) { - bones_to_process.push_back(current_bone_child_bones[i]); - } - } -} - -void SkeletonEditor::update_editors() { -} - -void SkeletonEditor::create_editors() { - set_h_size_flags(SIZE_EXPAND_FILL); - add_theme_constant_override("separation", 0); - - set_focus_mode(FOCUS_ALL); - - SpatialEditor *ne = SpatialEditor::get_singleton(); - AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor(); - - // Create Top Menu Bar - separator = memnew(VSeparator); - ne->add_control_to_menu_panel(separator); - - // Create Skeleton Option in Top Menu Bar. - skeleton_options = memnew(MenuButton); - ne->add_control_to_menu_panel(skeleton_options); - - skeleton_options->set_text(TTR("Skeleton")); - skeleton_options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Skeleton", "EditorIcons")); - - PopupMenu *p = skeleton_options->get_popup(); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/init_all_poses", TTR("Init all Poses")), SKELETON_OPTION_INIT_ALL_POSES); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/init_selected_poses", TTR("Init selected Poses")), SKELETON_OPTION_INIT_SELECTED_POSES); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/all_poses_to_rests", TTR("Apply all poses to rests")), SKELETON_OPTION_ALL_POSES_TO_RESTS); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/selected_poses_to_rests", TTR("Apply selected poses to rests")), SKELETON_OPTION_SELECTED_POSES_TO_RESTS); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/create_physical_skeleton", TTR("Create physical skeleton")), SKELETON_OPTION_CREATE_PHYSICAL_SKELETON); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/add_bone", TTR("Add bone")), SKELETON_OPTION_ADD_BONE); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/rename_bone", TTR("Rename bone")), SKELETON_OPTION_RENAME_BONE); - p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/remove_bone", TTR("Remove bone")), SKELETON_OPTION_REMOVE_BONE); - - skeleton_options->get_popup()->connect("id_pressed", this, "_on_click_skeleton_option"); - set_bone_options_enabled(false); - - Vector button_binds; - button_binds.resize(1); - - edit_mode_button = memnew(Button); - ne->add_control_to_menu_panel(edit_mode_button); - edit_mode_button->set_flat(true); - edit_mode_button->set_toggle_mode(true); - edit_mode_button->set_focus_mode(FOCUS_NONE); - edit_mode_button->set_tooltip(TTR("Edit Mode\nShow buttons on joints.")); - edit_mode_button->connect("toggled", this, "edit_mode_toggled"); - - edit_mode = false; - - if (skeleton) { - skeleton->add_child(handles_mesh_instance); - handles_mesh_instance->set_skeleton_path(NodePath("")); - } - - // Keying buttons. - animation_hb = memnew(HBoxContainer); - ne->add_control_to_menu_panel(animation_hb); - animation_hb->add_child(memnew(VSeparator)); - animation_hb->hide(); - - key_loc_button = memnew(Button); - key_loc_button->set_flat(true); - key_loc_button->set_toggle_mode(true); - key_loc_button->set_pressed(false); - key_loc_button->set_focus_mode(FOCUS_NONE); - key_loc_button->set_tooltip(TTR("Translation mask for inserting keys.")); - animation_hb->add_child(key_loc_button); - - key_rot_button = memnew(Button); - key_rot_button->set_flat(true); - key_rot_button->set_toggle_mode(true); - key_rot_button->set_pressed(true); - key_rot_button->set_focus_mode(FOCUS_NONE); - key_rot_button->set_tooltip(TTR("Rotation mask for inserting keys.")); - animation_hb->add_child(key_rot_button); - - key_scale_button = memnew(Button); - key_scale_button->set_flat(true); - key_scale_button->set_toggle_mode(true); - key_scale_button->set_pressed(false); - key_scale_button->set_focus_mode(FOCUS_NONE); - key_scale_button->set_tooltip(TTR("Scale mask for inserting keys.")); - animation_hb->add_child(key_scale_button); - - key_insert_button = memnew(Button); - key_insert_button->set_flat(true); - key_insert_button->set_focus_mode(FOCUS_NONE); - key_insert_button->connect("pressed", this, "insert_keys", varray(false)); - key_insert_button->set_tooltip(TTR("Insert key of bone poses already exist track.")); - key_insert_button->set_shortcut(ED_SHORTCUT("skeleton_3d_editor/insert_key_to_existing_tracks", TTR("Insert Key (Existing Tracks)"), KEY_INSERT)); - animation_hb->add_child(key_insert_button); - - key_insert_all_button = memnew(Button); - key_insert_all_button->set_flat(true); - key_insert_all_button->set_focus_mode(FOCUS_NONE); - key_insert_all_button->connect("pressed", this, "insert_keys", varray(true)); - key_insert_all_button->set_tooltip(TTR("Insert key of all bone poses.")); - key_insert_all_button->set_shortcut(ED_SHORTCUT("skeleton_3d_editor/insert_key_of_all_bones", TTR("Insert Key (All Bones)"), KEY_MASK_CMD + KEY_INSERT)); - animation_hb->add_child(key_insert_all_button); - - // Bone tree. - - const Color section_color = get_theme_color("prop_subsection", "Editor"); - - EditorInspectorSection *bones_section = memnew(EditorInspectorSection); - bones_section->setup("bones", "Bones", skeleton, section_color, true); - add_child(bones_section); - bones_section->unfold(); - - ScrollContainer *s_con = memnew(ScrollContainer); - s_con->set_h_size_flags(SIZE_EXPAND_FILL); - s_con->set_custom_minimum_size(Size2(1, 350) * EDSCALE); - bones_section->get_vbox()->add_child(s_con); - - joint_tree = memnew(Tree); - joint_tree->set_columns(1); - joint_tree->set_focus_mode(Control::FOCUS_NONE); - joint_tree->set_select_mode(Tree::SELECT_SINGLE); - joint_tree->set_hide_root(true); - joint_tree->set_v_size_flags(SIZE_EXPAND_FILL); - joint_tree->set_h_size_flags(SIZE_EXPAND_FILL); - joint_tree->set_allow_rmb_select(true); - joint_tree->set_drag_forwarding(this); - s_con->add_child(joint_tree); - - pose_editor = memnew(BoneTransformEditor(skeleton)); - pose_editor->set_label(TTR("Bone Transform")); - pose_editor->set_visible(false); - add_child(pose_editor); - - set_keyable(te->has_keying()); -} - -void SkeletonEditor::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_READY: { - edit_mode_button->set_icon(get_theme_icon("ToolBoneSelect", "EditorIcons")); - key_loc_button->set_icon(get_theme_icon("KeyPosition", "EditorIcons")); - key_rot_button->set_icon(get_theme_icon("KeyRotation", "EditorIcons")); - key_scale_button->set_icon(get_theme_icon("KeyScale", "EditorIcons")); - key_insert_button->set_icon(get_theme_icon("Key", "EditorIcons")); - key_insert_all_button->set_icon(get_theme_icon("NewKey", "EditorIcons")); - - get_tree()->connect("node_removed", this, "_node_removed", Vector(), Object::CONNECT_ONESHOT); - break; - } break; - case NOTIFICATION_ENTER_TREE: { - create_editors(); - update_joint_tree(); - update_editors(); - joint_tree->connect("item_selected", this, "_joint_tree_selection_changed"); - joint_tree->connect("item_rmb_selected", this, "_joint_tree_rmb_select"); - -#ifdef TOOLS_ENABLED - skeleton->connect("pose_updated", this, "_draw_gizmo"); - skeleton->connect("pose_updated", this, "_update_properties"); - skeleton->connect("bone_enabled_changed", this, "_bone_enabled_changed"); - skeleton->connect("show_rest_only_changed", this, "_update_gizmo_visible"); -#endif - break; - } - } -} - -void SkeletonEditor::_node_removed(Node *p_node) { - if (skeleton && p_node == skeleton) { - skeleton = nullptr; - skeleton_options->hide(); - } - - _update_properties(); -} - -void SkeletonEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_node_removed"), &SkeletonEditor::_node_removed); - ClassDB::bind_method(D_METHOD("_joint_tree_selection_changed"), &SkeletonEditor::_joint_tree_selection_changed); - ClassDB::bind_method(D_METHOD("_joint_tree_rmb_select"), &SkeletonEditor::_joint_tree_rmb_select); - ClassDB::bind_method(D_METHOD("_update_properties"), &SkeletonEditor::_update_properties); - ClassDB::bind_method(D_METHOD("_on_click_skeleton_option"), &SkeletonEditor::_on_click_skeleton_option); - ClassDB::bind_method(D_METHOD("insert_keys"), &SkeletonEditor::insert_keys); - ClassDB::bind_method(D_METHOD("edit_mode_toggled"), &SkeletonEditor::edit_mode_toggled); - - ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SkeletonEditor::get_drag_data_fw); - ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SkeletonEditor::can_drop_data_fw); - ClassDB::bind_method(D_METHOD("drop_data_fw"), &SkeletonEditor::drop_data_fw); - ClassDB::bind_method(D_METHOD("move_skeleton_bone"), &SkeletonEditor::move_skeleton_bone); - - ClassDB::bind_method(D_METHOD("_draw_handles"), &SkeletonEditor::_draw_handles); - ClassDB::bind_method(D_METHOD("_draw_gizmo"), &SkeletonEditor::_draw_gizmo); - ClassDB::bind_method(D_METHOD("_update_gizmo_visible"), &SkeletonEditor::_update_gizmo_visible); - - _bind_tool_popup_methods(); -} - -void SkeletonEditor::edit_mode_toggled(const bool pressed) { - edit_mode = pressed; - _update_gizmo_visible(); -} - -SkeletonEditor::SkeletonEditor(EditorInspectorPluginSkeleton *e_plugin, EditorNode *p_editor, Skeleton *p_skeleton) { - editor = p_editor; - editor_plugin = e_plugin; - - skeleton = p_skeleton; - - singleton = this; - - joint_tree = nullptr; - rest_editor = nullptr; - pose_editor = nullptr; - - skeleton_options = nullptr; - - edit_mode = false; - - file_dialog = nullptr; - - keyable = false; - - file_export_lib = nullptr; - - selected_bone = -1; - - handle_material = Ref(memnew(ShaderMaterial)); - handle_shader = Ref(memnew(Shader)); - - /* - handle_shader->set_code(" \ - shader_type spatial; \ - render_mode unshaded; \ - uniform vec4 albedo : hint_color = vec4(1,1,1,1); \ - uniform sampler2D texture_albedo : hint_albedo; \ - uniform float point_size : hint_range(0,128) = 32; \ - void vertex() { \ - if (!OUTPUT_IS_SRGB) { \ - COLOR.rgb = mix( pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb* (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)) ); \ - } \ - POINT_SIZE=point_size; \ - VERTEX = VERTEX; \ - POSITION=PROJECTION_MATRIX*INV_CAMERA_MATRIX*WORLD_MATRIX*vec4(VERTEX.xyz,1.0); \ - POSITION.z = mix(POSITION.z, -POSITION.w, 0.999); \ - } \ - void fragment() { \ - vec4 albedo_tex = texture(texture_albedo,POINT_COORD); \ - if (albedo.a * albedo_tex.a < 0.5) { discard; } \ - vec3 col = albedo_tex.rgb + COLOR.rgb; \ - col = vec3(min(col.r,1.0),min(col.g,1.0),min(col.b,1.0)); \ - ALBEDO = albedo.rgb * col; \ - } \ - "); - - handle_material->set_shader(handle_shader); - // handle_material->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true); - handle_material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN); - // handle_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true); - // handle_material->set_flag(SpatialMaterial::FLAG_USE_POINT_SIZE, true); - // handle_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true); - Ref handle = editor->get_gui_base()->get_theme_icon("EditorBoneHandle", "EditorIcons"); - handle_material->set_shader_param("point_size", handle->get_width()); - handle_material->set_shader_param("texture_albedo", handle); - //handle_material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, handle); - */ - - handle_shader->set_code(R"( - // Skeleton 3D gizmo handle shader. - shader_type spatial; - render_mode unshaded, shadows_disabled, depth_draw_always; - uniform sampler2D texture_albedo : hint_albedo; - uniform float point_size : hint_range(0,128) = 32; - void vertex() { - if (!OUTPUT_IS_SRGB) { - COLOR.rgb = mix( pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb* (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)) ); - } - VERTEX = VERTEX; - POSITION=PROJECTION_MATRIX*INV_CAMERA_MATRIX*WORLD_MATRIX*vec4(VERTEX.xyz,1.0); - POSITION.z = mix(POSITION.z, 0, 0.999); - POINT_SIZE = point_size; - } - void fragment() { - vec4 albedo_tex = texture(texture_albedo,POINT_COORD); - vec3 col = albedo_tex.rgb + COLOR.rgb; - col = vec3(min(col.r,1.0),min(col.g,1.0),min(col.b,1.0)); - ALBEDO = col; - if (albedo_tex.a < 0.5) { discard; } - ALPHA = albedo_tex.a; - } - )"); - - handle_material->set_shader(handle_shader); - Ref handle = editor->get_gui_base()->get_theme_icon("EditorBoneHandle", "EditorIcons"); - handle_material->set_shader_param("point_size", handle->get_width()); - handle_material->set_shader_param("texture_albedo", handle); - - handles_mesh_instance = memnew(MeshInstance); - handles_mesh_instance->set_cast_shadows_setting(GeometryInstance::SHADOW_CASTING_SETTING_OFF); - handles_mesh.instance(); - handles_mesh_instance->set_mesh(handles_mesh); - - create_bone_tool_popups(); -} - -SkeletonEditor::~SkeletonEditor() { - if (skeleton) { -#ifdef TOOLS_ENABLED - skeleton->disconnect("show_rest_only_changed", this, "_update_gizmo_visible"); - skeleton->disconnect("bone_enabled_changed", this, "_bone_enabled_changed"); - skeleton->disconnect("pose_updated", this, "_draw_gizmo"); - skeleton->disconnect("pose_updated", this, "_update_properties"); - skeleton->set_transform_gizmo_visible(true); -#endif - handles_mesh_instance->get_parent()->remove_child(handles_mesh_instance); - } - - handles_mesh_instance->queue_delete(); - - SpatialEditor *ne = SpatialEditor::get_singleton(); - - if (animation_hb) { - ne->remove_control_from_menu_panel(animation_hb); - memdelete(animation_hb); - } - - if (separator) { - ne->remove_control_from_menu_panel(separator); - memdelete(separator); - } - - if (skeleton_options) { - ne->remove_control_from_menu_panel(skeleton_options); - memdelete(skeleton_options); - } - - if (edit_mode_button) { - ne->remove_control_from_menu_panel(edit_mode_button); - memdelete(edit_mode_button); - } -} - -void SkeletonEditor::update_bone_original() { - if (!skeleton) { - return; - } - - if (skeleton->get_bone_count() == 0 || selected_bone == -1) { - return; - } - - bone_original_position = skeleton->get_bone_pose_position(selected_bone); - bone_original_rotation = skeleton->get_bone_pose_rotation(selected_bone); - bone_original_scale = skeleton->get_bone_pose_scale(selected_bone); -} - -void SkeletonEditor::_hide_handles() { - handles_mesh_instance->hide(); -} - -void SkeletonEditor::_draw_gizmo() { - if (!skeleton) { - return; - } - - // If you call get_bone_global_pose() while drawing the surface, such as toggle rest mode, - // the skeleton update will be done first and - // the drawing surface will be interrupted once and an error will occur. - skeleton->force_update_all_dirty_bones(); - - // Handles. - if (edit_mode) { - _draw_handles(); - } else { - _hide_handles(); - } -} - -void SkeletonEditor::_draw_handles() { - handles_mesh_instance->show(); - - const int bone_len = skeleton->get_bone_count(); - handles_mesh->clear_surfaces(); - handles_mesh->surface_begin(Mesh::PRIMITIVE_POINTS); - - for (int i = 0; i < bone_len; i++) { - Color c; - if (i == selected_bone) { - c = Color(1, 1, 0); - } else { - c = Color(0.1, 0.25, 0.8); - } - Vector3 point = skeleton->get_bone_global_pose(i).origin; - handles_mesh->surface_set_color(c); - handles_mesh->surface_add_vertex(point); - } - handles_mesh->surface_end(); - handles_mesh->surface_set_material(0, handle_material); -} - -TreeItem *SkeletonEditor::_find(TreeItem *p_node, const NodePath &p_path) { - if (!p_node) { - return nullptr; - } - - NodePath np = p_node->get_metadata(0); - if (np == p_path) { - return p_node; - } - - TreeItem *children = p_node->get_children(); - while (children) { - TreeItem *n = _find(children, p_path); - if (n) { - return n; - } - children = children->get_next(); - } - - return nullptr; -} - -void SkeletonEditor::_subgizmo_selection_change() { - if (!skeleton) { - return; - } - - // Once validated by subgizmos_intersect_ray, but required if through inspector's bones tree. - if (!edit_mode) { - skeleton->clear_subgizmo_selection(); - return; - } - - int selected = -1; - SkeletonEditor *se = SkeletonEditor::get_singleton(); - if (se) { - selected = se->get_selected_bone(); - } - - if (selected >= 0) { - Vector> gizmos = skeleton->get_gizmos(); - for (int i = 0; i < gizmos.size(); i++) { - Ref gizmo = gizmos[i]; - if (!gizmo.is_valid()) { - continue; - } - Ref plugin = gizmo->get_plugin(); - if (!plugin.is_valid()) { - continue; - } - skeleton->set_subgizmo_selection(gizmo, selected, skeleton->get_bone_global_pose(selected)); - break; - } - } else { - skeleton->clear_subgizmo_selection(); - } -} - -void SkeletonEditor::select_bone(int p_idx) { - if (p_idx >= 0) { - TreeItem *ti = _find(joint_tree->get_root(), "bones/" + itos(p_idx)); - if (ti) { - // Make visible when it's collapsed. - TreeItem *node = ti->get_parent(); - while (node && node != joint_tree->get_root()) { - node->set_collapsed(false); - node = node->get_parent(); - } - ti->select(0); - joint_tree->scroll_to_item(ti); - } - } else { - selected_bone = -1; - joint_tree->deselect_all(); - _joint_tree_selection_changed(); - } -} - -void SkeletonEditor::add_bone() { - _bone_add_dialog->popup_centered(); - _bone_add_line_edit->set_text(""); - _bone_add_line_edit->grab_focus(); -} -void SkeletonEditor::_add_bone_callback() { - skeleton->add_bone(_bone_add_line_edit->get_text()); - - update_joint_tree(); -} -void SkeletonEditor::remove_bone() { - if (skeleton->get_selected_bone() == -1) { - return; - } - - _bone_remove_dialog->popup_centered(); -} -void SkeletonEditor::_remove_bone_callback() { - if (skeleton->get_selected_bone() == -1) { - return; - } - - skeleton->remove_bone(skeleton->get_selected_bone()); - - update_joint_tree(); -} -void SkeletonEditor::rename_bone() { - if (skeleton->get_selected_bone() == -1) { - return; - } - - _bone_rename_line_edit->set_text(skeleton->get_bone_name(skeleton->get_selected_bone())); - - _bone_rename_dialog->popup_centered(); - _bone_rename_line_edit->grab_focus(); -} - -void SkeletonEditor::_rename_bone_callback() { - if (skeleton->get_selected_bone() == -1) { - return; - } - - skeleton->set_bone_name(skeleton->get_selected_bone(), _bone_rename_line_edit->get_text()); - - update_joint_tree(); -} - -void SkeletonEditor::create_bone_tool_popups() { - _bone_add_dialog = memnew(ConfirmationDialog); - _bone_add_dialog->set_title("Add bone"); - _bone_add_dialog->connect("confirmed", this, "_add_bone_callback"); - - _bone_add_line_edit = memnew(LineEdit); - _bone_add_line_edit->set_placeholder("Name"); - _bone_add_dialog->add_child(_bone_add_line_edit); - - add_child(_bone_add_dialog); - - _bone_rename_dialog = memnew(ConfirmationDialog); - _bone_rename_dialog->set_title("Rename bone"); - _bone_rename_dialog->connect("confirmed", this, "_rename_bone_callback"); - - _bone_rename_line_edit = memnew(LineEdit); - _bone_rename_dialog->add_child(_bone_rename_line_edit); - - add_child(_bone_rename_dialog); - - _bone_remove_dialog = memnew(ConfirmationDialog); - _bone_remove_dialog->set_title("Remove"); - _bone_remove_dialog->connect("confirmed", this, "_remove_bone_callback"); - - Label *rl = memnew(Label); - rl->set_text("Remove Bone?"); - _bone_remove_dialog->add_child(rl); - - add_child(_bone_remove_dialog); -} - -void SkeletonEditor::_bind_tool_popup_methods() { - ClassDB::bind_method(D_METHOD("_add_bone_callback"), &SkeletonEditor::_add_bone_callback); - ClassDB::bind_method(D_METHOD("_remove_bone_callback"), &SkeletonEditor::_remove_bone_callback); - ClassDB::bind_method(D_METHOD("_rename_bone_callback"), &SkeletonEditor::_rename_bone_callback); -} - -void EditorInspectorPluginSkeleton::_bind_methods() { -} - -bool EditorInspectorPluginSkeleton::can_handle(Object *p_object) { - return Object::cast_to(p_object) != nullptr; -} - -void EditorInspectorPluginSkeleton::parse_begin(Object *p_object) { - Skeleton *skeleton = Object::cast_to(p_object); - ERR_FAIL_COND(!skeleton); - - skel_editor = memnew(SkeletonEditor(this, editor, skeleton)); - add_custom_control(skel_editor); -} - -SkeletonEditorPlugin::SkeletonEditorPlugin(EditorNode *p_node) { - editor = p_node; - - skeleton_plugin = memnew(EditorInspectorPluginSkeleton); - skeleton_plugin->editor = editor; - - EditorInspector::add_inspector_plugin(skeleton_plugin); - - Ref gizmo_plugin = Ref(memnew(SkeletonGizmoPlugin)); - SpatialEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin); -} - -EditorPlugin::AfterGUIInput SkeletonEditorPlugin::forward_spatial_gui_input(Camera *p_camera, const Ref &p_event) { - SkeletonEditor *se = SkeletonEditor::get_singleton(); - SpatialEditor *ne = SpatialEditor::get_singleton(); - if (se->is_edit_mode()) { - const Ref mb = p_event; - if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) { - if (ne->get_tool_mode() != SpatialEditor::TOOL_MODE_SELECT) { - if (!ne->is_gizmo_visible()) { - return EditorPlugin::AFTER_GUI_INPUT_STOP; - } - } - if (mb->is_pressed()) { - se->update_bone_original(); - } - } - return EditorPlugin::AFTER_GUI_INPUT_NO_DESELECT; - } - return EditorPlugin::AFTER_GUI_INPUT_PASS; -} - -bool SkeletonEditorPlugin::handles(Object *p_object) const { - return p_object->is_class("Skeleton"); -} - -void SkeletonEditor::_bone_enabled_changed(const int p_bone_id) { - _update_gizmo_visible(); -} - -void SkeletonEditorPlugin::_notification(int p_what) { - if (p_what == NOTIFICATION_ENTER_TREE) { - add_spatial_gizmo_plugin(Ref(memnew(SkeletonGizmoPlugin))); - add_spatial_gizmo_plugin(Ref(memnew(PhysicalBoneSpatialGizmoPlugin))); - } -} - -void SkeletonEditor::_update_gizmo_visible() { - _subgizmo_selection_change(); - if (edit_mode) { - if (selected_bone == -1) { -#ifdef TOOLS_ENABLED - skeleton->set_transform_gizmo_visible(false); -#endif - } else { -#ifdef TOOLS_ENABLED - if (skeleton->is_bone_enabled(selected_bone) && !skeleton->is_show_rest_only()) { - skeleton->set_transform_gizmo_visible(true); - } else { - skeleton->set_transform_gizmo_visible(false); - } -#endif - } - } else { -#ifdef TOOLS_ENABLED - skeleton->set_transform_gizmo_visible(true); -#endif - } - _draw_gizmo(); -} - -int SkeletonEditor::get_selected_bone() const { - return selected_bone; -} - -SkeletonGizmoPlugin::SkeletonGizmoPlugin() { - unselected_mat.instance(); - unselected_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true); - unselected_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true); - unselected_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); - unselected_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true); - - selected_mat = Ref(memnew(ShaderMaterial)); - selected_sh = Ref(memnew(Shader)); - selected_sh->set_code(R"( - // Skeleton 3D gizmo bones shader. - shader_type spatial; - render_mode unshaded, shadows_disabled; - void vertex() { - if (!OUTPUT_IS_SRGB) { - COLOR.rgb = mix( pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb* (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)) ); - } - VERTEX = VERTEX; - POSITION=PROJECTION_MATRIX*INV_CAMERA_MATRIX*WORLD_MATRIX*vec4(VERTEX.xyz,1.0); - POSITION.z = mix(POSITION.z, 0, 0.998); - } - void fragment() { - ALBEDO = COLOR.rgb; - ALPHA = COLOR.a; - } - )"); - selected_mat->set_shader(selected_sh); - - // Register properties in editor settings. - EDITOR_DEF("editors/3d_gizmos/gizmo_colors/skeleton", Color(1, 0.8, 0.4)); - EDITOR_DEF("editors/3d_gizmos/gizmo_colors/selected_bone", Color(0.8, 0.3, 0.0)); - EDITOR_DEF("editors/3d_gizmos/gizmo_settings/bone_axis_length", (float)0.1); - EDITOR_DEF("editors/3d_gizmos/gizmo_settings/bone_shape", 1); - EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d_gizmos/gizmo_settings/bone_shape", PROPERTY_HINT_ENUM, "Wire,Octahedron")); -} - -bool SkeletonGizmoPlugin::has_gizmo(Spatial *p_spatial) { - return Object::cast_to(p_spatial) != nullptr; -} - -String SkeletonGizmoPlugin::get_gizmo_name() const { - return "Skeleton"; -} - -int SkeletonGizmoPlugin::get_priority() const { - return -1; -} - -int SkeletonGizmoPlugin::subgizmos_intersect_ray(const EditorSpatialGizmo *p_gizmo, Camera *p_camera, const Vector2 &p_point) const { - Skeleton *skeleton = Object::cast_to(p_gizmo->get_spatial_node()); - ERR_FAIL_COND_V(!skeleton, -1); - - SkeletonEditor *se = SkeletonEditor::get_singleton(); - - if (!se->is_edit_mode()) { - return -1; - } - - if (SpatialEditor::get_singleton()->get_tool_mode() != SpatialEditor::TOOL_MODE_SELECT) { - return -1; - } - - // Select bone. - real_t grab_threshold = 4 * EDSCALE; - Vector3 ray_from = p_camera->get_global_transform().origin; - Transform gt = skeleton->get_global_transform(); - int closest_idx = -1; - real_t closest_dist = 1e10; - const int bone_len = skeleton->get_bone_count(); - for (int i = 0; i < bone_len; i++) { - Vector3 joint_pos_3d = gt.xform(skeleton->get_bone_global_pose(i).origin); - Vector2 joint_pos_2d = p_camera->unproject_position(joint_pos_3d); - real_t dist_3d = ray_from.distance_to(joint_pos_3d); - real_t dist_2d = p_point.distance_to(joint_pos_2d); - if (dist_2d < grab_threshold && dist_3d < closest_dist) { - closest_dist = dist_3d; - closest_idx = i; - } - } - - if (closest_idx >= 0) { - se->select_bone(closest_idx); - return closest_idx; - } - - se->select_bone(-1); - return -1; -} - -Transform SkeletonGizmoPlugin::get_subgizmo_transform(const EditorSpatialGizmo *p_gizmo, int p_id) const { - Skeleton *skeleton = Object::cast_to(p_gizmo->get_spatial_node()); - ERR_FAIL_COND_V(!skeleton, Transform()); - - return skeleton->get_bone_global_pose(p_id); -} - -void SkeletonGizmoPlugin::set_subgizmo_transform(const EditorSpatialGizmo *p_gizmo, int p_id, Transform p_transform) { - Skeleton *skeleton = Object::cast_to(p_gizmo->get_spatial_node()); - ERR_FAIL_COND(!skeleton); - - // Prepare for global to local. - Transform original_to_local = Transform(); - int parent_idx = skeleton->get_bone_parent(p_id); - if (parent_idx >= 0) { - original_to_local = original_to_local * skeleton->get_bone_global_pose(parent_idx); - } - - Basis to_local = original_to_local.get_basis().inverse(); - - // Prepare transform. - Transform t = Transform(); - - // Basis. - t.basis = to_local * p_transform.get_basis(); - - // Origin. - Vector3 orig = Vector3(); - orig = skeleton->get_bone_pose(p_id).origin; - Vector3 sub = p_transform.origin - skeleton->get_bone_global_pose(p_id).origin; - t.origin = orig + to_local.xform(sub); - - // Apply transform. - skeleton->set_bone_pose_position(p_id, t.origin); - skeleton->set_bone_pose_rotation(p_id, t.basis.get_rotation_quaternion()); - skeleton->set_bone_pose_scale(p_id, t.basis.get_scale()); -} - -void SkeletonGizmoPlugin::commit_subgizmos(const EditorSpatialGizmo *p_gizmo, const Vector &p_ids, const Vector &p_restore, bool p_cancel) { - Skeleton *skeleton = Object::cast_to(p_gizmo->get_spatial_node()); - ERR_FAIL_COND(!skeleton); - - SkeletonEditor *se = SkeletonEditor::get_singleton(); - SpatialEditor *ne = SpatialEditor::get_singleton(); - - UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); - - ur->create_action(TTR("Set Bone Transform")); - if (ne->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || ne->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) { - for (int i = 0; i < p_ids.size(); i++) { - ur->add_do_method(skeleton, "set_bone_pose_position", p_ids[i], skeleton->get_bone_pose_position(p_ids[i])); - ur->add_undo_method(skeleton, "set_bone_pose_position", p_ids[i], se->get_bone_original_position()); - } - } - if (ne->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || ne->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) { - for (int i = 0; i < p_ids.size(); i++) { - ur->add_do_method(skeleton, "set_bone_pose_rotation", p_ids[i], skeleton->get_bone_pose_rotation(p_ids[i])); - ur->add_undo_method(skeleton, "set_bone_pose_rotation", p_ids[i], se->get_bone_original_rotation()); - } - } - if (ne->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) { - for (int i = 0; i < p_ids.size(); i++) { - // If the axis is swapped by scaling, the rotation can be changed. - ur->add_do_method(skeleton, "set_bone_pose_rotation", p_ids[i], skeleton->get_bone_pose_rotation(p_ids[i])); - ur->add_undo_method(skeleton, "set_bone_pose_rotation", p_ids[i], se->get_bone_original_rotation()); - ur->add_do_method(skeleton, "set_bone_pose_scale", p_ids[i], skeleton->get_bone_pose_scale(p_ids[i])); - ur->add_undo_method(skeleton, "set_bone_pose_scale", p_ids[i], se->get_bone_original_scale()); - } - } - ur->commit_action(); -} - -void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) { - Skeleton *skeleton = Object::cast_to(p_gizmo->get_spatial_node()); - p_gizmo->clear(); - - int selected = -1; - SkeletonEditor *se = SkeletonEditor::get_singleton(); - if (se) { - selected = se->get_selected_bone(); - } - - Color bone_color = EditorSettings::get_singleton()->get("editors/3d_gizmos/gizmo_colors/skeleton"); - Color selected_bone_color = EditorSettings::get_singleton()->get("editors/3d_gizmos/gizmo_colors/selected_bone"); - real_t bone_axis_length = EditorSettings::get_singleton()->get("editors/3d_gizmos/gizmo_settings/bone_axis_length"); - int bone_shape = EditorSettings::get_singleton()->get("editors/3d_gizmos/gizmo_settings/bone_shape"); - - LocalVector axis_colors; - axis_colors.push_back(SpatialEditor::get_singleton()->get_theme_color("axis_x_color", "Editor")); - axis_colors.push_back(SpatialEditor::get_singleton()->get_theme_color("axis_y_color", "Editor")); - axis_colors.push_back(SpatialEditor::get_singleton()->get_theme_color("axis_z_color", "Editor")); - - Ref surface_tool(memnew(SurfaceTool)); - surface_tool->begin(Mesh::PRIMITIVE_LINES); - - if (p_gizmo->is_selected()) { - surface_tool->set_material(selected_mat); - } else { - unselected_mat->set_albedo(bone_color); - surface_tool->set_material(unselected_mat); - } - - LocalVector bones; - LocalVector weights; - bones.resize(4); - weights.resize(4); - for (int i = 0; i < 4; i++) { - bones[i] = 0; - weights[i] = 0; - } - weights[0] = 1; - - int current_bone_index = 0; - Vector bones_to_process = skeleton->get_parentless_bones(); - - while (bones_to_process.size() > current_bone_index) { - int current_bone_idx = bones_to_process[current_bone_index]; - current_bone_index++; - - Color current_bone_color = (current_bone_idx == selected) ? selected_bone_color : bone_color; - - Vector child_bones_vector; - child_bones_vector = skeleton->get_bone_children(current_bone_idx); - int child_bones_size = child_bones_vector.size(); - - for (int i = 0; i < child_bones_size; i++) { - // Something wrong. - if (child_bones_vector[i] < 0) { - continue; - } - - int child_bone_idx = child_bones_vector[i]; - - Vector3 v0 = skeleton->get_bone_global_rest(current_bone_idx).origin; - Vector3 v1 = skeleton->get_bone_global_rest(child_bone_idx).origin; - Vector3 d = (v1 - v0).normalized(); - real_t dist = v0.distance_to(v1); - - // Find closest axis. - int closest = -1; - real_t closest_d = 0.0; - for (int j = 0; j < 3; j++) { - real_t dp = Math::abs(skeleton->get_bone_global_rest(current_bone_idx).basis[j].normalized().dot(d)); - if (j == 0 || dp > closest_d) { - closest = j; - } - } - - // Draw bone. - switch (bone_shape) { - case 0: { // Wire shape. - surface_tool->add_color(current_bone_color); - bones[0] = current_bone_idx; - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v0); - bones[0] = child_bone_idx; - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v1); - } break; - - case 1: { // Octahedron shape. - Vector3 first; - Vector3 points[6]; - int point_idx = 0; - for (int j = 0; j < 3; j++) { - Vector3 axis; - if (first == Vector3()) { - axis = d.cross(d.cross(skeleton->get_bone_global_rest(current_bone_idx).basis[j])).normalized(); - first = axis; - } else { - axis = d.cross(first).normalized(); - } - - surface_tool->add_color(current_bone_color); - for (int k = 0; k < 2; k++) { - if (k == 1) { - axis = -axis; - } - Vector3 point = v0 + d * dist * 0.2; - point += axis * dist * 0.1; - - bones[0] = current_bone_idx; - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v0); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(point); - - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(point); - bones[0] = child_bone_idx; - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v1); - points[point_idx++] = point; - } - } - surface_tool->add_color(current_bone_color); - SWAP(points[1], points[2]); - bones[0] = current_bone_idx; - for (int j = 0; j < 6; j++) { - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(points[j]); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(points[(j + 1) % 6]); - } - } break; - } - - // Axis as root of the bone. - for (int j = 0; j < 3; j++) { - bones[0] = current_bone_idx; - surface_tool->add_color(axis_colors[j]); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v0); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v0 + (skeleton->get_bone_global_rest(current_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length); - - if (j == closest) { - continue; - } - } - - // Axis at the end of the bone children. - if (i == child_bones_size - 1) { - for (int j = 0; j < 3; j++) { - bones[0] = child_bone_idx; - surface_tool->add_color(axis_colors[j]); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v1); - surface_tool->add_bones(bones); - surface_tool->add_weights(weights); - surface_tool->add_vertex(v1 + (skeleton->get_bone_global_rest(child_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length); - - if (j == closest) { - continue; - } - } - } - - // Add the bone's children to the list of bones to be processed. - bones_to_process.push_back(child_bones_vector[i]); - } - } - - Ref m = surface_tool->commit(); - p_gizmo->add_mesh(m, Ref(), Transform(), skeleton->register_skin(skeleton->create_skin_from_rest_transforms())); -} diff --git a/modules/skeleton_3d/editor/skeleton_editor_plugin.h b/modules/skeleton_3d/editor/skeleton_editor_plugin.h deleted file mode 100644 index d1d5a83..0000000 --- a/modules/skeleton_3d/editor/skeleton_editor_plugin.h +++ /dev/null @@ -1,315 +0,0 @@ -#ifndef MODULE_SKELETON_EDITOR_PLUGIN_H -#define MODULE_SKELETON_EDITOR_PLUGIN_H -/*************************************************************************/ -/* skeleton_editor_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/input/input_event.h" -#include "editor/editor_inspector.h" -#include "editor/editor_node.h" -#include "editor/editor_plugin.h" -#include "editor/spatial_editor_gizmos.h" -#include "scene/3d/camera.h" -#include "scene/3d/mesh_instance.h" -#include "../nodes/skeleton.h" -#include "scene/resources/mesh/immediate_mesh.h" - -class EditorInspectorPluginSkeleton; -class Joint; -class PhysicalBone; -class SkeletonEditorPlugin; -class Button; -class CheckBox; -class EditorSpinSlider; -class EditorInspectorSection; -class GridContainer; -class Tree; -class Label; -class PopupMenu; -class CheckBox; -class VSeparator; -class EditorPropertyTransform; -class EditorPropertyVector3; -class EditorPropertyCheck; -class EditorPropertyQuaternion; - -class BoneTransformEditor : public VBoxContainer { - GDCLASS(BoneTransformEditor, VBoxContainer); - - EditorInspectorSection *section; - - EditorPropertyCheck *enabled_checkbox = nullptr; - EditorPropertyVector3 *position_property = nullptr; - EditorPropertyQuaternion *rotation_property = nullptr; - EditorPropertyVector3 *scale_property; - - EditorInspectorSection *rest_section = nullptr; - EditorPropertyTransform *rest_matrix = nullptr; - - Rect2 background_rects[5]; - - Skeleton *skeleton; - //String property; - - UndoRedo *undo_redo; - - bool toggle_enabled; - bool updating; - - String label; - - void create_editors(); - - void _value_changed(const String &p_path, const Variant &p_value, const String &p_name = "", bool changing = false); - - void _property_keyed(const String &p_path, bool p_advance); - -protected: - void _notification(int p_what); - static void _bind_methods(); - -public: - BoneTransformEditor(Skeleton *p_skeleton); - - // Which transform target to modify - void set_target(const String &p_prop); - void set_label(const String &p_label) { label = p_label; } - void set_keyable(const bool p_keyable); - - void _update_properties(); -}; - -class SkeletonEditor : public VBoxContainer { - GDCLASS(SkeletonEditor, VBoxContainer); - - friend class SkeletonEditorPlugin; - - enum SkeletonOption { - SKELETON_OPTION_INIT_ALL_POSES, - SKELETON_OPTION_INIT_SELECTED_POSES, - SKELETON_OPTION_ALL_POSES_TO_RESTS, - SKELETON_OPTION_SELECTED_POSES_TO_RESTS, - SKELETON_OPTION_CREATE_PHYSICAL_SKELETON, - SKELETON_OPTION_ADD_BONE, - SKELETON_OPTION_REMOVE_BONE, - SKELETON_OPTION_RENAME_BONE - }; - - struct BoneInfo { - PhysicalBone *physical_bone; - Transform relative_rest; // Relative to skeleton node - - BoneInfo() { - physical_bone = NULL; - } - }; - - EditorNode *editor; - EditorInspectorPluginSkeleton *editor_plugin; - - Skeleton *skeleton; - - Tree *joint_tree; - BoneTransformEditor *rest_editor; - BoneTransformEditor *pose_editor; - - VSeparator *separator; - MenuButton *skeleton_options; - Button *edit_mode_button; - - bool edit_mode; - - HBoxContainer *animation_hb; - Button *key_loc_button; - Button *key_rot_button; - Button *key_scale_button; - Button *key_insert_button; - Button *key_insert_all_button; - - EditorFileDialog *file_dialog; - - bool keyable; - - static SkeletonEditor *singleton; - - void _on_click_skeleton_option(int p_skeleton_option); - void _file_selected(const String &p_file); - TreeItem *_find(TreeItem *p_node, const NodePath &p_path); - void edit_mode_toggled(const bool pressed); - - EditorFileDialog *file_export_lib; - - void update_joint_tree(); - void update_editors(); - - void create_editors(); - - void init_pose(const bool p_all_bones); - void pose_to_rest(const bool p_all_bones); - - void insert_keys(const bool p_all_bones); - - void create_physical_skeleton(); - PhysicalBone *create_physical_bone(int bone_id, int bone_child_id, const Vector &bones_infos); - - Variant get_drag_data_fw(const Point2 &p_point, Control *p_from); - bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; - void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); - - void set_keyable(const bool p_keyable); - void set_bone_options_enabled(const bool p_bone_options_enabled); - - MeshInstance *handles_mesh_instance; - Ref handles_mesh; - Ref handle_material; - Ref handle_shader; - - Vector3 bone_original_position; - Quaternion bone_original_rotation; - Vector3 bone_original_scale; - - void _update_gizmo_visible(); - void _bone_enabled_changed(const int p_bone_id); - - void _hide_handles(); - - void _draw_gizmo(); - void _draw_handles(); - - void _joint_tree_selection_changed(); - void _joint_tree_rmb_select(const Vector2 &p_pos); - void _update_properties(); - - void _subgizmo_selection_change(); - - int selected_bone; - -protected: - void _notification(int p_what); - void _node_removed(Node *p_node); - static void _bind_methods(); - -public: - static SkeletonEditor *get_singleton() { return singleton; } - - void select_bone(int p_idx); - - int get_selected_bone() const; - - void move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx); - - Skeleton *get_skeleton() const { return skeleton; }; - - bool is_edit_mode() const { return edit_mode; } - - void update_bone_original(); - Vector3 get_bone_original_position() const { return bone_original_position; }; - Quaternion get_bone_original_rotation() const { return bone_original_rotation; }; - Vector3 get_bone_original_scale() const { return bone_original_scale; }; - - SkeletonEditor(EditorInspectorPluginSkeleton *e_plugin, EditorNode *p_editor, Skeleton *skeleton); - ~SkeletonEditor(); - - void add_bone(); - void remove_bone(); - void rename_bone(); - - void _add_bone_callback(); - void _remove_bone_callback(); - void _rename_bone_callback(); - - void create_bone_tool_popups(); - static void _bind_tool_popup_methods(); - - ConfirmationDialog *_bone_add_dialog; - LineEdit *_bone_add_line_edit; - ConfirmationDialog *_bone_rename_dialog; - LineEdit *_bone_rename_line_edit; - ConfirmationDialog *_bone_remove_dialog; -}; - -class EditorInspectorPluginSkeleton : public EditorInspectorPlugin { - GDCLASS(EditorInspectorPluginSkeleton, EditorInspectorPlugin); - - friend class SkeletonEditorPlugin; - - SkeletonEditor *skel_editor; - EditorNode *editor; - -protected: - static void _bind_methods(); - -public: - virtual bool can_handle(Object *p_object); - virtual void parse_begin(Object *p_object); -}; - -class SkeletonEditorPlugin : public EditorPlugin { - GDCLASS(SkeletonEditorPlugin, EditorPlugin); - - EditorInspectorPluginSkeleton *skeleton_plugin; - EditorNode *editor; - -public: - virtual EditorPlugin::AfterGUIInput forward_spatial_gui_input(Camera *p_camera, const Ref &p_event); - - bool has_main_screen() const { return false; } - virtual bool handles(Object *p_object) const; - - virtual String get_name() const { return "Skeleton"; } - - SkeletonEditorPlugin(EditorNode *p_node); - -protected: - void _notification(int p_what); -}; - -class SkeletonGizmoPlugin : public EditorSpatialGizmoPlugin { - GDCLASS(SkeletonGizmoPlugin, EditorSpatialGizmoPlugin); - - Ref unselected_mat; - Ref selected_mat; - Ref selected_sh; - -public: - bool has_gizmo(Spatial *p_spatial); - String get_gizmo_name() const; - int get_priority() const; - - int subgizmos_intersect_ray(const EditorSpatialGizmo *p_gizmo, Camera *p_camera, const Vector2 &p_point) const; - Transform get_subgizmo_transform(const EditorSpatialGizmo *p_gizmo, int p_id) const; - void set_subgizmo_transform(const EditorSpatialGizmo *p_gizmo, int p_id, Transform p_transform); - void commit_subgizmos(const EditorSpatialGizmo *p_gizmo, const Vector &p_ids, const Vector &p_restore, bool p_cancel); - - void redraw(EditorSpatialGizmo *p_gizmo); - - SkeletonGizmoPlugin(); -}; - -#endif // SKELETON_EDITOR_PLUGIN_H diff --git a/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.cpp b/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.cpp deleted file mode 100644 index 332de42..0000000 --- a/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/*************************************************************************/ -/* skeleton_ik_editor_plugin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_ik_editor_plugin.h" - -#include "../nodes/skeleton.h" -#include "../nodes/skeleton_ik.h" -#include "core/object/class_db.h" -#include "core/os/memory.h" -#include "editor/editor_node.h" -#include "scene/gui/button.h" -#include "scene/main/control.h" - -void SkeletonIKEditorPlugin::_play() { - if (!skeleton_ik) { - return; - } - - if (!skeleton_ik->get_parent_skeleton()) { - return; - } - - if (play_btn->is_pressed()) { - skeleton_ik->start(); - } else { - skeleton_ik->stop(); - skeleton_ik->get_parent_skeleton()->clear_bones_global_pose_override(); - } -} - -void SkeletonIKEditorPlugin::edit(Object *p_object) { - if (p_object != skeleton_ik) { - if (skeleton_ik) { - play_btn->set_pressed(false); - _play(); - } - } - - SkeletonIK *s = Object::cast_to(p_object); - if (!s) { - return; - } - - skeleton_ik = s; -} - -bool SkeletonIKEditorPlugin::handles(Object *p_object) const { - return p_object->is_class("SkeletonIK"); -} - -void SkeletonIKEditorPlugin::make_visible(bool p_visible) { - if (p_visible) { - play_btn->show(); - } else { - play_btn->hide(); - } -} - -void SkeletonIKEditorPlugin::_bind_methods() { - ClassDB::bind_method("_play", &SkeletonIKEditorPlugin::_play); -} - -SkeletonIKEditorPlugin::SkeletonIKEditorPlugin(EditorNode *p_node) { - editor = p_node; - play_btn = memnew(Button); - play_btn->set_icon(editor->get_gui_base()->get_theme_icon("Play", "EditorIcons")); - play_btn->set_text(TTR("Play IK")); - play_btn->set_toggle_mode(true); - play_btn->hide(); - play_btn->connect("pressed", this, "_play"); - add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, play_btn); - skeleton_ik = nullptr; -} - -SkeletonIKEditorPlugin::~SkeletonIKEditorPlugin() {} diff --git a/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.h b/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.h deleted file mode 100644 index c1123fd..0000000 --- a/modules/skeleton_3d/editor/skeleton_ik_editor_plugin.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef SKELETON_IK_EDITOR_PLUGIN_H -#define SKELETON_IK_EDITOR_PLUGIN_H -/*************************************************************************/ -/* skeleton_ik_editor_plugin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "editor/editor_plugin.h" - -#include "core/object/object.h" -#include "core/string/ustring.h" - -class SkeletonIK; -class Button; -class EditorNode; - -class SkeletonIKEditorPlugin : public EditorPlugin { - GDCLASS(SkeletonIKEditorPlugin, EditorPlugin); - - SkeletonIK *skeleton_ik; - - Button *play_btn; - EditorNode *editor; - - void _play(); - -protected: - static void _bind_methods(); - -public: - virtual String get_name() const { return "SkeletonIK"; } - bool has_main_screen() const { return false; } - virtual void edit(Object *p_object); - virtual bool handles(Object *p_object) const; - virtual void make_visible(bool p_visible); - - SkeletonIKEditorPlugin(EditorNode *p_node); - ~SkeletonIKEditorPlugin(); -}; - -#endif // SKELETON_IK_EDITOR_PLUGIN_H diff --git a/modules/skeleton_3d/icons/icon_editor_bone_handle.svg b/modules/skeleton_3d/icons/icon_editor_bone_handle.svg deleted file mode 100644 index a6d7c3f..0000000 --- a/modules/skeleton_3d/icons/icon_editor_bone_handle.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_physical_bone.svg b/modules/skeleton_3d/icons/icon_physical_bone.svg deleted file mode 100644 index 55df1f1..0000000 --- a/modules/skeleton_3d/icons/icon_physical_bone.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_skeleton.svg b/modules/skeleton_3d/icons/icon_skeleton.svg deleted file mode 100644 index e0780de..0000000 --- a/modules/skeleton_3d/icons/icon_skeleton.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_skeleton_i_k.svg b/modules/skeleton_3d/icons/icon_skeleton_i_k.svg deleted file mode 100644 index f497934..0000000 --- a/modules/skeleton_3d/icons/icon_skeleton_i_k.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_tool_bone_move.svg b/modules/skeleton_3d/icons/icon_tool_bone_move.svg deleted file mode 100644 index 20f0efe..0000000 --- a/modules/skeleton_3d/icons/icon_tool_bone_move.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_tool_bone_rest.svg b/modules/skeleton_3d/icons/icon_tool_bone_rest.svg deleted file mode 100644 index a302d13..0000000 --- a/modules/skeleton_3d/icons/icon_tool_bone_rest.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_tool_bone_rotate.svg b/modules/skeleton_3d/icons/icon_tool_bone_rotate.svg deleted file mode 100644 index b4f2375..0000000 --- a/modules/skeleton_3d/icons/icon_tool_bone_rotate.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_tool_bone_scale.svg b/modules/skeleton_3d/icons/icon_tool_bone_scale.svg deleted file mode 100644 index 95730f9..0000000 --- a/modules/skeleton_3d/icons/icon_tool_bone_scale.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/icons/icon_tool_bone_select.svg b/modules/skeleton_3d/icons/icon_tool_bone_select.svg deleted file mode 100644 index cc12b69..0000000 --- a/modules/skeleton_3d/icons/icon_tool_bone_select.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/skeleton_3d/nodes/bone_attachment.cpp b/modules/skeleton_3d/nodes/bone_attachment.cpp deleted file mode 100644 index 256f538..0000000 --- a/modules/skeleton_3d/nodes/bone_attachment.cpp +++ /dev/null @@ -1,416 +0,0 @@ -/*************************************************************************/ -/* bone_attachment.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "bone_attachment.h" - -void BoneAttachment::set_bone_name(const String &p_name) { - bone_name = p_name; - Skeleton *sk = _get_skeleton(); - if (sk) { - set_bone_idx(sk->find_bone(bone_name)); - } -} - -String BoneAttachment::get_bone_name() const { - return bone_name; -} - -void BoneAttachment::set_bone_idx(const int p_idx) { - if (is_inside_tree()) { - _check_unbind(); - } - - bone_idx = p_idx; - - Skeleton *sk = _get_skeleton(); - - if (sk) { - if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) { - WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!"); - bone_idx = -1; - } else { - bone_name = sk->get_bone_name(bone_idx); - } - } - - if (is_inside_tree()) { - _check_bind(); - } - - property_list_changed_notify(); -} - -int BoneAttachment::get_bone_idx() const { - return bone_idx; -} - -void BoneAttachment::set_override_pose(bool p_override) { - override_pose = p_override; - set_notify_local_transform(override_pose); - set_process_internal(override_pose); - - if (!override_pose) { - Skeleton *sk = _get_skeleton(); - if (sk) { - if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) { - sk->set_bone_global_pose_override(bone_idx, Transform(), 0.0, false); - } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) { - sk->set_bone_local_pose_override(bone_idx, Transform(), 0.0, false); - } - } - _transform_changed(); - } - - property_list_changed_notify(); -} - -bool BoneAttachment::get_override_pose() const { - return override_pose; -} - -void BoneAttachment::set_override_mode(int p_mode) { - if (override_pose) { - Skeleton *sk = _get_skeleton(); - if (sk) { - if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) { - sk->set_bone_global_pose_override(bone_idx, Transform(), 0.0, false); - } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) { - sk->set_bone_local_pose_override(bone_idx, Transform(), 0.0, false); - } - } - - override_mode = p_mode; - _transform_changed(); - return; - } - override_mode = p_mode; -} - -int BoneAttachment::get_override_mode() const { - return override_mode; -} - -void BoneAttachment::set_use_external_skeleton(bool p_use_external) { - use_external_skeleton = p_use_external; - - if (use_external_skeleton) { - _check_unbind(); - _update_external_skeleton_cache(); - _check_bind(); - _transform_changed(); - } - - property_list_changed_notify(); -} - -bool BoneAttachment::get_use_external_skeleton() const { - return use_external_skeleton; -} - -void BoneAttachment::set_external_skeleton(NodePath p_path) { - external_skeleton_node = p_path; - _update_external_skeleton_cache(); - property_list_changed_notify(); -} - -NodePath BoneAttachment::get_external_skeleton() const { - return external_skeleton_node; -} - -void BoneAttachment::on_bone_pose_update(int p_bone_index) { - if (bone_idx == p_bone_index) { - Skeleton *sk = _get_skeleton(); - if (sk) { - if (!override_pose) { - if (use_external_skeleton) { - set_global_transform(sk->global_pose_to_world_transform(sk->get_bone_global_pose(bone_idx))); - } else { - set_transform(sk->get_bone_global_pose(bone_idx)); - } - } else { - if (!_override_dirty) { - _transform_changed(); - _override_dirty = true; - } - } - } - } -} - -String BoneAttachment::get_configuration_warning() const { - String warnings = Spatial::get_configuration_warning(); - - if (use_external_skeleton) { - if (external_skeleton_node_cache == 0) { - warnings += RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."); - } - } else { - Skeleton *parent = Object::cast_to(get_parent()); - if (!parent) { - warnings += RTR("Parent node is not a Skeleton3D node! Please use an external Skeleton3D if you intend to use the BoneAttachment3D without it being a child of a Skeleton3D node."); - } - } - - if (bone_idx == -1) { - warnings += RTR("BoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."); - } - - return warnings; -} - -BoneAttachment::BoneAttachment() { - bound = false; - bone_idx = -1; - override_pose = false; - override_mode = MODE_GLOBAL_POSE; - _override_dirty = false; - use_external_skeleton = false; - external_skeleton_node_cache = 0; - - set_notify_transform(true); -} - -void BoneAttachment::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - if (use_external_skeleton) { - _update_external_skeleton_cache(); - } - _check_bind(); - } break; - case NOTIFICATION_EXIT_TREE: { - _check_unbind(); - } break; - case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { - _transform_changed(); - } break; - case NOTIFICATION_INTERNAL_PROCESS: { - if (_override_dirty) { - _override_dirty = false; - } - } break; - } -} - -void BoneAttachment::_validate_property(PropertyInfo &property) const { - if (property.name == "bone_name") { - // Because it is a constant function, we cannot use the _get_skeleton_3d function. - const Skeleton *parent = nullptr; - if (use_external_skeleton) { - if (external_skeleton_node_cache != 0) { - parent = Object::cast_to(ObjectDB::get_instance(external_skeleton_node_cache)); - } - } else { - parent = Object::cast_to(get_parent()); - } - - if (parent) { - String names; - for (int i = 0; i < parent->get_bone_count(); i++) { - if (i > 0) { - names += ","; - } - names += parent->get_bone_name(i); - } - - property.hint = PROPERTY_HINT_ENUM; - property.hint_string = names; - } else { - property.hint = PROPERTY_HINT_NONE; - property.hint_string = ""; - } - } -} - -bool BoneAttachment::_set(const StringName &p_path, const Variant &p_value) { - if (p_path == "override_pose") { - set_override_pose(p_value); - } else if (p_path == "override_mode") { - set_override_mode(p_value); - } else if (p_path == "use_external_skeleton") { - set_use_external_skeleton(p_value); - } else if (p_path == "external_skeleton") { - set_external_skeleton(p_value); - } - - return true; -} - -bool BoneAttachment::_get(const StringName &p_path, Variant &r_ret) const { - if (p_path == "override_pose") { - r_ret = get_override_pose(); - } else if (p_path == "override_mode") { - r_ret = get_override_mode(); - } else if (p_path == "use_external_skeleton") { - r_ret = get_use_external_skeleton(); - } else if (p_path == "external_skeleton") { - r_ret = get_external_skeleton(); - } - - return true; -} - -void BoneAttachment::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::BOOL, "override_pose", PROPERTY_HINT_NONE, "")); - if (override_pose) { - p_list->push_back(PropertyInfo(Variant::INT, "override_mode", PROPERTY_HINT_ENUM, "Global Pose Override,Local Pose Override,Custom Pose")); - } - - p_list->push_back(PropertyInfo(Variant::BOOL, "use_external_skeleton", PROPERTY_HINT_NONE, "")); - if (use_external_skeleton) { - p_list->push_back(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D")); - } -} - -void BoneAttachment::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment::set_bone_name); - ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment::get_bone_name); - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name"); - - ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &BoneAttachment::set_bone_idx); - ClassDB::bind_method(D_METHOD("get_bone_idx"), &BoneAttachment::get_bone_idx); - ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx"); - - ClassDB::bind_method(D_METHOD("on_bone_pose_update", "bone_index"), &BoneAttachment::on_bone_pose_update); - - ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment::set_override_pose); - ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment::get_override_pose); - ClassDB::bind_method(D_METHOD("set_override_mode", "override_mode"), &BoneAttachment::set_override_mode); - ClassDB::bind_method(D_METHOD("get_override_mode"), &BoneAttachment::get_override_mode); - - ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment::set_use_external_skeleton); - ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment::get_use_external_skeleton); - ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &BoneAttachment::set_external_skeleton); - ClassDB::bind_method(D_METHOD("get_external_skeleton"), &BoneAttachment::get_external_skeleton); -} - -void BoneAttachment::_check_bind() { - Skeleton *sk = _get_skeleton(); - - if (sk && !bound) { - if (bone_idx <= -1) { - bone_idx = sk->find_bone(bone_name); - } - - if (bone_idx != -1) { - //sk->call_deferred("connect", "bone_pose_changed", this, "on_bone_pose_update"); - //call_deferred("on_bone_pose_update", bone_idx); - bound = true; - on_bone_pose_update(bone_idx); - sk->connect("bone_pose_changed", this, "on_bone_pose_update"); - } - } -} - -void BoneAttachment::_check_unbind() { - if (bound) { - Skeleton *sk = _get_skeleton(); - - if (sk) { - if (bone_idx != -1) { - sk->disconnect("bone_pose_changed", this, "on_bone_pose_update"); - } - } - bound = false; - } -} - -void BoneAttachment::_update_external_skeleton_cache() { - external_skeleton_node_cache = 0; - if (has_node(external_skeleton_node)) { - Node *node = get_node(external_skeleton_node); - ERR_FAIL_COND_MSG(!node, "Cannot update external skeleton cache: Node cannot be found!"); - - // Make sure it's a skeleton3D - Skeleton *sk = Object::cast_to(node); - ERR_FAIL_COND_MSG(!sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!"); - - external_skeleton_node_cache = node->get_instance_id(); - } else { - if (external_skeleton_node.is_empty()) { - BoneAttachment *parent_attachment = Object::cast_to(get_parent()); - if (parent_attachment) { - parent_attachment->_update_external_skeleton_cache(); - if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) { - Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node); - ERR_FAIL_COND_MSG(!node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!"); - - // Make sure it's a skeleton3D - Skeleton *sk = Object::cast_to(node); - ERR_FAIL_COND_MSG(!sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!"); - - external_skeleton_node_cache = node->get_instance_id(); - external_skeleton_node = get_path_to(node); - } - } - } - } -} - -Skeleton *BoneAttachment::_get_skeleton() { - if (use_external_skeleton) { - if (external_skeleton_node_cache != 0) { - return Object::cast_to(ObjectDB::get_instance(external_skeleton_node_cache)); - } else { - _update_external_skeleton_cache(); - if (external_skeleton_node_cache != 0) { - return Object::cast_to(ObjectDB::get_instance(external_skeleton_node_cache)); - } - } - } else { - return Object::cast_to(get_parent()); - } - - return nullptr; -} - -void BoneAttachment::_transform_changed() { - if (!is_inside_tree()) { - return; - } - - if (override_pose) { - Skeleton *sk = _get_skeleton(); - - ERR_FAIL_COND_MSG(!sk, "Cannot override pose: Skeleton not found!"); - ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!"); - - Transform our_trans = get_transform(); - if (use_external_skeleton) { - our_trans = sk->world_transform_to_global_pose(get_global_transform()); - } - - if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) { - sk->set_bone_global_pose_override(bone_idx, our_trans, 1.0, true); - } else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) { - sk->set_bone_local_pose_override(bone_idx, sk->global_pose_to_local_pose(bone_idx, our_trans), 1.0, true); - } - } -} diff --git a/modules/skeleton_3d/nodes/bone_attachment.h b/modules/skeleton_3d/nodes/bone_attachment.h deleted file mode 100644 index df06a0c..0000000 --- a/modules/skeleton_3d/nodes/bone_attachment.h +++ /dev/null @@ -1,100 +0,0 @@ -#ifndef BONE_ATTACHMENT_H -#define BONE_ATTACHMENT_H -/*************************************************************************/ -/* bone_attachment.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton.h" - -class BoneAttachment : public Spatial { - GDCLASS(BoneAttachment, Spatial); - -public: - void set_bone_name(const String &p_name); - String get_bone_name() const; - - void set_bone_idx(const int p_idx); - int get_bone_idx() const; - - void set_override_pose(bool p_override); - bool get_override_pose() const; - - void set_override_mode(int p_mode); - int get_override_mode() const; - - void set_use_external_skeleton(bool p_external_skeleton); - bool get_use_external_skeleton() const; - - void set_external_skeleton(NodePath p_skeleton); - NodePath get_external_skeleton() const; - - virtual void on_bone_pose_update(int p_bone_index); - - String get_configuration_warning() const; - - BoneAttachment(); - -protected: - void _notification(int p_what); - - void _validate_property(PropertyInfo &property) const; - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - - static void _bind_methods(); - -private: - void _check_bind(); - void _check_unbind(); - - void _transform_changed(); - void _update_external_skeleton_cache(); - Skeleton *_get_skeleton(); - -private: - enum OVERRIDE_MODES { - MODE_GLOBAL_POSE, - MODE_LOCAL_POSE, - }; - - bool bound; - String bone_name; - int bone_idx; - - bool override_pose; - int override_mode; - bool _override_dirty; - - bool use_external_skeleton = false; - NodePath external_skeleton_node; - ObjectID external_skeleton_node_cache; -}; - -#endif // BONE_ATTACHMENT_H diff --git a/modules/skeleton_3d/nodes/physical_bone.cpp b/modules/skeleton_3d/nodes/physical_bone.cpp deleted file mode 100644 index 77161ef..0000000 --- a/modules/skeleton_3d/nodes/physical_bone.cpp +++ /dev/null @@ -1,1242 +0,0 @@ -/*************************************************************************/ -/* physics_body.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "physical_bone.h" - -#include "core/core_string_names.h" -#include "core/config/engine.h" -#include "core/containers/list.h" -#include "core/object/method_bind_ext.gen.inc" -#include "core/object/object.h" -#include "core/config/project_settings.h" -#include "core/containers/rid.h" -#include "scene/resources/physics_material.h" -#include "scene/main/scene_string_names.h" -#include "skeleton.h" - -#ifdef TOOLS_ENABLED -#include "editor/plugins/spatial_editor_plugin.h" -#endif - -bool PhysicalBone::JointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - return false; -} - -bool PhysicalBone::JointData::_get(const StringName &p_name, Variant &r_ret) const { - return false; -} - -void PhysicalBone::JointData::_get_property_list(List *p_list) const { -} - -void PhysicalBone::apply_central_impulse(const Vector3 &p_impulse) { - PhysicsServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse); -} - -void PhysicalBone::apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse) { - PhysicsServer::get_singleton()->body_apply_impulse(get_rid(), p_pos, p_impulse); -} - -void PhysicalBone::reset_physics_simulation_state() { - if (simulate_physics) { - _start_physics_simulation(); - } else { - _stop_physics_simulation(); - } -} - -void PhysicalBone::reset_to_rest_position() { - if (parent_skeleton) { - if (-1 == bone_id) { - set_global_transform(parent_skeleton->get_global_transform() * body_offset); - } else { - set_global_transform(parent_skeleton->get_global_transform() * parent_skeleton->get_bone_global_pose(bone_id) * body_offset); - } - } -} - -bool PhysicalBone::PinJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - if (JointData::_set(p_name, p_value, j)) { - return true; - } - - if ("joint_constraints/bias" == p_name) { - bias = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_BIAS, bias); - } - - } else if ("joint_constraints/damping" == p_name) { - damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_DAMPING, damping); - } - - } else if ("joint_constraints/impulse_clamp" == p_name) { - impulse_clamp = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->pin_joint_set_param(j, PhysicsServer::PIN_JOINT_IMPULSE_CLAMP, impulse_clamp); - } - - } else { - return false; - } - - return true; -} - -bool PhysicalBone::PinJointData::_get(const StringName &p_name, Variant &r_ret) const { - if (JointData::_get(p_name, r_ret)) { - return true; - } - - if ("joint_constraints/bias" == p_name) { - r_ret = bias; - } else if ("joint_constraints/damping" == p_name) { - r_ret = damping; - } else if ("joint_constraints/impulse_clamp" == p_name) { - r_ret = impulse_clamp; - } else { - return false; - } - - return true; -} - -void PhysicalBone::PinJointData::_get_property_list(List *p_list) const { - JointData::_get_property_list(p_list); - - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/damping", PROPERTY_HINT_RANGE, "0.01,8.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/impulse_clamp", PROPERTY_HINT_RANGE, "0.0,64.0,0.01")); -} - -bool PhysicalBone::ConeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - if (JointData::_set(p_name, p_value, j)) { - return true; - } - - if ("joint_constraints/swing_span" == p_name) { - swing_span = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN, swing_span); - } - - } else if ("joint_constraints/twist_span" == p_name) { - twist_span = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN, twist_span); - } - - } else if ("joint_constraints/bias" == p_name) { - bias = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_BIAS, bias); - } - - } else if ("joint_constraints/softness" == p_name) { - softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_SOFTNESS, softness); - } - - } else if ("joint_constraints/relaxation" == p_name) { - relaxation = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->cone_twist_joint_set_param(j, PhysicsServer::CONE_TWIST_JOINT_RELAXATION, relaxation); - } - - } else { - return false; - } - - return true; -} - -bool PhysicalBone::ConeJointData::_get(const StringName &p_name, Variant &r_ret) const { - if (JointData::_get(p_name, r_ret)) { - return true; - } - - if ("joint_constraints/swing_span" == p_name) { - r_ret = Math::rad2deg(swing_span); - } else if ("joint_constraints/twist_span" == p_name) { - r_ret = Math::rad2deg(twist_span); - } else if ("joint_constraints/bias" == p_name) { - r_ret = bias; - } else if ("joint_constraints/softness" == p_name) { - r_ret = softness; - } else if ("joint_constraints/relaxation" == p_name) { - r_ret = relaxation; - } else { - return false; - } - - return true; -} - -void PhysicalBone::ConeJointData::_get_property_list(List *p_list) const { - JointData::_get_property_list(p_list); - - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/swing_span", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/twist_span", PROPERTY_HINT_RANGE, "-40000,40000,0.1,or_lesser,or_greater")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/bias", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/relaxation", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); -} - -bool PhysicalBone::HingeJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - if (JointData::_set(p_name, p_value, j)) { - return true; - } - - if ("joint_constraints/angular_limit_enabled" == p_name) { - angular_limit_enabled = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_flag(j, PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT, angular_limit_enabled); - } - - } else if ("joint_constraints/angular_limit_upper" == p_name) { - angular_limit_upper = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_UPPER, angular_limit_upper); - } - - } else if ("joint_constraints/angular_limit_lower" == p_name) { - angular_limit_lower = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_LOWER, angular_limit_lower); - } - - } else if ("joint_constraints/angular_limit_bias" == p_name) { - angular_limit_bias = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_BIAS, angular_limit_bias); - } - - } else if ("joint_constraints/angular_limit_softness" == p_name) { - angular_limit_softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS, angular_limit_softness); - } - - } else if ("joint_constraints/angular_limit_relaxation" == p_name) { - angular_limit_relaxation = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->hinge_joint_set_param(j, PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION, angular_limit_relaxation); - } - - } else { - return false; - } - - return true; -} - -bool PhysicalBone::HingeJointData::_get(const StringName &p_name, Variant &r_ret) const { - if (JointData::_get(p_name, r_ret)) { - return true; - } - - if ("joint_constraints/angular_limit_enabled" == p_name) { - r_ret = angular_limit_enabled; - } else if ("joint_constraints/angular_limit_upper" == p_name) { - r_ret = Math::rad2deg(angular_limit_upper); - } else if ("joint_constraints/angular_limit_lower" == p_name) { - r_ret = Math::rad2deg(angular_limit_lower); - } else if ("joint_constraints/angular_limit_bias" == p_name) { - r_ret = angular_limit_bias; - } else if ("joint_constraints/angular_limit_softness" == p_name) { - r_ret = angular_limit_softness; - } else if ("joint_constraints/angular_limit_relaxation" == p_name) { - r_ret = angular_limit_relaxation; - } else { - return false; - } - - return true; -} - -void PhysicalBone::HingeJointData::_get_property_list(List *p_list) const { - JointData::_get_property_list(p_list); - - p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/angular_limit_enabled")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_bias", PROPERTY_HINT_RANGE, "0.01,0.99,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_relaxation", PROPERTY_HINT_RANGE, "0.01,16,0.01")); -} - -bool PhysicalBone::SliderJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - if (JointData::_set(p_name, p_value, j)) { - return true; - } - - if ("joint_constraints/linear_limit_upper" == p_name) { - linear_limit_upper = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER, linear_limit_upper); - } - - } else if ("joint_constraints/linear_limit_lower" == p_name) { - linear_limit_lower = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER, linear_limit_lower); - } - - } else if ("joint_constraints/linear_limit_softness" == p_name) { - linear_limit_softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, linear_limit_softness); - } - - } else if ("joint_constraints/linear_limit_restitution" == p_name) { - linear_limit_restitution = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, linear_limit_restitution); - } - - } else if ("joint_constraints/linear_limit_damping" == p_name) { - linear_limit_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, linear_limit_restitution); - } - - } else if ("joint_constraints/angular_limit_upper" == p_name) { - angular_limit_upper = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, angular_limit_upper); - } - - } else if ("joint_constraints/angular_limit_lower" == p_name) { - angular_limit_lower = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, angular_limit_lower); - } - - } else if ("joint_constraints/angular_limit_softness" == p_name) { - angular_limit_softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness); - } - - } else if ("joint_constraints/angular_limit_restitution" == p_name) { - angular_limit_restitution = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, angular_limit_softness); - } - - } else if ("joint_constraints/angular_limit_damping" == p_name) { - angular_limit_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->slider_joint_set_param(j, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, angular_limit_damping); - } - - } else { - return false; - } - - return true; -} - -bool PhysicalBone::SliderJointData::_get(const StringName &p_name, Variant &r_ret) const { - if (JointData::_get(p_name, r_ret)) { - return true; - } - - if ("joint_constraints/linear_limit_upper" == p_name) { - r_ret = linear_limit_upper; - } else if ("joint_constraints/linear_limit_lower" == p_name) { - r_ret = linear_limit_lower; - } else if ("joint_constraints/linear_limit_softness" == p_name) { - r_ret = linear_limit_softness; - } else if ("joint_constraints/linear_limit_restitution" == p_name) { - r_ret = linear_limit_restitution; - } else if ("joint_constraints/linear_limit_damping" == p_name) { - r_ret = linear_limit_damping; - } else if ("joint_constraints/angular_limit_upper" == p_name) { - r_ret = Math::rad2deg(angular_limit_upper); - } else if ("joint_constraints/angular_limit_lower" == p_name) { - r_ret = Math::rad2deg(angular_limit_lower); - } else if ("joint_constraints/angular_limit_softness" == p_name) { - r_ret = angular_limit_softness; - } else if ("joint_constraints/angular_limit_restitution" == p_name) { - r_ret = angular_limit_restitution; - } else if ("joint_constraints/angular_limit_damping" == p_name) { - r_ret = angular_limit_damping; - } else { - return false; - } - - return true; -} - -void PhysicalBone::SliderJointData::_get_property_list(List *p_list) const { - JointData::_get_property_list(p_list); - - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_upper")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_lower")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/linear_limit_damping", PROPERTY_HINT_RANGE, "0,16.0,0.01")); - - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_restitution", PROPERTY_HINT_RANGE, "0.01,16.0,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/angular_limit_damping", PROPERTY_HINT_RANGE, "0,16.0,0.01")); -} - -bool PhysicalBone::SixDOFJointData::_set(const StringName &p_name, const Variant &p_value, RID j) { - if (JointData::_set(p_name, p_value, j)) { - return true; - } - - String path = p_name; - - if (!path.begins_with("joint_constraints/")) { - return false; - } - - Vector3::Axis axis; - { - const String axis_s = path.get_slicec('/', 1); - if ("x" == axis_s) { - axis = Vector3::AXIS_X; - } else if ("y" == axis_s) { - axis = Vector3::AXIS_Y; - } else if ("z" == axis_s) { - axis = Vector3::AXIS_Z; - } else { - return false; - } - } - - String var_name = path.get_slicec('/', 2); - - if ("linear_limit_enabled" == var_name) { - axis_data[axis].linear_limit_enabled = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, axis_data[axis].linear_limit_enabled); - } - - } else if ("linear_limit_upper" == var_name) { - axis_data[axis].linear_limit_upper = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT, axis_data[axis].linear_limit_upper); - } - - } else if ("linear_limit_lower" == var_name) { - axis_data[axis].linear_limit_lower = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT, axis_data[axis].linear_limit_lower); - } - - } else if ("linear_limit_softness" == var_name) { - axis_data[axis].linear_limit_softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, axis_data[axis].linear_limit_softness); - } - - } else if ("linear_spring_enabled" == var_name) { - axis_data[axis].linear_spring_enabled = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, axis_data[axis].linear_spring_enabled); - } - - } else if ("linear_spring_stiffness" == var_name) { - axis_data[axis].linear_spring_stiffness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, axis_data[axis].linear_spring_stiffness); - } - - } else if ("linear_spring_damping" == var_name) { - axis_data[axis].linear_spring_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING, axis_data[axis].linear_spring_damping); - } - - } else if ("linear_equilibrium_point" == var_name) { - axis_data[axis].linear_equilibrium_point = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].linear_equilibrium_point); - } - - } else if ("linear_restitution" == var_name) { - axis_data[axis].linear_restitution = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION, axis_data[axis].linear_restitution); - } - - } else if ("linear_damping" == var_name) { - axis_data[axis].linear_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING, axis_data[axis].linear_damping); - } - - } else if ("angular_limit_enabled" == var_name) { - axis_data[axis].angular_limit_enabled = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, axis_data[axis].angular_limit_enabled); - } - - } else if ("angular_limit_upper" == var_name) { - axis_data[axis].angular_limit_upper = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, axis_data[axis].angular_limit_upper); - } - - } else if ("angular_limit_lower" == var_name) { - axis_data[axis].angular_limit_lower = Math::deg2rad(real_t(p_value)); - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, axis_data[axis].angular_limit_lower); - } - - } else if ("angular_limit_softness" == var_name) { - axis_data[axis].angular_limit_softness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, axis_data[axis].angular_limit_softness); - } - - } else if ("angular_restitution" == var_name) { - axis_data[axis].angular_restitution = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION, axis_data[axis].angular_restitution); - } - - } else if ("angular_damping" == var_name) { - axis_data[axis].angular_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING, axis_data[axis].angular_damping); - } - - } else if ("erp" == var_name) { - axis_data[axis].erp = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_ERP, axis_data[axis].erp); - } - - } else if ("angular_spring_enabled" == var_name) { - axis_data[axis].angular_spring_enabled = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(j, axis, PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, axis_data[axis].angular_spring_enabled); - } - - } else if ("angular_spring_stiffness" == var_name) { - axis_data[axis].angular_spring_stiffness = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, axis_data[axis].angular_spring_stiffness); - } - - } else if ("angular_spring_damping" == var_name) { - axis_data[axis].angular_spring_damping = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, axis_data[axis].angular_spring_damping); - } - - } else if ("angular_equilibrium_point" == var_name) { - axis_data[axis].angular_equilibrium_point = p_value; - if (j.is_valid()) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(j, axis, PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, axis_data[axis].angular_equilibrium_point); - } - - } else { - return false; - } - - return true; -} - -bool PhysicalBone::SixDOFJointData::_get(const StringName &p_name, Variant &r_ret) const { - if (JointData::_get(p_name, r_ret)) { - return true; - } - - String path = p_name; - - if (!path.begins_with("joint_constraints/")) { - return false; - } - - int axis; - { - const String axis_s = path.get_slicec('/', 1); - if ("x" == axis_s) { - axis = 0; - } else if ("y" == axis_s) { - axis = 1; - } else if ("z" == axis_s) { - axis = 2; - } else { - return false; - } - } - - String var_name = path.get_slicec('/', 2); - - if ("linear_limit_enabled" == var_name) { - r_ret = axis_data[axis].linear_limit_enabled; - } else if ("linear_limit_upper" == var_name) { - r_ret = axis_data[axis].linear_limit_upper; - } else if ("linear_limit_lower" == var_name) { - r_ret = axis_data[axis].linear_limit_lower; - } else if ("linear_limit_softness" == var_name) { - r_ret = axis_data[axis].linear_limit_softness; - } else if ("linear_spring_enabled" == var_name) { - r_ret = axis_data[axis].linear_spring_enabled; - } else if ("linear_spring_stiffness" == var_name) { - r_ret = axis_data[axis].linear_spring_stiffness; - } else if ("linear_spring_damping" == var_name) { - r_ret = axis_data[axis].linear_spring_damping; - } else if ("linear_equilibrium_point" == var_name) { - r_ret = axis_data[axis].linear_equilibrium_point; - } else if ("linear_restitution" == var_name) { - r_ret = axis_data[axis].linear_restitution; - } else if ("linear_damping" == var_name) { - r_ret = axis_data[axis].linear_damping; - } else if ("angular_limit_enabled" == var_name) { - r_ret = axis_data[axis].angular_limit_enabled; - } else if ("angular_limit_upper" == var_name) { - r_ret = Math::rad2deg(axis_data[axis].angular_limit_upper); - } else if ("angular_limit_lower" == var_name) { - r_ret = Math::rad2deg(axis_data[axis].angular_limit_lower); - } else if ("angular_limit_softness" == var_name) { - r_ret = axis_data[axis].angular_limit_softness; - } else if ("angular_restitution" == var_name) { - r_ret = axis_data[axis].angular_restitution; - } else if ("angular_damping" == var_name) { - r_ret = axis_data[axis].angular_damping; - } else if ("erp" == var_name) { - r_ret = axis_data[axis].erp; - } else if ("angular_spring_enabled" == var_name) { - r_ret = axis_data[axis].angular_spring_enabled; - } else if ("angular_spring_stiffness" == var_name) { - r_ret = axis_data[axis].angular_spring_stiffness; - } else if ("angular_spring_damping" == var_name) { - r_ret = axis_data[axis].angular_spring_damping; - } else if ("angular_equilibrium_point" == var_name) { - r_ret = axis_data[axis].angular_equilibrium_point; - } else { - return false; - } - - return true; -} - -void PhysicalBone::SixDOFJointData::_get_property_list(List *p_list) const { - const StringName axis_names[] = { "x", "y", "z" }; - for (int i = 0; i < 3; ++i) { - p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/linear_limit_enabled")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_upper")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_lower")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/linear_spring_enabled")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_spring_stiffness")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_spring_damping")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_equilibrium_point")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/linear_damping", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/angular_limit_enabled")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_limit_softness", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_restitution", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_damping", PROPERTY_HINT_RANGE, "0.01,16,0.01")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/erp")); - p_list->push_back(PropertyInfo(Variant::BOOL, "joint_constraints/" + axis_names[i] + "/angular_spring_enabled")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_spring_stiffness")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_spring_damping")); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_constraints/" + axis_names[i] + "/angular_equilibrium_point")); - } -} - -bool PhysicalBone::_set(const StringName &p_name, const Variant &p_value) { - if (p_name == "bone_name") { - set_bone_name(p_value); - return true; - } - - if (joint_data) { - if (joint_data->_set(p_name, p_value, joint)) { -#ifdef TOOLS_ENABLED - update_gizmos(); -#endif - return true; - } - } - - return false; -} - -bool PhysicalBone::_get(const StringName &p_name, Variant &r_ret) const { - if (p_name == "bone_name") { - r_ret = get_bone_name(); - return true; - } - - if (joint_data) { - return joint_data->_get(p_name, r_ret); - } - - return false; -} - -void PhysicalBone::_get_property_list(List *p_list) const { - Skeleton *parent = find_skeleton_parent(get_parent()); - - if (parent) { - String names; - for (int i = 0; i < parent->get_bone_count(); i++) { - if (i > 0) { - names += ","; - } - names += parent->get_bone_name(i); - } - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, "bone_name", PROPERTY_HINT_ENUM, names)); - } else { - p_list->push_back(PropertyInfo(Variant::STRING_NAME, "bone_name")); - } - - if (joint_data) { - joint_data->_get_property_list(p_list); - } -} - -void PhysicalBone::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: - parent_skeleton = find_skeleton_parent(get_parent()); - update_bone_id(); - reset_to_rest_position(); - reset_physics_simulation_state(); - if (!joint.is_valid() && joint_data) { - _reload_joint(); - } - break; - case NOTIFICATION_EXIT_TREE: - if (parent_skeleton) { - if (-1 != bone_id) { - parent_skeleton->unbind_physical_bone_from_bone(bone_id); - bone_id = -1; - } - } - parent_skeleton = nullptr; - if (joint.is_valid()) { - PhysicsServer::get_singleton()->free(joint); - joint = RID(); - } - break; - case NOTIFICATION_TRANSFORM_CHANGED: - if (Engine::get_singleton()->is_editor_hint()) { - update_offset(); - } - break; - } -} - -void PhysicalBone::_direct_state_changed(Object *p_state) { - if (!simulate_physics || !_internal_simulate_physics) { - return; - } - - /// Update bone transform - - PhysicsDirectBodyState *state = Object::cast_to(p_state); - ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid PhysicsDirectBodyState object as argument"); - - Transform global_transform(state->get_transform()); - - set_ignore_transform_notification(true); - set_global_transform(global_transform); - set_ignore_transform_notification(false); - _on_transform_changed(); - - // Update skeleton - if (parent_skeleton) { - if (-1 != bone_id) { - parent_skeleton->set_bone_global_pose_override(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse), 1.0, true); - } - } -} - -void PhysicalBone::_bind_methods() { - ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicalBone::apply_central_impulse); - ClassDB::bind_method(D_METHOD("apply_impulse", "position", "impulse"), &PhysicalBone::apply_impulse); - - ClassDB::bind_method(D_METHOD("_direct_state_changed"), &PhysicalBone::_direct_state_changed); - - ClassDB::bind_method(D_METHOD("set_joint_type", "joint_type"), &PhysicalBone::set_joint_type); - ClassDB::bind_method(D_METHOD("get_joint_type"), &PhysicalBone::get_joint_type); - - ClassDB::bind_method(D_METHOD("set_joint_offset", "offset"), &PhysicalBone::set_joint_offset); - ClassDB::bind_method(D_METHOD("get_joint_offset"), &PhysicalBone::get_joint_offset); - - ClassDB::bind_method(D_METHOD("set_body_offset", "offset"), &PhysicalBone::set_body_offset); - ClassDB::bind_method(D_METHOD("get_body_offset"), &PhysicalBone::get_body_offset); - - ClassDB::bind_method(D_METHOD("get_simulate_physics"), &PhysicalBone::get_simulate_physics); - - ClassDB::bind_method(D_METHOD("is_simulating_physics"), &PhysicalBone::is_simulating_physics); - - ClassDB::bind_method(D_METHOD("get_bone_id"), &PhysicalBone::get_bone_id); - - ClassDB::bind_method(D_METHOD("set_mass", "mass"), &PhysicalBone::set_mass); - ClassDB::bind_method(D_METHOD("get_mass"), &PhysicalBone::get_mass); - - ClassDB::bind_method(D_METHOD("set_weight", "weight"), &PhysicalBone::set_weight); - ClassDB::bind_method(D_METHOD("get_weight"), &PhysicalBone::get_weight); - - ClassDB::bind_method(D_METHOD("set_friction", "friction"), &PhysicalBone::set_friction); - ClassDB::bind_method(D_METHOD("get_friction"), &PhysicalBone::get_friction); - - ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &PhysicalBone::set_bounce); - ClassDB::bind_method(D_METHOD("get_bounce"), &PhysicalBone::get_bounce); - - ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &PhysicalBone::set_gravity_scale); - ClassDB::bind_method(D_METHOD("get_gravity_scale"), &PhysicalBone::get_gravity_scale); - - ADD_GROUP("Joint", "joint_"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "joint_type", PROPERTY_HINT_ENUM, "None,PinJoint,ConeJoint,HingeJoint,SliderJoint,6DOFJoint"), "set_joint_type", "get_joint_type"); - ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "joint_offset"), "set_joint_offset", "get_joint_offset"); - - ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "body_offset"), "set_body_offset", "get_body_offset"); - - ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_weight", "get_weight"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_friction", "get_friction"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_bounce", "get_bounce"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-10,10,0.01"), "set_gravity_scale", "get_gravity_scale"); - - BIND_ENUM_CONSTANT(JOINT_TYPE_NONE); - BIND_ENUM_CONSTANT(JOINT_TYPE_PIN); - BIND_ENUM_CONSTANT(JOINT_TYPE_CONE); - BIND_ENUM_CONSTANT(JOINT_TYPE_HINGE); - BIND_ENUM_CONSTANT(JOINT_TYPE_SLIDER); - BIND_ENUM_CONSTANT(JOINT_TYPE_6DOF); -} - -Skeleton *PhysicalBone::find_skeleton_parent(Node *p_parent) { - if (!p_parent) { - return nullptr; - } - Skeleton *s = Object::cast_to(p_parent); - return s ? s : find_skeleton_parent(p_parent->get_parent()); -} - -void PhysicalBone::_fix_joint_offset() { - // Clamp joint origin to bone origin - if (parent_skeleton) { - joint_offset.origin = body_offset.affine_inverse().origin; - } -} - -void PhysicalBone::_reload_joint() { - if (joint.is_valid()) { - PhysicsServer::get_singleton()->free(joint); - joint = RID(); - } - - if (!parent_skeleton) { - return; - } - - PhysicalBone *body_a = parent_skeleton->get_physical_bone_parent(bone_id); - if (!body_a) { - return; - } - - Transform joint_transf = get_global_transform() * joint_offset; - Transform local_a = body_a->get_global_transform().affine_inverse() * joint_transf; - local_a.orthonormalize(); - - switch (get_joint_type()) { - case JOINT_TYPE_PIN: { - joint = RID_PRIME(PhysicsServer::get_singleton()->joint_create_pin(body_a->get_rid(), local_a.origin, get_rid(), joint_offset.origin)); - const PinJointData *pjd(static_cast(joint_data)); - PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_BIAS, pjd->bias); - PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_DAMPING, pjd->damping); - PhysicsServer::get_singleton()->pin_joint_set_param(joint, PhysicsServer::PIN_JOINT_IMPULSE_CLAMP, pjd->impulse_clamp); - - } break; - case JOINT_TYPE_CONE: { - joint = RID_PRIME(PhysicsServer::get_singleton()->joint_create_cone_twist(body_a->get_rid(), local_a, get_rid(), joint_offset)); - const ConeJointData *cjd(static_cast(joint_data)); - PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN, cjd->swing_span); - PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN, cjd->twist_span); - PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_BIAS, cjd->bias); - PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_SOFTNESS, cjd->softness); - PhysicsServer::get_singleton()->cone_twist_joint_set_param(joint, PhysicsServer::CONE_TWIST_JOINT_RELAXATION, cjd->relaxation); - - } break; - case JOINT_TYPE_HINGE: { - joint = RID_PRIME(PhysicsServer::get_singleton()->joint_create_hinge(body_a->get_rid(), local_a, get_rid(), joint_offset)); - const HingeJointData *hjd(static_cast(joint_data)); - PhysicsServer::get_singleton()->hinge_joint_set_flag(joint, PhysicsServer::HINGE_JOINT_FLAG_USE_LIMIT, hjd->angular_limit_enabled); - PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_UPPER, hjd->angular_limit_upper); - PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_LOWER, hjd->angular_limit_lower); - PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_BIAS, hjd->angular_limit_bias); - PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_SOFTNESS, hjd->angular_limit_softness); - PhysicsServer::get_singleton()->hinge_joint_set_param(joint, PhysicsServer::HINGE_JOINT_LIMIT_RELAXATION, hjd->angular_limit_relaxation); - - } break; - case JOINT_TYPE_SLIDER: { - joint = RID_PRIME(PhysicsServer::get_singleton()->joint_create_slider(body_a->get_rid(), local_a, get_rid(), joint_offset)); - const SliderJointData *sjd(static_cast(joint_data)); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_UPPER, sjd->linear_limit_upper); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_LOWER, sjd->linear_limit_lower); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS, sjd->linear_limit_softness); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION, sjd->linear_limit_restitution); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_LINEAR_LIMIT_DAMPING, sjd->linear_limit_restitution); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_UPPER, sjd->angular_limit_upper); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_LOWER, sjd->angular_limit_lower); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS, sjd->angular_limit_softness); - PhysicsServer::get_singleton()->slider_joint_set_param(joint, PhysicsServer::SLIDER_JOINT_ANGULAR_LIMIT_DAMPING, sjd->angular_limit_damping); - - } break; - case JOINT_TYPE_6DOF: { - joint = RID_PRIME(PhysicsServer::get_singleton()->joint_create_generic_6dof(body_a->get_rid(), local_a, get_rid(), joint_offset)); - const SixDOFJointData *g6dofjd(static_cast(joint_data)); - for (int axis = 0; axis < 3; ++axis) { - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT, g6dofjd->axis_data[axis].linear_limit_enabled); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_UPPER_LIMIT, g6dofjd->axis_data[axis].linear_limit_upper); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_LOWER_LIMIT, g6dofjd->axis_data[axis].linear_limit_lower); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].linear_limit_softness); - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING, g6dofjd->axis_data[axis].linear_spring_enabled); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].linear_spring_stiffness); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_DAMPING, g6dofjd->axis_data[axis].linear_spring_damping); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].linear_equilibrium_point); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_RESTITUTION, g6dofjd->axis_data[axis].linear_restitution); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_LINEAR_DAMPING, g6dofjd->axis_data[axis].linear_damping); - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT, g6dofjd->axis_data[axis].angular_limit_enabled); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_UPPER_LIMIT, g6dofjd->axis_data[axis].angular_limit_upper); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_LOWER_LIMIT, g6dofjd->axis_data[axis].angular_limit_lower); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS, g6dofjd->axis_data[axis].angular_limit_softness); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_RESTITUTION, g6dofjd->axis_data[axis].angular_restitution); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_DAMPING, g6dofjd->axis_data[axis].angular_damping); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_ERP, g6dofjd->axis_data[axis].erp); - PhysicsServer::get_singleton()->generic_6dof_joint_set_flag(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING, g6dofjd->axis_data[axis].angular_spring_enabled); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS, g6dofjd->axis_data[axis].angular_spring_stiffness); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_DAMPING, g6dofjd->axis_data[axis].angular_spring_damping); - PhysicsServer::get_singleton()->generic_6dof_joint_set_param(joint, static_cast(axis), PhysicsServer::G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT, g6dofjd->axis_data[axis].angular_equilibrium_point); - } - - } break; - case JOINT_TYPE_NONE: { - } break; - } -} - -void PhysicalBone::_on_bone_parent_changed() { - _reload_joint(); -} - -void PhysicalBone::_set_gizmo_move_joint(bool p_move_joint) { -#ifdef TOOLS_ENABLED - gizmo_move_joint = p_move_joint; - SpatialEditor::get_singleton()->update_transform_gizmo(); -#endif -} - -#ifdef TOOLS_ENABLED -Transform PhysicalBone::get_global_gizmo_transform() const { - return gizmo_move_joint ? get_global_transform() * joint_offset : get_global_transform(); -} - -Transform PhysicalBone::get_local_gizmo_transform() const { - return gizmo_move_joint ? get_transform() * joint_offset : get_transform(); -} -#endif - -const PhysicalBone::JointData *PhysicalBone::get_joint_data() const { - return joint_data; -} - -Skeleton *PhysicalBone::find_skeleton_parent() { - return find_skeleton_parent(this); -} - -void PhysicalBone::set_joint_type(JointType p_joint_type) { - if (p_joint_type == get_joint_type()) { - return; - } - - if (joint_data) { - memdelete(joint_data); - } - joint_data = nullptr; - switch (p_joint_type) { - case JOINT_TYPE_PIN: - joint_data = memnew(PinJointData); - break; - case JOINT_TYPE_CONE: - joint_data = memnew(ConeJointData); - break; - case JOINT_TYPE_HINGE: - joint_data = memnew(HingeJointData); - break; - case JOINT_TYPE_SLIDER: - joint_data = memnew(SliderJointData); - break; - case JOINT_TYPE_6DOF: - joint_data = memnew(SixDOFJointData); - break; - case JOINT_TYPE_NONE: - break; - } - - _reload_joint(); - -#ifdef TOOLS_ENABLED - _change_notify(); - update_gizmos(); -#endif -} - -PhysicalBone::JointType PhysicalBone::get_joint_type() const { - return joint_data ? joint_data->get_joint_type() : JOINT_TYPE_NONE; -} - -void PhysicalBone::set_joint_offset(const Transform &p_offset) { - joint_offset = p_offset; - - _fix_joint_offset(); - - set_ignore_transform_notification(true); - reset_to_rest_position(); - set_ignore_transform_notification(false); - -#ifdef TOOLS_ENABLED - update_gizmos(); -#endif -} - -const Transform &PhysicalBone::get_body_offset() const { - return body_offset; -} - -void PhysicalBone::set_body_offset(const Transform &p_offset) { - body_offset = p_offset; - body_offset_inverse = body_offset.affine_inverse(); - - _fix_joint_offset(); - - set_ignore_transform_notification(true); - reset_to_rest_position(); - set_ignore_transform_notification(false); - -#ifdef TOOLS_ENABLED - update_gizmos(); -#endif -} - -const Transform &PhysicalBone::get_joint_offset() const { - return joint_offset; -} - -void PhysicalBone::set_simulate_physics(bool p_simulate) { - if (simulate_physics == p_simulate) { - return; - } - - simulate_physics = p_simulate; - reset_physics_simulation_state(); -} - -bool PhysicalBone::get_simulate_physics() { - return simulate_physics; -} - -bool PhysicalBone::is_simulating_physics() { - return _internal_simulate_physics; -} - -void PhysicalBone::set_bone_name(const String &p_name) { - bone_name = p_name; - bone_id = -1; - - update_bone_id(); - reset_to_rest_position(); -} - -const String &PhysicalBone::get_bone_name() const { - return bone_name; -} - -void PhysicalBone::set_mass(real_t p_mass) { - ERR_FAIL_COND(p_mass <= 0); - mass = p_mass; - PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_MASS, mass); -} - -real_t PhysicalBone::get_mass() const { - return mass; -} - -void PhysicalBone::set_weight(real_t p_weight) { - set_mass(p_weight / real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8))); -} - -real_t PhysicalBone::get_weight() const { - return mass * real_t(GLOBAL_DEF("physics/3d/default_gravity", 9.8)); -} - -void PhysicalBone::set_friction(real_t p_friction) { - ERR_FAIL_COND(p_friction < 0 || p_friction > 1); - - friction = p_friction; - PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_FRICTION, friction); -} - -real_t PhysicalBone::get_friction() const { - return friction; -} - -void PhysicalBone::set_bounce(real_t p_bounce) { - ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1); - - bounce = p_bounce; - PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_BOUNCE, bounce); -} - -real_t PhysicalBone::get_bounce() const { - return bounce; -} - -void PhysicalBone::set_gravity_scale(real_t p_gravity_scale) { - gravity_scale = p_gravity_scale; - PhysicsServer::get_singleton()->body_set_param(get_rid(), PhysicsServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale); -} - -real_t PhysicalBone::get_gravity_scale() const { - return gravity_scale; -} - -PhysicalBone::PhysicalBone() : - PhysicsBody(PhysicsServer::BODY_MODE_STATIC), -#ifdef TOOLS_ENABLED - gizmo_move_joint(false), -#endif - joint_data(nullptr), - parent_skeleton(nullptr), - simulate_physics(false), - _internal_simulate_physics(false), - bone_id(-1), - bone_name(""), - bounce(0), - mass(1), - friction(1), - gravity_scale(1) { - - reset_physics_simulation_state(); -} - -PhysicalBone::~PhysicalBone() { - if (joint_data) { - memdelete(joint_data); - } -} - -void PhysicalBone::update_bone_id() { - if (!parent_skeleton) { - return; - } - - const int new_bone_id = parent_skeleton->find_bone(bone_name); - - if (new_bone_id != bone_id) { - if (-1 != bone_id) { - // Assert the unbind from old node - parent_skeleton->unbind_physical_bone_from_bone(bone_id); - } - - bone_id = new_bone_id; - - parent_skeleton->bind_physical_bone_to_bone(bone_id, this); - - _fix_joint_offset(); - reset_physics_simulation_state(); - } -} - -void PhysicalBone::update_offset() { -#ifdef TOOLS_ENABLED - if (parent_skeleton) { - Transform bone_transform(parent_skeleton->get_global_transform()); - if (-1 != bone_id) { - bone_transform *= parent_skeleton->get_bone_global_pose(bone_id); - } - - if (gizmo_move_joint) { - bone_transform *= body_offset; - set_joint_offset(bone_transform.affine_inverse() * get_global_transform()); - } else { - set_body_offset(bone_transform.affine_inverse() * get_global_transform()); - } - } -#endif -} - -void PhysicalBone::_start_physics_simulation() { - if (_internal_simulate_physics || !parent_skeleton) { - return; - } - reset_to_rest_position(); - PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_RIGID); - PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer()); - PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask()); - PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed"); - set_as_toplevel(true); - _internal_simulate_physics = true; -} - -void PhysicalBone::_stop_physics_simulation() { - if (!parent_skeleton) { - return; - } - - if (parent_skeleton->get_animate_physical_bones()) { - PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_KINEMATIC); - PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer()); - PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask()); - } else { - PhysicsServer::get_singleton()->body_set_mode(get_rid(), PhysicsServer::BODY_MODE_STATIC); - PhysicsServer::get_singleton()->body_set_collision_layer(get_rid(), 0); - PhysicsServer::get_singleton()->body_set_collision_mask(get_rid(), 0); - } - if (_internal_simulate_physics) { - PhysicsServer::get_singleton()->body_set_force_integration_callback(get_rid(), NULL, ""); - parent_skeleton->set_bone_global_pose_override(bone_id, Transform(), 0.0, false); - set_as_toplevel(false); - _internal_simulate_physics = false; - } -} diff --git a/modules/skeleton_3d/nodes/physical_bone.h b/modules/skeleton_3d/nodes/physical_bone.h deleted file mode 100644 index 9c6755f..0000000 --- a/modules/skeleton_3d/nodes/physical_bone.h +++ /dev/null @@ -1,321 +0,0 @@ -#ifndef PHYSICAL_BONE_H -#define PHYSICAL_BONE_H - -/*************************************************************************/ -/* physics_body.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/object/reference.h" -#include "core/containers/vset.h" -#include "scene/3d/collision_object.h" -#include "servers/physics_server.h" -#include "scene/3d/physics_body.h" - -class PhysicsMaterial; -class Skeleton; - -class PhysicalBone : public PhysicsBody { - GDCLASS(PhysicalBone, PhysicsBody); - -public: - enum JointType { - JOINT_TYPE_NONE, - JOINT_TYPE_PIN, - JOINT_TYPE_CONE, - JOINT_TYPE_HINGE, - JOINT_TYPE_SLIDER, - JOINT_TYPE_6DOF - }; - - struct JointData { - virtual JointType get_joint_type() { return JOINT_TYPE_NONE; } - - /// "j" is used to set the parameter inside the PhysicsServer - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - virtual ~JointData() {} - }; - - struct PinJointData : public JointData { - virtual JointType get_joint_type() { return JOINT_TYPE_PIN; } - - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - real_t bias; - real_t damping; - real_t impulse_clamp; - - PinJointData() : - bias(0.3), - damping(1.), - impulse_clamp(0) {} - }; - - struct ConeJointData : public JointData { - virtual JointType get_joint_type() { return JOINT_TYPE_CONE; } - - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - real_t swing_span; - real_t twist_span; - real_t bias; - real_t softness; - real_t relaxation; - - ConeJointData() : - swing_span(Math_PI * 0.25), - twist_span(Math_PI), - bias(0.3), - softness(0.8), - relaxation(1.) {} - }; - - struct HingeJointData : public JointData { - virtual JointType get_joint_type() { return JOINT_TYPE_HINGE; } - - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - bool angular_limit_enabled; - real_t angular_limit_upper; - real_t angular_limit_lower; - real_t angular_limit_bias; - real_t angular_limit_softness; - real_t angular_limit_relaxation; - - HingeJointData() : - angular_limit_enabled(false), - angular_limit_upper(Math_PI * 0.5), - angular_limit_lower(-Math_PI * 0.5), - angular_limit_bias(0.3), - angular_limit_softness(0.9), - angular_limit_relaxation(1.) {} - }; - - struct SliderJointData : public JointData { - virtual JointType get_joint_type() { return JOINT_TYPE_SLIDER; } - - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - real_t linear_limit_upper; - real_t linear_limit_lower; - real_t linear_limit_softness; - real_t linear_limit_restitution; - real_t linear_limit_damping; - real_t angular_limit_upper; - real_t angular_limit_lower; - real_t angular_limit_softness; - real_t angular_limit_restitution; - real_t angular_limit_damping; - - SliderJointData() : - linear_limit_upper(1.), - linear_limit_lower(-1.), - linear_limit_softness(1.), - linear_limit_restitution(0.7), - linear_limit_damping(1.), - angular_limit_upper(0), - angular_limit_lower(0), - angular_limit_softness(1.), - angular_limit_restitution(0.7), - angular_limit_damping(1.) {} - }; - - struct SixDOFJointData : public JointData { - struct SixDOFAxisData { - bool linear_limit_enabled; - real_t linear_limit_upper; - real_t linear_limit_lower; - real_t linear_limit_softness; - real_t linear_restitution; - real_t linear_damping; - bool linear_spring_enabled; - real_t linear_spring_stiffness; - real_t linear_spring_damping; - real_t linear_equilibrium_point; - bool angular_limit_enabled; - real_t angular_limit_upper; - real_t angular_limit_lower; - real_t angular_limit_softness; - real_t angular_restitution; - real_t angular_damping; - real_t erp; - bool angular_spring_enabled; - real_t angular_spring_stiffness; - real_t angular_spring_damping; - real_t angular_equilibrium_point; - - SixDOFAxisData() : - linear_limit_enabled(true), - linear_limit_upper(0), - linear_limit_lower(0), - linear_limit_softness(0.7), - linear_restitution(0.5), - linear_damping(1.), - linear_spring_enabled(false), - linear_spring_stiffness(0), - linear_spring_damping(0), - linear_equilibrium_point(0), - angular_limit_enabled(true), - angular_limit_upper(0), - angular_limit_lower(0), - angular_limit_softness(0.5), - angular_restitution(0), - angular_damping(1.), - erp(0.5), - angular_spring_enabled(false), - angular_spring_stiffness(0), - angular_spring_damping(0.), - angular_equilibrium_point(0) {} - }; - - virtual JointType get_joint_type() { return JOINT_TYPE_6DOF; } - - virtual bool _set(const StringName &p_name, const Variant &p_value, RID j); - virtual bool _get(const StringName &p_name, Variant &r_ret) const; - virtual void _get_property_list(List *p_list) const; - - SixDOFAxisData axis_data[3]; - - SixDOFJointData() {} - }; - -private: -#ifdef TOOLS_ENABLED - // if false gizmo move body - bool gizmo_move_joint; -#endif - - JointData *joint_data; - Transform joint_offset; - RID joint; - - Skeleton *parent_skeleton; - Transform body_offset; - Transform body_offset_inverse; - bool simulate_physics; - bool _internal_simulate_physics; - int bone_id; - - String bone_name; - real_t bounce; - real_t mass; - real_t friction; - real_t gravity_scale; - -protected: - bool _set(const StringName &p_name, const Variant &p_value); - bool _get(const StringName &p_name, Variant &r_ret) const; - void _get_property_list(List *p_list) const; - void _notification(int p_what); - void _direct_state_changed(Object *p_state); - - static void _bind_methods(); - -private: - static Skeleton *find_skeleton_parent(Node *p_parent); - - void _fix_joint_offset(); - void _reload_joint(); - -public: - void _on_bone_parent_changed(); - void _set_gizmo_move_joint(bool p_move_joint); - -public: -#ifdef TOOLS_ENABLED - virtual Transform get_global_gizmo_transform() const; - virtual Transform get_local_gizmo_transform() const; -#endif - - const JointData *get_joint_data() const; - Skeleton *find_skeleton_parent(); - - int get_bone_id() const { return bone_id; } - - void set_joint_type(JointType p_joint_type); - JointType get_joint_type() const; - - void set_joint_offset(const Transform &p_offset); - const Transform &get_joint_offset() const; - - void set_body_offset(const Transform &p_offset); - const Transform &get_body_offset() const; - - void set_simulate_physics(bool p_simulate); - bool get_simulate_physics(); - bool is_simulating_physics(); - - void set_bone_name(const String &p_name); - const String &get_bone_name() const; - - void set_mass(real_t p_mass); - real_t get_mass() const; - - void set_weight(real_t p_weight); - real_t get_weight() const; - - void set_friction(real_t p_friction); - real_t get_friction() const; - - void set_bounce(real_t p_bounce); - real_t get_bounce() const; - - void set_gravity_scale(real_t p_gravity_scale); - real_t get_gravity_scale() const; - - void apply_central_impulse(const Vector3 &p_impulse); - void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse); - - void reset_physics_simulation_state(); - void reset_to_rest_position(); - - PhysicalBone(); - ~PhysicalBone(); - -private: - void update_bone_id(); - void update_offset(); - - void _start_physics_simulation(); - void _stop_physics_simulation(); -}; - -VARIANT_ENUM_CAST(PhysicalBone::JointType); - -#endif // PHYSICS_BODY__H diff --git a/modules/skeleton_3d/nodes/skeleton.cpp b/modules/skeleton_3d/nodes/skeleton.cpp deleted file mode 100644 index a74f79c..0000000 --- a/modules/skeleton_3d/nodes/skeleton.cpp +++ /dev/null @@ -1,1475 +0,0 @@ -/*************************************************************************/ -/* skeleton.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton.h" - -#include "core/object/message_queue.h" - -#include "../resources/skeleton_modification_3d.h" -#include "../resources/skeleton_modification_stack_3d.h" -#include "../resources/skin.h" -#include "core/config/engine.h" -#include "core/config/project_settings.h" -#include "physical_bone.h" -#include "scene/resources/mesh/surface_tool.h" -#include "scene/main/scene_string_names.h" - -void SkinReference::_skin_changed() { - if (skeleton_node) { - skeleton_node->_make_dirty(); - } - skeleton_version = 0; -} - -void SkinReference::_bind_methods() { - ClassDB::bind_method(D_METHOD("_skin_changed"), &SkinReference::_skin_changed); - ClassDB::bind_method(D_METHOD("get_skeleton"), &SkinReference::get_skeleton); - ClassDB::bind_method(D_METHOD("get_skin"), &SkinReference::get_skin); -} - -RID SkinReference::get_skeleton() const { - return skeleton; -} - -Skeleton *SkinReference::get_skeleton_node() const { - return skeleton_node; -} - -Ref SkinReference::get_skin() const { - return skin; -} - -SkinReference::~SkinReference() { - if (skeleton_node) { - skeleton_node->skin_bindings.erase(this); - } - - RS::get_singleton()->free(skeleton); -} - -bool Skeleton::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - -#ifndef _3D_DISABLED - if (path.begins_with("modification_stack")) { - set_modification_stack(p_value); - return true; - } -#endif //_3D_DISABLED - - if (!path.begins_with("bones/")) { - return false; - } - - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - - if (which == bones.size() && what == "name") { - add_bone(p_value); - return true; - } - - ERR_FAIL_INDEX_V(which, bones.size(), false); - - if (what == "parent") { - set_bone_parent(which, p_value); - } else if (what == "rest") { - set_bone_rest(which, p_value); - - } else if (what == "enabled") { - set_bone_enabled(which, p_value); - } else if (what == "position") { - set_bone_pose_position(which, p_value); - } else if (what == "rotation") { - set_bone_pose_rotation(which, p_value); - } else if (what == "scale") { - set_bone_pose_scale(which, p_value); - } else if (what == "pose") { - set_bone_pose(which, p_value); - } else { - return false; - } - - return true; -} - -bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - -#ifndef _3D_DISABLED - if (path.begins_with("modification_stack")) { - r_ret = modification_stack; - return true; - } -#endif //_3D_DISABLED - - if (!path.begins_with("bones/")) { - return false; - } - - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - - ERR_FAIL_INDEX_V(which, bones.size(), false); - - if (what == "name") { - r_ret = get_bone_name(which); - } else if (what == "parent") { - r_ret = get_bone_parent(which); - } else if (what == "rest") { - r_ret = get_bone_rest(which); - } else if (what == "enabled") { - r_ret = is_bone_enabled(which); - } else if (what == "position") { - r_ret = get_bone_pose_position(which); - } else if (what == "rotation") { - r_ret = get_bone_pose_rotation(which); - } else if (what == "scale") { - r_ret = get_bone_pose_scale(which); - } else if (what == "pose") { - r_ret = get_bone_pose(which); - } else { - return false; - } - - return true; -} -void Skeleton::_get_property_list(List *p_list) const { - for (int i = 0; i < bones.size(); i++) { - String prep = "bones/" + itos(i) + "/"; - p_list->push_back(PropertyInfo(Variant::STRING, prep + "name")); - p_list->push_back(PropertyInfo(Variant::INT, prep + "parent", PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1")); - p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "rest")); - p_list->push_back(PropertyInfo(Variant::BOOL, prep + "enabled")); - p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + "position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); - p_list->push_back(PropertyInfo(Variant::QUATERNION, prep + "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); - p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); - } - -#ifndef _3D_DISABLED - p_list->push_back(PropertyInfo(Variant::OBJECT, "modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); -#endif //_3D_DISABLED -} - -/* -void Skeleton::_validate_property(PropertyInfo &property) const { - Vector spr = property.name.split("/"); - if (spr.size() == 3 && spr[0] == "bones") { - if (spr[2] == "rest") { - property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; - } - if (is_show_rest_only()) { - if (spr[2] == "enabled") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - if (spr[2] == "position") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - if (spr[2] == "rotation") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - if (spr[2] == "scale") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - } else if (!is_bone_enabled(spr[1].to_int())) { - if (spr[2] == "position") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - if (spr[2] == "rotation") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - if (spr[2] == "scale") { - property.usage |= PROPERTY_USAGE_READ_ONLY; - } - } - } -} -*/ - -void Skeleton::_update_process_order() { - if (!process_order_dirty) { - return; - } - - ++updating; - - Bone *bonesptr = bones.ptrw(); - int len = bones.size(); - - parentless_bones.clear(); - - for (int i = 0; i < len; i++) { - bonesptr[i].child_bones.clear(); - } - - for (int i = 0; i < len; i++) { - if (bonesptr[i].parent >= len) { - //validate this just in case - ERR_PRINT("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent)); - bonesptr[i].parent = -1; - } - - if (bonesptr[i].parent != -1) { - int parent_bone_idx = bonesptr[i].parent; - - // Check to see if this node is already added to the parent: - if (bonesptr[parent_bone_idx].child_bones.find(i) < 0) { - // Add the child node - bonesptr[parent_bone_idx].child_bones.push_back(i); - } else { - ERR_PRINT("Skeleton parenthood graph is cyclic"); - } - } else { - parentless_bones.push_back(i); - } - } - - process_order_dirty = false; - - --updating; - - emit_signal("bones_updated"); -} - -void Skeleton::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_UPDATE_SKELETON: { - ++updating; - // Update bone transforms - force_update_all_bone_transforms(); - - RenderingServer *vs = RenderingServer::get_singleton(); - - Bone *bonesptr = bones.ptrw(); - - int len = bones.size(); - dirty = false; - - //update skins - for (RBSet::Element *E = skin_bindings.front(); E; E = E->next()) { - const Skin *skin = E->get()->skin.operator->(); - RID skeleton = E->get()->skeleton; - uint32_t bind_count = skin->get_bind_count(); - - if (E->get()->bind_count != bind_count) { - RS::get_singleton()->skeleton_allocate(skeleton, bind_count); - E->get()->bind_count = bind_count; - E->get()->skin_bone_indices.resize(bind_count); - E->get()->skin_bone_indices_ptrs = E->get()->skin_bone_indices.ptrw(); - } - - if (E->get()->skeleton_version != version) { - for (uint32_t i = 0; i < bind_count; i++) { - StringName bind_name = skin->get_bind_name(i); - - if (bind_name != StringName()) { - //bind name used, use this - bool found = false; - for (int j = 0; j < len; j++) { - if (bonesptr[j].name == bind_name) { - E->get()->skin_bone_indices_ptrs[i] = j; - found = true; - break; - } - } - - if (!found) { - ERR_PRINT("Skin bind #" + itos(i) + " contains named bind '" + String(bind_name) + "' but Skeleton has no bone by that name."); - E->get()->skin_bone_indices_ptrs[i] = 0; - } - } else if (skin->get_bind_bone(i) >= 0) { - int bind_index = skin->get_bind_bone(i); - if (bind_index >= len) { - ERR_PRINT("Skin bind #" + itos(i) + " contains bone index bind: " + itos(bind_index) + " , which is greater than the skeleton bone count: " + itos(len) + "."); - E->get()->skin_bone_indices_ptrs[i] = 0; - } else { - E->get()->skin_bone_indices_ptrs[i] = bind_index; - } - } else { - ERR_PRINT("Skin bind #" + itos(i) + " does not contain a name nor a bone index."); - E->get()->skin_bone_indices_ptrs[i] = 0; - } - } - - E->get()->skeleton_version = version; - } - - for (uint32_t i = 0; i < bind_count; i++) { - uint32_t bone_index = E->get()->skin_bone_indices_ptrs[i]; - ERR_CONTINUE(bone_index >= (uint32_t)len); - vs->skeleton_bone_set_transform(skeleton, i, bonesptr[bone_index].pose_global * skin->get_bind_pose(i)); - } - } - - --updating; - - //TODO Not sure if this is useful or not in runtime - //#ifdef TOOLS_ENABLED - emit_signal("pose_updated"); - //#endif // TOOLS_ENABLED - } break; - -#ifndef _3D_DISABLED - case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { - // This is active only if the skeleton animates the physical bones - // and the state of the bone is not active. - if (Engine::get_singleton()->is_editor_hint()) { - if (animate_physical_bones) { - for (int i = 0; i < bones.size(); i += 1) { - if (bones[i].physical_bone) { - if (bones[i].physical_bone->is_simulating_physics() == false) { - bones[i].physical_bone->reset_to_rest_position(); - } - } - } - } - } - - if (modification_stack.is_valid()) { - execute_modifications(get_physics_process_delta_time(), SkeletonModificationStack3D::EXECUTION_MODE::execution_mode_physics_process); - } - } break; -#endif - -#ifndef _3D_DISABLED - case NOTIFICATION_INTERNAL_PROCESS: { - if (modification_stack.is_valid()) { - execute_modifications(get_process_delta_time(), SkeletonModificationStack3D::EXECUTION_MODE::execution_mode_process); - } - } break; -#endif // _3D_DISABLED - -#ifndef _3D_DISABLED - - case NOTIFICATION_READY: { - set_physics_process_internal(true); - set_process_internal(true); - - if (modification_stack.is_valid()) { - set_modification_stack(modification_stack); - } - - init_pose(); - } break; -#endif - } -} - -void Skeleton::clear_bones_global_pose_override() { - for (int i = 0; i < bones.size(); i += 1) { - bones.write[i].global_pose_override_amount = 0; - bones.write[i].global_pose_override_reset = true; - } - _make_dirty(); -} - -void Skeleton::set_bone_global_pose_override(int p_bone, const Transform &p_pose, float p_amount, bool p_persistent) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].global_pose_override_amount = p_amount; - bones.write[p_bone].global_pose_override = p_pose; - bones.write[p_bone].global_pose_override_reset = !p_persistent; - _make_dirty(); -} - -Transform Skeleton::get_bone_global_pose_override(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - - return bones[p_bone].global_pose_override; -} - -Transform Skeleton::get_bone_global_pose(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - - if (dirty && updating == 0) { - const_cast(this)->notification(NOTIFICATION_UPDATE_SKELETON); - } - - return bones[p_bone].pose_global; -} - -Transform Skeleton::get_bone_global_pose_no_override(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - - if (dirty && updating == 0) { - const_cast(this)->notification(NOTIFICATION_UPDATE_SKELETON); - } - - return bones[p_bone].pose_global_no_override; -} - -void Skeleton::clear_bones_local_pose_override() { - for (int i = 0; i < bones.size(); i += 1) { - bones.write[i].local_pose_override_amount = 0; - } - _make_dirty(); -} - -void Skeleton::set_bone_local_pose_override(int p_bone, const Transform &p_pose, real_t p_amount, bool p_persistent) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].local_pose_override_amount = p_amount; - bones.write[p_bone].local_pose_override = p_pose; - bones.write[p_bone].local_pose_override_reset = !p_persistent; - _make_dirty(); -} - -Transform Skeleton::get_bone_local_pose_override(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - return bones[p_bone].local_pose_override; -} - -void Skeleton::update_bone_rest_forward_vector(int p_bone, bool p_force_update) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - if (bones[p_bone].rest_bone_forward_vector.length_squared() > 0 && p_force_update == false) { - update_bone_rest_forward_axis(p_bone, p_force_update); - } - - // If it is a child/leaf bone... - if (get_bone_parent(p_bone) > 0) { - bones.write[p_bone].rest_bone_forward_vector = bones[p_bone].rest.origin.normalized(); - } else { - // If it has children... - Vector child_bones = get_bone_children(p_bone); - if (child_bones.size() > 0) { - Vector3 combined_child_dir = Vector3(0, 0, 0); - for (int i = 0; i < child_bones.size(); i++) { - combined_child_dir += bones[child_bones[i]].rest.origin.normalized(); - } - combined_child_dir = combined_child_dir / child_bones.size(); - bones.write[p_bone].rest_bone_forward_vector = combined_child_dir.normalized(); - } else { - WARN_PRINT_ONCE("Cannot calculate forward direction for bone " + itos(p_bone)); - WARN_PRINT_ONCE("Assuming direction of (0, 1, 0) for bone"); - bones.write[p_bone].rest_bone_forward_vector = Vector3(0, 1, 0); - } - } - update_bone_rest_forward_axis(p_bone, p_force_update); -} - -void Skeleton::update_bone_rest_forward_axis(int p_bone, bool p_force_update) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - if (bones[p_bone].rest_bone_forward_axis > -1 && p_force_update == false) { - return; - } - - Vector3 forward_axis_absolute = bones[p_bone].rest_bone_forward_vector.abs(); - if (forward_axis_absolute.x > forward_axis_absolute.y && forward_axis_absolute.x > forward_axis_absolute.z) { - if (bones[p_bone].rest_bone_forward_vector.x > 0) { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_X_FORWARD; - } else { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_NEGATIVE_X_FORWARD; - } - } else if (forward_axis_absolute.y > forward_axis_absolute.x && forward_axis_absolute.y > forward_axis_absolute.z) { - if (bones[p_bone].rest_bone_forward_vector.y > 0) { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_Y_FORWARD; - } else { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_NEGATIVE_Y_FORWARD; - } - } else { - if (bones[p_bone].rest_bone_forward_vector.z > 0) { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_Z_FORWARD; - } else { - bones.write[p_bone].rest_bone_forward_axis = BONE_AXIS_NEGATIVE_Z_FORWARD; - } - } -} - -Vector3 Skeleton::get_bone_axis_forward_vector(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3(0, 0, 0)); - return bones[p_bone].rest_bone_forward_vector; -} - -int Skeleton::get_bone_axis_forward_enum(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, -1); - return bones[p_bone].rest_bone_forward_axis; -} - -// skeleton creation api -void Skeleton::add_bone(const String &p_name) { - ERR_FAIL_COND(p_name == "" || p_name.find(":") != -1 || p_name.find("/") != -1); - - for (int i = 0; i < bones.size(); i++) { - ERR_FAIL_COND(bones[i].name == p_name); - } - - Bone b; - b.name = p_name; - bones.push_back(b); - process_order_dirty = true; - version++; - rest_dirty = true; - _make_dirty(); - update_gizmos(); -} -int Skeleton::find_bone(const String &p_name) const { - for (int i = 0; i < bones.size(); i++) { - if (bones[i].name == p_name) { - return i; - } - } - - return -1; -} -String Skeleton::get_bone_name(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, ""); - - return bones[p_bone].name; -} - -void Skeleton::set_bone_name(int p_bone, const String &p_name) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - for (int i = 0; i < bone_size; i++) { - if (i != p_bone) { - ERR_FAIL_COND(bones[i].name == p_name); - } - } - - bones.write[p_bone].name = p_name; -} - -bool Skeleton::is_bone_parent_of(int p_bone, int p_parent_bone_id) const { - int parent_of_bone = get_bone_parent(p_bone); - - if (-1 == parent_of_bone) { - return false; - } - - if (parent_of_bone == p_parent_bone_id) { - return true; - } - - return is_bone_parent_of(parent_of_bone, p_parent_bone_id); -} - -int Skeleton::get_bone_count() const { - return bones.size(); -} - -void Skeleton::set_bone_parent(int p_bone, int p_parent) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - ERR_FAIL_COND(p_parent != -1 && (p_parent < 0)); - ERR_FAIL_COND(p_bone == p_parent); - - bones.write[p_bone].parent = p_parent; - process_order_dirty = true; - rest_dirty = true; - - _make_dirty(); -} - -void Skeleton::unparent_bone_and_rest(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - _update_process_order(); - - int parent = bones[p_bone].parent; - while (parent >= 0) { - bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest; - parent = bones[parent].parent; - } - - bones.write[p_bone].parent = -1; - process_order_dirty = true; - rest_dirty = true; - - _make_dirty(); -} - -int Skeleton::get_bone_parent(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, -1); - - return bones[p_bone].parent; -} - -Vector Skeleton::get_bone_children(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Vector()); - return bones[p_bone].child_bones; -} - -void Skeleton::set_bone_children(int p_bone, Vector p_children) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].child_bones = p_children; - - process_order_dirty = true; - rest_dirty = true; - - _make_dirty(); -} - -void Skeleton::add_bone_child(int p_bone, int p_child) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].child_bones.push_back(p_child); - - process_order_dirty = true; - rest_dirty = true; - - _make_dirty(); -} - -void Skeleton::remove_bone_child(int p_bone, int p_child) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - int child_idx = bones[p_bone].child_bones.find(p_child); - if (child_idx >= 0) { - bones.write[p_bone].child_bones.remove(child_idx); - } else { - WARN_PRINT("Cannot remove child bone: Child bone not found."); - } - - process_order_dirty = true; - rest_dirty = true; - - _make_dirty(); -} - -Vector Skeleton::get_parentless_bones() { - return parentless_bones; -} - -void Skeleton::set_bone_rest(int p_bone, const Transform &p_rest) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].rest = p_rest; - rest_dirty = true; - _make_dirty(); -} -Transform Skeleton::get_bone_rest(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - - return bones[p_bone].rest; -} - -Transform Skeleton::get_bone_global_rest(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - - if (rest_dirty && updating == 0) { - const_cast(this)->notification(NOTIFICATION_UPDATE_SKELETON); - } - - return bones[p_bone].global_rest; -} - -void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].enabled = p_enabled; - emit_signal(SceneStringNames::get_singleton()->bone_enabled_changed, p_bone); - _make_dirty(); -} - -bool Skeleton::is_bone_enabled(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, false); - return bones[p_bone].enabled; -} - -void Skeleton::set_show_rest_only(bool p_enabled) { - show_rest_only = p_enabled; - emit_signal(SceneStringNames::get_singleton()->show_rest_only_changed); - _make_dirty(); -} - -bool Skeleton::is_show_rest_only() const { - return show_rest_only; -} - -void Skeleton::clear_bones() { - bones.clear(); - process_order_dirty = true; - version++; - _make_dirty(); -} - -// posing api -void Skeleton::set_bone_pose(int p_bone, const Transform &p_pose) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].pose_position = p_pose.origin; - bones.write[p_bone].pose_rotation = p_pose.basis.get_rotation_quaternion(); - bones.write[p_bone].pose_scale = p_pose.basis.get_scale(); - bones.write[p_bone].pose_cache_dirty = true; - - if (is_inside_tree()) { - _make_dirty(); - } -} - -void Skeleton::set_bone_pose_position(int p_bone, const Vector3 &p_position) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].pose_position = p_position; - bones.write[p_bone].pose_cache_dirty = true; - - if (is_inside_tree()) { - _make_dirty(); - } -} -void Skeleton::set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].pose_rotation = p_rotation; - bones.write[p_bone].pose_cache_dirty = true; - - if (is_inside_tree()) { - _make_dirty(); - } -} -void Skeleton::set_bone_pose_scale(int p_bone, const Vector3 &p_scale) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - - bones.write[p_bone].pose_scale = p_scale; - bones.write[p_bone].pose_cache_dirty = true; - - if (is_inside_tree()) { - _make_dirty(); - } -} - -Transform Skeleton::get_bone_pose(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Transform()); - ((Skeleton *)this)->bones.write[p_bone].update_pose_cache(); - return bones[p_bone].pose_cache; -} - -Vector3 Skeleton::get_bone_pose_position(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3()); - return bones[p_bone].pose_position; -} - -Quaternion Skeleton::get_bone_pose_rotation(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Quaternion()); - return bones[p_bone].pose_rotation; -} - -Vector3 Skeleton::get_bone_pose_scale(int p_bone) const { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3()); - return bones[p_bone].pose_scale; -} - -void Skeleton::_make_dirty() { - if (dirty) { - return; - } - - MessageQueue::get_singleton()->push_notification(this, NOTIFICATION_UPDATE_SKELETON); - dirty = true; -} - -void Skeleton::localize_rests() { - _update_process_order(); - - Vector bones_to_process = get_parentless_bones(); - while (bones_to_process.size() > 0) { - int current_bone_idx = bones_to_process[0]; - bones_to_process.erase(current_bone_idx); - - if (bones[current_bone_idx].parent >= 0) { - set_bone_rest(current_bone_idx, bones[bones[current_bone_idx].parent].rest.affine_inverse() * bones[current_bone_idx].rest); - } - - // Add the bone's children to the list of bones to be processed - int child_bone_size = bones[current_bone_idx].child_bones.size(); - for (int i = 0; i < child_bone_size; i++) { - bones_to_process.push_back(bones[current_bone_idx].child_bones[i]); - } - } -} - -#ifndef _3D_DISABLED - -void Skeleton::set_animate_physical_bones(bool p_animate) { - animate_physical_bones = p_animate; - - if (Engine::get_singleton()->is_editor_hint() == false) { - bool sim = false; - for (int i = 0; i < bones.size(); i += 1) { - if (bones[i].physical_bone) { - bones[i].physical_bone->reset_physics_simulation_state(); - if (bones[i].physical_bone->is_simulating_physics()) { - sim = true; - } - } - } - set_physics_process_internal(sim == false && p_animate); - } -} - -bool Skeleton::get_animate_physical_bones() const { - return animate_physical_bones; -} - -void Skeleton::bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - ERR_FAIL_COND(bones[p_bone].physical_bone); - ERR_FAIL_COND(!p_physical_bone); - bones.write[p_bone].physical_bone = p_physical_bone; - - _rebuild_physical_bones_cache(); -} - -void Skeleton::unbind_physical_bone_from_bone(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone, bone_size); - bones.write[p_bone].physical_bone = nullptr; - - _rebuild_physical_bones_cache(); -} - -PhysicalBone *Skeleton::get_physical_bone(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); - - return bones[p_bone].physical_bone; -} - -PhysicalBone *Skeleton::get_physical_bone_parent(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); - - if (bones[p_bone].cache_parent_physical_bone) { - return bones[p_bone].cache_parent_physical_bone; - } - - return _get_physical_bone_parent(p_bone); -} - -PhysicalBone *Skeleton::_get_physical_bone_parent(int p_bone) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr); - - const int parent_bone = bones[p_bone].parent; - if (0 > parent_bone) { - return nullptr; - } - - PhysicalBone *pb = bones[parent_bone].physical_bone; - if (pb) { - return pb; - } else { - return get_physical_bone_parent(parent_bone); - } -} - -void Skeleton::_rebuild_physical_bones_cache() { - const int b_size = bones.size(); - for (int i = 0; i < b_size; ++i) { - PhysicalBone *parent_pb = _get_physical_bone_parent(i); - if (parent_pb != bones[i].cache_parent_physical_bone) { - bones.write[i].cache_parent_physical_bone = parent_pb; - if (bones[i].physical_bone) { - bones[i].physical_bone->_on_bone_parent_changed(); - } - } - } -} - -void _pb_stop_simulation(Node *p_node) { - for (int i = p_node->get_child_count() - 1; 0 <= i; --i) { - _pb_stop_simulation(p_node->get_child(i)); - } - - PhysicalBone *pb = Object::cast_to(p_node); - if (pb) { - pb->set_simulate_physics(false); - } -} - -void Skeleton::physical_bones_stop_simulation() { - _pb_stop_simulation(this); -} - -void _pb_start_simulation(const Skeleton *p_skeleton, Node *p_node, const Vector &p_sim_bones) { - for (int i = p_node->get_child_count() - 1; 0 <= i; --i) { - _pb_start_simulation(p_skeleton, p_node->get_child(i), p_sim_bones); - } - - PhysicalBone *pb = Object::cast_to(p_node); - if (pb) { - for (int i = p_sim_bones.size() - 1; 0 <= i; --i) { - if (p_sim_bones[i] == pb->get_bone_id() || p_skeleton->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) { - pb->set_simulate_physics(true); - break; - } - } - } -} - -void Skeleton::physical_bones_start_simulation_on(const Array &p_bones) { - set_physics_process_internal(false); - - Vector sim_bones; - if (p_bones.size() <= 0) { - sim_bones.push_back(0); // if no bones is specified, activate ragdoll on full body - } else { - sim_bones.resize(p_bones.size()); - int c = 0; - for (int i = sim_bones.size() - 1; 0 <= i; --i) { - Variant::Type type = p_bones.get(i).get_type(); - if (Variant::STRING == type || Variant::STRING_NAME == type) { - int bone_id = find_bone(p_bones.get(i)); - if (bone_id != -1) { - sim_bones.write[c++] = bone_id; - } - } - } - sim_bones.resize(c); - } - - _pb_start_simulation(this, this, sim_bones); -} - -void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) { - for (int i = p_node->get_child_count() - 1; 0 <= i; --i) { - _physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception); - } - - CollisionObject *co = Object::cast_to(p_node); - if (co) { - if (p_add) { - PhysicsServer::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception); - } else { - PhysicsServer::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception); - } - } -} - -void Skeleton::physical_bones_add_collision_exception(RID p_exception) { - _physical_bones_add_remove_collision_exception(true, this, p_exception); -} - -void Skeleton::physical_bones_remove_collision_exception(RID p_exception) { - _physical_bones_add_remove_collision_exception(false, this, p_exception); -} - -#endif // _3D_DISABLED - -void Skeleton::_skin_changed() { - _make_dirty(); -} - -Ref Skeleton::create_skin_from_rest_transforms() { - Ref skin; - - skin.instance(); - skin->set_bind_count(bones.size()); - _update_process_order(); // Just in case. - - // Pose changed, rebuild cache of inverses. - const Bone *bonesptr = bones.ptr(); - int len = bones.size(); - - // Calculate global rests and invert them. - LocalVector bones_to_process; - bones_to_process = get_parentless_bones(); - while (bones_to_process.size() > 0) { - int current_bone_idx = bones_to_process[0]; - const Bone &b = bonesptr[current_bone_idx]; - bones_to_process.erase(current_bone_idx); - LocalVector child_bones_vector; - child_bones_vector = get_bone_children(current_bone_idx); - int child_bones_size = child_bones_vector.size(); - - if (b.parent < 0) { - skin->set_bind_pose(current_bone_idx, b.rest); - } - - for (int i = 0; i < child_bones_size; i++) { - int child_bone_idx = child_bones_vector[i]; - const Bone &cb = bonesptr[child_bone_idx]; - skin->set_bind_pose(child_bone_idx, skin->get_bind_pose(current_bone_idx) * cb.rest); - // Add the bone's children to the list of bones to be processed. - bones_to_process.push_back(child_bones_vector[i]); - } - } - - for (int i = 0; i < len; i++) { - // The inverse is what is actually required. - skin->set_bind_bone(i, i); - skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse()); - } - - return skin; -} - -Ref Skeleton::register_skin(const Ref &p_skin) { - ERR_FAIL_COND_V(p_skin.is_null(), Ref()); - - for (RBSet::Element *E = skin_bindings.front(); E; E = E->next()) { - if (E->get()->skin == p_skin) { - return Ref(E->get()); - } - } - - Ref skin_ref; - skin_ref.instance(); - - skin_ref->skeleton_node = this; - skin_ref->bind_count = 0; - skin_ref->skeleton = RID_PRIME(RenderingServer::get_singleton()->skeleton_create()); - skin_ref->skeleton_node = this; - skin_ref->skin = p_skin; - - skin_bindings.insert(skin_ref.operator->()); - - skin_ref->skin->connect("changed", skin_ref.operator->(), "_skin_changed"); - - _make_dirty(); // Skin needs to be updated, so update skeleton. - - return skin_ref; -} - -void Skeleton::init_pose() { - const int bone_len = get_bone_count(); - if (!bone_len) { - return; - } - - for (int i = 0; i < bone_len; i++) { - Transform rest = get_bone_rest(i); - set_bone_pose_position(i, rest.origin); - set_bone_pose_rotation(i, rest.basis.get_rotation_quaternion()); - set_bone_pose_scale(i, rest.basis.get_scale()); - } -} - -void Skeleton::force_update_all_dirty_bones() { - if (dirty && updating == 0) { - const_cast(this)->notification(NOTIFICATION_UPDATE_SKELETON); - } -} - -void Skeleton::force_update_all_bone_transforms() { - ++updating; - _update_process_order(); - - for (int i = 0; i < parentless_bones.size(); i++) { - force_update_bone_children_transforms(parentless_bones[i]); - } - --updating; -} - -void Skeleton::force_update_bone_children_transforms(int p_bone_idx) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX(p_bone_idx, bone_size); - - Bone *bonesptr = bones.ptrw(); - List bones_to_process = List(); - bones_to_process.push_back(p_bone_idx); - - while (bones_to_process.size() > 0) { - int current_bone_idx = bones_to_process[0]; - bones_to_process.erase(current_bone_idx); - - Bone &b = bonesptr[current_bone_idx]; - bool bone_enabled = b.enabled && !show_rest_only; - - if (bone_enabled) { - b.update_pose_cache(); - Transform pose = b.pose_cache; - - if (b.parent >= 0) { - b.pose_global = bonesptr[b.parent].pose_global * pose; - b.pose_global_no_override = b.pose_global; - } else { - b.pose_global = pose; - b.pose_global_no_override = b.pose_global; - } - } else { - if (b.parent >= 0) { - b.pose_global = bonesptr[b.parent].pose_global * b.rest; - b.pose_global_no_override = b.pose_global; - } else { - b.pose_global = b.rest; - b.pose_global_no_override = b.pose_global; - } - } - - if (rest_dirty) { - b.global_rest = b.parent >= 0 ? bonesptr[b.parent].global_rest * b.rest : b.rest; - } - - if (b.local_pose_override_amount >= CMP_EPSILON) { - Transform override_local_pose; - if (b.parent >= 0) { - override_local_pose = bonesptr[b.parent].pose_global * b.local_pose_override; - } else { - override_local_pose = b.local_pose_override; - } - b.pose_global = b.pose_global.interpolate_with(override_local_pose, b.local_pose_override_amount); - } - - if (b.global_pose_override_amount >= CMP_EPSILON) { - b.pose_global = b.pose_global.interpolate_with(b.global_pose_override, b.global_pose_override_amount); - } - - if (b.local_pose_override_reset) { - b.local_pose_override_amount = 0.0; - } - if (b.global_pose_override_reset) { - b.global_pose_override_amount = 0.0; - } - - // Add the bone's children to the list of bones to be processed - int child_bone_size = b.child_bones.size(); - for (int i = 0; i < child_bone_size; i++) { - bones_to_process.push_back(b.child_bones[i]); - } - - emit_signal(SceneStringNames::get_singleton()->bone_pose_changed, current_bone_idx); - } - - rest_dirty = false; -} - -Transform Skeleton::global_pose_to_world_transform(Transform p_global_pose) { - return get_global_transform() * p_global_pose; -} - -Transform Skeleton::world_transform_to_global_pose(Transform p_world_transform) { - return get_global_transform().affine_inverse() * p_world_transform; -} - -Transform Skeleton::global_pose_to_local_pose(int p_bone_idx, Transform p_global_pose) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone_idx, bone_size, Transform()); - - if (bones[p_bone_idx].parent >= 0) { - int parent_bone_idx = bones[p_bone_idx].parent; - Transform conversion_transform = get_bone_global_pose(parent_bone_idx).affine_inverse(); - return conversion_transform * p_global_pose; - } else { - return p_global_pose; - } -} - -Transform Skeleton::local_pose_to_global_pose(int p_bone_idx, Transform p_local_pose) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone_idx, bone_size, Transform()); - if (bones[p_bone_idx].parent >= 0) { - int parent_bone_idx = bones[p_bone_idx].parent; - return bones[parent_bone_idx].pose_global * p_local_pose; - } else { - return p_local_pose; - } -} - -Basis Skeleton::global_pose_z_forward_to_bone_forward(int p_bone_idx, Basis p_basis) { - const int bone_size = bones.size(); - ERR_FAIL_INDEX_V(p_bone_idx, bone_size, Basis()); - Basis return_basis = p_basis; - - if (bones[p_bone_idx].rest_bone_forward_axis < 0) { - update_bone_rest_forward_vector(p_bone_idx, true); - } - - if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_X_FORWARD) { - return_basis.rotate_local(Vector3(0, 1, 0), (Math_PI / 2.0)); - } else if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_NEGATIVE_X_FORWARD) { - return_basis.rotate_local(Vector3(0, 1, 0), -(Math_PI / 2.0)); - } else if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_Y_FORWARD) { - return_basis.rotate_local(Vector3(1, 0, 0), -(Math_PI / 2.0)); - } else if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_NEGATIVE_Y_FORWARD) { - return_basis.rotate_local(Vector3(1, 0, 0), (Math_PI / 2.0)); - } else if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_Z_FORWARD) { - // Do nothing! - } else if (bones[p_bone_idx].rest_bone_forward_axis == BONE_AXIS_NEGATIVE_Z_FORWARD) { - return_basis.rotate_local(Vector3(0, 0, 1), Math_PI); - } - - return return_basis; -} - -// Modifications - -#ifndef _3D_DISABLED - -void Skeleton::set_modification_stack(Ref p_stack) { - if (modification_stack.is_valid()) { - modification_stack->is_setup = false; - modification_stack->set_skeleton(nullptr); - } - - modification_stack = p_stack; - if (modification_stack.is_valid()) { - modification_stack->set_skeleton(this); - modification_stack->setup(); - } -} -Ref Skeleton::get_modification_stack() { - return modification_stack; -} - -void Skeleton::execute_modifications(real_t p_delta, int p_execution_mode) { - if (!modification_stack.is_valid()) { - return; - } - - // Needed to avoid the issue where the stack looses reference to the skeleton when the scene is saved. - if (modification_stack->skeleton != this) { - modification_stack->set_skeleton(this); - } - - modification_stack->execute(p_delta, p_execution_mode); -} - -#endif // _3D_DISABLED - -void Skeleton::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton::add_bone); - ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton::find_bone); - ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton::get_bone_name); - ClassDB::bind_method(D_METHOD("set_bone_name", "bone_idx", "name"), &Skeleton::set_bone_name); - - ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton::get_bone_parent); - ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton::set_bone_parent); - - ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton::get_bone_count); - - ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton::unparent_bone_and_rest); - - ClassDB::bind_method(D_METHOD("get_bone_children", "bone_idx"), &Skeleton::get_bone_children); - ClassDB::bind_method(D_METHOD("set_bone_children", "bone_idx", "bone_children"), &Skeleton::set_bone_children); - ClassDB::bind_method(D_METHOD("add_bone_child", "bone_idx", "child_bone_idx"), &Skeleton::add_bone_child); - ClassDB::bind_method(D_METHOD("remove_bone_child", "bone_idx", "child_bone_idx"), &Skeleton::remove_bone_child); - - ClassDB::bind_method(D_METHOD("get_parentless_bones"), &Skeleton::get_parentless_bones); - - ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton::get_bone_rest); - ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton::set_bone_rest); - ClassDB::bind_method(D_METHOD("get_bone_global_rest", "bone_idx"), &Skeleton::get_bone_global_rest); - - ClassDB::bind_method(D_METHOD("create_skin_from_rest_transforms"), &Skeleton::create_skin_from_rest_transforms); - - ClassDB::bind_method(D_METHOD("register_skin", "skin"), &Skeleton::register_skin); - - ClassDB::bind_method(D_METHOD("localize_rests"), &Skeleton::localize_rests); - - ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton::clear_bones); - - ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton::set_bone_pose); - ClassDB::bind_method(D_METHOD("set_bone_pose_position", "bone_idx", "position"), &Skeleton::set_bone_pose_position); - ClassDB::bind_method(D_METHOD("set_bone_pose_rotation", "bone_idx", "rotation"), &Skeleton::set_bone_pose_rotation); - ClassDB::bind_method(D_METHOD("set_bone_pose_scale", "bone_idx", "scale"), &Skeleton::set_bone_pose_scale); - - ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton::get_bone_pose); - ClassDB::bind_method(D_METHOD("get_bone_pose_position", "bone_idx"), &Skeleton::get_bone_pose_position); - ClassDB::bind_method(D_METHOD("get_bone_pose_rotation", "bone_idx"), &Skeleton::get_bone_pose_rotation); - ClassDB::bind_method(D_METHOD("get_bone_pose_scale", "bone_idx"), &Skeleton::get_bone_pose_scale); - - ClassDB::bind_method(D_METHOD("is_bone_enabled", "bone_idx"), &Skeleton::is_bone_enabled); - ClassDB::bind_method(D_METHOD("set_bone_enabled", "bone_idx", "enabled"), &Skeleton::set_bone_enabled, true); - - ClassDB::bind_method(D_METHOD("clear_bones_global_pose_override"), &Skeleton::clear_bones_global_pose_override); - ClassDB::bind_method(D_METHOD("set_bone_global_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton::set_bone_global_pose_override, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_bone_global_pose_override", "bone_idx"), &Skeleton::get_bone_global_pose_override); - ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton::get_bone_global_pose); - ClassDB::bind_method(D_METHOD("get_bone_global_pose_no_override", "bone_idx"), &Skeleton::get_bone_global_pose_no_override); - - ClassDB::bind_method(D_METHOD("clear_bones_local_pose_override"), &Skeleton::clear_bones_local_pose_override); - ClassDB::bind_method(D_METHOD("set_bone_local_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton::set_bone_local_pose_override, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_bone_local_pose_override", "bone_idx"), &Skeleton::get_bone_local_pose_override); - - ClassDB::bind_method(D_METHOD("init_pose"), &Skeleton::init_pose); - - ClassDB::bind_method(D_METHOD("force_update_all_dirty_bones"), &Skeleton::force_update_all_dirty_bones); - ClassDB::bind_method(D_METHOD("force_update_all_bone_transforms"), &Skeleton::force_update_all_bone_transforms); - ClassDB::bind_method(D_METHOD("force_update_bone_children_transforms", "bone_idx"), &Skeleton::force_update_bone_children_transforms); - - // Helper functions - ClassDB::bind_method(D_METHOD("global_pose_to_world_transform", "global_pose"), &Skeleton::global_pose_to_world_transform); - ClassDB::bind_method(D_METHOD("world_transform_to_global_pose", "world_transform"), &Skeleton::world_transform_to_global_pose); - ClassDB::bind_method(D_METHOD("global_pose_to_local_pose", "bone_idx", "global_pose"), &Skeleton::global_pose_to_local_pose); - ClassDB::bind_method(D_METHOD("local_pose_to_global_pose", "bone_idx", "local_pose"), &Skeleton::local_pose_to_global_pose); - ClassDB::bind_method(D_METHOD("global_pose_z_forward_to_bone_forward", "bone_idx", "basis"), &Skeleton::global_pose_z_forward_to_bone_forward); - - ClassDB::bind_method(D_METHOD("set_show_rest_only"), &Skeleton::set_show_rest_only); - ClassDB::bind_method(D_METHOD("is_show_rest_only"), &Skeleton::is_show_rest_only); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_rest_only"), "set_show_rest_only", "is_show_rest_only"); - -#ifndef _3D_DISABLED - - ClassDB::bind_method(D_METHOD("get_animate_physical_bones"), &Skeleton::get_animate_physical_bones); - ClassDB::bind_method(D_METHOD("set_animate_physical_bones"), &Skeleton::set_animate_physical_bones); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "animate_physical_bones"), "set_animate_physical_bones", "get_animate_physical_bones"); - - ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton::physical_bones_stop_simulation); - ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton::physical_bones_start_simulation_on, DEFVAL(Array())); - ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton::physical_bones_add_collision_exception); - ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton::physical_bones_remove_collision_exception); - -#endif // _3D_DISABLED - - // Modifications - ClassDB::bind_method(D_METHOD("set_modification_stack", "modification_stack"), &Skeleton::set_modification_stack); - ClassDB::bind_method(D_METHOD("get_modification_stack"), &Skeleton::get_modification_stack); - ClassDB::bind_method(D_METHOD("execute_modifications", "delta", "execution_mode"), &Skeleton::execute_modifications); - - //TODO Not sure if this is useful or not in runtime - //#ifdef TOOLS_ENABLED - ADD_SIGNAL(MethodInfo("pose_updated")); - //#endif // TOOLS_ENABLED - - ADD_SIGNAL(MethodInfo("bone_pose_changed", PropertyInfo(Variant::INT, "bone_idx"))); - ADD_SIGNAL(MethodInfo("bones_updated")); - ADD_SIGNAL(MethodInfo("bone_enabled_changed", PropertyInfo(Variant::INT, "bone_idx"))); - ADD_SIGNAL(MethodInfo("show_rest_only_changed")); - - BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON); - - ClassDB::bind_method(D_METHOD("remove_bone", "bone_idx"), &Skeleton::remove_bone); -} - -Skeleton::Skeleton() { - animate_physical_bones = true; - dirty = false; - version = 1; - process_order_dirty = true; - show_rest_only = false; - rest_dirty = false; - updating = 0; -} - -Skeleton::~Skeleton() { - //some skins may remain bound - for (RBSet::Element *E = skin_bindings.front(); E; E = E->next()) { - E->get()->skeleton_node = nullptr; - } -} - -void Skeleton::set_selected_bone(int p_bone) { - selected_bone = p_bone; - update_gizmos(); - return; -} - -int Skeleton::get_selected_bone() const { - return selected_bone; -} - -void Skeleton::remove_bone(const int p_bone_idx) { - ERR_FAIL_INDEX(p_bone_idx, bones.size()); - - for (int i = 0; i < bones.size(); ++i) { - ERR_FAIL_COND_MSG(bones[i].parent == p_bone_idx, "Cannot remove bone if it has children."); - } - -#ifndef _3D_DISABLED - const Bone &bonet = bones[p_bone_idx]; - - ERR_FAIL_COND_MSG(bonet.physical_bone || bonet.cache_parent_physical_bone, "Cannot remove bone if it has a physical bone attached, please remove physical bones first."); -#endif // _3D_DISABLED - - bones.remove(p_bone_idx); - - for (int i = 0; i < bones.size(); ++i) { - int parent = bones[i].parent; - - if (parent > p_bone_idx) { - bones.write[i].parent = parent - 1; - } - } - - process_order_dirty = true; - version++; - - _update_process_order(); - - // pose changed, rebuild cache of inverses - const Bone *bonesptr = bones.ptr(); - int len = bones.size(); - - for (RBSet::Element *E = skin_bindings.front(); E; E = E->next()) { - Ref sr = E->get(); - Ref skin = sr->skin; - - skin->set_bind_count(bones.size()); - - // calculate global rests and invert them - Vector bones_to_process = get_parentless_bones(); - while (bones_to_process.size() > 0) { - int current_bone_idx = bones_to_process[0]; - bones_to_process.erase(current_bone_idx); - const Bone &b = bonesptr[current_bone_idx]; - - // Note: the code below may not work by default. May need to track an integer for the bone pose index order - // in the while loop, instead of using current_bone_idx. - - if (b.parent >= 0) { - skin->set_bind_pose(current_bone_idx, skin->get_bind_pose(b.parent) * b.rest); - } else { - skin->set_bind_pose(current_bone_idx, b.rest); - } - } - - for (int i = 0; i < len; i++) { - //the inverse is what is actually required - skin->set_bind_bone(i, i); - skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse()); - } - } - - property_list_changed_notify(); - _make_dirty(); - update_gizmos(); -} diff --git a/modules/skeleton_3d/nodes/skeleton.h b/modules/skeleton_3d/nodes/skeleton.h deleted file mode 100644 index ffd137d..0000000 --- a/modules/skeleton_3d/nodes/skeleton.h +++ /dev/null @@ -1,314 +0,0 @@ -#ifndef SKELETON_H -#define SKELETON_H -/*************************************************************************/ -/* skeleton.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/object/reference.h" -#include "core/containers/rid.h" -#include "scene/main/spatial.h" - -#ifndef _3D_DISABLED -typedef int BoneId; - -class PhysicalBone; -#endif // _3D_DISABLED - -class SkeletonModificationStack3D; -class Skeleton; -class Skin; - -class SkinReference : public Reference { - GDCLASS(SkinReference, Reference) - friend class Skeleton; - - Skeleton *skeleton_node = nullptr; - RID skeleton; - Ref skin; - uint32_t bind_count = 0; - uint64_t skeleton_version = 0; - Vector skin_bone_indices; - uint32_t *skin_bone_indices_ptrs = nullptr; - void _skin_changed(); - -protected: - static void _bind_methods(); - -public: - RID get_skeleton() const; - Skeleton *get_skeleton_node() const; - Ref get_skin() const; - ~SkinReference(); -}; - -class Skeleton : public Spatial { - GDCLASS(Skeleton, Spatial); - -private: - friend class SkinReference; - - RBSet skin_bindings; - - void _skin_changed(); - - struct Bone { - String name; - - bool enabled; - int parent; - - Transform rest; - Transform global_rest; - - _FORCE_INLINE_ void update_pose_cache() { - if (pose_cache_dirty) { - pose_cache.basis.set_quaternion_scale(pose_rotation, pose_scale); - pose_cache.origin = pose_position; - pose_cache_dirty = false; - } - } - bool pose_cache_dirty; - Transform pose_cache; - Vector3 pose_position; - Quaternion pose_rotation; - Vector3 pose_scale; - - Transform pose_global; - Transform pose_global_no_override; - - float global_pose_override_amount; - bool global_pose_override_reset; - Transform global_pose_override; - -#ifndef _3D_DISABLED - PhysicalBone *physical_bone; - PhysicalBone *cache_parent_physical_bone; -#endif // _3D_DISABLED - - real_t local_pose_override_amount; - bool local_pose_override_reset; - Transform local_pose_override; - - Vector child_bones; - - // The forward direction vector and rest bone forward axis are cached because they do not change - // 99% of the time, but recalculating them can be expensive on models with many bones. - Vector3 rest_bone_forward_vector; - int rest_bone_forward_axis; - - Bone() { - parent = -1; - enabled = true; - pose_cache_dirty = true; - pose_scale = Vector3(1, 1, 1); - global_pose_override_amount = 0; - global_pose_override_reset = false; -#ifndef _3D_DISABLED - physical_bone = nullptr; - cache_parent_physical_bone = nullptr; -#endif // _3D_DISABLED - local_pose_override_amount = 0; - local_pose_override_reset = false; - child_bones = Vector(); - - rest_bone_forward_vector = Vector3(0, 0, 0); - rest_bone_forward_axis = -1; - } - }; - - bool animate_physical_bones; - Vector bones; - bool process_order_dirty; - - Vector parentless_bones; - - void _make_dirty(); - bool dirty; - bool rest_dirty; - int updating; - - bool show_rest_only; - - uint64_t version; - - void _update_process_order(); - -protected: - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - //void _validate_property(PropertyInfo &property) const; - - void _notification(int p_what); - - static void _bind_methods(); - -#ifndef _3D_DISABLED - Ref modification_stack; -#endif // _3D_DISABLED - -public: - enum Bone_Forward_Axis { - BONE_AXIS_X_FORWARD = 0, - BONE_AXIS_Y_FORWARD = 1, - BONE_AXIS_Z_FORWARD = 2, - BONE_AXIS_NEGATIVE_X_FORWARD = 3, - BONE_AXIS_NEGATIVE_Y_FORWARD = 4, - BONE_AXIS_NEGATIVE_Z_FORWARD = 5, - }; - - enum { - - NOTIFICATION_UPDATE_SKELETON = 50 - }; - - // skeleton creation api - void add_bone(const String &p_name); - int find_bone(const String &p_name) const; - String get_bone_name(int p_bone) const; - void set_bone_name(int p_bone, const String &p_name); - - bool is_bone_parent_of(int p_bone_id, int p_parent_bone_id) const; - - void set_bone_parent(int p_bone, int p_parent); - int get_bone_parent(int p_bone) const; - - void unparent_bone_and_rest(int p_bone); - - Vector get_bone_children(int p_bone); - void set_bone_children(int p_bone, Vector p_children); - void add_bone_child(int p_bone, int p_child); - void remove_bone_child(int p_bone, int p_child); - Vector get_parentless_bones(); - - int get_bone_count() const; - - void set_bone_rest(int p_bone, const Transform &p_rest); - Transform get_bone_rest(int p_bone) const; - Transform get_bone_global_rest(int p_bone) const; - Transform get_bone_global_pose(int p_bone) const; - Transform get_bone_global_pose_no_override(int p_bone) const; - - void set_bone_enabled(int p_bone, bool p_enabled); - bool is_bone_enabled(int p_bone) const; - - void set_show_rest_only(bool p_enabled); - bool is_show_rest_only() const; - - void clear_bones(); - - // posing api - void set_bone_pose(int p_bone, const Transform &p_pose); - void set_bone_pose_position(int p_bone, const Vector3 &p_position); - void set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation); - void set_bone_pose_scale(int p_bone, const Vector3 &p_scale); - - Transform get_bone_pose(int p_bone) const; - Vector3 get_bone_pose_position(int p_bone) const; - Quaternion get_bone_pose_rotation(int p_bone) const; - Vector3 get_bone_pose_scale(int p_bone) const; - - void clear_bones_global_pose_override(); - Transform get_bone_global_pose_override(int p_bone) const; - void set_bone_global_pose_override(int p_bone, const Transform &p_pose, real_t p_amount, bool p_persistent = false); - - void clear_bones_local_pose_override(); - Transform get_bone_local_pose_override(int p_bone) const; - void set_bone_local_pose_override(int p_bone, const Transform &p_pose, real_t p_amount, bool p_persistent = false); - - void localize_rests(); // used for loaders and tools - - Ref create_skin_from_rest_transforms(); - - Ref register_skin(const Ref &p_skin); - - void init_pose(); - - void force_update_all_dirty_bones(); - void force_update_all_bone_transforms(); - void force_update_bone_children_transforms(int bone_idx); - - void update_bone_rest_forward_vector(int p_bone, bool p_force_update = false); - void update_bone_rest_forward_axis(int p_bone, bool p_force_update = false); - Vector3 get_bone_axis_forward_vector(int p_bone); - int get_bone_axis_forward_enum(int p_bone); - - // Helper functions - Transform global_pose_to_world_transform(Transform p_global_pose); - Transform world_transform_to_global_pose(Transform p_transform); - Transform global_pose_to_local_pose(int p_bone_idx, Transform p_global_pose); - Transform local_pose_to_global_pose(int p_bone_idx, Transform p_local_pose); - - Basis global_pose_z_forward_to_bone_forward(int p_bone_idx, Basis p_basis); - - // Modifications -#ifndef _3D_DISABLED - Ref get_modification_stack(); - void set_modification_stack(Ref p_stack); - void execute_modifications(real_t p_delta, int p_execution_mode); -#endif // _3D_DISABLED - - // Physical bone API -#ifndef _3D_DISABLED - - void set_animate_physical_bones(bool p_animate); - bool get_animate_physical_bones() const; - - void bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone); - void unbind_physical_bone_from_bone(int p_bone); - - PhysicalBone *get_physical_bone(int p_bone); - PhysicalBone *get_physical_bone_parent(int p_bone); - -private: - /// This is a slow API os it's cached - PhysicalBone *_get_physical_bone_parent(int p_bone); - void _rebuild_physical_bones_cache(); - -public: - void physical_bones_stop_simulation(); - void physical_bones_start_simulation_on(const Array &p_bones); - void physical_bones_add_collision_exception(RID p_exception); - void physical_bones_remove_collision_exception(RID p_exception); -#endif // _3D_DISABLED - -public: - Skeleton(); - ~Skeleton(); - -public: - void set_selected_bone(int p_bone); - int get_selected_bone() const; - - int selected_bone = -1; - - void remove_bone(const int p_bone_idx); -}; - -#endif diff --git a/modules/skeleton_3d/nodes/skeleton_ik.cpp b/modules/skeleton_3d/nodes/skeleton_ik.cpp deleted file mode 100644 index 26f14f1..0000000 --- a/modules/skeleton_3d/nodes/skeleton_ik.cpp +++ /dev/null @@ -1,595 +0,0 @@ -/*************************************************************************/ -/* skeleton_ik.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -/** - * @author AndreaCatania - */ - -#include "skeleton_ik.h" - -#ifndef _3D_DISABLED - -#include "skeleton.h" - -FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::find_child(const BoneId p_bone_id) { - for (int i = children.size() - 1; 0 <= i; --i) { - if (p_bone_id == children[i].bone) { - return &children.write[i]; - } - } - return nullptr; -} - -FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::add_child(const BoneId p_bone_id) { - const int infant_child_id = children.size(); - children.resize(infant_child_id + 1); - children.write[infant_child_id].bone = p_bone_id; - children.write[infant_child_id].parent_item = this; - return &children.write[infant_child_id]; -} - -/// Build a chain that starts from the root to tip -bool FabrikInverseKinematic::build_chain(Task *p_task, bool p_force_simple_chain) { - ERR_FAIL_COND_V(-1 == p_task->root_bone, false); - - Chain &chain(p_task->chain); - - chain.tips.resize(p_task->end_effectors.size()); - chain.chain_root.bone = p_task->root_bone; - chain.chain_root.initial_transform = p_task->skeleton->get_bone_global_pose(chain.chain_root.bone); - chain.chain_root.current_pos = chain.chain_root.initial_transform.origin; - chain.middle_chain_item = nullptr; - - // Holds all IDs that are composing a single chain in reverse order - Vector chain_ids; - // This is used to know the chain size - int sub_chain_size; - // Resize only one time in order to fit all joints for performance reason - chain_ids.resize(p_task->skeleton->get_bone_count()); - - for (int x = p_task->end_effectors.size() - 1; 0 <= x; --x) { - const EndEffector *ee(&p_task->end_effectors[x]); - ERR_FAIL_COND_V(p_task->root_bone >= ee->tip_bone, false); - ERR_FAIL_INDEX_V(ee->tip_bone, p_task->skeleton->get_bone_count(), false); - - sub_chain_size = 0; - // Picks all IDs that composing a single chain in reverse order (except the root) - BoneId chain_sub_tip(ee->tip_bone); - while (chain_sub_tip > p_task->root_bone) { - chain_ids.write[sub_chain_size++] = chain_sub_tip; - chain_sub_tip = p_task->skeleton->get_bone_parent(chain_sub_tip); - } - - BoneId middle_chain_item_id = (((float)sub_chain_size) * 0.5); - - // Build chain by reading chain ids in reverse order - // For each chain item id will be created a ChainItem if doesn't exists - ChainItem *sub_chain(&chain.chain_root); - for (int i = sub_chain_size - 1; 0 <= i; --i) { - ChainItem *child_ci(sub_chain->find_child(chain_ids[i])); - if (!child_ci) { - child_ci = sub_chain->add_child(chain_ids[i]); - - child_ci->initial_transform = p_task->skeleton->get_bone_global_pose(child_ci->bone); - child_ci->current_pos = child_ci->initial_transform.origin; - - if (child_ci->parent_item) { - child_ci->length = (child_ci->current_pos - child_ci->parent_item->current_pos).length(); - } - } - - sub_chain = child_ci; - - if (middle_chain_item_id == i) { - chain.middle_chain_item = child_ci; - } - } - - if (!middle_chain_item_id) { - chain.middle_chain_item = nullptr; - } - - // Initialize current tip - chain.tips.write[x].chain_item = sub_chain; - chain.tips.write[x].end_effector = ee; - - if (p_force_simple_chain) { - // NOTE: - // This is an "hack" that force to create only one tip per chain since the solver of multi tip (end effector) - // is not yet created. - // Remove this code when this is done - break; - } - } - return true; -} - -void FabrikInverseKinematic::solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos) { - real_t distance_to_goal(1e4); - real_t previous_distance_to_goal(0); - int can_solve(p_task->max_iterations); - while (distance_to_goal > p_task->min_distance && Math::abs(previous_distance_to_goal - distance_to_goal) > 0.005 && can_solve) { - previous_distance_to_goal = distance_to_goal; - --can_solve; - - solve_simple_backwards(p_task->chain, p_solve_magnet); - solve_simple_forwards(p_task->chain, p_solve_magnet, p_origin_pos); - - distance_to_goal = (p_task->chain.tips[0].chain_item->current_pos - p_task->chain.tips[0].end_effector->goal_transform.origin).length(); - } -} - -void FabrikInverseKinematic::solve_simple_backwards(Chain &r_chain, bool p_solve_magnet) { - if (p_solve_magnet && !r_chain.middle_chain_item) { - return; - } - - Vector3 goal; - ChainItem *sub_chain_tip; - if (p_solve_magnet) { - goal = r_chain.magnet_position; - sub_chain_tip = r_chain.middle_chain_item; - } else { - goal = r_chain.tips[0].end_effector->goal_transform.origin; - sub_chain_tip = r_chain.tips[0].chain_item; - } - - while (sub_chain_tip) { - sub_chain_tip->current_pos = goal; - - if (sub_chain_tip->parent_item) { - // Not yet in the chain root - // So calculate next goal location - - const Vector3 look_parent((sub_chain_tip->parent_item->current_pos - sub_chain_tip->current_pos).normalized()); - goal = sub_chain_tip->current_pos + (look_parent * sub_chain_tip->length); - - // [TODO] Constraints goes here - } - - sub_chain_tip = sub_chain_tip->parent_item; - } -} - -void FabrikInverseKinematic::solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos) { - if (p_solve_magnet && !r_chain.middle_chain_item) { - return; - } - - ChainItem *sub_chain_root(&r_chain.chain_root); - Vector3 origin = p_origin_pos; - - while (sub_chain_root) { // Reach the tip - sub_chain_root->current_pos = origin; - - if (!sub_chain_root->children.empty()) { - ChainItem &child(sub_chain_root->children.write[0]); - - // Is not tip - // So calculate next origin location - - // Look child - sub_chain_root->current_ori = (child.current_pos - sub_chain_root->current_pos).normalized(); - origin = sub_chain_root->current_pos + (sub_chain_root->current_ori * child.length); - - // [TODO] Constraints goes here - - if (p_solve_magnet && sub_chain_root == r_chain.middle_chain_item) { - // In case of magnet solving this is the tip - sub_chain_root = nullptr; - } else { - sub_chain_root = &child; - } - } else { - // Is tip - sub_chain_root = nullptr; - } - } -} - -FabrikInverseKinematic::Task *FabrikInverseKinematic::create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform) { - FabrikInverseKinematic::EndEffector ee; - ee.tip_bone = tip_bone; - - Task *task(memnew(Task)); - task->skeleton = p_sk; - task->root_bone = root_bone; - task->end_effectors.push_back(ee); - task->goal_global_transform = goal_transform; - - if (!build_chain(task)) { - free_task(task); - return nullptr; - } - - return task; -} - -void FabrikInverseKinematic::free_task(Task *p_task) { - if (p_task) { - memdelete(p_task); - } -} - -void FabrikInverseKinematic::set_goal(Task *p_task, const Transform &p_goal) { - p_task->goal_global_transform = p_goal; -} - -void FabrikInverseKinematic::make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta) { - if (blending_delta >= 0.99f) { - // Update the end_effector (local transform) without blending - p_task->end_effectors.write[0].goal_transform = p_inverse_transf * p_task->goal_global_transform; - } else { - // End effector in local transform - const Transform end_effector_pose(p_task->skeleton->get_bone_global_pose_no_override(p_task->end_effectors[0].tip_bone)); - - // Update the end_effector (local transform) by blending with current pose - p_task->end_effectors.write[0].goal_transform = end_effector_pose.interpolate_with(p_inverse_transf * p_task->goal_global_transform, blending_delta); - } -} - -void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position) { - if (blending_delta <= 0.01f) { - // Before skipping, make sure we undo the global pose overrides - ChainItem *ci(&p_task->chain.chain_root); - while (ci) { - p_task->skeleton->set_bone_global_pose_override(ci->bone, ci->initial_transform, 0.0, false); - - if (!ci->children.empty()) { - ci = &ci->children.write[0]; - } else { - ci = nullptr; - } - } - - return; // Skip solving - } - - // Update the initial root transform so its synced with any animation changes - _update_chain(p_task->skeleton, &p_task->chain.chain_root); - - p_task->skeleton->set_bone_global_pose_override(p_task->chain.chain_root.bone, Transform(), 0.0, false); - Vector3 origin_pos = p_task->skeleton->get_bone_global_pose(p_task->chain.chain_root.bone).origin; - - make_goal(p_task, p_task->skeleton->get_global_transform().affine_inverse(), blending_delta); - - if (p_use_magnet && p_task->chain.middle_chain_item) { - p_task->chain.magnet_position = p_task->chain.middle_chain_item->initial_transform.origin.linear_interpolate(p_magnet_position, blending_delta); - solve_simple(p_task, true, origin_pos); - } - solve_simple(p_task, false, origin_pos); - - // Assign new bone position. - ChainItem *ci(&p_task->chain.chain_root); - while (ci) { - Transform new_bone_pose(ci->initial_transform); - new_bone_pose.origin = ci->current_pos; - - if (!ci->children.empty()) { - /// Rotate basis - const Vector3 initial_ori((ci->children[0].initial_transform.origin - ci->initial_transform.origin).normalized()); - const Vector3 rot_axis(initial_ori.cross(ci->current_ori).normalized()); - - if (rot_axis[0] != 0 && rot_axis[1] != 0 && rot_axis[2] != 0) { - // acos does clamping. - const real_t rot_angle(Math::acos(initial_ori.dot(ci->current_ori))); - new_bone_pose.basis.rotate(rot_axis, rot_angle); - } - - } else { - // Set target orientation to tip - if (override_tip_basis) - new_bone_pose.basis = p_task->chain.tips[0].end_effector->goal_transform.basis; - else - new_bone_pose.basis = new_bone_pose.basis * p_task->chain.tips[0].end_effector->goal_transform.basis; - } - - // IK should not affect scale, so undo any scaling - new_bone_pose.basis.orthonormalize(); - new_bone_pose.basis.scale(p_task->skeleton->get_bone_global_pose(ci->bone).basis.get_scale()); - - p_task->skeleton->set_bone_global_pose_override(ci->bone, new_bone_pose, 1.0, true); - - if (!ci->children.empty()) { - ci = &ci->children.write[0]; - } else { - ci = nullptr; - } - } -} - -void FabrikInverseKinematic::_update_chain(const Skeleton *p_sk, ChainItem *p_chain_item) { - if (!p_chain_item) { - return; - } - - p_chain_item->initial_transform = p_sk->get_bone_global_pose_no_override(p_chain_item->bone); - p_chain_item->current_pos = p_chain_item->initial_transform.origin; - - ChainItem *items = p_chain_item->children.ptrw(); - for (int i = 0; i < p_chain_item->children.size(); i += 1) { - _update_chain(p_sk, items + i); - } -} - -void SkeletonIK::_validate_property(PropertyInfo &property) const { - if (property.name == "root_bone" || property.name == "tip_bone") { - if (skeleton) { - String names("--,"); - for (int i = 0; i < skeleton->get_bone_count(); i++) { - if (i > 0) { - names += ","; - } - names += skeleton->get_bone_name(i); - } - - property.hint = PROPERTY_HINT_ENUM; - property.hint_string = names; - } else { - property.hint = PROPERTY_HINT_NONE; - property.hint_string = ""; - } - } -} - -void SkeletonIK::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_root_bone", "root_bone"), &SkeletonIK::set_root_bone); - ClassDB::bind_method(D_METHOD("get_root_bone"), &SkeletonIK::get_root_bone); - - ClassDB::bind_method(D_METHOD("set_tip_bone", "tip_bone"), &SkeletonIK::set_tip_bone); - ClassDB::bind_method(D_METHOD("get_tip_bone"), &SkeletonIK::get_tip_bone); - - ClassDB::bind_method(D_METHOD("set_interpolation", "interpolation"), &SkeletonIK::set_interpolation); - ClassDB::bind_method(D_METHOD("get_interpolation"), &SkeletonIK::get_interpolation); - - ClassDB::bind_method(D_METHOD("set_target_transform", "target"), &SkeletonIK::set_target_transform); - ClassDB::bind_method(D_METHOD("get_target_transform"), &SkeletonIK::get_target_transform); - - ClassDB::bind_method(D_METHOD("set_target_node", "node"), &SkeletonIK::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonIK::get_target_node); - - ClassDB::bind_method(D_METHOD("set_override_tip_basis", "override"), &SkeletonIK::set_override_tip_basis); - ClassDB::bind_method(D_METHOD("is_override_tip_basis"), &SkeletonIK::is_override_tip_basis); - - ClassDB::bind_method(D_METHOD("set_use_magnet", "use"), &SkeletonIK::set_use_magnet); - ClassDB::bind_method(D_METHOD("is_using_magnet"), &SkeletonIK::is_using_magnet); - - ClassDB::bind_method(D_METHOD("set_magnet_position", "local_position"), &SkeletonIK::set_magnet_position); - ClassDB::bind_method(D_METHOD("get_magnet_position"), &SkeletonIK::get_magnet_position); - - ClassDB::bind_method(D_METHOD("get_parent_skeleton"), &SkeletonIK::get_parent_skeleton); - ClassDB::bind_method(D_METHOD("is_running"), &SkeletonIK::is_running); - - ClassDB::bind_method(D_METHOD("set_min_distance", "min_distance"), &SkeletonIK::set_min_distance); - ClassDB::bind_method(D_METHOD("get_min_distance"), &SkeletonIK::get_min_distance); - - ClassDB::bind_method(D_METHOD("set_max_iterations", "iterations"), &SkeletonIK::set_max_iterations); - ClassDB::bind_method(D_METHOD("get_max_iterations"), &SkeletonIK::get_max_iterations); - - ClassDB::bind_method(D_METHOD("start", "one_time"), &SkeletonIK::start, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("stop"), &SkeletonIK::stop); - - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "root_bone"), "set_root_bone", "get_root_bone"); - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "tip_bone"), "set_tip_bone", "get_tip_bone"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "interpolation", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_interpolation", "get_interpolation"); - ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "target"), "set_target_transform", "get_target_transform"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_tip_basis"), "set_override_tip_basis", "is_override_tip_basis"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_magnet"), "set_use_magnet", "is_using_magnet"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "magnet"), "set_magnet_position", "get_magnet_position"); - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node"), "set_target_node", "get_target_node"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_distance"), "set_min_distance", "get_min_distance"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "max_iterations"), "set_max_iterations", "get_max_iterations"); -} - -void SkeletonIK::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - skeleton = Object::cast_to(get_parent()); - set_process_priority(1); - reload_chain(); - } break; - case NOTIFICATION_INTERNAL_PROCESS: { - if (target_node_override) { - reload_goal(); - } - - _solve_chain(); - - } break; - case NOTIFICATION_EXIT_TREE: { - reload_chain(); - } break; - } -} - -SkeletonIK::SkeletonIK() : - interpolation(1), - override_tip_basis(true), - use_magnet(false), - min_distance(0.01), - max_iterations(10), - skeleton(nullptr), - target_node_override(nullptr), - task(nullptr) { -} - -SkeletonIK::~SkeletonIK() { - FabrikInverseKinematic::free_task(task); - task = nullptr; -} - -void SkeletonIK::set_root_bone(const StringName &p_root_bone) { - root_bone = p_root_bone; - reload_chain(); -} - -StringName SkeletonIK::get_root_bone() const { - return root_bone; -} - -void SkeletonIK::set_tip_bone(const StringName &p_tip_bone) { - tip_bone = p_tip_bone; - reload_chain(); -} - -StringName SkeletonIK::get_tip_bone() const { - return tip_bone; -} - -void SkeletonIK::set_interpolation(real_t p_interpolation) { - interpolation = p_interpolation; -} - -real_t SkeletonIK::get_interpolation() const { - return interpolation; -} - -void SkeletonIK::set_target_transform(const Transform &p_target) { - target = p_target; - reload_goal(); -} - -const Transform &SkeletonIK::get_target_transform() const { - return target; -} - -void SkeletonIK::set_target_node(const NodePath &p_node) { - target_node_path_override = p_node; - target_node_override = nullptr; - reload_goal(); -} - -NodePath SkeletonIK::get_target_node() { - return target_node_path_override; -} - -void SkeletonIK::set_override_tip_basis(bool p_override) { - override_tip_basis = p_override; -} - -bool SkeletonIK::is_override_tip_basis() const { - return override_tip_basis; -} - -void SkeletonIK::set_use_magnet(bool p_use) { - use_magnet = p_use; -} - -bool SkeletonIK::is_using_magnet() const { - return use_magnet; -} - -void SkeletonIK::set_magnet_position(const Vector3 &p_local_position) { - magnet_position = p_local_position; -} - -const Vector3 &SkeletonIK::get_magnet_position() const { - return magnet_position; -} - -void SkeletonIK::set_min_distance(real_t p_min_distance) { - min_distance = p_min_distance; -} - -void SkeletonIK::set_max_iterations(int p_iterations) { - max_iterations = p_iterations; -} - -bool SkeletonIK::is_running() { - return is_processing_internal(); -} - -void SkeletonIK::start(bool p_one_time) { - if (p_one_time) { - set_process_internal(false); - - if (target_node_override) { - reload_goal(); - } - - _solve_chain(); - } else { - set_process_internal(true); - } -} - -void SkeletonIK::stop() { - set_process_internal(false); - if (skeleton) { - skeleton->clear_bones_global_pose_override(); - } -} - -Transform SkeletonIK::_get_target_transform() { - if (!target_node_override && !target_node_path_override.is_empty()) { - target_node_override = Object::cast_to(get_node(target_node_path_override)); - } - - if (target_node_override && target_node_override->is_inside_tree()) { - // Make sure to use the interpolated transform as target. - // This will pass through to get_global_transform() when physics interpolation is off, and when using interpolation, - // ensure that the target matches the interpolated visual position of the target when updating the IK each frame. - return target_node_override->get_global_transform_interpolated(); - } else { - return target; - } -} - -void SkeletonIK::reload_chain() { - FabrikInverseKinematic::free_task(task); - task = nullptr; - - if (!skeleton) { - return; - } - - task = FabrikInverseKinematic::create_simple_task(skeleton, skeleton->find_bone(root_bone), skeleton->find_bone(tip_bone), _get_target_transform()); - if (task) { - task->max_iterations = max_iterations; - task->min_distance = min_distance; - } -} - -void SkeletonIK::reload_goal() { - if (!task) { - return; - } - - FabrikInverseKinematic::set_goal(task, _get_target_transform()); -} - -void SkeletonIK::_solve_chain() { - if (!task) { - return; - } - FabrikInverseKinematic::solve(task, interpolation, override_tip_basis, use_magnet, magnet_position); -} - -#endif // _3D_DISABLED diff --git a/modules/skeleton_3d/nodes/skeleton_ik.h b/modules/skeleton_3d/nodes/skeleton_ik.h deleted file mode 100644 index fc8b6a1..0000000 --- a/modules/skeleton_3d/nodes/skeleton_ik.h +++ /dev/null @@ -1,215 +0,0 @@ -#ifndef SKELETON_IK_H -#define SKELETON_IK_H -/*************************************************************************/ -/* skeleton_ik.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef _3D_DISABLED - -/** - * @author AndreaCatania - */ - -#include "core/math/transform.h" -#include "scene/main/node.h" - -class Skeleton; - -typedef int BoneId; - -class FabrikInverseKinematic { - struct EndEffector { - BoneId tip_bone; - Transform goal_transform; - }; - - struct ChainItem { - Vector children; - ChainItem *parent_item; - - // Bone info - BoneId bone; - - real_t length; - /// Positions relative to root bone - Transform initial_transform; - Vector3 current_pos; - // Direction from this bone to child - Vector3 current_ori; - - ChainItem() : - parent_item(nullptr), - bone(-1), - length(0) {} - - ChainItem *find_child(const BoneId p_bone_id); - ChainItem *add_child(const BoneId p_bone_id); - }; - - struct ChainTip { - ChainItem *chain_item; - const EndEffector *end_effector; - - ChainTip() : - chain_item(nullptr), - end_effector(nullptr) {} - - ChainTip(ChainItem *p_chain_item, const EndEffector *p_end_effector) : - chain_item(p_chain_item), - end_effector(p_end_effector) {} - }; - - struct Chain { - ChainItem chain_root; - ChainItem *middle_chain_item; - Vector tips; - Vector3 magnet_position; - }; - -public: - struct Task : public RID_Data { - RID self; - Skeleton *skeleton; - - Chain chain; - - // Settings - real_t min_distance; - int max_iterations; - - // Bone data - BoneId root_bone; - Vector end_effectors; - - Transform goal_global_transform; - - Task() : - skeleton(nullptr), - min_distance(0.01), - max_iterations(10), - root_bone(-1) {} - }; - -private: - /// Init a chain that starts from the root to tip - static bool build_chain(Task *p_task, bool p_force_simple_chain = true); - - static void solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos); - /// Special solvers that solve only chains with one end effector - static void solve_simple_backwards(Chain &r_chain, bool p_solve_magnet); - static void solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos); - -public: - static Task *create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform); - static void free_task(Task *p_task); - // The goal of chain should be always in local space - static void set_goal(Task *p_task, const Transform &p_goal); - static void make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta); - static void solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position); - - static void _update_chain(const Skeleton *p_skeleton, ChainItem *p_chain_item); -}; - -class SkeletonIK : public Node { - GDCLASS(SkeletonIK, Node); - - StringName root_bone; - StringName tip_bone; - real_t interpolation; - Transform target; - NodePath target_node_path_override; - bool override_tip_basis; - bool use_magnet; - Vector3 magnet_position; - - real_t min_distance; - int max_iterations; - - Skeleton *skeleton; - Spatial *target_node_override; - FabrikInverseKinematic::Task *task; - -protected: - virtual void - _validate_property(PropertyInfo &property) const; - - static void _bind_methods(); - virtual void _notification(int p_what); - -public: - SkeletonIK(); - virtual ~SkeletonIK(); - - void set_root_bone(const StringName &p_root_bone); - StringName get_root_bone() const; - - void set_tip_bone(const StringName &p_tip_bone); - StringName get_tip_bone() const; - - void set_interpolation(real_t p_interpolation); - real_t get_interpolation() const; - - void set_target_transform(const Transform &p_target); - const Transform &get_target_transform() const; - - void set_target_node(const NodePath &p_node); - NodePath get_target_node(); - - void set_override_tip_basis(bool p_override); - bool is_override_tip_basis() const; - - void set_use_magnet(bool p_use); - bool is_using_magnet() const; - - void set_magnet_position(const Vector3 &p_local_position); - const Vector3 &get_magnet_position() const; - - void set_min_distance(real_t p_min_distance); - real_t get_min_distance() const { return min_distance; } - - void set_max_iterations(int p_iterations); - int get_max_iterations() const { return max_iterations; } - - Skeleton *get_parent_skeleton() const { return skeleton; } - - bool is_running(); - - void start(bool p_one_time = false); - void stop(); - -private: - Transform _get_target_transform(); - void reload_chain(); - void reload_goal(); - void _solve_chain(); -}; - -#endif // _3D_DISABLED - -#endif // SKELETON_IK_H diff --git a/modules/skeleton_3d/register_types.cpp b/modules/skeleton_3d/register_types.cpp deleted file mode 100644 index b051f00..0000000 --- a/modules/skeleton_3d/register_types.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include "register_types.h" - -#include "nodes/skeleton.h" -#include "resources/skin.h" - -#include "nodes/bone_attachment.h" -#include "nodes/skeleton_ik.h" - -#include "nodes/physical_bone.h" - -#include "resources/skeleton_modification_3d.h" -#include "resources/skeleton_modification_3d_ccdik.h" -#include "resources/skeleton_modification_3d_fabrik.h" -#include "resources/skeleton_modification_3d_jiggle.h" -#include "resources/skeleton_modification_3d_lookat.h" -#include "resources/skeleton_modification_3d_stackholder.h" -#include "resources/skeleton_modification_3d_twoboneik.h" -#include "resources/skeleton_modification_stack_3d.h" - -#ifdef TOOLS_ENABLED -#include "editor/physical_bone_plugin.h" -#include "editor/skeleton_editor_plugin.h" -#include "editor/skeleton_ik_editor_plugin.h" -#endif - -void register_skeleton_3d_types(ModuleRegistrationLevel p_level) { - if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) { - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - - ClassDB::register_class(); - - ClassDB::register_class(); - - ClassDB::register_class(); - - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - } - -#ifdef TOOLS_ENABLED - if (p_level == MODULE_REGISTRATION_LEVEL_EDITOR) { - EditorPlugins::add_by_type(); - EditorPlugins::add_by_type(); - EditorPlugins::add_by_type(); - } -#endif -} - -void unregister_skeleton_3d_types(ModuleRegistrationLevel p_level) { -} diff --git a/modules/skeleton_3d/register_types.h b/modules/skeleton_3d/register_types.h deleted file mode 100644 index af5bfec..0000000 --- a/modules/skeleton_3d/register_types.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef SKELETON_3D_REGISTER_TYPES_H -#define SKELETON_3D_REGISTER_TYPES_H - -#include "modules/register_module_types.h" - - -void register_skeleton_3d_types(ModuleRegistrationLevel p_level); -void unregister_skeleton_3d_types(ModuleRegistrationLevel p_level); - -#endif \ No newline at end of file diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d.cpp deleted file mode 100644 index 011a4b5..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d.h" -#include "../nodes/skeleton.h" - -#include "skeleton_modification_stack_3d.h" - -void SkeletonModification3D::set_enabled(bool p_enabled) { - enabled = p_enabled; -} - -bool SkeletonModification3D::get_enabled() { - return enabled; -} - -// Helper function. Needed for CCDIK. -real_t SkeletonModification3D::clamp_angle(real_t p_angle, real_t p_min_bound, real_t p_max_bound, bool p_invert) { - // Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range. - if (p_angle < 0) { - p_angle = Math_TAU + p_angle; - } - - // Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order - if (p_min_bound < 0) { - p_min_bound = Math_TAU + p_min_bound; - } - if (p_max_bound < 0) { - p_max_bound = Math_TAU + p_max_bound; - } - if (p_min_bound > p_max_bound) { - SWAP(p_min_bound, p_max_bound); - } - - bool is_beyond_bounds = (p_angle < p_min_bound || p_angle > p_max_bound); - bool is_within_bounds = (p_angle > p_min_bound && p_angle < p_max_bound); - - // Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle. - if ((!p_invert && is_beyond_bounds) || (p_invert && is_within_bounds)) { - Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound)); - Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound)); - Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle)); - - if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) { - p_angle = p_min_bound; - } else { - p_angle = p_max_bound; - } - } - - return p_angle; -} - -bool SkeletonModification3D::_print_execution_error(bool p_condition, String p_message) { - // If the modification is not setup, don't bother printing the error - if (!is_setup) { - return p_condition; - } - - if (p_condition && !execution_error_found) { - ERR_PRINT(p_message); - execution_error_found = true; - } - return p_condition; -} - -Ref SkeletonModification3D::get_modification_stack() { - return stack; -} - -void SkeletonModification3D::set_is_setup(bool p_is_setup) { - is_setup = p_is_setup; -} - -bool SkeletonModification3D::get_is_setup() const { - return is_setup; -} - -void SkeletonModification3D::set_execution_mode(int p_mode) { - execution_mode = p_mode; -} - -int SkeletonModification3D::get_execution_mode() const { - return execution_mode; -} - -void SkeletonModification3D::execute(real_t p_delta) { - if (!enabled) { - return; - } - - call("_execute", p_delta); -} - -void SkeletonModification3D::setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack) { - is_setup = true; - } else { - WARN_PRINT("Could not setup modification with name " + this->get_name()); - } - - call("_setup_modification", p_stack); -} - -void SkeletonModification3D::_execute(real_t p_delta) { -} -void SkeletonModification3D::_setup_modification(Ref p_stack) { -} - -void SkeletonModification3D::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModification3D::set_enabled); - ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModification3D::get_enabled); - ClassDB::bind_method(D_METHOD("get_modification_stack"), &SkeletonModification3D::get_modification_stack); - ClassDB::bind_method(D_METHOD("set_is_setup", "is_setup"), &SkeletonModification3D::set_is_setup); - ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModification3D::get_is_setup); - ClassDB::bind_method(D_METHOD("set_execution_mode", "execution_mode"), &SkeletonModification3D::set_execution_mode); - ClassDB::bind_method(D_METHOD("get_execution_mode"), &SkeletonModification3D::get_execution_mode); - ClassDB::bind_method(D_METHOD("clamp_angle", "angle", "min", "max", "invert"), &SkeletonModification3D::clamp_angle); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "execution_mode", PROPERTY_HINT_ENUM, "process, physics_process"), "set_execution_mode", "get_execution_mode"); - - ClassDB::bind_method(D_METHOD("execute", "p_delta"), &SkeletonModification3D::execute); - ClassDB::bind_method(D_METHOD("setup_modification", "stack"), &SkeletonModification3D::setup_modification); - - BIND_VMETHOD(MethodInfo("_execute", PropertyInfo(Variant::OBJECT, "delta"))); - BIND_VMETHOD(MethodInfo("_setup_modification", PropertyInfo(Variant::OBJECT, "stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack3D"))); - ClassDB::bind_method(D_METHOD("_execute", "p_delta"), &SkeletonModification3D::_execute); - ClassDB::bind_method(D_METHOD("_setup_modification", "stack"), &SkeletonModification3D::_setup_modification); -} - -SkeletonModification3D::SkeletonModification3D() { - stack = nullptr; - is_setup = false; - - execution_mode = 0; // 0 = process - - enabled = true; - execution_error_found = false; -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d.h b/modules/skeleton_3d/resources/skeleton_modification_3d.h deleted file mode 100644 index 27be76b..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d.h +++ /dev/null @@ -1,79 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef SKELETON_MODIFICATION_3D_H -#define SKELETON_MODIFICATION_3D_H - -#include "core/object/reference.h" -#include "../nodes/skeleton.h" - -class SkeletonModificationStack3D; - -class SkeletonModification3D : public Resource { - GDCLASS(SkeletonModification3D, Resource); - friend class Skeleton; - friend class SkeletonModificationStack3D; - -protected: - static void _bind_methods(); - - SkeletonModificationStack3D *stack; - int execution_mode; // 0 = process - - bool enabled; - bool is_setup; - bool execution_error_found; - - bool _print_execution_error(bool p_condition, String p_message); - -public: - real_t clamp_angle(real_t p_angle, real_t p_min_bound, real_t p_max_bound, bool p_invert); - - void set_enabled(bool p_enabled); - bool get_enabled(); - - void set_execution_mode(int p_mode); - int get_execution_mode() const; - - Ref get_modification_stack(); - - void set_is_setup(bool p_setup); - bool get_is_setup() const; - - void execute(real_t p_delta); - void setup_modification(Ref p_stack); - - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - SkeletonModification3D(); -}; - -#endif // SKELETON_MODIFICATION_3D_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.cpp deleted file mode 100644 index 0e5c093..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.cpp +++ /dev/null @@ -1,479 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_ccdik.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_ccdik.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" - -bool SkeletonModification3DCCDIK::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path.begins_with("joint_data/")) { - int ccdik_data_size = ccdik_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, ccdik_data_size, false); - - if (what == "bone_name") { - set_ccdik_joint_bone_name(which, p_value); - } else if (what == "bone_index") { - set_ccdik_joint_bone_index(which, p_value); - } else if (what == "ccdik_axis") { - set_ccdik_joint_ccdik_axis(which, p_value); - } else if (what == "enable_joint_constraint") { - set_ccdik_joint_enable_constraint(which, p_value); - } else if (what == "joint_constraint_angle_min") { - set_ccdik_joint_constraint_angle_min(which, Math::deg2rad(real_t(p_value))); - } else if (what == "joint_constraint_angle_max") { - set_ccdik_joint_constraint_angle_max(which, Math::deg2rad(real_t(p_value))); - } else if (what == "joint_constraint_angles_invert") { - set_ccdik_joint_constraint_invert(which, p_value); - } - return true; - } - return true; -} - -bool SkeletonModification3DCCDIK::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path.begins_with("joint_data/")) { - const int ccdik_data_size = ccdik_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, ccdik_data_size, false); - - if (what == "bone_name") { - r_ret = get_ccdik_joint_bone_name(which); - } else if (what == "bone_index") { - r_ret = get_ccdik_joint_bone_index(which); - } else if (what == "ccdik_axis") { - r_ret = get_ccdik_joint_ccdik_axis(which); - } else if (what == "enable_joint_constraint") { - r_ret = get_ccdik_joint_enable_constraint(which); - } else if (what == "joint_constraint_angle_min") { - r_ret = Math::rad2deg(get_ccdik_joint_constraint_angle_min(which)); - } else if (what == "joint_constraint_angle_max") { - r_ret = Math::rad2deg(get_ccdik_joint_constraint_angle_max(which)); - } else if (what == "joint_constraint_angles_invert") { - r_ret = get_ccdik_joint_constraint_invert(which); - } - return true; - } - return true; -} - -void SkeletonModification3DCCDIK::_get_property_list(List *p_list) const { - for (uint32_t i = 0; i < ccdik_data_chain.size(); i++) { - String base_string = "joint_data/" + itos(i) + "/"; - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, base_string + "bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - - p_list->push_back(PropertyInfo(Variant::INT, base_string + "ccdik_axis", - PROPERTY_HINT_ENUM, "X Axis, Y Axis, Z Axis", PROPERTY_USAGE_DEFAULT)); - - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "enable_joint_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (ccdik_data_chain[i].enable_constraint) { - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "joint_constraint_angle_min", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "joint_constraint_angle_max", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "joint_constraint_angles_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - } - } -} - -void SkeletonModification3DCCDIK::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, "Modification is not setup and therefore cannot execute!"); - - if (!enabled) { - return; - } - - if (target_node_cache== 0) { - _print_execution_error(true, "Target cache is out of date. Attempting to update"); - update_target_cache(); - return; - } - if (tip_node_cache == 0) { - _print_execution_error(true, "Tip cache is out of date. Attempting to update"); - update_tip_cache(); - return; - } - - // Reset the local bone overrides for CCDIK affected nodes - for (uint32_t i = 0; i < ccdik_data_chain.size(); i++) { - stack->skeleton->set_bone_local_pose_override(ccdik_data_chain[i].bone_idx, - stack->skeleton->get_bone_local_pose_override(ccdik_data_chain[i].bone_idx), - 0.0, false); - } - - Spatial *node_target = Object::cast_to(ObjectDB::get_instance(target_node_cache)); - Spatial *node_tip = Object::cast_to(ObjectDB::get_instance(tip_node_cache)); - - if (_print_execution_error(!node_target || !node_target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!")) { - return; - } - if (_print_execution_error(!node_tip || !node_tip->is_inside_tree(), "Tip node is not in the scene tree. Cannot execute modification!")) { - return; - } - - if (use_high_quality_solve) { - for (uint32_t i = 0; i < ccdik_data_chain.size(); i++) { - for (uint32_t j = i; j < ccdik_data_chain.size(); j++) { - _execute_ccdik_joint(j, node_target, node_tip); - } - } - } else { - for (uint32_t i = 0; i < ccdik_data_chain.size(); i++) { - _execute_ccdik_joint(i, node_target, node_tip); - } - } - - execution_error_found = false; -} - -void SkeletonModification3DCCDIK::_execute_ccdik_joint(int p_joint_idx, Spatial *p_target, Spatial *p_tip) { - CCDIK_Joint_Data ccdik_data = ccdik_data_chain[p_joint_idx]; - - if (_print_execution_error(ccdik_data.bone_idx < 0 || ccdik_data.bone_idx > stack->skeleton->get_bone_count(), - "CCDIK joint: bone index for joint" + itos(p_joint_idx) + " not found. Cannot execute modification!")) { - return; - } - - Transform bone_trans = stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->get_bone_global_pose(ccdik_data.bone_idx)); - Transform tip_trans = stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(p_tip->get_global_transform())); - Transform target_trans = stack->skeleton->global_pose_to_local_pose(ccdik_data.bone_idx, stack->skeleton->world_transform_to_global_pose(p_target->get_global_transform())); - - if (tip_trans.origin.distance_to(target_trans.origin) <= 0.01) { - return; - } - - // Inspired (and very loosely based on) by the CCDIK algorithm made by Zalo on GitHub (https://github.com/zalo/MathUtilities) - // Convert the 3D position to a 2D position so we can use Atan2 (via the angle function) - // to know how much rotation we need on the given axis to place the tip at the target. - Vector2 tip_pos_2d; - Vector2 target_pos_2d; - if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_X) { - tip_pos_2d = Vector2(tip_trans.origin.y, tip_trans.origin.z); - target_pos_2d = Vector2(target_trans.origin.y, target_trans.origin.z); - bone_trans.basis.rotate_local(Vector3(1, 0, 0), target_pos_2d.angle() - tip_pos_2d.angle()); - } else if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_Y) { - tip_pos_2d = Vector2(tip_trans.origin.z, tip_trans.origin.x); - target_pos_2d = Vector2(target_trans.origin.z, target_trans.origin.x); - bone_trans.basis.rotate_local(Vector3(0, 1, 0), target_pos_2d.angle() - tip_pos_2d.angle()); - } else if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_Z) { - tip_pos_2d = Vector2(tip_trans.origin.x, tip_trans.origin.y); - target_pos_2d = Vector2(target_trans.origin.x, target_trans.origin.y); - bone_trans.basis.rotate_local(Vector3(0, 0, 1), target_pos_2d.angle() - tip_pos_2d.angle()); - } else { - // Should never happen, but... - ERR_FAIL_MSG("CCDIK joint: Unknown axis vector passed for joint" + itos(p_joint_idx) + ". Cannot execute modification!"); - } - - if (ccdik_data.enable_constraint) { - Vector3 rotation_axis; - real_t rotation_angle; - bone_trans.basis.get_axis_angle(rotation_axis, rotation_angle); - - // Note: When the axis has a negative direction, the angle is OVER 180 degrees and therefore we need to account for this - // when constraining. - if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_X) { - if (rotation_axis.x < 0) { - rotation_angle += Math_PI; - rotation_axis = Vector3(1, 0, 0); - } - } else if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_Y) { - if (rotation_axis.y < 0) { - rotation_angle += Math_PI; - rotation_axis = Vector3(0, 1, 0); - } - } else if (ccdik_data.ccdik_axis == CCDIK_Axes::AXIS_Z) { - if (rotation_axis.z < 0) { - rotation_angle += Math_PI; - rotation_axis = Vector3(0, 0, 1); - } - } else { - // Should never happen, but... - ERR_FAIL_MSG("CCDIK joint: Unknown axis vector passed for joint" + itos(p_joint_idx) + ". Cannot execute modification!"); - } - rotation_angle = clamp_angle(rotation_angle, ccdik_data.constraint_angle_min, ccdik_data.constraint_angle_max, ccdik_data.constraint_angles_invert); - - bone_trans.basis.set_axis_angle(rotation_axis, rotation_angle); - } - - stack->skeleton->set_bone_local_pose_override(ccdik_data.bone_idx, bone_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(ccdik_data.bone_idx); -} - -void SkeletonModification3DCCDIK::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack != nullptr) { - is_setup = true; - execution_error_found = false; - update_target_cache(); - update_tip_cache(); - } -} - -void SkeletonModification3DCCDIK::update_target_cache() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update target cache: modification is not properly setup!"); - return; - } - - target_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(target_node)) { - Node *node = stack->skeleton->get_node(target_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update target cache: node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update target cache: node is not in scene tree!"); - target_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DCCDIK::update_tip_cache() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update tip cache: modification is not properly setup!"); - return; - } - - tip_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(tip_node)) { - Node *node = stack->skeleton->get_node(tip_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update tip cache: node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update tip cache: node is not in scene tree!"); - tip_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DCCDIK::set_target_node(const NodePath &p_target_node) { - target_node = p_target_node; - update_target_cache(); -} - -NodePath SkeletonModification3DCCDIK::get_target_node() const { - return target_node; -} - -void SkeletonModification3DCCDIK::set_tip_node(const NodePath &p_tip_node) { - tip_node = p_tip_node; - update_tip_cache(); -} - -NodePath SkeletonModification3DCCDIK::get_tip_node() const { - return tip_node; -} - -void SkeletonModification3DCCDIK::set_use_high_quality_solve(bool p_high_quality) { - use_high_quality_solve = p_high_quality; -} - -bool SkeletonModification3DCCDIK::get_use_high_quality_solve() const { - return use_high_quality_solve; -} - -// CCDIK joint data functions -String SkeletonModification3DCCDIK::get_ccdik_joint_bone_name(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, String()); - return ccdik_data_chain[p_joint_idx].bone_name; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_bone_name(int p_joint_idx, String p_bone_name) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ccdik_data_chain[p_joint_idx].bone_name = p_bone_name; - - if (stack) { - if (stack->skeleton) { - ccdik_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(p_bone_name); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -int SkeletonModification3DCCDIK::get_ccdik_joint_bone_index(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return ccdik_data_chain[p_joint_idx].bone_idx; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_bone_index(int p_joint_idx, int p_bone_idx) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!"); - ccdik_data_chain[p_joint_idx].bone_idx = p_bone_idx; - - if (stack) { - if (stack->skeleton) { - ccdik_data_chain[p_joint_idx].bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -int SkeletonModification3DCCDIK::get_ccdik_joint_ccdik_axis(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return ccdik_data_chain[p_joint_idx].ccdik_axis; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_ccdik_axis(int p_joint_idx, int p_axis) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ERR_FAIL_COND_MSG(p_axis < 0, "CCDIK axis is out of range: The axis mode is too low!"); - ccdik_data_chain[p_joint_idx].ccdik_axis = p_axis; - property_list_changed_notify(); -} - -bool SkeletonModification3DCCDIK::get_ccdik_joint_enable_constraint(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return ccdik_data_chain[p_joint_idx].enable_constraint; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_enable_constraint(int p_joint_idx, bool p_enable) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ccdik_data_chain[p_joint_idx].enable_constraint = p_enable; - property_list_changed_notify(); -} - -real_t SkeletonModification3DCCDIK::get_ccdik_joint_constraint_angle_min(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return ccdik_data_chain[p_joint_idx].constraint_angle_min; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_constraint_angle_min(int p_joint_idx, real_t p_angle_min) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ccdik_data_chain[p_joint_idx].constraint_angle_min = p_angle_min; -} - -real_t SkeletonModification3DCCDIK::get_ccdik_joint_constraint_angle_max(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return ccdik_data_chain[p_joint_idx].constraint_angle_max; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_constraint_angle_max(int p_joint_idx, real_t p_angle_max) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ccdik_data_chain[p_joint_idx].constraint_angle_max = p_angle_max; -} - -bool SkeletonModification3DCCDIK::get_ccdik_joint_constraint_invert(int p_joint_idx) const { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return ccdik_data_chain[p_joint_idx].constraint_angles_invert; -} - -void SkeletonModification3DCCDIK::set_ccdik_joint_constraint_invert(int p_joint_idx, bool p_invert) { - const int bone_chain_size = ccdik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ccdik_data_chain[p_joint_idx].constraint_angles_invert = p_invert; -} - -int SkeletonModification3DCCDIK::get_ccdik_data_chain_length() { - return ccdik_data_chain.size(); -} -void SkeletonModification3DCCDIK::set_ccdik_data_chain_length(int p_length) { - ERR_FAIL_COND(p_length < 0); - ccdik_data_chain.resize(p_length); - execution_error_found = false; - property_list_changed_notify(); -} - -void SkeletonModification3DCCDIK::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DCCDIK::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DCCDIK::get_target_node); - - ClassDB::bind_method(D_METHOD("set_tip_node", "tip_nodepath"), &SkeletonModification3DCCDIK::set_tip_node); - ClassDB::bind_method(D_METHOD("get_tip_node"), &SkeletonModification3DCCDIK::get_tip_node); - - ClassDB::bind_method(D_METHOD("set_use_high_quality_solve", "high_quality_solve"), &SkeletonModification3DCCDIK::set_use_high_quality_solve); - ClassDB::bind_method(D_METHOD("get_use_high_quality_solve"), &SkeletonModification3DCCDIK::get_use_high_quality_solve); - - // CCDIK joint data functions - ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone_name", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_bone_name); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone_name", "joint_idx", "bone_name"), &SkeletonModification3DCCDIK::set_ccdik_joint_bone_name); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_bone_index", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_bone_index); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_bone_index", "joint_idx", "bone_index"), &SkeletonModification3DCCDIK::set_ccdik_joint_bone_index); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_ccdik_axis", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_ccdik_axis); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_ccdik_axis", "joint_idx", "axis"), &SkeletonModification3DCCDIK::set_ccdik_joint_ccdik_axis); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_enable_joint_constraint", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_enable_constraint); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_enable_joint_constraint", "joint_idx", "enable"), &SkeletonModification3DCCDIK::set_ccdik_joint_enable_constraint); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_min", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_constraint_angle_min); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_min", "joint_idx", "min_angle"), &SkeletonModification3DCCDIK::set_ccdik_joint_constraint_angle_min); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_angle_max", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_constraint_angle_max); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_angle_max", "joint_idx", "max_angle"), &SkeletonModification3DCCDIK::set_ccdik_joint_constraint_angle_max); - ClassDB::bind_method(D_METHOD("get_ccdik_joint_constraint_invert", "joint_idx"), &SkeletonModification3DCCDIK::get_ccdik_joint_constraint_invert); - ClassDB::bind_method(D_METHOD("set_ccdik_joint_constraint_invert", "joint_idx", "invert"), &SkeletonModification3DCCDIK::set_ccdik_joint_constraint_invert); - - ClassDB::bind_method(D_METHOD("set_ccdik_data_chain_length", "length"), &SkeletonModification3DCCDIK::set_ccdik_data_chain_length); - ClassDB::bind_method(D_METHOD("get_ccdik_data_chain_length"), &SkeletonModification3DCCDIK::get_ccdik_data_chain_length); - - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_target_node", "get_target_node"); - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "tip_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_tip_node", "get_tip_node"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "high_quality_solve", PROPERTY_HINT_NONE, ""), "set_use_high_quality_solve", "get_use_high_quality_solve"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "ccdik_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_ccdik_data_chain_length", "get_ccdik_data_chain_length"); -} - -SkeletonModification3DCCDIK::SkeletonModification3DCCDIK() { - stack = nullptr; - is_setup = false; - enabled = true; - - target_node_cache = 0; - tip_node_cache = 0; - use_high_quality_solve = true; -} - -SkeletonModification3DCCDIK::~SkeletonModification3DCCDIK() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.h b/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.h deleted file mode 100644 index 2cbb660..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_ccdik.h +++ /dev/null @@ -1,125 +0,0 @@ - -#ifndef SKELETON_MODIFICATION_3D_CCDIK_H -#define SKELETON_MODIFICATION_3D_CCDIK_H - -/*************************************************************************/ -/* skeleton_modification_3d_ccdik.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/containers/local_vector.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_3d.h" - -class SkeletonModification3DCCDIK : public SkeletonModification3D { - GDCLASS(SkeletonModification3DCCDIK, SkeletonModification3D); - -private: - enum CCDIK_Axes { - AXIS_X, - AXIS_Y, - AXIS_Z - }; - - struct CCDIK_Joint_Data { - String bone_name; - int bone_idx; - int ccdik_axis; - - bool enable_constraint; - real_t constraint_angle_min; - real_t constraint_angle_max; - bool constraint_angles_invert; - - CCDIK_Joint_Data() { - bone_idx = -1; - ccdik_axis = 0; - - enable_constraint = false; - constraint_angle_min = 0; - constraint_angle_max = (2.0 * Math_PI); - constraint_angles_invert = false; - } - }; - - LocalVector ccdik_data_chain; - NodePath target_node; - ObjectID target_node_cache; - - NodePath tip_node; - ObjectID tip_node_cache; - - bool use_high_quality_solve; - - void update_target_cache(); - void update_tip_cache(); - - void _execute_ccdik_joint(int p_joint_idx, Spatial *p_target, Spatial *p_tip); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_target_node(const NodePath &p_target_node); - NodePath get_target_node() const; - - void set_tip_node(const NodePath &p_tip_node); - NodePath get_tip_node() const; - - void set_use_high_quality_solve(bool p_solve); - bool get_use_high_quality_solve() const; - - String get_ccdik_joint_bone_name(int p_joint_idx) const; - void set_ccdik_joint_bone_name(int p_joint_idx, String p_bone_name); - int get_ccdik_joint_bone_index(int p_joint_idx) const; - void set_ccdik_joint_bone_index(int p_joint_idx, int p_bone_idx); - int get_ccdik_joint_ccdik_axis(int p_joint_idx) const; - void set_ccdik_joint_ccdik_axis(int p_joint_idx, int p_axis); - bool get_ccdik_joint_enable_constraint(int p_joint_idx) const; - void set_ccdik_joint_enable_constraint(int p_joint_idx, bool p_enable); - real_t get_ccdik_joint_constraint_angle_min(int p_joint_idx) const; - void set_ccdik_joint_constraint_angle_min(int p_joint_idx, real_t p_angle_min); - real_t get_ccdik_joint_constraint_angle_max(int p_joint_idx) const; - void set_ccdik_joint_constraint_angle_max(int p_joint_idx, real_t p_angle_max); - bool get_ccdik_joint_constraint_invert(int p_joint_idx) const; - void set_ccdik_joint_constraint_invert(int p_joint_idx, bool p_invert); - - int get_ccdik_data_chain_length(); - void set_ccdik_data_chain_length(int p_new_length); - - SkeletonModification3DCCDIK(); - ~SkeletonModification3DCCDIK(); -}; - -#endif // SKELETON_MODIFICATION_3D_CCDIK_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.cpp deleted file mode 100644 index 1b194da..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.cpp +++ /dev/null @@ -1,638 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_fabrik.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_fabrik.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" - -bool SkeletonModification3DFABRIK::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path.begins_with("joint_data/")) { - int fabrik_data_size = fabrik_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, fabrik_data_size, false); - - if (what == "bone_name") { - set_fabrik_joint_bone_name(which, p_value); - } else if (what == "bone_index") { - set_fabrik_joint_bone_index(which, p_value); - } else if (what == "length") { - set_fabrik_joint_length(which, p_value); - } else if (what == "magnet_position") { - set_fabrik_joint_magnet(which, p_value); - } else if (what == "auto_calculate_length") { - set_fabrik_joint_auto_calculate_length(which, p_value); - } else if (what == "use_tip_node") { - set_fabrik_joint_use_tip_node(which, p_value); - } else if (what == "tip_node") { - set_fabrik_joint_tip_node(which, p_value); - } else if (what == "use_target_basis") { - set_fabrik_joint_use_target_basis(which, p_value); - } else if (what == "roll") { - set_fabrik_joint_roll(which, Math::deg2rad(real_t(p_value))); - } - return true; - } - return true; -} - -bool SkeletonModification3DFABRIK::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path.begins_with("joint_data/")) { - const int fabrik_data_size = fabrik_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, fabrik_data_size, false); - - if (what == "bone_name") { - r_ret = get_fabrik_joint_bone_name(which); - } else if (what == "bone_index") { - r_ret = get_fabrik_joint_bone_index(which); - } else if (what == "length") { - r_ret = get_fabrik_joint_length(which); - } else if (what == "magnet_position") { - r_ret = get_fabrik_joint_magnet(which); - } else if (what == "auto_calculate_length") { - r_ret = get_fabrik_joint_auto_calculate_length(which); - } else if (what == "use_tip_node") { - r_ret = get_fabrik_joint_use_tip_node(which); - } else if (what == "tip_node") { - r_ret = get_fabrik_joint_tip_node(which); - } else if (what == "use_target_basis") { - r_ret = get_fabrik_joint_use_target_basis(which); - } else if (what == "roll") { - r_ret = Math::rad2deg(get_fabrik_joint_roll(which)); - } - return true; - } - return true; -} - -void SkeletonModification3DFABRIK::_get_property_list(List *p_list) const { - for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { - String base_string = "joint_data/" + itos(i) + "/"; - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, base_string + "bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "roll", PROPERTY_HINT_RANGE, "-360,360,0.01", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "auto_calculate_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - - if (!fabrik_data_chain[i].auto_calculate_length) { - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - } else { - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_tip_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (fabrik_data_chain[i].use_tip_node) { - p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "tip_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial", PROPERTY_USAGE_DEFAULT)); - } - } - - // Cannot apply magnet to the origin of the chain, as it will not do anything. - if (i > 0) { - p_list->push_back(PropertyInfo(Variant::VECTOR3, base_string + "magnet_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - } - // Only give the override basis option on the last bone in the chain, so only include it for the last bone. - if (i == fabrik_data_chain.size() - 1) { - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_target_basis", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - } - } -} - -void SkeletonModification3DFABRIK::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, - "Modification is not setup and therefore cannot execute!"); - if (!enabled) { - return; - } - - if (target_node_cache == 0) { - _print_execution_error(true, "Target cache is out of date. Attempting to update..."); - update_target_cache(); - return; - } - - if (_print_execution_error(fabrik_data_chain.size() <= 1, "FABRIK requires at least two joints to operate. Cannot execute modification!")) { - return; - } - - Spatial *node_target = Object::cast_to(ObjectDB::get_instance(target_node_cache)); - if (_print_execution_error(!node_target || !node_target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!")) { - return; - } - - // Make sure the transform cache is the correct size - if (fabrik_transforms.size() != fabrik_data_chain.size()) { - fabrik_transforms.resize(fabrik_data_chain.size()); - } - - // Verify that all joints have a valid bone ID, and that all bone lengths are zero or more - // Also, while we are here, apply magnet positions. - for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { - if (_print_execution_error(fabrik_data_chain[i].bone_idx < 0, "FABRIK Joint " + itos(i) + " has an invalid bone ID. Cannot execute!")) { - return; - } - - if (fabrik_data_chain[i].length < 0 && fabrik_data_chain[i].auto_calculate_length) { - fabrik_joint_auto_calculate_length(i); - } - if (_print_execution_error(fabrik_data_chain[i].length < 0, "FABRIK Joint " + itos(i) + " has an invalid joint length. Cannot execute!")) { - return; - } - fabrik_transforms[i] = stack->skeleton->get_bone_global_pose(fabrik_data_chain[i].bone_idx); - - // Apply magnet positions: - if (stack->skeleton->get_bone_parent(fabrik_data_chain[i].bone_idx) >= 0) { - int parent_bone_idx = stack->skeleton->get_bone_parent(fabrik_data_chain[i].bone_idx); - Transform conversion_transform = (stack->skeleton->get_bone_global_pose(parent_bone_idx)); - fabrik_transforms[i].origin += conversion_transform.basis.xform_inv(fabrik_data_chain[i].magnet_position); - } else { - fabrik_transforms[i].origin += fabrik_data_chain[i].magnet_position; - } - } - Transform origin_global_pose_trans = stack->skeleton->get_bone_global_pose_no_override(fabrik_data_chain[0].bone_idx); - - target_global_pose = stack->skeleton->world_transform_to_global_pose(node_target->get_global_transform()); - origin_global_pose = origin_global_pose_trans; - - final_joint_idx = fabrik_data_chain.size() - 1; - real_t target_distance = fabrik_transforms[final_joint_idx].origin.distance_to(target_global_pose.origin); - chain_iterations = 0; - - while (target_distance > chain_tolerance) { - chain_backwards(); - chain_forwards(); - - // update the target distance - target_distance = fabrik_transforms[final_joint_idx].origin.distance_to(target_global_pose.origin); - - // update chain iterations - chain_iterations += 1; - if (chain_iterations >= chain_max_iterations) { - break; - } - } - chain_apply(); - - execution_error_found = false; -} - -void SkeletonModification3DFABRIK::chain_backwards() { - int final_bone_idx = fabrik_data_chain[final_joint_idx].bone_idx; - Transform final_joint_trans = fabrik_transforms[final_joint_idx]; - - // Get the direction the final bone is facing in. - stack->skeleton->update_bone_rest_forward_vector(final_bone_idx); - Transform final_bone_direction_trans = final_joint_trans.looking_at(target_global_pose.origin, Vector3(0, 1, 0)); - final_bone_direction_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(final_bone_idx, final_bone_direction_trans.basis); - Vector3 direction = final_bone_direction_trans.basis.xform(stack->skeleton->get_bone_axis_forward_vector(final_bone_idx)).normalized(); - - // If set to override, then use the target's Basis rather than the bone's - if (fabrik_data_chain[final_joint_idx].use_target_basis) { - direction = target_global_pose.basis.xform(stack->skeleton->get_bone_axis_forward_vector(final_bone_idx)).normalized(); - } - - // set the position of the final joint to the target position - final_joint_trans.origin = target_global_pose.origin - (direction * fabrik_data_chain[final_joint_idx].length); - fabrik_transforms[final_joint_idx] = final_joint_trans; - - // for all other joints, move them towards the target - int i = final_joint_idx; - while (i >= 1) { - Transform next_bone_trans = fabrik_transforms[i]; - i -= 1; - Transform current_trans = fabrik_transforms[i]; - - real_t length = fabrik_data_chain[i].length / (current_trans.origin.distance_to(next_bone_trans.origin)); - current_trans.origin = next_bone_trans.origin.linear_interpolate(current_trans.origin, length); - - // Save the result - fabrik_transforms[i] = current_trans; - } -} - -void SkeletonModification3DFABRIK::chain_forwards() { - // Set root at the initial position. - Transform root_transform = fabrik_transforms[0]; - - root_transform.origin = origin_global_pose.origin; - fabrik_transforms[0] = origin_global_pose; - - for (uint32_t i = 0; i < fabrik_data_chain.size() - 1; i++) { - Transform current_trans = fabrik_transforms[i]; - Transform next_bone_trans = fabrik_transforms[i + 1]; - - real_t length = fabrik_data_chain[i].length / (next_bone_trans.origin.distance_to(current_trans.origin)); - next_bone_trans.origin = current_trans.origin.linear_interpolate(next_bone_trans.origin, length); - - // Save the result - fabrik_transforms[i + 1] = next_bone_trans; - } -} - -void SkeletonModification3DFABRIK::chain_apply() { - for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { - int current_bone_idx = fabrik_data_chain[i].bone_idx; - Transform current_trans = fabrik_transforms[i]; - Transform current_bone_trans = stack->skeleton->get_bone_global_rest(current_bone_idx); - - // If this is the last bone in the chain... - if (i == fabrik_data_chain.size() - 1) { - if (fabrik_data_chain[i].use_target_basis == false) { // Point to target... - // Get the forward direction that the basis is facing in right now. - stack->skeleton->update_bone_rest_forward_vector(current_bone_idx); - Vector3 forward_vector = stack->skeleton->get_bone_axis_forward_vector(current_bone_idx); - // Rotate the bone towards the target: - current_bone_trans.basis.rotate_to_align(forward_vector, current_trans.origin.direction_to(target_global_pose.origin)); - current_bone_trans.basis.rotate_local(forward_vector, fabrik_data_chain[i].roll); - } else { // Use the target's Basis... - current_bone_trans.basis = target_global_pose.basis.orthonormalized().scaled(current_trans.basis.get_scale()); - } - } else { // every other bone in the chain... - Transform next_trans = fabrik_transforms[i + 1]; - - // Get the forward direction that the basis is facing in right now. - stack->skeleton->update_bone_rest_forward_vector(current_bone_idx); - Vector3 forward_vector = stack->skeleton->get_bone_axis_forward_vector(current_bone_idx); - // Rotate the bone towards the next bone in the chain: - current_bone_trans.basis.rotate_to_align(forward_vector, current_trans.origin.direction_to(next_trans.origin)); - current_bone_trans.basis.rotate_local(forward_vector, fabrik_data_chain[i].roll); - } - current_bone_trans.origin = current_trans.origin; - stack->skeleton->set_bone_local_pose_override(current_bone_idx, stack->skeleton->global_pose_to_local_pose(current_bone_idx, current_bone_trans), stack->strength, true); - } - - // Update all the bones so the next modification has up-to-date data. - stack->skeleton->force_update_all_bone_transforms(); -} - -void SkeletonModification3DFABRIK::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - if (stack != nullptr) { - is_setup = true; - execution_error_found = false; - update_target_cache(); - - for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { - update_joint_tip_cache(i); - } - } -} - -void SkeletonModification3DFABRIK::update_target_cache() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update target cache: modification is not properly setup!"); - return; - } - target_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree() && target_node.is_empty() == false) { - if (stack->skeleton->has_node(target_node)) { - Node *node = stack->skeleton->get_node(target_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update target cache: node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update target cache: node is not in the scene tree!"); - target_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DFABRIK::update_joint_tip_cache(int p_joint_idx) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_MSG(p_joint_idx, bone_chain_size, "FABRIK joint not found"); - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update tip cache: modification is not properly setup!"); - return; - } - fabrik_data_chain[p_joint_idx].tip_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree() && fabrik_data_chain[p_joint_idx].tip_node.is_empty() == false) { - if (stack->skeleton->has_node(fabrik_data_chain[p_joint_idx].tip_node)) { - Node *node = stack->skeleton->get_node(fabrik_data_chain[p_joint_idx].tip_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update tip cache for joint " + itos(p_joint_idx) + ": node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update tip cache for joint " + itos(p_joint_idx) + ": node is not in scene tree!"); - fabrik_data_chain[p_joint_idx].tip_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DFABRIK::set_target_node(const NodePath &p_target_node) { - target_node = p_target_node; - update_target_cache(); -} - -NodePath SkeletonModification3DFABRIK::get_target_node() const { - return target_node; -} - -int SkeletonModification3DFABRIK::get_fabrik_data_chain_length() { - return fabrik_data_chain.size(); -} - -void SkeletonModification3DFABRIK::set_fabrik_data_chain_length(int p_length) { - ERR_FAIL_COND(p_length < 0); - fabrik_data_chain.resize(p_length); - fabrik_transforms.resize(p_length); - execution_error_found = false; - property_list_changed_notify(); -} - -real_t SkeletonModification3DFABRIK::get_chain_tolerance() { - return chain_tolerance; -} - -void SkeletonModification3DFABRIK::set_chain_tolerance(real_t p_tolerance) { - ERR_FAIL_COND_MSG(p_tolerance <= 0, "FABRIK chain tolerance must be more than zero!"); - chain_tolerance = p_tolerance; -} - -int SkeletonModification3DFABRIK::get_chain_max_iterations() { - return chain_max_iterations; -} -void SkeletonModification3DFABRIK::set_chain_max_iterations(int p_iterations) { - ERR_FAIL_COND_MSG(p_iterations <= 0, "FABRIK chain iterations must be at least one. Set enabled to false to disable the FABRIK chain."); - chain_max_iterations = p_iterations; -} - -// FABRIK joint data functions -String SkeletonModification3DFABRIK::get_fabrik_joint_bone_name(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, String()); - return fabrik_data_chain[p_joint_idx].bone_name; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_bone_name(int p_joint_idx, String p_bone_name) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].bone_name = p_bone_name; - - if (stack) { - if (stack->skeleton) { - fabrik_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(p_bone_name); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -int SkeletonModification3DFABRIK::get_fabrik_joint_bone_index(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return fabrik_data_chain[p_joint_idx].bone_idx; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!"); - fabrik_data_chain[p_joint_idx].bone_idx = p_bone_idx; - - if (stack) { - if (stack->skeleton) { - fabrik_data_chain[p_joint_idx].bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -real_t SkeletonModification3DFABRIK::get_fabrik_joint_length(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return fabrik_data_chain[p_joint_idx].length; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_length(int p_joint_idx, real_t p_bone_length) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ERR_FAIL_COND_MSG(p_bone_length < 0, "FABRIK joint length cannot be less than zero!"); - - if (!is_setup) { - fabrik_data_chain[p_joint_idx].length = p_bone_length; - return; - } - - if (fabrik_data_chain[p_joint_idx].auto_calculate_length) { - WARN_PRINT("FABRIK Length not set: auto calculate length is enabled for this joint!"); - fabrik_joint_auto_calculate_length(p_joint_idx); - } else { - fabrik_data_chain[p_joint_idx].length = p_bone_length; - } - - execution_error_found = false; -} - -Vector3 SkeletonModification3DFABRIK::get_fabrik_joint_magnet(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, Vector3()); - return fabrik_data_chain[p_joint_idx].magnet_position; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_magnet(int p_joint_idx, Vector3 p_magnet) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].magnet_position = p_magnet; -} - -bool SkeletonModification3DFABRIK::get_fabrik_joint_auto_calculate_length(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return fabrik_data_chain[p_joint_idx].auto_calculate_length; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_auto_calculate_length(int p_joint_idx, bool p_auto_calculate) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].auto_calculate_length = p_auto_calculate; - fabrik_joint_auto_calculate_length(p_joint_idx); - property_list_changed_notify(); -} - -void SkeletonModification3DFABRIK::fabrik_joint_auto_calculate_length(int p_joint_idx) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - if (!fabrik_data_chain[p_joint_idx].auto_calculate_length) { - return; - } - - if (!stack || !stack->skeleton || !is_setup) { - _print_execution_error(true, "Cannot auto calculate joint length: modification is not properly setup!"); - return; - } - ERR_FAIL_INDEX_MSG(fabrik_data_chain[p_joint_idx].bone_idx, stack->skeleton->get_bone_count(), - "Bone for joint " + itos(p_joint_idx) + " is not set or points to an unknown bone!"); - - if (fabrik_data_chain[p_joint_idx].use_tip_node) { // Use the tip node to update joint length. - - update_joint_tip_cache(p_joint_idx); - - Spatial *tip_node = Object::cast_to(ObjectDB::get_instance(fabrik_data_chain[p_joint_idx].tip_node_cache)); - ERR_FAIL_COND_MSG(!tip_node, "Tip node for joint " + itos(p_joint_idx) + "is not a Spatial-based node. Cannot calculate length..."); - ERR_FAIL_COND_MSG(!tip_node->is_inside_tree(), "Tip node for joint " + itos(p_joint_idx) + "is not in the scene tree. Cannot calculate length..."); - - Transform node_trans = tip_node->get_global_transform(); - node_trans = stack->skeleton->world_transform_to_global_pose(node_trans); - //node_trans = stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, node_trans); - //fabrik_data_chain[p_joint_idx].length = node_trans.origin.length(); - - fabrik_data_chain[p_joint_idx].length = stack->skeleton->get_bone_global_pose(fabrik_data_chain[p_joint_idx].bone_idx).origin.distance_to(node_trans.origin); - - } else { // Use child bone(s) to update joint length, if possible - Vector bone_children = stack->skeleton->get_bone_children(fabrik_data_chain[p_joint_idx].bone_idx); - if (bone_children.size() <= 0) { - ERR_FAIL_MSG("Cannot calculate length for joint " + itos(p_joint_idx) + "joint uses leaf bone. \nPlease manually set the bone length or use a tip node!"); - return; - } - - Transform bone_trans = stack->skeleton->get_bone_global_pose(fabrik_data_chain[p_joint_idx].bone_idx); - - real_t final_length = 0; - for (int i = 0; i < bone_children.size(); i++) { - Transform child_transform = stack->skeleton->get_bone_global_pose(bone_children[i]); - final_length += bone_trans.origin.distance_to(child_transform.origin); - //final_length += stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, child_transform).origin.length(); - } - fabrik_data_chain[p_joint_idx].length = final_length / bone_children.size(); - } - execution_error_found = false; - property_list_changed_notify(); -} - -bool SkeletonModification3DFABRIK::get_fabrik_joint_use_tip_node(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return fabrik_data_chain[p_joint_idx].use_tip_node; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_use_tip_node(int p_joint_idx, bool p_use_tip_node) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].use_tip_node = p_use_tip_node; - property_list_changed_notify(); -} - -NodePath SkeletonModification3DFABRIK::get_fabrik_joint_tip_node(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, NodePath()); - return fabrik_data_chain[p_joint_idx].tip_node; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_tip_node(int p_joint_idx, NodePath p_tip_node) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].tip_node = p_tip_node; - update_joint_tip_cache(p_joint_idx); -} - -bool SkeletonModification3DFABRIK::get_fabrik_joint_use_target_basis(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return fabrik_data_chain[p_joint_idx].use_target_basis; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_use_target_basis(int p_joint_idx, bool p_use_target_basis) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].use_target_basis = p_use_target_basis; -} - -real_t SkeletonModification3DFABRIK::get_fabrik_joint_roll(int p_joint_idx) const { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, 0.0); - return fabrik_data_chain[p_joint_idx].roll; -} - -void SkeletonModification3DFABRIK::set_fabrik_joint_roll(int p_joint_idx, real_t p_roll) { - const int bone_chain_size = fabrik_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - fabrik_data_chain[p_joint_idx].roll = p_roll; -} - -void SkeletonModification3DFABRIK::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DFABRIK::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DFABRIK::get_target_node); - ClassDB::bind_method(D_METHOD("set_fabrik_data_chain_length", "length"), &SkeletonModification3DFABRIK::set_fabrik_data_chain_length); - ClassDB::bind_method(D_METHOD("get_fabrik_data_chain_length"), &SkeletonModification3DFABRIK::get_fabrik_data_chain_length); - ClassDB::bind_method(D_METHOD("set_chain_tolerance", "tolerance"), &SkeletonModification3DFABRIK::set_chain_tolerance); - ClassDB::bind_method(D_METHOD("get_chain_tolerance"), &SkeletonModification3DFABRIK::get_chain_tolerance); - ClassDB::bind_method(D_METHOD("set_chain_max_iterations", "max_iterations"), &SkeletonModification3DFABRIK::set_chain_max_iterations); - ClassDB::bind_method(D_METHOD("get_chain_max_iterations"), &SkeletonModification3DFABRIK::get_chain_max_iterations); - - // FABRIK joint data functions - ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone_name", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_bone_name); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone_name", "joint_idx", "bone_name"), &SkeletonModification3DFABRIK::set_fabrik_joint_bone_name); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_bone_index", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_bone_index); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_bone_index", "joint_idx", "bone_index"), &SkeletonModification3DFABRIK::set_fabrik_joint_bone_index); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_length", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_length); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_length", "joint_idx", "length"), &SkeletonModification3DFABRIK::set_fabrik_joint_length); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_magnet", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_magnet); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_magnet", "joint_idx", "magnet_position"), &SkeletonModification3DFABRIK::set_fabrik_joint_magnet); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_auto_calculate_length", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_auto_calculate_length); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_auto_calculate_length", "joint_idx", "auto_calculate_length"), &SkeletonModification3DFABRIK::set_fabrik_joint_auto_calculate_length); - ClassDB::bind_method(D_METHOD("fabrik_joint_auto_calculate_length", "joint_idx"), &SkeletonModification3DFABRIK::fabrik_joint_auto_calculate_length); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_use_tip_node", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_use_tip_node); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_use_tip_node", "joint_idx", "use_tip_node"), &SkeletonModification3DFABRIK::set_fabrik_joint_use_tip_node); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_tip_node", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_tip_node); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_tip_node", "joint_idx", "tip_node"), &SkeletonModification3DFABRIK::set_fabrik_joint_tip_node); - ClassDB::bind_method(D_METHOD("get_fabrik_joint_use_target_basis", "joint_idx"), &SkeletonModification3DFABRIK::get_fabrik_joint_use_target_basis); - ClassDB::bind_method(D_METHOD("set_fabrik_joint_use_target_basis", "joint_idx", "use_target_basis"), &SkeletonModification3DFABRIK::set_fabrik_joint_use_target_basis); - - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_target_node", "get_target_node"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "fabrik_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_fabrik_data_chain_length", "get_fabrik_data_chain_length"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "chain_tolerance", PROPERTY_HINT_RANGE, "0,100,0.001"), "set_chain_tolerance", "get_chain_tolerance"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "chain_max_iterations", PROPERTY_HINT_RANGE, "1,50,1"), "set_chain_max_iterations", "get_chain_max_iterations"); -} - -SkeletonModification3DFABRIK::SkeletonModification3DFABRIK() { - stack = nullptr; - is_setup = false; - enabled = true; - - target_node_cache = 0; - - chain_tolerance = 0.01; - chain_max_iterations = 10; - chain_iterations = 0; - - final_joint_idx = 0; -} - -SkeletonModification3DFABRIK::~SkeletonModification3DFABRIK() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.h b/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.h deleted file mode 100644 index 1ae6e61..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_fabrik.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef SKELETON_MODIFICATION_3D_FABRIK_H -#define SKELETON_MODIFICATION_3D_FABRIK_H - -/*************************************************************************/ -/* skeleton_modification_3d_fabrik.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/containers/local_vector.h" -#include "skeleton_modification_3d.h" - -class SkeletonModification3DFABRIK : public SkeletonModification3D { - GDCLASS(SkeletonModification3DFABRIK, SkeletonModification3D); - -private: - struct FABRIK_Joint_Data { - String bone_name; - int bone_idx; - real_t length; - Vector3 magnet_position; - - bool auto_calculate_length; - bool use_tip_node; - NodePath tip_node; - ObjectID tip_node_cache; - - bool use_target_basis; - real_t roll; - - FABRIK_Joint_Data() { - bone_idx = -1; - length = -1; - - auto_calculate_length = true; - use_tip_node = false; - tip_node_cache = 0; - - use_target_basis = false; - roll = 0; - } - }; - - LocalVector fabrik_data_chain; - LocalVector fabrik_transforms; - - NodePath target_node; - ObjectID target_node_cache; - - real_t chain_tolerance; - int chain_max_iterations; - int chain_iterations; - - void update_target_cache(); - void update_joint_tip_cache(int p_joint_idx); - - int final_joint_idx; - Transform target_global_pose; - Transform origin_global_pose; - - void chain_backwards(); - void chain_forwards(); - void chain_apply(); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_target_node(const NodePath &p_target_node); - NodePath get_target_node() const; - - int get_fabrik_data_chain_length(); - void set_fabrik_data_chain_length(int p_new_length); - - real_t get_chain_tolerance(); - void set_chain_tolerance(real_t p_tolerance); - - int get_chain_max_iterations(); - void set_chain_max_iterations(int p_iterations); - - String get_fabrik_joint_bone_name(int p_joint_idx) const; - void set_fabrik_joint_bone_name(int p_joint_idx, String p_bone_name); - int get_fabrik_joint_bone_index(int p_joint_idx) const; - void set_fabrik_joint_bone_index(int p_joint_idx, int p_bone_idx); - real_t get_fabrik_joint_length(int p_joint_idx) const; - void set_fabrik_joint_length(int p_joint_idx, real_t p_bone_length); - Vector3 get_fabrik_joint_magnet(int p_joint_idx) const; - void set_fabrik_joint_magnet(int p_joint_idx, Vector3 p_magnet); - bool get_fabrik_joint_auto_calculate_length(int p_joint_idx) const; - void set_fabrik_joint_auto_calculate_length(int p_joint_idx, bool p_auto_calculate); - void fabrik_joint_auto_calculate_length(int p_joint_idx); - bool get_fabrik_joint_use_tip_node(int p_joint_idx) const; - void set_fabrik_joint_use_tip_node(int p_joint_idx, bool p_use_tip_node); - NodePath get_fabrik_joint_tip_node(int p_joint_idx) const; - void set_fabrik_joint_tip_node(int p_joint_idx, NodePath p_tip_node); - bool get_fabrik_joint_use_target_basis(int p_joint_idx) const; - void set_fabrik_joint_use_target_basis(int p_joint_idx, bool p_use_basis); - real_t get_fabrik_joint_roll(int p_joint_idx) const; - void set_fabrik_joint_roll(int p_joint_idx, real_t p_roll); - - SkeletonModification3DFABRIK(); - ~SkeletonModification3DFABRIK(); -}; - -#endif // SKELETON_MODIFICATION_3D_FABRIK_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.cpp deleted file mode 100644 index 642bacc..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.cpp +++ /dev/null @@ -1,582 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_jiggle.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_jiggle.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" -#include "servers/physics_server.h" -#include "scene/resources/world_3d.h" - -bool SkeletonModification3DJiggle::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path.begins_with("joint_data/")) { - const int jiggle_size = jiggle_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, jiggle_size, false); - - if (what == "bone_name") { - set_jiggle_joint_bone_name(which, p_value); - } else if (what == "bone_index") { - set_jiggle_joint_bone_index(which, p_value); - } else if (what == "override_defaults") { - set_jiggle_joint_override(which, p_value); - } else if (what == "stiffness") { - set_jiggle_joint_stiffness(which, p_value); - } else if (what == "mass") { - set_jiggle_joint_mass(which, p_value); - } else if (what == "damping") { - set_jiggle_joint_damping(which, p_value); - } else if (what == "use_gravity") { - set_jiggle_joint_use_gravity(which, p_value); - } else if (what == "gravity") { - set_jiggle_joint_gravity(which, p_value); - } else if (what == "roll") { - set_jiggle_joint_roll(which, Math::deg2rad(real_t(p_value))); - } - return true; - } else { - if (path == "use_colliders") { - set_use_colliders(p_value); - } else if (path == "collision_mask") { - set_collision_mask(p_value); - } - return true; - } - return true; -} - -bool SkeletonModification3DJiggle::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path.begins_with("joint_data/")) { - const int jiggle_size = jiggle_data_chain.size(); - int which = path.get_slicec('/', 1).to_int(); - String what = path.get_slicec('/', 2); - ERR_FAIL_INDEX_V(which, jiggle_size, false); - - if (what == "bone_name") { - r_ret = get_jiggle_joint_bone_name(which); - } else if (what == "bone_index") { - r_ret = get_jiggle_joint_bone_index(which); - } else if (what == "override_defaults") { - r_ret = get_jiggle_joint_override(which); - } else if (what == "stiffness") { - r_ret = get_jiggle_joint_stiffness(which); - } else if (what == "mass") { - r_ret = get_jiggle_joint_mass(which); - } else if (what == "damping") { - r_ret = get_jiggle_joint_damping(which); - } else if (what == "use_gravity") { - r_ret = get_jiggle_joint_use_gravity(which); - } else if (what == "gravity") { - r_ret = get_jiggle_joint_gravity(which); - } else if (what == "roll") { - r_ret = Math::rad2deg(get_jiggle_joint_roll(which)); - } - return true; - } else { - if (path == "use_colliders") { - r_ret = get_use_colliders(); - } else if (path == "collision_mask") { - r_ret = get_collision_mask(); - } - return true; - } - return true; -} - -void SkeletonModification3DJiggle::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::BOOL, "use_colliders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (use_colliders) { - p_list->push_back(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS, "", PROPERTY_USAGE_DEFAULT)); - } - - for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) { - String base_string = "joint_data/" + itos(i) + "/"; - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, base_string + "bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "roll", PROPERTY_HINT_RANGE, "-360,360,0.01", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "override_defaults", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - - if (jiggle_data_chain[i].override_defaults) { - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "stiffness", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "mass", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, base_string + "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (jiggle_data_chain[i].use_gravity) { - p_list->push_back(PropertyInfo(Variant::VECTOR3, base_string + "gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - } - } - } -} - -void SkeletonModification3DJiggle::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, - "Modification is not setup and therefore cannot execute!"); - if (!enabled) { - return; - } - if (target_node_cache == 0) { - _print_execution_error(true, "Target cache is out of date. Attempting to update..."); - update_cache(); - return; - } - Spatial *target = Object::cast_to(ObjectDB::get_instance(target_node_cache)); - _print_execution_error(!target || !target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!"); - - for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) { - _execute_jiggle_joint(i, target, p_delta); - } - - execution_error_found = false; -} - -void SkeletonModification3DJiggle::_execute_jiggle_joint(int p_joint_idx, Spatial *p_target, real_t p_delta) { - // Adopted from: https://wiki.unity3d.com/index.php/JiggleBone - // With modifications by TwistedTwigleg. - - if (jiggle_data_chain[p_joint_idx].bone_idx <= -2) { - jiggle_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(jiggle_data_chain[p_joint_idx].bone_name); - } - if (_print_execution_error( - jiggle_data_chain[p_joint_idx].bone_idx < 0 || jiggle_data_chain[p_joint_idx].bone_idx > stack->skeleton->get_bone_count(), - "Jiggle joint " + itos(p_joint_idx) + " bone index is invalid. Cannot execute modification!")) { - return; - } - - Transform bone_local_pos = stack->skeleton->get_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx); - if (bone_local_pos == Transform()) { - bone_local_pos = stack->skeleton->get_bone_pose(jiggle_data_chain[p_joint_idx].bone_idx); - } - - Transform new_bone_trans = stack->skeleton->local_pose_to_global_pose(jiggle_data_chain[p_joint_idx].bone_idx, bone_local_pos); - Vector3 target_position = stack->skeleton->world_transform_to_global_pose(p_target->get_global_transform()).origin; - - jiggle_data_chain[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta; - - if (jiggle_data_chain[p_joint_idx].use_gravity) { - Vector3 gravity_to_apply = new_bone_trans.basis.inverse().xform(jiggle_data_chain[p_joint_idx].gravity); - jiggle_data_chain[p_joint_idx].force += gravity_to_apply * p_delta; - } - - jiggle_data_chain[p_joint_idx].acceleration = jiggle_data_chain[p_joint_idx].force / jiggle_data_chain[p_joint_idx].mass; - jiggle_data_chain[p_joint_idx].velocity += jiggle_data_chain[p_joint_idx].acceleration * (1 - jiggle_data_chain[p_joint_idx].damping); - - jiggle_data_chain[p_joint_idx].dynamic_position += jiggle_data_chain[p_joint_idx].velocity + jiggle_data_chain[p_joint_idx].force; - jiggle_data_chain[p_joint_idx].dynamic_position += new_bone_trans.origin - jiggle_data_chain[p_joint_idx].last_position; - jiggle_data_chain[p_joint_idx].last_position = new_bone_trans.origin; - - // Collision detection/response - if (use_colliders) { - if (execution_mode == SkeletonModificationStack3D::EXECUTION_MODE::execution_mode_physics_process) { - Ref world_3d = stack->skeleton->get_world_3d(); - ERR_FAIL_COND(world_3d.is_null()); - PhysicsDirectSpaceState *space_state = PhysicsServer::get_singleton()->space_get_direct_state(world_3d->get_space()); - PhysicsDirectSpaceState::RayResult ray_result; - - // Convert to world transforms, which is what the physics server needs - Transform new_bone_trans_world = stack->skeleton->global_pose_to_world_transform(new_bone_trans); - Transform dynamic_position_world = stack->skeleton->global_pose_to_world_transform(Transform(Basis(), jiggle_data_chain[p_joint_idx].dynamic_position)); - - bool ray_hit = space_state->intersect_ray(new_bone_trans_world.origin, dynamic_position_world.get_origin(), ray_result, RBSet(), collision_mask); - - if (ray_hit) { - jiggle_data_chain[p_joint_idx].dynamic_position = jiggle_data_chain[p_joint_idx].last_noncollision_position; - jiggle_data_chain[p_joint_idx].acceleration = Vector3(0, 0, 0); - jiggle_data_chain[p_joint_idx].velocity = Vector3(0, 0, 0); - } else { - jiggle_data_chain[p_joint_idx].last_noncollision_position = jiggle_data_chain[p_joint_idx].dynamic_position; - } - - } else { - WARN_PRINT_ONCE("Jiggle modifier: You cannot detect colliders without the stack mode being set to _physics_process!"); - } - } - - // Get the forward direction that the basis is facing in right now. - stack->skeleton->update_bone_rest_forward_vector(jiggle_data_chain[p_joint_idx].bone_idx); - Vector3 forward_vector = stack->skeleton->get_bone_axis_forward_vector(jiggle_data_chain[p_joint_idx].bone_idx); - - // Rotate the bone using the dynamic position! - new_bone_trans.basis.rotate_to_align(forward_vector, new_bone_trans.origin.direction_to(jiggle_data_chain[p_joint_idx].dynamic_position)); - - // Roll - new_bone_trans.basis.rotate_local(forward_vector, jiggle_data_chain[p_joint_idx].roll); - - new_bone_trans = stack->skeleton->global_pose_to_local_pose(jiggle_data_chain[p_joint_idx].bone_idx, new_bone_trans); - stack->skeleton->set_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx, new_bone_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(jiggle_data_chain[p_joint_idx].bone_idx); -} - -void SkeletonModification3DJiggle::_update_jiggle_joint_data() { - for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) { - if (!jiggle_data_chain[i].override_defaults) { - set_jiggle_joint_stiffness(i, stiffness); - set_jiggle_joint_mass(i, mass); - set_jiggle_joint_damping(i, damping); - set_jiggle_joint_use_gravity(i, use_gravity); - set_jiggle_joint_gravity(i, gravity); - } - } -} - -void SkeletonModification3DJiggle::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack) { - is_setup = true; - execution_error_found = false; - - if (stack->skeleton) { - for (uint32_t i = 0; i < jiggle_data_chain.size(); i++) { - int bone_idx = jiggle_data_chain[i].bone_idx; - if (bone_idx > 0 && bone_idx < stack->skeleton->get_bone_count()) { - jiggle_data_chain[i].dynamic_position = stack->skeleton->local_pose_to_global_pose(bone_idx, stack->skeleton->get_bone_local_pose_override(bone_idx)).origin; - } - } - } - - update_cache(); - } -} - -void SkeletonModification3DJiggle::update_cache() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update target cache: modification is not properly setup!"); - return; - } - - target_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(target_node)) { - Node *node = stack->skeleton->get_node(target_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update target cache: node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update target cache: node is not in the scene tree!"); - target_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DJiggle::set_target_node(const NodePath &p_target_node) { - target_node = p_target_node; - update_cache(); -} - -NodePath SkeletonModification3DJiggle::get_target_node() const { - return target_node; -} - -void SkeletonModification3DJiggle::set_stiffness(real_t p_stiffness) { - ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!"); - stiffness = p_stiffness; - _update_jiggle_joint_data(); -} - -real_t SkeletonModification3DJiggle::get_stiffness() const { - return stiffness; -} - -void SkeletonModification3DJiggle::set_mass(real_t p_mass) { - ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!"); - mass = p_mass; - _update_jiggle_joint_data(); -} - -real_t SkeletonModification3DJiggle::get_mass() const { - return mass; -} - -void SkeletonModification3DJiggle::set_damping(real_t p_damping) { - ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!"); - ERR_FAIL_COND_MSG(p_damping > 1, "Damping cannot be more than one!"); - damping = p_damping; - _update_jiggle_joint_data(); -} - -real_t SkeletonModification3DJiggle::get_damping() const { - return damping; -} - -void SkeletonModification3DJiggle::set_use_gravity(bool p_use_gravity) { - use_gravity = p_use_gravity; - _update_jiggle_joint_data(); -} - -bool SkeletonModification3DJiggle::get_use_gravity() const { - return use_gravity; -} - -void SkeletonModification3DJiggle::set_gravity(Vector3 p_gravity) { - gravity = p_gravity; - _update_jiggle_joint_data(); -} - -Vector3 SkeletonModification3DJiggle::get_gravity() const { - return gravity; -} - -void SkeletonModification3DJiggle::set_use_colliders(bool p_use_collider) { - use_colliders = p_use_collider; - property_list_changed_notify(); -} - -bool SkeletonModification3DJiggle::get_use_colliders() const { - return use_colliders; -} - -void SkeletonModification3DJiggle::set_collision_mask(int p_mask) { - collision_mask = p_mask; -} - -int SkeletonModification3DJiggle::get_collision_mask() const { - return collision_mask; -} - -// Jiggle joint data functions -int SkeletonModification3DJiggle::get_jiggle_data_chain_length() { - return jiggle_data_chain.size(); -} - -void SkeletonModification3DJiggle::set_jiggle_data_chain_length(int p_length) { - ERR_FAIL_COND(p_length < 0); - jiggle_data_chain.resize(p_length); - execution_error_found = false; - property_list_changed_notify(); -} - -void SkeletonModification3DJiggle::set_jiggle_joint_bone_name(int p_joint_idx, String p_name) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - - jiggle_data_chain[p_joint_idx].bone_name = p_name; - if (stack && stack->skeleton) { - jiggle_data_chain[p_joint_idx].bone_idx = stack->skeleton->find_bone(p_name); - } - execution_error_found = false; - property_list_changed_notify(); -} - -String SkeletonModification3DJiggle::get_jiggle_joint_bone_name(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, ""); - return jiggle_data_chain[p_joint_idx].bone_name; -} - -int SkeletonModification3DJiggle::get_jiggle_joint_bone_index(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return jiggle_data_chain[p_joint_idx].bone_idx; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!"); - jiggle_data_chain[p_joint_idx].bone_idx = p_bone_idx; - - if (stack) { - if (stack->skeleton) { - jiggle_data_chain[p_joint_idx].bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -void SkeletonModification3DJiggle::set_jiggle_joint_override(int p_joint_idx, bool p_override) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].override_defaults = p_override; - _update_jiggle_joint_data(); - property_list_changed_notify(); -} - -bool SkeletonModification3DJiggle::get_jiggle_joint_override(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return jiggle_data_chain[p_joint_idx].override_defaults; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_stiffness(int p_joint_idx, real_t p_stiffness) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!"); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].stiffness = p_stiffness; -} - -real_t SkeletonModification3DJiggle::get_jiggle_joint_stiffness(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return jiggle_data_chain[p_joint_idx].stiffness; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_mass(int p_joint_idx, real_t p_mass) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!"); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].mass = p_mass; -} - -real_t SkeletonModification3DJiggle::get_jiggle_joint_mass(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return jiggle_data_chain[p_joint_idx].mass; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_damping(int p_joint_idx, real_t p_damping) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!"); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].damping = p_damping; -} - -real_t SkeletonModification3DJiggle::get_jiggle_joint_damping(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, -1); - return jiggle_data_chain[p_joint_idx].damping; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].use_gravity = p_use_gravity; - property_list_changed_notify(); -} - -bool SkeletonModification3DJiggle::get_jiggle_joint_use_gravity(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, false); - return jiggle_data_chain[p_joint_idx].use_gravity; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_gravity(int p_joint_idx, Vector3 p_gravity) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].gravity = p_gravity; -} - -Vector3 SkeletonModification3DJiggle::get_jiggle_joint_gravity(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, Vector3(0, 0, 0)); - return jiggle_data_chain[p_joint_idx].gravity; -} - -void SkeletonModification3DJiggle::set_jiggle_joint_roll(int p_joint_idx, real_t p_roll) { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX(p_joint_idx, bone_chain_size); - jiggle_data_chain[p_joint_idx].roll = p_roll; -} - -real_t SkeletonModification3DJiggle::get_jiggle_joint_roll(int p_joint_idx) const { - const int bone_chain_size = jiggle_data_chain.size(); - ERR_FAIL_INDEX_V(p_joint_idx, bone_chain_size, 0.0); - return jiggle_data_chain[p_joint_idx].roll; -} - -void SkeletonModification3DJiggle::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DJiggle::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DJiggle::get_target_node); - - ClassDB::bind_method(D_METHOD("set_jiggle_data_chain_length", "length"), &SkeletonModification3DJiggle::set_jiggle_data_chain_length); - ClassDB::bind_method(D_METHOD("get_jiggle_data_chain_length"), &SkeletonModification3DJiggle::get_jiggle_data_chain_length); - - ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &SkeletonModification3DJiggle::set_stiffness); - ClassDB::bind_method(D_METHOD("get_stiffness"), &SkeletonModification3DJiggle::get_stiffness); - ClassDB::bind_method(D_METHOD("set_mass", "mass"), &SkeletonModification3DJiggle::set_mass); - ClassDB::bind_method(D_METHOD("get_mass"), &SkeletonModification3DJiggle::get_mass); - ClassDB::bind_method(D_METHOD("set_damping", "damping"), &SkeletonModification3DJiggle::set_damping); - ClassDB::bind_method(D_METHOD("get_damping"), &SkeletonModification3DJiggle::get_damping); - ClassDB::bind_method(D_METHOD("set_use_gravity", "use_gravity"), &SkeletonModification3DJiggle::set_use_gravity); - ClassDB::bind_method(D_METHOD("get_use_gravity"), &SkeletonModification3DJiggle::get_use_gravity); - ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &SkeletonModification3DJiggle::set_gravity); - ClassDB::bind_method(D_METHOD("get_gravity"), &SkeletonModification3DJiggle::get_gravity); - - ClassDB::bind_method(D_METHOD("set_use_colliders", "use_colliders"), &SkeletonModification3DJiggle::set_use_colliders); - ClassDB::bind_method(D_METHOD("get_use_colliders"), &SkeletonModification3DJiggle::get_use_colliders); - ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SkeletonModification3DJiggle::set_collision_mask); - ClassDB::bind_method(D_METHOD("get_collision_mask"), &SkeletonModification3DJiggle::get_collision_mask); - - // Jiggle joint data functions - ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_name", "joint_idx", "name"), &SkeletonModification3DJiggle::set_jiggle_joint_bone_name); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_name", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_bone_name); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification3DJiggle::set_jiggle_joint_bone_index); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_index", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_bone_index); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_override", "joint_idx", "override"), &SkeletonModification3DJiggle::set_jiggle_joint_override); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_override", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_override); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_stiffness", "joint_idx", "stiffness"), &SkeletonModification3DJiggle::set_jiggle_joint_stiffness); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_stiffness", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_stiffness); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_mass", "joint_idx", "mass"), &SkeletonModification3DJiggle::set_jiggle_joint_mass); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_mass", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_mass); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_damping", "joint_idx", "damping"), &SkeletonModification3DJiggle::set_jiggle_joint_damping); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_damping", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_damping); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_use_gravity", "joint_idx", "use_gravity"), &SkeletonModification3DJiggle::set_jiggle_joint_use_gravity); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_use_gravity", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_use_gravity); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_gravity", "joint_idx", "gravity"), &SkeletonModification3DJiggle::set_jiggle_joint_gravity); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_gravity", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_gravity); - ClassDB::bind_method(D_METHOD("set_jiggle_joint_roll", "joint_idx", "roll"), &SkeletonModification3DJiggle::set_jiggle_joint_roll); - ClassDB::bind_method(D_METHOD("get_jiggle_joint_roll", "joint_idx"), &SkeletonModification3DJiggle::get_jiggle_joint_roll); - - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_target_node", "get_target_node"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "jiggle_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_jiggle_data_chain_length", "get_jiggle_data_chain_length"); - ADD_GROUP("Default Joint Settings", ""); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "stiffness"), "set_stiffness", "get_stiffness"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass"), "set_mass", "get_mass"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01"), "set_damping", "get_damping"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_gravity"), "set_use_gravity", "get_use_gravity"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity"); - ADD_GROUP("", ""); -} - -SkeletonModification3DJiggle::SkeletonModification3DJiggle() { - stack = nullptr; - is_setup = false; - target_node_cache = 0; - stiffness = 3; - mass = 0.75; - damping = 0.75; - use_gravity = false; - gravity = Vector3(0, -6.0, 0); - enabled = true; - - use_colliders = false; - collision_mask = 1; -} - -SkeletonModification3DJiggle::~SkeletonModification3DJiggle() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.h b/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.h deleted file mode 100644 index 47c2b7d..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_jiggle.h +++ /dev/null @@ -1,148 +0,0 @@ -#ifndef SKELETON_MODIFICATION_3D_JIGGLE_H -#define SKELETON_MODIFICATION_3D_JIGGLE_H - -/*************************************************************************/ -/* skeleton_modification_3d_jiggle.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/containers/local_vector.h" -#include "skeleton_modification_3d.h" - -class SkeletonModification3DJiggle : public SkeletonModification3D { - GDCLASS(SkeletonModification3DJiggle, SkeletonModification3D); - -private: - struct Jiggle_Joint_Data { - String bone_name; - int bone_idx; - - bool override_defaults; - real_t stiffness; - real_t mass; - real_t damping; - bool use_gravity; - Vector3 gravity; - real_t roll; - - Vector3 cached_rotation; - Vector3 force; - Vector3 acceleration; - Vector3 velocity; - Vector3 last_position; - Vector3 dynamic_position; - - Vector3 last_noncollision_position; - - Jiggle_Joint_Data() { - bone_idx = -1; - override_defaults = false; - stiffness = 3; - mass = 0.75; - damping = 0.75; - use_gravity = false; - gravity = Vector3(0, -6.0, 0); - roll = 0; - } - }; - - NodePath target_node; - ObjectID target_node_cache; - LocalVector jiggle_data_chain; - - real_t stiffness; - real_t mass; - real_t damping; - bool use_gravity; - Vector3 gravity; - - bool use_colliders; - uint32_t collision_mask; - - void update_cache(); - void _execute_jiggle_joint(int p_joint_idx, Spatial *p_target, real_t p_delta); - void _update_jiggle_joint_data(); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_target_node(const NodePath &p_target_node); - NodePath get_target_node() const; - - void set_stiffness(real_t p_stiffness); - real_t get_stiffness() const; - void set_mass(real_t p_mass); - real_t get_mass() const; - void set_damping(real_t p_damping); - real_t get_damping() const; - - void set_use_gravity(bool p_use_gravity); - bool get_use_gravity() const; - void set_gravity(Vector3 p_gravity); - Vector3 get_gravity() const; - - void set_use_colliders(bool p_use_colliders); - bool get_use_colliders() const; - void set_collision_mask(int p_mask); - int get_collision_mask() const; - - int get_jiggle_data_chain_length(); - void set_jiggle_data_chain_length(int p_new_length); - - void set_jiggle_joint_bone_name(int p_joint_idx, String p_name); - String get_jiggle_joint_bone_name(int p_joint_idx) const; - void set_jiggle_joint_bone_index(int p_joint_idx, int p_idx); - int get_jiggle_joint_bone_index(int p_joint_idx) const; - - void set_jiggle_joint_override(int p_joint_idx, bool p_override); - bool get_jiggle_joint_override(int p_joint_idx) const; - void set_jiggle_joint_stiffness(int p_joint_idx, real_t p_stiffness); - real_t get_jiggle_joint_stiffness(int p_joint_idx) const; - void set_jiggle_joint_mass(int p_joint_idx, real_t p_mass); - real_t get_jiggle_joint_mass(int p_joint_idx) const; - void set_jiggle_joint_damping(int p_joint_idx, real_t p_damping); - real_t get_jiggle_joint_damping(int p_joint_idx) const; - void set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity); - bool get_jiggle_joint_use_gravity(int p_joint_idx) const; - void set_jiggle_joint_gravity(int p_joint_idx, Vector3 p_gravity); - Vector3 get_jiggle_joint_gravity(int p_joint_idx) const; - void set_jiggle_joint_roll(int p_joint_idx, real_t p_roll); - real_t get_jiggle_joint_roll(int p_joint_idx) const; - - SkeletonModification3DJiggle(); - ~SkeletonModification3DJiggle(); -}; - -#endif // SKELETON_MODIFICATION_3D_JIGGLE_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.cpp deleted file mode 100644 index ea31c31..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.cpp +++ /dev/null @@ -1,269 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_lookat.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_lookat.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" - -bool SkeletonModification3DLookAt::_set(const StringName &p_path, const Variant &p_value) { - if (p_path == "lock_rotation_to_plane") { - set_lock_rotation_to_plane(p_value); - } else if (p_path == "lock_rotation_plane") { - set_lock_rotation_plane(p_value); - } else if (p_path == "additional_rotation") { - Vector3 tmp = p_value; - tmp.x = Math::deg2rad(tmp.x); - tmp.y = Math::deg2rad(tmp.y); - tmp.z = Math::deg2rad(tmp.z); - set_additional_rotation(tmp); - } - - return true; -} - -bool SkeletonModification3DLookAt::_get(const StringName &p_path, Variant &r_ret) const { - if (p_path == "lock_rotation_to_plane") { - r_ret = get_lock_rotation_to_plane(); - } else if (p_path == "lock_rotation_plane") { - r_ret = get_lock_rotation_plane(); - } else if (p_path == "additional_rotation") { - Vector3 tmp = get_additional_rotation(); - tmp.x = Math::rad2deg(tmp.x); - tmp.y = Math::rad2deg(tmp.y); - tmp.z = Math::rad2deg(tmp.z); - r_ret = tmp; - } - - return true; -} - -void SkeletonModification3DLookAt::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::BOOL, "lock_rotation_to_plane", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (lock_rotation_to_plane) { - p_list->push_back(PropertyInfo(Variant::INT, "lock_rotation_plane", PROPERTY_HINT_ENUM, "X plane, Y plane, Z plane", PROPERTY_USAGE_DEFAULT)); - } - p_list->push_back(PropertyInfo(Variant::VECTOR3, "additional_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); -} - -void SkeletonModification3DLookAt::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, - "Modification is not setup and therefore cannot execute!"); - if (!enabled) { - return; - } - - if (target_node_cache == 0) { - _print_execution_error(true, "Target cache is out of date. Attempting to update..."); - update_cache(); - return; - } - - if (bone_idx <= -2) { - bone_idx = stack->skeleton->find_bone(bone_name); - } - - Spatial *target = Object::cast_to(ObjectDB::get_instance(target_node_cache)); - if (_print_execution_error(!target || !target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!")) { - return; - } - if (_print_execution_error(bone_idx <= -1, "Bone index is invalid. Cannot execute modification!")) { - return; - } - Transform new_bone_trans = stack->skeleton->get_bone_local_pose_override(bone_idx); - if (new_bone_trans == Transform()) { - new_bone_trans = stack->skeleton->get_bone_pose(bone_idx); - } - Vector3 target_pos = stack->skeleton->global_pose_to_local_pose(bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin; - - // Lock the rotation to a plane relative to the bone by changing the target position - if (lock_rotation_to_plane) { - if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_X) { - target_pos.x = new_bone_trans.origin.x; - } else if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_Y) { - target_pos.y = new_bone_trans.origin.y; - } else if (lock_rotation_plane == ROTATION_PLANE::ROTATION_PLANE_Z) { - target_pos.z = new_bone_trans.origin.z; - } - } - - // Look at the target! - new_bone_trans = new_bone_trans.looking_at(target_pos, Vector3(0, 1, 0)); - // Convert from Z-forward to whatever direction the bone faces. - stack->skeleton->update_bone_rest_forward_vector(bone_idx); - new_bone_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(bone_idx, new_bone_trans.basis); - - // Apply additional rotation - new_bone_trans.basis.rotate_local(Vector3(1, 0, 0), additional_rotation.x); - new_bone_trans.basis.rotate_local(Vector3(0, 1, 0), additional_rotation.y); - new_bone_trans.basis.rotate_local(Vector3(0, 0, 1), additional_rotation.z); - - stack->skeleton->set_bone_local_pose_override(bone_idx, new_bone_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(bone_idx); - - // If we completed it successfully, then we can set execution_error_found to false - execution_error_found = false; -} - -void SkeletonModification3DLookAt::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack != nullptr) { - is_setup = true; - execution_error_found = false; - update_cache(); - } -} - -void SkeletonModification3DLookAt::set_bone_name(String p_name) { - bone_name = p_name; - if (stack) { - if (stack->skeleton) { - bone_idx = stack->skeleton->find_bone(bone_name); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -String SkeletonModification3DLookAt::get_bone_name() const { - return bone_name; -} - -int SkeletonModification3DLookAt::get_bone_index() const { - return bone_idx; -} - -void SkeletonModification3DLookAt::set_bone_index(int p_bone_idx) { - ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!"); - bone_idx = p_bone_idx; - - if (stack) { - if (stack->skeleton) { - bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - } - execution_error_found = false; - property_list_changed_notify(); -} - -void SkeletonModification3DLookAt::update_cache() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update target cache: modification is not properly setup!"); - return; - } - - target_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(target_node)) { - Node *node = stack->skeleton->get_node(target_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update target cache: Node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update target cache: Node is not in the scene tree!"); - target_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DLookAt::set_target_node(const NodePath &p_target_node) { - target_node = p_target_node; - update_cache(); -} - -NodePath SkeletonModification3DLookAt::get_target_node() const { - return target_node; -} - -Vector3 SkeletonModification3DLookAt::get_additional_rotation() const { - return additional_rotation; -} - -void SkeletonModification3DLookAt::set_additional_rotation(Vector3 p_offset) { - additional_rotation = p_offset; -} - -bool SkeletonModification3DLookAt::get_lock_rotation_to_plane() const { - return lock_rotation_plane; -} - -void SkeletonModification3DLookAt::set_lock_rotation_to_plane(bool p_lock_rotation) { - lock_rotation_to_plane = p_lock_rotation; - property_list_changed_notify(); -} - -int SkeletonModification3DLookAt::get_lock_rotation_plane() const { - return lock_rotation_plane; -} - -void SkeletonModification3DLookAt::set_lock_rotation_plane(int p_plane) { - lock_rotation_plane = p_plane; -} - -void SkeletonModification3DLookAt::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_bone_name", "name"), &SkeletonModification3DLookAt::set_bone_name); - ClassDB::bind_method(D_METHOD("get_bone_name"), &SkeletonModification3DLookAt::get_bone_name); - - ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification3DLookAt::set_bone_index); - ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification3DLookAt::get_bone_index); - - ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DLookAt::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DLookAt::get_target_node); - - ClassDB::bind_method(D_METHOD("set_additional_rotation", "additional_rotation"), &SkeletonModification3DLookAt::set_additional_rotation); - ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification3DLookAt::get_additional_rotation); - - ClassDB::bind_method(D_METHOD("set_lock_rotation_to_plane", "lock_to_plane"), &SkeletonModification3DLookAt::set_lock_rotation_to_plane); - ClassDB::bind_method(D_METHOD("get_lock_rotation_to_plane"), &SkeletonModification3DLookAt::get_lock_rotation_to_plane); - ClassDB::bind_method(D_METHOD("set_lock_rotation_plane", "plane"), &SkeletonModification3DLookAt::set_lock_rotation_plane); - ClassDB::bind_method(D_METHOD("get_lock_rotation_plane"), &SkeletonModification3DLookAt::get_lock_rotation_plane); - - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index"); - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_target_node", "get_target_node"); -} - -SkeletonModification3DLookAt::SkeletonModification3DLookAt() { - stack = nullptr; - is_setup = false; - - bone_idx = -1; - target_node_cache = 0; - additional_rotation = Vector3(1, 0, 0); - lock_rotation_to_plane = false; - enabled = true; - lock_rotation_plane = ROTATION_PLANE_X; -} - -SkeletonModification3DLookAt::~SkeletonModification3DLookAt() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.h b/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.h deleted file mode 100644 index e312f7e..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_lookat.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef SKELETON_MODIFICATION_3D_LOOKAT_H -#define SKELETON_MODIFICATION_3D_LOOKAT_H - -/*************************************************************************/ -/* skeleton_modification_3d_lookat.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d.h" - -class SkeletonModification3DLookAt : public SkeletonModification3D { - GDCLASS(SkeletonModification3DLookAt, SkeletonModification3D); - -private: - String bone_name; - int bone_idx; - NodePath target_node; - ObjectID target_node_cache; - - Vector3 additional_rotation; - bool lock_rotation_to_plane; - int lock_rotation_plane; - - void update_cache(); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - enum ROTATION_PLANE { - ROTATION_PLANE_X, - ROTATION_PLANE_Y, - ROTATION_PLANE_Z - }; - - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_bone_name(String p_name); - String get_bone_name() const; - - void set_bone_index(int p_idx); - int get_bone_index() const; - - void set_target_node(const NodePath &p_target_node); - NodePath get_target_node() const; - - void set_additional_rotation(Vector3 p_offset); - Vector3 get_additional_rotation() const; - - void set_lock_rotation_to_plane(bool p_lock_to_plane); - bool get_lock_rotation_to_plane() const; - void set_lock_rotation_plane(int p_plane); - int get_lock_rotation_plane() const; - - SkeletonModification3DLookAt(); - ~SkeletonModification3DLookAt(); -}; - -#endif // SKELETON_MODIFICATION_3D_LOOKAT_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.cpp deleted file mode 100644 index 0cf91a8..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_stackholder.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_stackholder.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" - -bool SkeletonModification3DStackHolder::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path == "held_modification_stack") { - set_held_modification_stack(p_value); - } - return true; -} - -bool SkeletonModification3DStackHolder::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path == "held_modification_stack") { - r_ret = get_held_modification_stack(); - } - return true; -} - -void SkeletonModification3DStackHolder::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::OBJECT, "held_modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); -} - -void SkeletonModification3DStackHolder::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, "Modification is not setup and therefore cannot execute!"); - - if (held_modification_stack.is_valid()) { - held_modification_stack->execute(p_delta, execution_mode); - } -} - -void SkeletonModification3DStackHolder::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack != nullptr) { - is_setup = true; - - if (held_modification_stack.is_valid()) { - held_modification_stack->set_skeleton(stack->get_skeleton()); - held_modification_stack->setup(); - } - } -} - -void SkeletonModification3DStackHolder::set_held_modification_stack(Ref p_held_stack) { - held_modification_stack = p_held_stack; - - if (is_setup && held_modification_stack.is_valid()) { - held_modification_stack->set_skeleton(stack->get_skeleton()); - held_modification_stack->setup(); - } -} - -Ref SkeletonModification3DStackHolder::get_held_modification_stack() const { - return held_modification_stack; -} - -void SkeletonModification3DStackHolder::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_held_modification_stack", "held_modification_stack"), &SkeletonModification3DStackHolder::set_held_modification_stack); - ClassDB::bind_method(D_METHOD("get_held_modification_stack"), &SkeletonModification3DStackHolder::get_held_modification_stack); -} - -SkeletonModification3DStackHolder::SkeletonModification3DStackHolder() { - stack = nullptr; - is_setup = false; - enabled = true; -} - -SkeletonModification3DStackHolder::~SkeletonModification3DStackHolder() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.h b/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.h deleted file mode 100644 index 2b79cd2..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_stackholder.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef SKELETON_MODIFICATION_3D_STACKHOLDER_H -#define SKELETON_MODIFICATION_3D_STACKHOLDER_H - -/*************************************************************************/ -/* skeleton_modification_3d_stackholder.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d.h" - -class SkeletonModification3DStackHolder : public SkeletonModification3D { - GDCLASS(SkeletonModification3DStackHolder, SkeletonModification3D); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - Ref held_modification_stack; - - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_held_modification_stack(Ref p_held_stack); - Ref get_held_modification_stack() const; - - SkeletonModification3DStackHolder(); - ~SkeletonModification3DStackHolder(); -}; - -#endif // SKELETON_MODIFICATION_3D_STACKHOLDER_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.cpp b/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.cpp deleted file mode 100644 index 0bb22da..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.cpp +++ /dev/null @@ -1,639 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_3d_twoboneik.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d_twoboneik.h" -#include "../nodes/skeleton.h" -#include "skeleton_modification_stack_3d.h" - -bool SkeletonModification3DTwoBoneIK::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path == "use_tip_node") { - set_use_tip_node(p_value); - } else if (path == "tip_node") { - set_tip_node(p_value); - } else if (path == "auto_calculate_joint_length") { - set_auto_calculate_joint_length(p_value); - } else if (path == "use_pole_node") { - set_use_pole_node(p_value); - } else if (path == "pole_node") { - set_pole_node(p_value); - } else if (path == "joint_one_length") { - set_joint_one_length(p_value); - } else if (path == "joint_two_length") { - set_joint_two_length(p_value); - } else if (path == "joint_one/bone_name") { - set_joint_one_bone_name(p_value); - } else if (path == "joint_one/bone_idx") { - set_joint_one_bone_idx(p_value); - } else if (path == "joint_one/roll") { - set_joint_one_roll(Math::deg2rad(real_t(p_value))); - } else if (path == "joint_two/bone_name") { - set_joint_two_bone_name(p_value); - } else if (path == "joint_two/bone_idx") { - set_joint_two_bone_idx(p_value); - } else if (path == "joint_two/roll") { - set_joint_two_roll(Math::deg2rad(real_t(p_value))); - } - - return true; -} - -bool SkeletonModification3DTwoBoneIK::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path == "use_tip_node") { - r_ret = get_use_tip_node(); - } else if (path == "tip_node") { - r_ret = get_tip_node(); - } else if (path == "auto_calculate_joint_length") { - r_ret = get_auto_calculate_joint_length(); - } else if (path == "use_pole_node") { - r_ret = get_use_pole_node(); - } else if (path == "pole_node") { - r_ret = get_pole_node(); - } else if (path == "joint_one_length") { - r_ret = get_joint_one_length(); - } else if (path == "joint_two_length") { - r_ret = get_joint_two_length(); - } else if (path == "joint_one/bone_name") { - r_ret = get_joint_one_bone_name(); - } else if (path == "joint_one/bone_idx") { - r_ret = get_joint_one_bone_idx(); - } else if (path == "joint_one/roll") { - r_ret = Math::rad2deg(get_joint_one_roll()); - } else if (path == "joint_two/bone_name") { - r_ret = get_joint_two_bone_name(); - } else if (path == "joint_two/bone_idx") { - r_ret = get_joint_two_bone_idx(); - } else if (path == "joint_two/roll") { - r_ret = Math::rad2deg(get_joint_two_roll()); - } - - return true; -} - -void SkeletonModification3DTwoBoneIK::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::BOOL, "use_tip_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (use_tip_node) { - p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tip_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial", PROPERTY_USAGE_DEFAULT)); - } - - p_list->push_back(PropertyInfo(Variant::BOOL, "auto_calculate_joint_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (!auto_calculate_joint_length) { - p_list->push_back(PropertyInfo(Variant::REAL, "joint_one_length", PROPERTY_HINT_RANGE, "-1, 10000, 0.001", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_two_length", PROPERTY_HINT_RANGE, "-1, 10000, 0.001", PROPERTY_USAGE_DEFAULT)); - } - - p_list->push_back(PropertyInfo(Variant::BOOL, "use_pole_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - if (use_pole_node) { - p_list->push_back(PropertyInfo(Variant::NODE_PATH, "pole_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial", PROPERTY_USAGE_DEFAULT)); - } - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, "joint_one/bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::INT, "joint_one/bone_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_one/roll", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT)); - - p_list->push_back(PropertyInfo(Variant::STRING_NAME, "joint_two/bone_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::INT, "joint_two/bone_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::REAL, "joint_two/roll", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT)); -} - -void SkeletonModification3DTwoBoneIK::_execute(real_t p_delta) { - ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr, "Modification is not setup and therefore cannot execute!"); - - if (!enabled) { - return; - } - - if (_print_execution_error(joint_one_bone_idx < 0 || joint_two_bone_idx < 0, - "One (or more) of the bones in the modification have invalid bone indexes. Cannot execute modification!")) { - return; - } - - if (target_node_cache == 0) { - _print_execution_error(true, "Target cache is out of date. Attempting to update..."); - update_cache_target(); - return; - } - - // Update joint lengths (if needed) - if (auto_calculate_joint_length && (joint_one_length < 0 || joint_two_length < 0)) { - calculate_joint_lengths(); - } - - // Adopted from the links below: - // http://theorangeduck.com/page/simple-two-joint - // https://www.alanzucconi.com/2018/05/02/ik-2d-2/ - // With modifications by TwistedTwigleg - Spatial *target = Object::cast_to(ObjectDB::get_instance(target_node_cache)); - if (_print_execution_error(!target || !target->is_inside_tree(), "Target node is not in the scene tree. Cannot execute modification!")) { - return; - } - - Transform target_trans = stack->skeleton->world_transform_to_global_pose(target->get_global_transform()); - - Transform bone_one_trans; - Transform bone_two_trans; - - // Make the first joint look at the pole, and the second look at the target. That way, the - // TwoBoneIK solver has to really only handle extension/contraction, which should make it align with the pole. - if (use_pole_node) { - if (pole_node_cache == 0) { - _print_execution_error(true, "Pole cache is out of date. Attempting to update..."); - update_cache_pole(); - return; - } - - Spatial *pole = Object::cast_to(ObjectDB::get_instance(pole_node_cache)); - if (_print_execution_error(!pole || !pole->is_inside_tree(), "Pole node is not in the scene tree. Cannot execute modification!")) { - return; - } - - Transform pole_trans = stack->skeleton->world_transform_to_global_pose(pole->get_global_transform()); - - Transform bone_one_local_pos = stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx); - if (bone_one_local_pos == Transform()) { - bone_one_local_pos = stack->skeleton->get_bone_pose(joint_one_bone_idx); - } - - Transform bone_two_local_pos = stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx); - if (bone_two_local_pos == Transform()) { - bone_two_local_pos = stack->skeleton->get_bone_pose(joint_two_bone_idx); - } - - bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, bone_one_local_pos); - bone_one_trans = bone_one_trans.looking_at(pole_trans.origin, Vector3(0, 1, 0)); - bone_one_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(joint_one_bone_idx, bone_one_trans.basis); - stack->skeleton->update_bone_rest_forward_vector(joint_one_bone_idx); - bone_one_trans.basis.rotate_local(stack->skeleton->get_bone_axis_forward_vector(joint_one_bone_idx), joint_one_roll); - stack->skeleton->set_bone_local_pose_override(joint_one_bone_idx, stack->skeleton->global_pose_to_local_pose(joint_one_bone_idx, bone_one_trans), stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(joint_one_bone_idx); - - bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, bone_two_local_pos); - bone_two_trans = bone_two_trans.looking_at(target_trans.origin, Vector3(0, 1, 0)); - bone_two_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(joint_two_bone_idx, bone_two_trans.basis); - stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); - bone_two_trans.basis.rotate_local(stack->skeleton->get_bone_axis_forward_vector(joint_two_bone_idx), joint_two_roll); - stack->skeleton->set_bone_local_pose_override(joint_two_bone_idx, stack->skeleton->global_pose_to_local_pose(joint_two_bone_idx, bone_two_trans), stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(joint_two_bone_idx); - } else { - Transform bone_one_local_pos = stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx); - if (bone_one_local_pos == Transform()) { - bone_one_local_pos = stack->skeleton->get_bone_pose(joint_one_bone_idx); - } - Transform bone_two_local_pos = stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx); - if (bone_two_local_pos == Transform()) { - bone_two_local_pos = stack->skeleton->get_bone_pose(joint_two_bone_idx); - } - - bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, bone_one_local_pos); - bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, bone_two_local_pos); - } - - Transform bone_two_tip_trans; - if (use_tip_node) { - if (tip_node_cache == 0) { - _print_execution_error(true, "Tip cache is out of date. Attempting to update..."); - update_cache_tip(); - return; - } - - Spatial *tip = Object::cast_to(ObjectDB::get_instance(tip_node_cache)); - if (_print_execution_error(!tip || !tip->is_inside_tree(), "Tip node is not in the scene tree. Cannot execute modification!")) { - return; - } - - bone_two_tip_trans = stack->skeleton->world_transform_to_global_pose(tip->get_global_transform()); - } else { - stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); - bone_two_tip_trans = bone_two_trans; - bone_two_tip_trans.origin += bone_two_trans.basis.xform(stack->skeleton->get_bone_axis_forward_vector(joint_two_bone_idx)).normalized() * joint_two_length; - } - - real_t joint_one_to_target_length = bone_one_trans.origin.distance_to(target_trans.origin); - if (joint_one_length + joint_two_length < joint_one_to_target_length) { - // Set the target *just* out of reach to straighten the bones - joint_one_to_target_length = joint_one_length + joint_two_length + 0.01; - } else if (joint_one_to_target_length < joint_one_length) { - // Place the target in reach so the solver doesn't do crazy things - joint_one_to_target_length = joint_one_length; - } - - // Get the square lengths for all three sides of the triangle we'll use to calculate the angles - real_t sqr_one_length = joint_one_length * joint_one_length; - real_t sqr_two_length = joint_two_length * joint_two_length; - real_t sqr_three_length = joint_one_to_target_length * joint_one_to_target_length; - - // Calculate the angles for the first joint using the law of cosigns - real_t ac_ab_0 = Math::acos(CLAMP(bone_two_tip_trans.origin.direction_to(bone_one_trans.origin).dot(bone_two_trans.origin.direction_to(bone_one_trans.origin)), -1, 1)); - real_t ac_at_0 = Math::acos(CLAMP(bone_one_trans.origin.direction_to(bone_two_tip_trans.origin).dot(bone_one_trans.origin.direction_to(target_trans.origin)), -1, 1)); - real_t ac_ab_1 = Math::acos(CLAMP((sqr_two_length - sqr_one_length - sqr_three_length) / (-2.0 * joint_one_length * joint_one_to_target_length), -1, 1)); - - // Calculate the angles of rotation. Angle 0 is the extension/contraction axis, while angle 1 is the rotation axis to align the triangle to the target - Vector3 axis_0 = bone_one_trans.origin.direction_to(bone_two_tip_trans.origin).cross(bone_one_trans.origin.direction_to(bone_two_trans.origin)); - Vector3 axis_1 = bone_one_trans.origin.direction_to(bone_two_tip_trans.origin).cross(bone_one_trans.origin.direction_to(target_trans.origin)); - - // Make a quaternion with the delta rotation needed to rotate the first joint into alignment and apply it to the transform. - Quaternion bone_one_quat = bone_one_trans.basis.get_rotation_quaternion(); - Quaternion rot_0 = Quaternion(bone_one_quat.inverse().xform(axis_0).normalized(), (ac_ab_1 - ac_ab_0)); - Quaternion rot_2 = Quaternion(bone_one_quat.inverse().xform(axis_1).normalized(), ac_at_0); - bone_one_trans.basis.set_quaternion(bone_one_quat * (rot_0 * rot_2)); - - stack->skeleton->update_bone_rest_forward_vector(joint_one_bone_idx); - bone_one_trans.basis.rotate_local(stack->skeleton->get_bone_axis_forward_vector(joint_one_bone_idx), joint_one_roll); - - // Apply the rotation to the first joint - bone_one_trans = stack->skeleton->global_pose_to_local_pose(joint_one_bone_idx, bone_one_trans); - bone_one_trans.origin = Vector3(0, 0, 0); - stack->skeleton->set_bone_local_pose_override(joint_one_bone_idx, bone_one_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(joint_one_bone_idx); - - if (use_pole_node) { - // Update bone_two_trans so its at the latest position, with the rotation of bone_one_trans taken into account, then look at the target. - bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx)); - stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); - Vector3 forward_vector = stack->skeleton->get_bone_axis_forward_vector(joint_two_bone_idx); - bone_two_trans.basis.rotate_to_align(forward_vector, bone_two_trans.origin.direction_to(target_trans.origin)); - - stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); - bone_two_trans.basis.rotate_local(stack->skeleton->get_bone_axis_forward_vector(joint_two_bone_idx), joint_two_roll); - - bone_two_trans = stack->skeleton->global_pose_to_local_pose(joint_two_bone_idx, bone_two_trans); - stack->skeleton->set_bone_local_pose_override(joint_two_bone_idx, bone_two_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(joint_two_bone_idx); - } else { - // Calculate the angles for the second joint using the law of cosigns, make a quaternion with the delta rotation needed to rotate the joint into - // alignment, and then apply it to the second joint. - real_t ba_bc_0 = Math::acos(CLAMP(bone_two_trans.origin.direction_to(bone_one_trans.origin).dot(bone_two_trans.origin.direction_to(bone_two_tip_trans.origin)), -1, 1)); - real_t ba_bc_1 = Math::acos(CLAMP((sqr_three_length - sqr_one_length - sqr_two_length) / (-2.0 * joint_one_length * joint_two_length), -1, 1)); - Quaternion bone_two_quat = bone_two_trans.basis.get_rotation_quaternion(); - Quaternion rot_1 = Quaternion(bone_two_quat.inverse().xform(axis_0).normalized(), (ba_bc_1 - ba_bc_0)); - bone_two_trans.basis.set_quaternion(bone_two_quat * rot_1); - - stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); - bone_two_trans.basis.rotate_local(stack->skeleton->get_bone_axis_forward_vector(joint_two_bone_idx), joint_two_roll); - - bone_two_trans = stack->skeleton->global_pose_to_local_pose(joint_two_bone_idx, bone_two_trans); - bone_two_trans.origin = Vector3(0, 0, 0); - stack->skeleton->set_bone_local_pose_override(joint_two_bone_idx, bone_two_trans, stack->strength, true); - stack->skeleton->force_update_bone_children_transforms(joint_two_bone_idx); - } -} - -void SkeletonModification3DTwoBoneIK::_setup_modification(Ref p_stack) { - stack = p_stack.ptr(); - - if (stack != nullptr) { - is_setup = true; - execution_error_found = false; - update_cache_target(); - update_cache_tip(); - } -} - -void SkeletonModification3DTwoBoneIK::update_cache_target() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update target cache: modification is not properly setup!"); - return; - } - - target_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree() && target_node.is_empty() == false) { - if (stack->skeleton->has_node(target_node)) { - Node *node = stack->skeleton->get_node(target_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update target cache: Target node is this modification's skeleton or cannot be found. Cannot execute modification"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update target cache: Target node is not in the scene tree. Cannot execute modification!"); - target_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DTwoBoneIK::update_cache_tip() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update tip cache: modification is not properly setup!"); - return; - } - - tip_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(tip_node)) { - Node *node = stack->skeleton->get_node(tip_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update tip cache: Tip node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update tip cache: Tip node is not in the scene tree. Cannot execute modification!"); - tip_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DTwoBoneIK::update_cache_pole() { - if (!is_setup || !stack) { - _print_execution_error(true, "Cannot update pole cache: modification is not properly setup!"); - return; - } - - pole_node_cache = ObjectID(); - if (stack->skeleton) { - if (stack->skeleton->is_inside_tree()) { - if (stack->skeleton->has_node(pole_node)) { - Node *node = stack->skeleton->get_node(pole_node); - ERR_FAIL_COND_MSG(!node || stack->skeleton == node, - "Cannot update pole cache: Pole node is this modification's skeleton or cannot be found!"); - ERR_FAIL_COND_MSG(!node->is_inside_tree(), - "Cannot update pole cache: Pole node is not in the scene tree. Cannot execute modification!"); - pole_node_cache = node->get_instance_id(); - - execution_error_found = false; - } - } - } -} - -void SkeletonModification3DTwoBoneIK::set_target_node(const NodePath &p_target_node) { - target_node = p_target_node; - update_cache_target(); -} - -NodePath SkeletonModification3DTwoBoneIK::get_target_node() const { - return target_node; -} - -void SkeletonModification3DTwoBoneIK::set_use_tip_node(const bool p_use_tip_node) { - use_tip_node = p_use_tip_node; - property_list_changed_notify(); -} - -bool SkeletonModification3DTwoBoneIK::get_use_tip_node() const { - return use_tip_node; -} - -void SkeletonModification3DTwoBoneIK::set_tip_node(const NodePath &p_tip_node) { - tip_node = p_tip_node; - update_cache_tip(); -} - -NodePath SkeletonModification3DTwoBoneIK::get_tip_node() const { - return tip_node; -} - -void SkeletonModification3DTwoBoneIK::set_use_pole_node(const bool p_use_pole_node) { - use_pole_node = p_use_pole_node; - property_list_changed_notify(); -} - -bool SkeletonModification3DTwoBoneIK::get_use_pole_node() const { - return use_pole_node; -} - -void SkeletonModification3DTwoBoneIK::set_pole_node(const NodePath &p_pole_node) { - pole_node = p_pole_node; - update_cache_pole(); -} - -NodePath SkeletonModification3DTwoBoneIK::get_pole_node() const { - return pole_node; -} - -void SkeletonModification3DTwoBoneIK::set_auto_calculate_joint_length(bool p_calculate) { - auto_calculate_joint_length = p_calculate; - if (p_calculate) { - calculate_joint_lengths(); - } - property_list_changed_notify(); -} - -bool SkeletonModification3DTwoBoneIK::get_auto_calculate_joint_length() const { - return auto_calculate_joint_length; -} - -void SkeletonModification3DTwoBoneIK::calculate_joint_lengths() { - if (!is_setup) { - return; // fail silently, as we likely just loaded the scene. - } - ERR_FAIL_COND_MSG(!stack || stack->skeleton == nullptr, - "Modification is not setup and therefore cannot calculate joint lengths!"); - ERR_FAIL_COND_MSG(joint_one_bone_idx <= -1 || joint_two_bone_idx <= -1, - "One of the bones in the TwoBoneIK modification are not set! Cannot calculate joint lengths!"); - - Transform bone_one_rest_trans = stack->skeleton->get_bone_global_pose(joint_one_bone_idx); - Transform bone_two_rest_trans = stack->skeleton->get_bone_global_pose(joint_two_bone_idx); - - joint_one_length = bone_one_rest_trans.origin.distance_to(bone_two_rest_trans.origin); - - if (use_tip_node) { - if (tip_node_cache == 0) { - update_cache_tip(); - WARN_PRINT("Tip cache is out of date. Updating..."); - } - - Spatial *tip = Object::cast_to(ObjectDB::get_instance(tip_node_cache)); - if (tip) { - Transform bone_tip_trans = stack->skeleton->world_transform_to_global_pose(tip->get_global_transform()); - joint_two_length = bone_two_rest_trans.origin.distance_to(bone_tip_trans.origin); - } - } else { - // Attempt to use children bones to get the length - Vector bone_two_children = stack->skeleton->get_bone_children(joint_two_bone_idx); - if (bone_two_children.size() > 0) { - joint_two_length = 0; - for (int i = 0; i < bone_two_children.size(); i++) { - joint_two_length += bone_two_rest_trans.origin.distance_to( - stack->skeleton->get_bone_global_pose(bone_two_children[i]).origin); - } - joint_two_length = joint_two_length / bone_two_children.size(); - } else { - WARN_PRINT("TwoBoneIK modification: Cannot auto calculate length for joint 2! Auto setting the length to 1..."); - joint_two_length = 1.0; - } - } - execution_error_found = false; -} - -void SkeletonModification3DTwoBoneIK::set_joint_one_bone_name(String p_bone_name) { - joint_one_bone_name = p_bone_name; - if (stack && stack->skeleton) { - joint_one_bone_idx = stack->skeleton->find_bone(p_bone_name); - } - execution_error_found = false; - property_list_changed_notify(); -} - -String SkeletonModification3DTwoBoneIK::get_joint_one_bone_name() const { - return joint_one_bone_name; -} - -void SkeletonModification3DTwoBoneIK::set_joint_one_bone_idx(int p_bone_idx) { - joint_one_bone_idx = p_bone_idx; - if (stack && stack->skeleton) { - joint_one_bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - execution_error_found = false; - property_list_changed_notify(); -} - -int SkeletonModification3DTwoBoneIK::get_joint_one_bone_idx() const { - return joint_one_bone_idx; -} - -void SkeletonModification3DTwoBoneIK::set_joint_one_length(real_t p_length) { - joint_one_length = p_length; -} - -real_t SkeletonModification3DTwoBoneIK::get_joint_one_length() const { - return joint_one_length; -} - -void SkeletonModification3DTwoBoneIK::set_joint_two_bone_name(String p_bone_name) { - joint_two_bone_name = p_bone_name; - if (stack && stack->skeleton) { - joint_two_bone_idx = stack->skeleton->find_bone(p_bone_name); - } - execution_error_found = false; - property_list_changed_notify(); -} - -String SkeletonModification3DTwoBoneIK::get_joint_two_bone_name() const { - return joint_two_bone_name; -} - -void SkeletonModification3DTwoBoneIK::set_joint_two_bone_idx(int p_bone_idx) { - joint_two_bone_idx = p_bone_idx; - if (stack && stack->skeleton) { - joint_two_bone_name = stack->skeleton->get_bone_name(p_bone_idx); - } - execution_error_found = false; - property_list_changed_notify(); -} - -int SkeletonModification3DTwoBoneIK::get_joint_two_bone_idx() const { - return joint_two_bone_idx; -} - -void SkeletonModification3DTwoBoneIK::set_joint_two_length(real_t p_length) { - joint_two_length = p_length; -} - -real_t SkeletonModification3DTwoBoneIK::get_joint_two_length() const { - return joint_two_length; -} - -void SkeletonModification3DTwoBoneIK::set_joint_one_roll(real_t p_roll) { - joint_one_roll = p_roll; -} - -real_t SkeletonModification3DTwoBoneIK::get_joint_one_roll() const { - return joint_one_roll; -} - -void SkeletonModification3DTwoBoneIK::set_joint_two_roll(real_t p_roll) { - joint_two_roll = p_roll; -} - -real_t SkeletonModification3DTwoBoneIK::get_joint_two_roll() const { - return joint_two_roll; -} - -void SkeletonModification3DTwoBoneIK::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification3DTwoBoneIK::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification3DTwoBoneIK::get_target_node); - - ClassDB::bind_method(D_METHOD("set_use_pole_node", "use_pole_node"), &SkeletonModification3DTwoBoneIK::set_use_pole_node); - ClassDB::bind_method(D_METHOD("get_use_pole_node"), &SkeletonModification3DTwoBoneIK::get_use_pole_node); - ClassDB::bind_method(D_METHOD("set_pole_node", "pole_nodepath"), &SkeletonModification3DTwoBoneIK::set_pole_node); - ClassDB::bind_method(D_METHOD("get_pole_node"), &SkeletonModification3DTwoBoneIK::get_pole_node); - - ClassDB::bind_method(D_METHOD("set_use_tip_node", "use_tip_node"), &SkeletonModification3DTwoBoneIK::set_use_tip_node); - ClassDB::bind_method(D_METHOD("get_use_tip_node"), &SkeletonModification3DTwoBoneIK::get_use_tip_node); - ClassDB::bind_method(D_METHOD("set_tip_node", "tip_nodepath"), &SkeletonModification3DTwoBoneIK::set_tip_node); - ClassDB::bind_method(D_METHOD("get_tip_node"), &SkeletonModification3DTwoBoneIK::get_tip_node); - - ClassDB::bind_method(D_METHOD("set_auto_calculate_joint_length", "auto_calculate_joint_length"), &SkeletonModification3DTwoBoneIK::set_auto_calculate_joint_length); - ClassDB::bind_method(D_METHOD("get_auto_calculate_joint_length"), &SkeletonModification3DTwoBoneIK::get_auto_calculate_joint_length); - - ClassDB::bind_method(D_METHOD("set_joint_one_bone_name", "bone_name"), &SkeletonModification3DTwoBoneIK::set_joint_one_bone_name); - ClassDB::bind_method(D_METHOD("get_joint_one_bone_name"), &SkeletonModification3DTwoBoneIK::get_joint_one_bone_name); - ClassDB::bind_method(D_METHOD("set_joint_one_bone_idx", "bone_idx"), &SkeletonModification3DTwoBoneIK::set_joint_one_bone_idx); - ClassDB::bind_method(D_METHOD("get_joint_one_bone_idx"), &SkeletonModification3DTwoBoneIK::get_joint_one_bone_idx); - ClassDB::bind_method(D_METHOD("set_joint_one_length", "bone_length"), &SkeletonModification3DTwoBoneIK::set_joint_one_length); - ClassDB::bind_method(D_METHOD("get_joint_one_length"), &SkeletonModification3DTwoBoneIK::get_joint_one_length); - - ClassDB::bind_method(D_METHOD("set_joint_two_bone_name", "bone_name"), &SkeletonModification3DTwoBoneIK::set_joint_two_bone_name); - ClassDB::bind_method(D_METHOD("get_joint_two_bone_name"), &SkeletonModification3DTwoBoneIK::get_joint_two_bone_name); - ClassDB::bind_method(D_METHOD("set_joint_two_bone_idx", "bone_idx"), &SkeletonModification3DTwoBoneIK::set_joint_two_bone_idx); - ClassDB::bind_method(D_METHOD("get_joint_two_bone_idx"), &SkeletonModification3DTwoBoneIK::get_joint_two_bone_idx); - ClassDB::bind_method(D_METHOD("set_joint_two_length", "bone_length"), &SkeletonModification3DTwoBoneIK::set_joint_two_length); - ClassDB::bind_method(D_METHOD("get_joint_two_length"), &SkeletonModification3DTwoBoneIK::get_joint_two_length); - - ClassDB::bind_method(D_METHOD("set_joint_one_roll", "roll"), &SkeletonModification3DTwoBoneIK::set_joint_one_roll); - ClassDB::bind_method(D_METHOD("get_joint_one_roll"), &SkeletonModification3DTwoBoneIK::get_joint_one_roll); - ClassDB::bind_method(D_METHOD("set_joint_two_roll", "roll"), &SkeletonModification3DTwoBoneIK::set_joint_two_roll); - ClassDB::bind_method(D_METHOD("get_joint_two_roll"), &SkeletonModification3DTwoBoneIK::get_joint_two_roll); - - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Spatial"), "set_target_node", "get_target_node"); - ADD_GROUP("", ""); -} - -SkeletonModification3DTwoBoneIK::SkeletonModification3DTwoBoneIK() { - stack = nullptr; - is_setup = false; - - target_node_cache = 0; - - use_tip_node = false; - tip_node_cache = 0; - - use_pole_node = false; - pole_node_cache = 0; - - joint_one_bone_idx = -1; - joint_two_bone_idx = -1; - - auto_calculate_joint_length = false; - joint_one_length = -1; - joint_two_length = -1; - - joint_one_roll = 0; - joint_two_roll = 0; -} - -SkeletonModification3DTwoBoneIK::~SkeletonModification3DTwoBoneIK() { -} diff --git a/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.h b/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.h deleted file mode 100644 index aac2250..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_3d_twoboneik.h +++ /dev/null @@ -1,120 +0,0 @@ - -#ifndef SKELETON_MODIFICATION_3D_TWOBONEIK_H -#define SKELETON_MODIFICATION_3D_TWOBONEIK_H - -/*************************************************************************/ -/* skeleton_modification_3d_twoboneik.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_3d.h" - -class Skeleton; - -class SkeletonModification3DTwoBoneIK : public SkeletonModification3D { - GDCLASS(SkeletonModification3DTwoBoneIK, SkeletonModification3D); - -private: - NodePath target_node; - ObjectID target_node_cache; - - bool use_tip_node; - NodePath tip_node; - ObjectID tip_node_cache; - - bool use_pole_node; - NodePath pole_node; - ObjectID pole_node_cache; - - String joint_one_bone_name; - int joint_one_bone_idx; - String joint_two_bone_name; - int joint_two_bone_idx; - - bool auto_calculate_joint_length; - real_t joint_one_length; - real_t joint_two_length; - - real_t joint_one_roll; - real_t joint_two_roll; - - void update_cache_target(); - void update_cache_tip(); - void update_cache_pole(); - -protected: - static void _bind_methods(); - bool _get(const StringName &p_path, Variant &r_ret) const; - bool _set(const StringName &p_path, const Variant &p_value); - void _get_property_list(List *p_list) const; - -public: - virtual void _execute(real_t p_delta); - virtual void _setup_modification(Ref p_stack); - - void set_target_node(const NodePath &p_target_node); - NodePath get_target_node() const; - - void set_use_tip_node(const bool p_use_tip_node); - bool get_use_tip_node() const; - void set_tip_node(const NodePath &p_tip_node); - NodePath get_tip_node() const; - - void set_use_pole_node(const bool p_use_pole_node); - bool get_use_pole_node() const; - void set_pole_node(const NodePath &p_pole_node); - NodePath get_pole_node() const; - - void set_auto_calculate_joint_length(bool p_calculate); - bool get_auto_calculate_joint_length() const; - void calculate_joint_lengths(); - - void set_joint_one_bone_name(String p_bone_name); - String get_joint_one_bone_name() const; - void set_joint_one_bone_idx(int p_bone_idx); - int get_joint_one_bone_idx() const; - void set_joint_one_length(real_t p_length); - real_t get_joint_one_length() const; - - void set_joint_two_bone_name(String p_bone_name); - String get_joint_two_bone_name() const; - void set_joint_two_bone_idx(int p_bone_idx); - int get_joint_two_bone_idx() const; - void set_joint_two_length(real_t p_length); - real_t get_joint_two_length() const; - - void set_joint_one_roll(real_t p_roll); - real_t get_joint_one_roll() const; - void set_joint_two_roll(real_t p_roll); - real_t get_joint_two_roll() const; - - SkeletonModification3DTwoBoneIK(); - ~SkeletonModification3DTwoBoneIK(); -}; - -#endif // SKELETON_MODIFICATION_3D_TWOBONEIK_H diff --git a/modules/skeleton_3d/resources/skeleton_modification_stack_3d.cpp b/modules/skeleton_3d/resources/skeleton_modification_stack_3d.cpp deleted file mode 100644 index 8eb1be9..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_stack_3d.cpp +++ /dev/null @@ -1,230 +0,0 @@ -/*************************************************************************/ -/* skeleton_modification_stack_3d.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skeleton_modification_stack_3d.h" -#include "../nodes/skeleton.h" - -#include "skeleton_modification_3d.h" - -/////////////////////////////////////// -// ModificationStack3D -/////////////////////////////////////// - -void SkeletonModificationStack3D::_get_property_list(List *p_list) const { - for (int i = 0; i < modifications.size(); i++) { - p_list->push_back( - PropertyInfo(Variant::OBJECT, "modifications/" + itos(i), - PROPERTY_HINT_RESOURCE_TYPE, - "SkeletonModification3D", - PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE)); - } -} - -bool SkeletonModificationStack3D::_set(const StringName &p_path, const Variant &p_value) { - String path = p_path; - - if (path.begins_with("modifications/")) { - int mod_idx = path.get_slicec('/', 1).to_int(); - set_modification(mod_idx, p_value); - return true; - } - return true; -} - -bool SkeletonModificationStack3D::_get(const StringName &p_path, Variant &r_ret) const { - String path = p_path; - - if (path.begins_with("modifications/")) { - int mod_idx = path.get_slicec('/', 1).to_int(); - r_ret = get_modification(mod_idx); - return true; - } - return true; -} - -void SkeletonModificationStack3D::setup() { - if (is_setup) { - return; - } - - if (skeleton != nullptr) { - is_setup = true; - for (int i = 0; i < modifications.size(); i++) { - if (!modifications[i].is_valid()) { - continue; - } - modifications.write[i]->setup_modification(this); - } - } else { - WARN_PRINT("Cannot setup SkeletonModificationStack3D: no skeleton set!"); - } -} - -void SkeletonModificationStack3D::execute(real_t p_delta, int p_execution_mode) { - ERR_FAIL_COND_MSG(!is_setup || skeleton == nullptr || is_queued_for_deletion(), - "Modification stack is not properly setup and therefore cannot execute!"); - - if (!skeleton->is_inside_tree()) { - ERR_PRINT_ONCE("Skeleton is not inside SceneTree! Cannot execute modification!"); - return; - } - - if (!enabled) { - return; - } - - for (int i = 0; i < modifications.size(); i++) { - if (!modifications[i].is_valid()) { - continue; - } - - if (modifications[i]->get_execution_mode() == p_execution_mode) { - modifications.write[i]->execute(p_delta); - } - } -} - -void SkeletonModificationStack3D::enable_all_modifications(bool p_enabled) { - for (int i = 0; i < modifications.size(); i++) { - if (!modifications[i].is_valid()) { - continue; - } - modifications.write[i]->set_enabled(p_enabled); - } -} - -Ref SkeletonModificationStack3D::get_modification(int p_mod_idx) const { - const int modifications_size = modifications.size(); - ERR_FAIL_INDEX_V(p_mod_idx, modifications_size, nullptr); - return modifications[p_mod_idx]; -} - -void SkeletonModificationStack3D::add_modification(Ref p_mod) { - ERR_FAIL_COND(p_mod.is_null()); - p_mod->setup_modification(this); - modifications.push_back(p_mod); -} - -void SkeletonModificationStack3D::delete_modification(int p_mod_idx) { - const int modifications_size = modifications.size(); - ERR_FAIL_INDEX(p_mod_idx, modifications_size); - modifications.remove(p_mod_idx); -} - -void SkeletonModificationStack3D::set_modification(int p_mod_idx, Ref p_mod) { - const int modifications_size = modifications.size(); - ERR_FAIL_INDEX(p_mod_idx, modifications_size); - - if (p_mod == nullptr) { - modifications.remove(p_mod_idx); - } else { - p_mod->setup_modification(this); - modifications.write[p_mod_idx] = p_mod; - } -} - -void SkeletonModificationStack3D::set_modification_count(int p_count) { - ERR_FAIL_COND_MSG(p_count < 0, "Modification count cannot be less than zero."); - modifications.resize(p_count); - property_list_changed_notify(); -} - -int SkeletonModificationStack3D::get_modification_count() const { - return modifications.size(); -} - -void SkeletonModificationStack3D::set_skeleton(Skeleton *p_skeleton) { - skeleton = p_skeleton; -} - -Skeleton *SkeletonModificationStack3D::get_skeleton() const { - return skeleton; -} - -bool SkeletonModificationStack3D::get_is_setup() const { - return is_setup; -} - -void SkeletonModificationStack3D::set_enabled(bool p_enabled) { - enabled = p_enabled; - - if (!enabled && is_setup && skeleton != nullptr) { - skeleton->clear_bones_local_pose_override(); - } -} - -bool SkeletonModificationStack3D::get_enabled() const { - return enabled; -} - -void SkeletonModificationStack3D::set_strength(real_t p_strength) { - ERR_FAIL_COND_MSG(p_strength < 0, "Strength cannot be less than zero!"); - ERR_FAIL_COND_MSG(p_strength > 1, "Strength cannot be more than one!"); - strength = p_strength; -} - -real_t SkeletonModificationStack3D::get_strength() const { - return strength; -} - -void SkeletonModificationStack3D::_bind_methods() { - ClassDB::bind_method(D_METHOD("setup"), &SkeletonModificationStack3D::setup); - ClassDB::bind_method(D_METHOD("execute", "delta", "execution_mode"), &SkeletonModificationStack3D::execute); - - ClassDB::bind_method(D_METHOD("enable_all_modifications", "enabled"), &SkeletonModificationStack3D::enable_all_modifications); - ClassDB::bind_method(D_METHOD("get_modification", "mod_idx"), &SkeletonModificationStack3D::get_modification); - ClassDB::bind_method(D_METHOD("add_modification", "modification"), &SkeletonModificationStack3D::add_modification); - ClassDB::bind_method(D_METHOD("delete_modification", "mod_idx"), &SkeletonModificationStack3D::delete_modification); - ClassDB::bind_method(D_METHOD("set_modification", "mod_idx", "modification"), &SkeletonModificationStack3D::set_modification); - - ClassDB::bind_method(D_METHOD("set_modification_count", "count"), &SkeletonModificationStack3D::set_modification_count); - ClassDB::bind_method(D_METHOD("get_modification_count"), &SkeletonModificationStack3D::get_modification_count); - - ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModificationStack3D::get_is_setup); - - ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModificationStack3D::set_enabled); - ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModificationStack3D::get_enabled); - - ClassDB::bind_method(D_METHOD("set_strength", "strength"), &SkeletonModificationStack3D::set_strength); - ClassDB::bind_method(D_METHOD("get_strength"), &SkeletonModificationStack3D::get_strength); - - ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModificationStack3D::get_skeleton); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled"); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "strength", PROPERTY_HINT_RANGE, "0, 1, 0.001"), "set_strength", "get_strength"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "modification_count", PROPERTY_HINT_RANGE, "0, 100, 1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_modification_count", "get_modification_count"); -} - -SkeletonModificationStack3D::SkeletonModificationStack3D() { - modifications_count = 0; -} - -SkeletonModificationStack3D::~SkeletonModificationStack3D() { -} \ No newline at end of file diff --git a/modules/skeleton_3d/resources/skeleton_modification_stack_3d.h b/modules/skeleton_3d/resources/skeleton_modification_stack_3d.h deleted file mode 100644 index 7c1adbf..0000000 --- a/modules/skeleton_3d/resources/skeleton_modification_stack_3d.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef SKELETON_MODIFICATION_STACK_3D_H -#define SKELETON_MODIFICATION_STACK_3D_H - -/*************************************************************************/ -/* skeleton_modification_stack_3d.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/containers/vector.h" -#include "core/object/resource.h" - -class Skeleton; -class SkeletonModification3D; - -class SkeletonModificationStack3D : public Resource { - GDCLASS(SkeletonModificationStack3D, Resource); - friend class Skeleton; - friend class SkeletonModification3D; - -protected: - static void _bind_methods(); - virtual void _get_property_list(List *p_list) const; - virtual bool _set(const StringName &p_path, const Variant &p_value); - virtual bool _get(const StringName &p_path, Variant &r_ret) const; - -public: - Skeleton *skeleton = nullptr; - bool is_setup = false; - bool enabled = false; - real_t strength = 1.0; - - enum EXECUTION_MODE { - execution_mode_process, - execution_mode_physics_process, - }; - - Vector> modifications; - int modifications_count = 0; - - virtual void setup(); - virtual void execute(real_t p_delta, int p_execution_mode); - - void enable_all_modifications(bool p_enable); - Ref get_modification(int p_mod_idx) const; - void add_modification(Ref p_mod); - void delete_modification(int p_mod_idx); - void set_modification(int p_mod_idx, Ref p_mod); - - void set_modification_count(int p_count); - int get_modification_count() const; - - void set_skeleton(Skeleton *p_skeleton); - Skeleton *get_skeleton() const; - - bool get_is_setup() const; - - void set_enabled(bool p_enabled); - bool get_enabled() const; - - void set_strength(real_t p_strength); - real_t get_strength() const; - - SkeletonModificationStack3D(); - ~SkeletonModificationStack3D(); -}; - -#endif // SKELETON_MODIFICATION_STACK_3D_H diff --git a/modules/skeleton_3d/resources/skin.cpp b/modules/skeleton_3d/resources/skin.cpp deleted file mode 100644 index db418a7..0000000 --- a/modules/skeleton_3d/resources/skin.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/*************************************************************************/ -/* skin.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "skin.h" - -void Skin::set_bind_count(int p_size) { - ERR_FAIL_COND(p_size < 0); - binds.resize(p_size); - binds_ptr = binds.ptrw(); - bind_count = p_size; - emit_changed(); -} - -void Skin::add_bind(int p_bone, const Transform &p_pose) { - uint32_t index = bind_count; - set_bind_count(bind_count + 1); - set_bind_bone(index, p_bone); - set_bind_pose(index, p_pose); -} - -void Skin::add_named_bind(const String &p_name, const Transform &p_pose) { - uint32_t index = bind_count; - set_bind_count(bind_count + 1); - set_bind_name(index, p_name); - set_bind_pose(index, p_pose); -} - -void Skin::set_bind_name(int p_index, const StringName &p_name) { - ERR_FAIL_INDEX(p_index, bind_count); - bool notify_change = (binds_ptr[p_index].name != StringName()) != (p_name != StringName()); - binds_ptr[p_index].name = p_name; - emit_changed(); - if (notify_change) { - _change_notify(); - } -} - -void Skin::set_bind_bone(int p_index, int p_bone) { - ERR_FAIL_INDEX(p_index, bind_count); - binds_ptr[p_index].bone = p_bone; - emit_changed(); -} - -void Skin::set_bind_pose(int p_index, const Transform &p_pose) { - ERR_FAIL_INDEX(p_index, bind_count); - binds_ptr[p_index].pose = p_pose; - emit_changed(); -} - -void Skin::clear_binds() { - binds.clear(); - binds_ptr = nullptr; - bind_count = 0; - emit_changed(); -} - -bool Skin::_set(const StringName &p_name, const Variant &p_value) { - String name = p_name; - if (name == "bind_count") { - set_bind_count(p_value); - return true; - } else if (name.begins_with("bind/")) { - int index = name.get_slicec('/', 1).to_int(); - String what = name.get_slicec('/', 2); - if (what == "bone") { - set_bind_bone(index, p_value); - return true; - } else if (what == "name") { - set_bind_name(index, p_value); - return true; - } else if (what == "pose") { - set_bind_pose(index, p_value); - return true; - } - } - return false; -} - -bool Skin::_get(const StringName &p_name, Variant &r_ret) const { - String name = p_name; - if (name == "bind_count") { - r_ret = get_bind_count(); - return true; - } else if (name.begins_with("bind/")) { - int index = name.get_slicec('/', 1).to_int(); - String what = name.get_slicec('/', 2); - if (what == "bone") { - r_ret = get_bind_bone(index); - return true; - } else if (what == "name") { - r_ret = get_bind_name(index); - return true; - } else if (what == "pose") { - r_ret = get_bind_pose(index); - return true; - } - } - return false; -} -void Skin::_get_property_list(List *p_list) const { - p_list->push_back(PropertyInfo(Variant::INT, "bind_count", PROPERTY_HINT_RANGE, "0,16384,1,or_greater")); - for (int i = 0; i < get_bind_count(); i++) { - p_list->push_back(PropertyInfo(Variant::STRING_NAME, "bind/" + itos(i) + "/name")); - p_list->push_back(PropertyInfo(Variant::INT, "bind/" + itos(i) + "/bone", PROPERTY_HINT_RANGE, "0,16384,1,or_greater", get_bind_name(i) != StringName() ? PROPERTY_USAGE_NOEDITOR : PROPERTY_USAGE_DEFAULT)); - p_list->push_back(PropertyInfo(Variant::TRANSFORM, "bind/" + itos(i) + "/pose")); - } -} - -void Skin::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_bind_count", "bind_count"), &Skin::set_bind_count); - ClassDB::bind_method(D_METHOD("get_bind_count"), &Skin::get_bind_count); - - ClassDB::bind_method(D_METHOD("add_bind", "bone", "pose"), &Skin::add_bind); - - ClassDB::bind_method(D_METHOD("set_bind_pose", "bind_index", "pose"), &Skin::set_bind_pose); - ClassDB::bind_method(D_METHOD("get_bind_pose", "bind_index"), &Skin::get_bind_pose); - - ClassDB::bind_method(D_METHOD("set_bind_name", "bind_index", "name"), &Skin::set_bind_name); - ClassDB::bind_method(D_METHOD("get_bind_name", "bind_index"), &Skin::get_bind_name); - - ClassDB::bind_method(D_METHOD("set_bind_bone", "bind_index", "bone"), &Skin::set_bind_bone); - ClassDB::bind_method(D_METHOD("get_bind_bone", "bind_index"), &Skin::get_bind_bone); - - ClassDB::bind_method(D_METHOD("clear_binds"), &Skin::clear_binds); -} - -Skin::Skin() { - bind_count = 0; - binds_ptr = nullptr; -} diff --git a/modules/skeleton_3d/resources/skin.h b/modules/skeleton_3d/resources/skin.h deleted file mode 100644 index 5e5d45f..0000000 --- a/modules/skeleton_3d/resources/skin.h +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef SKIN_H -#define SKIN_H -/*************************************************************************/ -/* skin.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "core/object/resource.h" - -class Skin : public Resource { - GDCLASS(Skin, Resource) - - struct Bind { - int bone = -1; - StringName name; - Transform pose; - }; - - Vector binds; - - Bind *binds_ptr; - int bind_count; - -protected: - bool _set(const StringName &p_name, const Variant &p_value); - bool _get(const StringName &p_name, Variant &r_ret) const; - void _get_property_list(List *p_list) const; - - static void _bind_methods(); - -public: - void set_bind_count(int p_size); - inline int get_bind_count() const { return bind_count; } - - void add_bind(int p_bone, const Transform &p_pose); - void add_named_bind(const String &p_name, const Transform &p_pose); - - void set_bind_bone(int p_index, int p_bone); - void set_bind_pose(int p_index, const Transform &p_pose); - void set_bind_name(int p_index, const StringName &p_name); - - inline int get_bind_bone(int p_index) const { -#ifdef DEBUG_ENABLED - ERR_FAIL_INDEX_V(p_index, bind_count, -1); -#endif - return binds_ptr[p_index].bone; - } - - inline StringName get_bind_name(int p_index) const { -#ifdef DEBUG_ENABLED - ERR_FAIL_INDEX_V(p_index, bind_count, StringName()); -#endif - return binds_ptr[p_index].name; - } - - inline Transform get_bind_pose(int p_index) const { -#ifdef DEBUG_ENABLED - ERR_FAIL_INDEX_V(p_index, bind_count, Transform()); -#endif - return binds_ptr[p_index].pose; - } - - void clear_binds(); - - Skin(); -}; - -#endif // SKIN_H