xr_face_tracker.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* xr_face_tracker.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. #include "xr_face_tracker.h"
  31. void XRFaceTracker::_bind_methods() {
  32. // Base Shapes
  33. BIND_ENUM_CONSTANT(FT_EYE_LOOK_OUT_RIGHT);
  34. BIND_ENUM_CONSTANT(FT_EYE_LOOK_IN_RIGHT);
  35. BIND_ENUM_CONSTANT(FT_EYE_LOOK_UP_RIGHT);
  36. BIND_ENUM_CONSTANT(FT_EYE_LOOK_DOWN_RIGHT);
  37. BIND_ENUM_CONSTANT(FT_EYE_LOOK_OUT_LEFT);
  38. BIND_ENUM_CONSTANT(FT_EYE_LOOK_IN_LEFT);
  39. BIND_ENUM_CONSTANT(FT_EYE_LOOK_UP_LEFT);
  40. BIND_ENUM_CONSTANT(FT_EYE_LOOK_DOWN_LEFT);
  41. BIND_ENUM_CONSTANT(FT_EYE_CLOSED_RIGHT);
  42. BIND_ENUM_CONSTANT(FT_EYE_CLOSED_LEFT);
  43. BIND_ENUM_CONSTANT(FT_EYE_SQUINT_RIGHT);
  44. BIND_ENUM_CONSTANT(FT_EYE_SQUINT_LEFT);
  45. BIND_ENUM_CONSTANT(FT_EYE_WIDE_RIGHT);
  46. BIND_ENUM_CONSTANT(FT_EYE_WIDE_LEFT);
  47. BIND_ENUM_CONSTANT(FT_EYE_DILATION_RIGHT);
  48. BIND_ENUM_CONSTANT(FT_EYE_DILATION_LEFT);
  49. BIND_ENUM_CONSTANT(FT_EYE_CONSTRICT_RIGHT);
  50. BIND_ENUM_CONSTANT(FT_EYE_CONSTRICT_LEFT);
  51. BIND_ENUM_CONSTANT(FT_BROW_PINCH_RIGHT);
  52. BIND_ENUM_CONSTANT(FT_BROW_PINCH_LEFT);
  53. BIND_ENUM_CONSTANT(FT_BROW_LOWERER_RIGHT);
  54. BIND_ENUM_CONSTANT(FT_BROW_LOWERER_LEFT);
  55. BIND_ENUM_CONSTANT(FT_BROW_INNER_UP_RIGHT);
  56. BIND_ENUM_CONSTANT(FT_BROW_INNER_UP_LEFT);
  57. BIND_ENUM_CONSTANT(FT_BROW_OUTER_UP_RIGHT);
  58. BIND_ENUM_CONSTANT(FT_BROW_OUTER_UP_LEFT);
  59. BIND_ENUM_CONSTANT(FT_NOSE_SNEER_RIGHT);
  60. BIND_ENUM_CONSTANT(FT_NOSE_SNEER_LEFT);
  61. BIND_ENUM_CONSTANT(FT_NASAL_DILATION_RIGHT);
  62. BIND_ENUM_CONSTANT(FT_NASAL_DILATION_LEFT);
  63. BIND_ENUM_CONSTANT(FT_NASAL_CONSTRICT_RIGHT);
  64. BIND_ENUM_CONSTANT(FT_NASAL_CONSTRICT_LEFT);
  65. BIND_ENUM_CONSTANT(FT_CHEEK_SQUINT_RIGHT);
  66. BIND_ENUM_CONSTANT(FT_CHEEK_SQUINT_LEFT);
  67. BIND_ENUM_CONSTANT(FT_CHEEK_PUFF_RIGHT);
  68. BIND_ENUM_CONSTANT(FT_CHEEK_PUFF_LEFT);
  69. BIND_ENUM_CONSTANT(FT_CHEEK_SUCK_RIGHT);
  70. BIND_ENUM_CONSTANT(FT_CHEEK_SUCK_LEFT);
  71. BIND_ENUM_CONSTANT(FT_JAW_OPEN);
  72. BIND_ENUM_CONSTANT(FT_MOUTH_CLOSED);
  73. BIND_ENUM_CONSTANT(FT_JAW_RIGHT);
  74. BIND_ENUM_CONSTANT(FT_JAW_LEFT);
  75. BIND_ENUM_CONSTANT(FT_JAW_FORWARD);
  76. BIND_ENUM_CONSTANT(FT_JAW_BACKWARD);
  77. BIND_ENUM_CONSTANT(FT_JAW_CLENCH);
  78. BIND_ENUM_CONSTANT(FT_JAW_MANDIBLE_RAISE);
  79. BIND_ENUM_CONSTANT(FT_LIP_SUCK_UPPER_RIGHT);
  80. BIND_ENUM_CONSTANT(FT_LIP_SUCK_UPPER_LEFT);
  81. BIND_ENUM_CONSTANT(FT_LIP_SUCK_LOWER_RIGHT);
  82. BIND_ENUM_CONSTANT(FT_LIP_SUCK_LOWER_LEFT);
  83. BIND_ENUM_CONSTANT(FT_LIP_SUCK_CORNER_RIGHT);
  84. BIND_ENUM_CONSTANT(FT_LIP_SUCK_CORNER_LEFT);
  85. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_UPPER_RIGHT);
  86. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_UPPER_LEFT);
  87. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_LOWER_RIGHT);
  88. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_LOWER_LEFT);
  89. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_UPPER_RIGHT);
  90. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_UPPER_LEFT);
  91. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_LOWER_RIGHT);
  92. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_LOWER_LEFT);
  93. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_UP_RIGHT);
  94. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_UP_LEFT);
  95. BIND_ENUM_CONSTANT(FT_MOUTH_LOWER_DOWN_RIGHT);
  96. BIND_ENUM_CONSTANT(FT_MOUTH_LOWER_DOWN_LEFT);
  97. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_DEEPEN_RIGHT);
  98. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_DEEPEN_LEFT);
  99. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_RIGHT);
  100. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_LEFT);
  101. BIND_ENUM_CONSTANT(FT_MOUTH_LOWER_RIGHT);
  102. BIND_ENUM_CONSTANT(FT_MOUTH_LOWER_LEFT);
  103. BIND_ENUM_CONSTANT(FT_MOUTH_CORNER_PULL_RIGHT);
  104. BIND_ENUM_CONSTANT(FT_MOUTH_CORNER_PULL_LEFT);
  105. BIND_ENUM_CONSTANT(FT_MOUTH_CORNER_SLANT_RIGHT);
  106. BIND_ENUM_CONSTANT(FT_MOUTH_CORNER_SLANT_LEFT);
  107. BIND_ENUM_CONSTANT(FT_MOUTH_FROWN_RIGHT);
  108. BIND_ENUM_CONSTANT(FT_MOUTH_FROWN_LEFT);
  109. BIND_ENUM_CONSTANT(FT_MOUTH_STRETCH_RIGHT);
  110. BIND_ENUM_CONSTANT(FT_MOUTH_STRETCH_LEFT);
  111. BIND_ENUM_CONSTANT(FT_MOUTH_DIMPLE_RIGHT);
  112. BIND_ENUM_CONSTANT(FT_MOUTH_DIMPLE_LEFT);
  113. BIND_ENUM_CONSTANT(FT_MOUTH_RAISER_UPPER);
  114. BIND_ENUM_CONSTANT(FT_MOUTH_RAISER_LOWER);
  115. BIND_ENUM_CONSTANT(FT_MOUTH_PRESS_RIGHT);
  116. BIND_ENUM_CONSTANT(FT_MOUTH_PRESS_LEFT);
  117. BIND_ENUM_CONSTANT(FT_MOUTH_TIGHTENER_RIGHT);
  118. BIND_ENUM_CONSTANT(FT_MOUTH_TIGHTENER_LEFT);
  119. BIND_ENUM_CONSTANT(FT_TONGUE_OUT);
  120. BIND_ENUM_CONSTANT(FT_TONGUE_UP);
  121. BIND_ENUM_CONSTANT(FT_TONGUE_DOWN);
  122. BIND_ENUM_CONSTANT(FT_TONGUE_RIGHT);
  123. BIND_ENUM_CONSTANT(FT_TONGUE_LEFT);
  124. BIND_ENUM_CONSTANT(FT_TONGUE_ROLL);
  125. BIND_ENUM_CONSTANT(FT_TONGUE_BLEND_DOWN);
  126. BIND_ENUM_CONSTANT(FT_TONGUE_CURL_UP);
  127. BIND_ENUM_CONSTANT(FT_TONGUE_SQUISH);
  128. BIND_ENUM_CONSTANT(FT_TONGUE_FLAT);
  129. BIND_ENUM_CONSTANT(FT_TONGUE_TWIST_RIGHT);
  130. BIND_ENUM_CONSTANT(FT_TONGUE_TWIST_LEFT);
  131. BIND_ENUM_CONSTANT(FT_SOFT_PALATE_CLOSE);
  132. BIND_ENUM_CONSTANT(FT_THROAT_SWALLOW);
  133. BIND_ENUM_CONSTANT(FT_NECK_FLEX_RIGHT);
  134. BIND_ENUM_CONSTANT(FT_NECK_FLEX_LEFT);
  135. // Blended Shapes
  136. BIND_ENUM_CONSTANT(FT_EYE_CLOSED);
  137. BIND_ENUM_CONSTANT(FT_EYE_WIDE);
  138. BIND_ENUM_CONSTANT(FT_EYE_SQUINT);
  139. BIND_ENUM_CONSTANT(FT_EYE_DILATION);
  140. BIND_ENUM_CONSTANT(FT_EYE_CONSTRICT);
  141. BIND_ENUM_CONSTANT(FT_BROW_DOWN_RIGHT);
  142. BIND_ENUM_CONSTANT(FT_BROW_DOWN_LEFT);
  143. BIND_ENUM_CONSTANT(FT_BROW_DOWN);
  144. BIND_ENUM_CONSTANT(FT_BROW_UP_RIGHT);
  145. BIND_ENUM_CONSTANT(FT_BROW_UP_LEFT);
  146. BIND_ENUM_CONSTANT(FT_BROW_UP);
  147. BIND_ENUM_CONSTANT(FT_NOSE_SNEER);
  148. BIND_ENUM_CONSTANT(FT_NASAL_DILATION);
  149. BIND_ENUM_CONSTANT(FT_NASAL_CONSTRICT);
  150. BIND_ENUM_CONSTANT(FT_CHEEK_PUFF);
  151. BIND_ENUM_CONSTANT(FT_CHEEK_SUCK);
  152. BIND_ENUM_CONSTANT(FT_CHEEK_SQUINT);
  153. BIND_ENUM_CONSTANT(FT_LIP_SUCK_UPPER);
  154. BIND_ENUM_CONSTANT(FT_LIP_SUCK_LOWER);
  155. BIND_ENUM_CONSTANT(FT_LIP_SUCK);
  156. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_UPPER);
  157. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL_LOWER);
  158. BIND_ENUM_CONSTANT(FT_LIP_FUNNEL);
  159. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_UPPER);
  160. BIND_ENUM_CONSTANT(FT_LIP_PUCKER_LOWER);
  161. BIND_ENUM_CONSTANT(FT_LIP_PUCKER);
  162. BIND_ENUM_CONSTANT(FT_MOUTH_UPPER_UP);
  163. BIND_ENUM_CONSTANT(FT_MOUTH_LOWER_DOWN);
  164. BIND_ENUM_CONSTANT(FT_MOUTH_OPEN);
  165. BIND_ENUM_CONSTANT(FT_MOUTH_RIGHT);
  166. BIND_ENUM_CONSTANT(FT_MOUTH_LEFT);
  167. BIND_ENUM_CONSTANT(FT_MOUTH_SMILE_RIGHT);
  168. BIND_ENUM_CONSTANT(FT_MOUTH_SMILE_LEFT);
  169. BIND_ENUM_CONSTANT(FT_MOUTH_SMILE);
  170. BIND_ENUM_CONSTANT(FT_MOUTH_SAD_RIGHT);
  171. BIND_ENUM_CONSTANT(FT_MOUTH_SAD_LEFT);
  172. BIND_ENUM_CONSTANT(FT_MOUTH_SAD);
  173. BIND_ENUM_CONSTANT(FT_MOUTH_STRETCH);
  174. BIND_ENUM_CONSTANT(FT_MOUTH_DIMPLE);
  175. BIND_ENUM_CONSTANT(FT_MOUTH_TIGHTENER);
  176. BIND_ENUM_CONSTANT(FT_MOUTH_PRESS);
  177. BIND_ENUM_CONSTANT(FT_MAX);
  178. ClassDB::bind_method(D_METHOD("get_blend_shape", "blend_shape"), &XRFaceTracker::get_blend_shape);
  179. ClassDB::bind_method(D_METHOD("set_blend_shape", "blend_shape", "weight"), &XRFaceTracker::set_blend_shape);
  180. ClassDB::bind_method(D_METHOD("get_blend_shapes"), &XRFaceTracker::get_blend_shapes);
  181. ClassDB::bind_method(D_METHOD("set_blend_shapes", "weights"), &XRFaceTracker::set_blend_shapes);
  182. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "blend_shapes"), "set_blend_shapes", "get_blend_shapes");
  183. ADD_PROPERTY_DEFAULT("blend_shapes", PackedFloat32Array()); // To prevent ludicrously large default values.
  184. }
  185. void XRFaceTracker::set_tracker_type(XRServer::TrackerType p_type) {
  186. ERR_FAIL_COND_MSG(p_type != XRServer::TRACKER_FACE, "XRFaceTracker must be of type TRACKER_FACE.");
  187. }
  188. float XRFaceTracker::get_blend_shape(BlendShapeEntry p_blend_shape) const {
  189. // Fail if the blend shape index is out of range.
  190. ERR_FAIL_INDEX_V(p_blend_shape, FT_MAX, 0.0f);
  191. // Return the blend shape value.
  192. return blend_shape_values[p_blend_shape];
  193. }
  194. void XRFaceTracker::set_blend_shape(BlendShapeEntry p_blend_shape, float p_value) {
  195. // Fail if the blend shape index is out of range.
  196. ERR_FAIL_INDEX(p_blend_shape, FT_MAX);
  197. // Save the new blend shape value.
  198. blend_shape_values[p_blend_shape] = p_value;
  199. }
  200. PackedFloat32Array XRFaceTracker::get_blend_shapes() const {
  201. // Create a packed float32 array and copy the blend shape values into it.
  202. PackedFloat32Array data;
  203. data.resize(FT_MAX);
  204. memcpy(data.ptrw(), blend_shape_values, sizeof(blend_shape_values));
  205. // Return the blend shape array.
  206. return data;
  207. }
  208. void XRFaceTracker::set_blend_shapes(const PackedFloat32Array &p_blend_shapes) {
  209. // Fail if the blend shape array is not the correct size.
  210. ERR_FAIL_COND(p_blend_shapes.size() != FT_MAX);
  211. // Copy the blend shape values into the blend shape array.
  212. memcpy(blend_shape_values, p_blend_shapes.ptr(), sizeof(blend_shape_values));
  213. }
  214. XRFaceTracker::XRFaceTracker() {
  215. type = XRServer::TRACKER_FACE;
  216. }