look_at_modifier_3d.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* look_at_modifier_3d.h */
  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. #ifndef LOOK_AT_MODIFIER_3D_H
  31. #define LOOK_AT_MODIFIER_3D_H
  32. #include "scene/3d/skeleton_modifier_3d.h"
  33. #include "scene/animation/tween.h"
  34. class LookAtModifier3D : public SkeletonModifier3D {
  35. GDCLASS(LookAtModifier3D, SkeletonModifier3D);
  36. public:
  37. enum BoneAxis {
  38. BONE_AXIS_PLUS_X,
  39. BONE_AXIS_MINUS_X,
  40. BONE_AXIS_PLUS_Y,
  41. BONE_AXIS_MINUS_Y,
  42. BONE_AXIS_PLUS_Z,
  43. BONE_AXIS_MINUS_Z,
  44. };
  45. enum OriginFrom {
  46. ORIGIN_FROM_SELF,
  47. ORIGIN_FROM_SPECIFIC_BONE,
  48. ORIGIN_FROM_EXTERNAL_NODE,
  49. };
  50. private:
  51. int bone = 0;
  52. Vector3 forward_vector;
  53. Vector3 forward_vector_nrm;
  54. BoneAxis forward_axis = BONE_AXIS_PLUS_Z;
  55. Vector3::Axis primary_rotation_axis = Vector3::AXIS_Y;
  56. Vector3::Axis secondary_rotation_axis = Vector3::AXIS_X;
  57. bool use_secondary_rotation = true;
  58. OriginFrom origin_from = ORIGIN_FROM_SELF;
  59. int origin_bone = -1;
  60. NodePath origin_external_node;
  61. Vector3 origin_offset;
  62. float origin_safe_margin = 0.1;
  63. NodePath target_node;
  64. float duration = 0;
  65. Tween::TransitionType transition_type = Tween::TRANS_LINEAR;
  66. Tween::EaseType ease_type = Tween::EASE_IN;
  67. bool use_angle_limitation = false;
  68. bool symmetry_limitation = true;
  69. float primary_limit_angle = Math_TAU;
  70. float primary_damp_threshold = 1.0f;
  71. float primary_positive_limit_angle = Math_PI;
  72. float primary_positive_damp_threshold = 1.0f;
  73. float primary_negative_limit_angle = Math_PI;
  74. float primary_negative_damp_threshold = 1.0f;
  75. float secondary_limit_angle = Math_TAU;
  76. float secondary_damp_threshold = 1.0f;
  77. float secondary_positive_limit_angle = Math_PI;
  78. float secondary_positive_damp_threshold = 1.0f;
  79. float secondary_negative_limit_angle = Math_PI;
  80. float secondary_negative_damp_threshold = 1.0f;
  81. bool is_within_limitations = false;
  82. // For time-based interpolation.
  83. Quaternion from_q;
  84. Quaternion prev_q;
  85. float remaining = 0;
  86. float time_step = 1.0;
  87. Vector3 get_basis_vector_from_bone_axis(const Basis &p_basis, BoneAxis p_axis) const;
  88. Vector3 get_vector_from_bone_axis(const BoneAxis &p_axis) const;
  89. Vector3 get_vector_from_axis(const Vector3::Axis &p_axis) const;
  90. Vector3::Axis get_axis_from_bone_axis(BoneAxis p_axis) const;
  91. Vector2 get_projection_vector(const Vector3 &p_vector, Vector3::Axis p_axis) const;
  92. float remap_damped(float p_from, float p_to, float p_damp_threshold, float p_value) const;
  93. double get_bspline_y(const Vector2 &p_from, const Vector2 &p_control, const Vector2 &p_to, double p_x) const;
  94. bool is_intersecting_axis(const Vector3 &p_prev, const Vector3 &p_current, Vector3::Axis p_flipping_axis, Vector3::Axis p_check_axis, bool p_check_plane = false) const;
  95. Transform3D look_at_with_axes(const Transform3D &p_rest);
  96. void init_transition();
  97. protected:
  98. virtual PackedStringArray get_configuration_warnings() const override;
  99. void _validate_property(PropertyInfo &p_property) const;
  100. static void _bind_methods();
  101. virtual void _process_modification() override;
  102. public:
  103. void set_bone(int p_bone);
  104. int get_bone() const;
  105. void set_forward_axis(BoneAxis p_axis);
  106. BoneAxis get_forward_axis() const;
  107. void set_primary_rotation_axis(Vector3::Axis p_axis);
  108. Vector3::Axis get_primary_rotation_axis() const;
  109. void set_use_secondary_rotation(bool p_enabled);
  110. bool is_using_secondary_rotation() const;
  111. void set_origin_from(OriginFrom p_origin_from);
  112. OriginFrom get_origin_from() const;
  113. void set_origin_bone(int p_bone);
  114. int get_origin_bone() const;
  115. void set_origin_external_node(const NodePath &p_external_node);
  116. NodePath get_origin_external_node() const;
  117. void set_origin_offset(const Vector3 &p_offset);
  118. Vector3 get_origin_offset() const;
  119. void set_origin_safe_margin(float p_margin);
  120. float get_origin_safe_margin() const;
  121. void set_target_node(const NodePath &p_target_node);
  122. NodePath get_target_node() const;
  123. void set_duration(float p_duration);
  124. float get_duration() const;
  125. void set_transition_type(Tween::TransitionType p_transition_type);
  126. Tween::TransitionType get_transition_type() const;
  127. void set_ease_type(Tween::EaseType p_ease_type);
  128. Tween::EaseType get_ease_type() const;
  129. void set_use_angle_limitation(bool p_enabled);
  130. bool is_using_angle_limitation() const;
  131. void set_symmetry_limitation(bool p_enabled);
  132. bool is_limitation_symmetry() const;
  133. void set_primary_limit_angle(float p_angle);
  134. float get_primary_limit_angle() const;
  135. void set_primary_damp_threshold(float p_power);
  136. float get_primary_damp_threshold() const;
  137. void set_primary_positive_limit_angle(float p_angle);
  138. float get_primary_positive_limit_angle() const;
  139. void set_primary_positive_damp_threshold(float p_power);
  140. float get_primary_positive_damp_threshold() const;
  141. void set_primary_negative_limit_angle(float p_angle);
  142. float get_primary_negative_limit_angle() const;
  143. void set_primary_negative_damp_threshold(float p_power);
  144. float get_primary_negative_damp_threshold() const;
  145. void set_secondary_limit_angle(float p_angle);
  146. float get_secondary_limit_angle() const;
  147. void set_secondary_damp_threshold(float p_power);
  148. float get_secondary_damp_threshold() const;
  149. void set_secondary_positive_limit_angle(float p_angle);
  150. float get_secondary_positive_limit_angle() const;
  151. void set_secondary_positive_damp_threshold(float p_power);
  152. float get_secondary_positive_damp_threshold() const;
  153. void set_secondary_negative_limit_angle(float p_angle);
  154. float get_secondary_negative_limit_angle() const;
  155. void set_secondary_negative_damp_threshold(float p_power);
  156. float get_secondary_negative_damp_threshold() const;
  157. float get_interpolation_remaining() const;
  158. bool is_interpolating() const;
  159. bool is_target_within_limitation() const;
  160. };
  161. VARIANT_ENUM_CAST(LookAtModifier3D::BoneAxis);
  162. VARIANT_ENUM_CAST(LookAtModifier3D::OriginFrom);
  163. #endif // LOOK_AT_MODIFIER_3D_H