look_at_modifier_3d.h 7.7 KB

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