Просмотр исходного кода

Implement ModifierBoneTarget3D which can be target of the other mods

Silc Lizard (Tokage) Renew 2 месяцев назад
Родитель
Сommit
aa2c3da63a

+ 20 - 0
doc/classes/ModifierBoneTarget3D.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<class name="ModifierBoneTarget3D" inherits="SkeletonModifier3D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
+	<brief_description>
+		А node that dynamically copies the 3D transform of a bone in its parent [Skeleton3D].
+	</brief_description>
+	<description>
+		This node selects a bone in a [Skeleton3D] and attaches to it. This means that the [ModifierBoneTarget3D] node will dynamically copy the 3D transform of the selected bone.
+		The functionality is similar to [BoneAttachment3D], but this node adopts the [SkeletonModifier3D] cycle and is intended to be used as another [SkeletonModifier3D]'s target.
+	</description>
+	<tutorials>
+	</tutorials>
+	<members>
+		<member name="bone" type="int" setter="set_bone" getter="get_bone" default="-1">
+			The index of the attached bone.
+		</member>
+		<member name="bone_name" type="String" setter="set_bone_name" getter="get_bone_name" default="&quot;&quot;">
+			The name of the attached bone.
+		</member>
+	</members>
+</class>

+ 1 - 0
editor/icons/ModifierBoneTarget3D.svg

@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><g fill="#fc7f7f"><path d="M12 9h1v2h-1zM9 12h2v1h-2zM14 12h2v1h-2zM12 14h1v2h-1z"/><path d="m14.594 6.927c.754-1.134.446-2.665-.688-3.419-.309-.205-.66-.338-1.026-.389-.188-1.349-1.433-2.291-2.782-2.103s-2.29 1.433-2.103 2.782c.051.367.184.717.389 1.026l-3.56 3.56c-1.134-.754-2.665-.446-3.419.688s-.446 2.664.688 3.419c.308.205.659.338 1.026.389.188 1.349 1.433 2.29 2.782 2.103 1.349-.188 2.291-1.433 2.103-2.781-.051-.367-.184-.718-.389-1.026l3.56-3.56c1.134.753 2.665.445 3.419-.689z"/></g></svg>

+ 98 - 0
scene/3d/modifier_bone_target_3d.cpp

@@ -0,0 +1,98 @@
+/**************************************************************************/
+/*  modifier_bone_target_3d.cpp                                           */
+/**************************************************************************/
+/*                         This file is part of:                          */
+/*                             GODOT ENGINE                               */
+/*                        https://godotengine.org                         */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
+/*                                                                        */
+/* 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 "modifier_bone_target_3d.h"
+
+void ModifierBoneTarget3D::_validate_bone_names() {
+	// Prior bone name.
+	if (!bone_name.is_empty()) {
+		set_bone_name(bone_name);
+	} else if (bone != -1) {
+		set_bone(bone);
+	}
+}
+
+void ModifierBoneTarget3D::set_bone_name(const String &p_bone_name) {
+	bone_name = p_bone_name;
+	Skeleton3D *sk = get_skeleton();
+	if (sk) {
+		set_bone(sk->find_bone(bone_name));
+	}
+}
+
+String ModifierBoneTarget3D::get_bone_name() const {
+	return bone_name;
+}
+
+void ModifierBoneTarget3D::set_bone(int p_bone) {
+	bone = p_bone;
+	Skeleton3D *sk = get_skeleton();
+	if (sk) {
+		if (bone <= -1 || bone >= sk->get_bone_count()) {
+			WARN_PRINT("Bone index out of range!");
+			bone = -1;
+		} else {
+			bone_name = sk->get_bone_name(bone);
+		}
+	}
+}
+
+int ModifierBoneTarget3D::get_bone() const {
+	return bone;
+}
+
+void ModifierBoneTarget3D::_validate_property(PropertyInfo &p_property) const {
+	if (p_property.name == "influence") {
+		p_property.usage = PROPERTY_USAGE_READ_ONLY;
+	}
+}
+
+void ModifierBoneTarget3D::_bind_methods() {
+	ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &ModifierBoneTarget3D::set_bone_name);
+	ClassDB::bind_method(D_METHOD("get_bone_name"), &ModifierBoneTarget3D::get_bone_name);
+	ClassDB::bind_method(D_METHOD("set_bone", "bone"), &ModifierBoneTarget3D::set_bone);
+	ClassDB::bind_method(D_METHOD("get_bone"), &ModifierBoneTarget3D::get_bone);
+
+	ADD_PROPERTY(PropertyInfo(Variant::STRING, "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, ""), "set_bone_name", "get_bone_name");
+	ADD_PROPERTY(PropertyInfo(Variant::INT, "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_bone", "get_bone");
+}
+
+void ModifierBoneTarget3D::_process_modification(double p_delta) {
+	if (!is_inside_tree()) {
+		return;
+	}
+
+	Skeleton3D *skeleton = get_skeleton();
+	if (!skeleton || bone < 0 || bone >= skeleton->get_bone_count()) {
+		return;
+	}
+
+	set_transform(skeleton->get_bone_global_pose(bone));
+}

+ 52 - 0
scene/3d/modifier_bone_target_3d.h

@@ -0,0 +1,52 @@
+/**************************************************************************/
+/*  modifier_bone_target_3d.h                                             */
+/**************************************************************************/
+/*                         This file is part of:                          */
+/*                             GODOT ENGINE                               */
+/*                        https://godotengine.org                         */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
+/*                                                                        */
+/* 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.                 */
+/**************************************************************************/
+
+#pragma once
+
+#include "scene/3d/skeleton_modifier_3d.h"
+
+class ModifierBoneTarget3D : public SkeletonModifier3D {
+	GDCLASS(ModifierBoneTarget3D, SkeletonModifier3D);
+
+	String bone_name;
+	int bone = -1;
+
+protected:
+	void _validate_property(PropertyInfo &p_property) const;
+	virtual void _validate_bone_names() override;
+	static void _bind_methods();
+	virtual void _process_modification(double p_delta) override;
+
+public:
+	void set_bone_name(const String &p_bone_name);
+	String get_bone_name() const;
+	void set_bone(int p_bone);
+	int get_bone() const;
+};

+ 2 - 0
scene/register_scene_types.cpp

@@ -231,6 +231,7 @@
 #include "scene/3d/look_at_modifier_3d.h"
 #include "scene/3d/look_at_modifier_3d.h"
 #include "scene/3d/marker_3d.h"
 #include "scene/3d/marker_3d.h"
 #include "scene/3d/mesh_instance_3d.h"
 #include "scene/3d/mesh_instance_3d.h"
+#include "scene/3d/modifier_bone_target_3d.h"
 #include "scene/3d/multimesh_instance_3d.h"
 #include "scene/3d/multimesh_instance_3d.h"
 #include "scene/3d/node_3d.h"
 #include "scene/3d/node_3d.h"
 #include "scene/3d/occluder_instance_3d.h"
 #include "scene/3d/occluder_instance_3d.h"
@@ -642,6 +643,7 @@ void register_scene_types() {
 	GDREGISTER_CLASS(GPUParticlesAttractorVectorField3D);
 	GDREGISTER_CLASS(GPUParticlesAttractorVectorField3D);
 	GDREGISTER_CLASS(CPUParticles3D);
 	GDREGISTER_CLASS(CPUParticles3D);
 	GDREGISTER_CLASS(Marker3D);
 	GDREGISTER_CLASS(Marker3D);
+	GDREGISTER_CLASS(ModifierBoneTarget3D);
 	GDREGISTER_CLASS(RootMotionView);
 	GDREGISTER_CLASS(RootMotionView);
 	GDREGISTER_VIRTUAL_CLASS(SkeletonModifier3D);
 	GDREGISTER_VIRTUAL_CLASS(SkeletonModifier3D);
 	GDREGISTER_CLASS(RetargetModifier3D);
 	GDREGISTER_CLASS(RetargetModifier3D);