| 1 | /**************************************************************************/ |
| 2 | /* aim_modifier_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "aim_modifier_3d.h" |
| 32 | #include "scene/3d/look_at_modifier_3d.h" |
| 33 | |
| 34 | bool AimModifier3D::_set(const StringName &p_path, const Variant &p_value) { |
| 35 | String path = p_path; |
| 36 | |
| 37 | if (path.begins_with("settings/")) { |
| 38 | int which = path.get_slicec('/', 1).to_int(); |
| 39 | String what = path.get_slicec('/', 2); |
| 40 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 41 | |
| 42 | if (what == "forward_axis") { |
| 43 | set_forward_axis(which, static_cast<BoneAxis>((int)p_value)); |
| 44 | } else if (what == "use_euler") { |
| 45 | set_use_euler(which, p_value); |
| 46 | } else if (what == "primary_rotation_axis") { |
| 47 | set_primary_rotation_axis(which, static_cast<Vector3::Axis>((int)p_value)); |
| 48 | } else if (what == "use_secondary_rotation") { |
| 49 | set_use_secondary_rotation(which, p_value); |
| 50 | } else if (what == "relative") { |
| 51 | set_relative(which, p_value); |
| 52 | } else { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool AimModifier3D::_get(const StringName &p_path, Variant &r_ret) const { |
| 60 | String path = p_path; |
| 61 | |
| 62 | if (path.begins_with("settings/")) { |
| 63 | int which = path.get_slicec('/', 1).to_int(); |
| 64 | String what = path.get_slicec('/', 2); |
| 65 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 66 | |
| 67 | if (what == "forward_axis") { |
| 68 | r_ret = (int)get_forward_axis(which); |
| 69 | } else if (what == "use_euler") { |
| 70 | r_ret = is_using_euler(which); |
| 71 | } else if (what == "primary_rotation_axis") { |
| 72 | r_ret = (int)get_primary_rotation_axis(which); |
| 73 | } else if (what == "use_secondary_rotation") { |
| 74 | r_ret = is_using_secondary_rotation(which); |
| 75 | } else if (what == "relative") { |
| 76 | r_ret = is_relative(which); |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | void AimModifier3D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 85 | BoneConstraint3D::get_property_list(p_list); |
| 86 | |
| 87 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 88 | String path = "settings/" + itos(i) + "/"; |
| 89 | int rotation_usage = is_using_euler(i) ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_NONE; |
| 90 | |
| 91 | p_list->push_back(PropertyInfo(Variant::INT, path + "forward_axis", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_axis())); |
| 92 | p_list->push_back(PropertyInfo(Variant::BOOL, path + "use_euler")); |
| 93 | p_list->push_back(PropertyInfo(Variant::INT, path + "primary_rotation_axis", PROPERTY_HINT_ENUM, "X,Y,Z", rotation_usage)); |
| 94 | p_list->push_back(PropertyInfo(Variant::BOOL, path + "use_secondary_rotation", PROPERTY_HINT_NONE, "", rotation_usage)); |
| 95 | p_list->push_back(PropertyInfo(Variant::BOOL, path + "relative")); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | PackedStringArray AimModifier3D::get_configuration_warnings() const { |
| 100 | PackedStringArray warnings = BoneConstraint3D::get_configuration_warnings(); |
| 101 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 102 | if (is_using_euler(i) && get_axis_from_bone_axis(get_forward_axis(i)) == get_primary_rotation_axis(i)) { |
| 103 | warnings.push_back(vformat(RTR("Forward axis and primary rotation axis must not be parallel in setting %s."), itos(i))); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return warnings; |
| 108 | } |
| 109 | |
| 110 | void AimModifier3D::_validate_setting(int p_index) { |
| 111 | settings[p_index] = memnew(AimModifier3DSetting); |
| 112 | } |
| 113 | |
| 114 | void AimModifier3D::set_forward_axis(int p_index, BoneAxis p_axis) { |
| 115 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 116 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 117 | setting->forward_axis = p_axis; |
| 118 | update_configuration_warnings(); |
| 119 | } |
| 120 | |
| 121 | SkeletonModifier3D::BoneAxis AimModifier3D::get_forward_axis(int p_index) const { |
| 122 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), BONE_AXIS_PLUS_Y); |
| 123 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 124 | return setting->forward_axis; |
| 125 | } |
| 126 | |
| 127 | void AimModifier3D::set_use_euler(int p_index, bool p_enabled) { |
| 128 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 129 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 130 | setting->use_euler = p_enabled; |
| 131 | notify_property_list_changed(); |
| 132 | update_configuration_warnings(); |
| 133 | } |
| 134 | |
| 135 | bool AimModifier3D::is_using_euler(int p_index) const { |
| 136 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 137 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 138 | return setting->use_euler; |
| 139 | } |
| 140 | |
| 141 | void AimModifier3D::set_primary_rotation_axis(int p_index, Vector3::Axis p_axis) { |
| 142 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 143 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 144 | setting->primary_rotation_axis = p_axis; |
| 145 | update_configuration_warnings(); |
| 146 | } |
| 147 | |
| 148 | Vector3::Axis AimModifier3D::get_primary_rotation_axis(int p_index) const { |
| 149 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3::AXIS_X); |
| 150 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 151 | return setting->primary_rotation_axis; |
| 152 | } |
| 153 | |
| 154 | void AimModifier3D::set_use_secondary_rotation(int p_index, bool p_enabled) { |
| 155 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 156 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 157 | setting->use_secondary_rotation = p_enabled; |
| 158 | } |
| 159 | |
| 160 | bool AimModifier3D::is_using_secondary_rotation(int p_index) const { |
| 161 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 162 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 163 | return setting->use_secondary_rotation; |
| 164 | } |
| 165 | |
| 166 | void AimModifier3D::set_relative(int p_index, bool p_enabled) { |
| 167 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 168 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 169 | setting->relative = p_enabled; |
| 170 | } |
| 171 | |
| 172 | bool AimModifier3D::is_relative(int p_index) const { |
| 173 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 174 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 175 | return setting->relative; |
| 176 | } |
| 177 | |
| 178 | void AimModifier3D::_bind_methods() { |
| 179 | ClassDB::bind_method(D_METHOD("set_forward_axis", "index", "axis"), &AimModifier3D::set_forward_axis); |
| 180 | ClassDB::bind_method(D_METHOD("get_forward_axis", "index"), &AimModifier3D::get_forward_axis); |
| 181 | ClassDB::bind_method(D_METHOD("set_use_euler", "index", "enabled"), &AimModifier3D::set_use_euler); |
| 182 | ClassDB::bind_method(D_METHOD("is_using_euler", "index"), &AimModifier3D::is_using_euler); |
| 183 | ClassDB::bind_method(D_METHOD("set_primary_rotation_axis", "index", "axis"), &AimModifier3D::set_primary_rotation_axis); |
| 184 | ClassDB::bind_method(D_METHOD("get_primary_rotation_axis", "index"), &AimModifier3D::get_primary_rotation_axis); |
| 185 | ClassDB::bind_method(D_METHOD("set_use_secondary_rotation", "index", "enabled"), &AimModifier3D::set_use_secondary_rotation); |
| 186 | ClassDB::bind_method(D_METHOD("is_using_secondary_rotation", "index"), &AimModifier3D::is_using_secondary_rotation); |
| 187 | ClassDB::bind_method(D_METHOD("set_relative", "index", "enabled"), &AimModifier3D::set_relative); |
| 188 | ClassDB::bind_method(D_METHOD("is_relative", "index"), &AimModifier3D::is_relative); |
| 189 | |
| 190 | ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/"); |
| 191 | } |
| 192 | |
| 193 | void AimModifier3D::_process_constraint_by_bone(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, int p_reference_bone, float p_amount) { |
| 194 | if (p_apply_bone == p_reference_bone) { |
| 195 | ERR_PRINT_ONCE_ED(vformat("In setting %s, the reference bone must not be same with the apply bone.", itos(p_index))); |
| 196 | return; |
| 197 | } |
| 198 | Vector3 reference_origin = p_skeleton->get_bone_global_pose(p_reference_bone).origin; |
| 199 | _process_aim(p_index, p_skeleton, p_apply_bone, reference_origin, p_amount); |
| 200 | } |
| 201 | |
| 202 | void AimModifier3D::_process_constraint_by_node(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, const NodePath &p_reference_node, float p_amount) { |
| 203 | Node3D *nd = Object::cast_to<Node3D>(get_node_or_null(p_reference_node)); |
| 204 | if (!nd) { |
| 205 | return; |
| 206 | } |
| 207 | Transform3D skel_tr = p_skeleton->get_global_transform_interpolated(); |
| 208 | Vector3 reference_origin = nd->get_global_transform_interpolated().origin - skel_tr.origin; |
| 209 | _process_aim(p_index, p_skeleton, p_apply_bone, skel_tr.basis.get_rotation_quaternion().xform_inv(reference_origin), p_amount); |
| 210 | } |
| 211 | |
| 212 | void AimModifier3D::_process_aim(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, Vector3 p_target, float p_amount) { |
| 213 | AimModifier3DSetting *setting = static_cast<AimModifier3DSetting *>(settings[p_index]); |
| 214 | |
| 215 | // Prepare forward_vector and rest. |
| 216 | Transform3D src_bone_rest = setting->relative ? p_skeleton->get_bone_pose(p_apply_bone) : p_skeleton->get_bone_rest(p_apply_bone); |
| 217 | Transform3D bone_rest_space; |
| 218 | int parent_bone = p_skeleton->get_bone_parent(p_apply_bone); |
| 219 | if (parent_bone < 0) { |
| 220 | bone_rest_space.translate_local(src_bone_rest.origin); |
| 221 | } else { |
| 222 | bone_rest_space = p_skeleton->get_bone_global_pose(parent_bone); |
| 223 | bone_rest_space.translate_local(src_bone_rest.origin); |
| 224 | } |
| 225 | Vector3 forward_vector = bone_rest_space.basis.get_rotation_quaternion().xform_inv(p_target - bone_rest_space.origin); |
| 226 | if (forward_vector.is_zero_approx()) { |
| 227 | return; |
| 228 | } |
| 229 | forward_vector.normalize(); |
| 230 | |
| 231 | // Calculate look at rotation. |
| 232 | Quaternion destination; |
| 233 | if (setting->use_euler) { |
| 234 | Vector3 current_vector = LookAtModifier3D::get_basis_vector_from_bone_axis(src_bone_rest.basis, setting->forward_axis).normalized(); |
| 235 | Vector2 src_vec2 = LookAtModifier3D::get_projection_vector(src_bone_rest.basis.xform_inv(forward_vector), setting->primary_rotation_axis).normalized(); |
| 236 | Vector2 dst_vec2 = LookAtModifier3D::get_projection_vector(src_bone_rest.basis.xform_inv(current_vector), setting->primary_rotation_axis).normalized(); |
| 237 | real_t calculated_angle = src_vec2.angle_to(dst_vec2); |
| 238 | Transform3D primary_result = src_bone_rest.rotated_local(get_vector_from_axis(setting->primary_rotation_axis), calculated_angle); |
| 239 | Transform3D current_result = primary_result; |
| 240 | if (setting->use_secondary_rotation) { |
| 241 | Vector3::Axis secondary_rotation_axis = LookAtModifier3D::get_secondary_rotation_axis(setting->forward_axis, setting->primary_rotation_axis); |
| 242 | current_vector = LookAtModifier3D::get_basis_vector_from_bone_axis(primary_result.basis, setting->forward_axis).normalized(); |
| 243 | src_vec2 = LookAtModifier3D::get_projection_vector(primary_result.basis.xform_inv(forward_vector), secondary_rotation_axis).normalized(); |
| 244 | dst_vec2 = LookAtModifier3D::get_projection_vector(primary_result.basis.xform_inv(current_vector), secondary_rotation_axis).normalized(); |
| 245 | calculated_angle = src_vec2.angle_to(dst_vec2); |
| 246 | current_result = primary_result.rotated_local(get_vector_from_axis(secondary_rotation_axis), calculated_angle); |
| 247 | } |
| 248 | destination = current_result.basis.get_rotation_quaternion(); |
| 249 | } else { |
| 250 | Vector3 current_vector = LookAtModifier3D::get_basis_vector_from_bone_axis(src_bone_rest.basis, setting->forward_axis).normalized(); |
| 251 | destination = Quaternion(current_vector, forward_vector) * src_bone_rest.basis.get_rotation_quaternion(); |
| 252 | } |
| 253 | |
| 254 | p_skeleton->set_bone_pose_rotation(p_apply_bone, p_skeleton->get_bone_pose_rotation(p_apply_bone).slerp(destination, p_amount)); |
| 255 | } |
| 256 | |
| 257 | AimModifier3D::~AimModifier3D() { |
| 258 | clear_settings(); |
| 259 | } |