SkeletonUtilityBone.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2.1
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software (typically granted by licensing Spine), you
  12. * may not (a) modify, translate, adapt or otherwise create derivative works,
  13. * improvements of the Software or develop new applications using the Software
  14. * or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. * trademark, patent or other intellectual property or proprietary rights
  16. * notices on or in the Software, including any copy thereof. Redistributions
  17. * in binary or source form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. /*****************************************************************************
  31. * Skeleton Utility created by Mitch Thompson
  32. * Full irrevocable rights and permissions granted to Esoteric Software
  33. *****************************************************************************/
  34. using System;
  35. using System.IO;
  36. using System.Collections.Generic;
  37. using UnityEngine;
  38. using Spine;
  39. /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
  40. [ExecuteInEditMode]
  41. [AddComponentMenu("Spine/SkeletonUtilityBone")]
  42. public class SkeletonUtilityBone : MonoBehaviour
  43. {
  44. public enum Mode
  45. {
  46. Follow,
  47. Override
  48. }
  49. [System.NonSerialized]
  50. public bool valid;
  51. [System.NonSerialized]
  52. public SkeletonUtility skeletonUtility;
  53. [System.NonSerialized]
  54. public Bone bone;
  55. public Mode mode;
  56. public bool zPosition = true;
  57. public bool position;
  58. public bool rotation;
  59. public bool scale;
  60. public bool flip;
  61. public bool flipX;
  62. [Range(0f, 1f)]
  63. public float overrideAlpha = 1;
  64. /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
  65. public String boneName;
  66. public Transform parentReference;
  67. [HideInInspector]
  68. public bool transformLerpComplete;
  69. protected Transform cachedTransform;
  70. protected Transform skeletonTransform;
  71. public bool NonUniformScaleWarning
  72. {
  73. get
  74. {
  75. return nonUniformScaleWarning;
  76. }
  77. }
  78. private bool nonUniformScaleWarning;
  79. public void Reset()
  80. {
  81. bone = null;
  82. cachedTransform = transform;
  83. valid = skeletonUtility != null && skeletonUtility.skeletonRenderer != null && skeletonUtility.skeletonRenderer.valid;
  84. if (!valid)
  85. return;
  86. skeletonTransform = skeletonUtility.transform;
  87. skeletonUtility.OnReset -= HandleOnReset;
  88. skeletonUtility.OnReset += HandleOnReset;
  89. DoUpdate();
  90. }
  91. void OnEnable()
  92. {
  93. skeletonUtility = SkeletonUtility.GetInParent<SkeletonUtility>(transform);
  94. if (skeletonUtility == null)
  95. return;
  96. skeletonUtility.RegisterBone(this);
  97. skeletonUtility.OnReset += HandleOnReset;
  98. }
  99. void HandleOnReset()
  100. {
  101. Reset();
  102. }
  103. void OnDisable()
  104. {
  105. if (skeletonUtility != null)
  106. {
  107. skeletonUtility.OnReset -= HandleOnReset;
  108. skeletonUtility.UnregisterBone(this);
  109. }
  110. }
  111. public void DoUpdate()
  112. {
  113. if (!valid)
  114. {
  115. Reset();
  116. return;
  117. }
  118. Spine.Skeleton skeleton = skeletonUtility.skeletonRenderer.skeleton;
  119. if (bone == null)
  120. {
  121. if (boneName == null || boneName.Length == 0)
  122. return;
  123. bone = skeleton.FindBone(boneName);
  124. if (bone == null)
  125. {
  126. Debug.LogError("Bone not found: " + boneName, this);
  127. return;
  128. }
  129. }
  130. float skeletonFlipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
  131. float flipCompensation = 0;
  132. if (flip && (flipX || (flipX != bone.flipX)) && bone.parent != null)
  133. {
  134. flipCompensation = bone.parent.WorldRotation * -2;
  135. }
  136. if (mode == Mode.Follow)
  137. {
  138. if (flip)
  139. {
  140. flipX = bone.flipX;
  141. }
  142. if (position)
  143. {
  144. cachedTransform.localPosition = new Vector3(bone.x, bone.y, 0);
  145. }
  146. if (rotation)
  147. {
  148. if (bone.Data.InheritRotation)
  149. {
  150. if (bone.FlipX)
  151. {
  152. cachedTransform.localRotation = Quaternion.Euler(0, 180, bone.rotationIK - flipCompensation);
  153. }
  154. else
  155. {
  156. cachedTransform.localRotation = Quaternion.Euler(0, 0, bone.rotationIK);
  157. }
  158. }
  159. else
  160. {
  161. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  162. cachedTransform.rotation = Quaternion.Euler(euler.x, euler.y, skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * skeletonFlipRotation));
  163. }
  164. }
  165. if (scale)
  166. {
  167. cachedTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, bone.worldFlipX ? -1 : 1);
  168. nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
  169. }
  170. }
  171. else if (mode == Mode.Override)
  172. {
  173. if (transformLerpComplete)
  174. return;
  175. if (parentReference == null)
  176. {
  177. if (position)
  178. {
  179. bone.x = Mathf.Lerp(bone.x, cachedTransform.localPosition.x, overrideAlpha);
  180. bone.y = Mathf.Lerp(bone.y, cachedTransform.localPosition.y, overrideAlpha);
  181. }
  182. if (rotation)
  183. {
  184. float angle = Mathf.LerpAngle(bone.Rotation, cachedTransform.localRotation.eulerAngles.z, overrideAlpha) + flipCompensation;
  185. if (flip)
  186. {
  187. if ((!flipX && bone.flipX))
  188. {
  189. angle -= flipCompensation;
  190. }
  191. //TODO fix this...
  192. if (angle >= 360)
  193. angle -= 360;
  194. else if (angle <= -360)
  195. angle += 360;
  196. }
  197. bone.Rotation = angle;
  198. bone.RotationIK = angle;
  199. }
  200. if (scale)
  201. {
  202. bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
  203. bone.scaleY = Mathf.Lerp(bone.scaleY, cachedTransform.localScale.y, overrideAlpha);
  204. nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
  205. }
  206. if (flip)
  207. {
  208. bone.flipX = flipX;
  209. }
  210. }
  211. else
  212. {
  213. if (transformLerpComplete)
  214. return;
  215. if (position)
  216. {
  217. Vector3 pos = parentReference.InverseTransformPoint(cachedTransform.position);
  218. bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
  219. bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha);
  220. }
  221. if (rotation)
  222. {
  223. float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(flipX ? Vector3.forward * -1 : Vector3.forward, parentReference.InverseTransformDirection(cachedTransform.up)).eulerAngles.z, overrideAlpha) + flipCompensation;
  224. if (flip)
  225. {
  226. if ((!flipX && bone.flipX))
  227. {
  228. angle -= flipCompensation;
  229. }
  230. //TODO fix this...
  231. if (angle >= 360)
  232. angle -= 360;
  233. else if (angle <= -360)
  234. angle += 360;
  235. }
  236. bone.Rotation = angle;
  237. bone.RotationIK = angle;
  238. }
  239. //TODO: Something about this
  240. if (scale)
  241. {
  242. bone.scaleX = Mathf.Lerp(bone.scaleX, cachedTransform.localScale.x, overrideAlpha);
  243. bone.scaleY = Mathf.Lerp(bone.scaleY, cachedTransform.localScale.y, overrideAlpha);
  244. nonUniformScaleWarning = (bone.scaleX != bone.scaleY);
  245. }
  246. if (flip)
  247. {
  248. bone.flipX = flipX;
  249. }
  250. }
  251. transformLerpComplete = true;
  252. }
  253. }
  254. public void FlipX(bool state)
  255. {
  256. if (state != flipX)
  257. {
  258. flipX = state;
  259. if (flipX && Mathf.Abs(transform.localRotation.eulerAngles.y) > 90)
  260. {
  261. skeletonUtility.skeletonAnimation.LateUpdate();
  262. return;
  263. }
  264. else if (!flipX && Mathf.Abs(transform.localRotation.eulerAngles.y) < 90)
  265. {
  266. skeletonUtility.skeletonAnimation.LateUpdate();
  267. return;
  268. }
  269. }
  270. bone.FlipX = state;
  271. transform.RotateAround(transform.position, skeletonUtility.transform.up, 180);
  272. Vector3 euler = transform.localRotation.eulerAngles;
  273. euler.x = 0;
  274. euler.y = bone.FlipX ? 180 : 0;
  275. transform.localRotation = Quaternion.Euler(euler);
  276. }
  277. public void AddBoundingBox(string skinName, string slotName, string attachmentName)
  278. {
  279. SkeletonUtility.AddBoundingBox(bone.skeleton, skinName, slotName, attachmentName, transform);
  280. }
  281. void OnDrawGizmos()
  282. {
  283. if (NonUniformScaleWarning)
  284. {
  285. Gizmos.DrawIcon(transform.position + new Vector3(0, 0.128f, 0), "icon-warning");
  286. }
  287. }
  288. }