源战役客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

235 rindas
7.9 KiB

pirms 1 mēnesi
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * 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 SOFTWARE 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, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. // Contributed by: Mitch Thompson
  31. using UnityEngine;
  32. using Spine;
  33. namespace Spine.Unity {
  34. /// <summary>Sets a GameObject's transform to match a bone on a Spine skeleton.</summary>
  35. [ExecuteInEditMode]
  36. [AddComponentMenu("Spine/SkeletonUtilityBone")]
  37. public class SkeletonUtilityBone : MonoBehaviour {
  38. public enum Mode {
  39. Follow,
  40. Override
  41. }
  42. public enum UpdatePhase {
  43. Local,
  44. World,
  45. Complete
  46. }
  47. #region Inspector
  48. /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
  49. public string boneName;
  50. public Transform parentReference;
  51. public Mode mode;
  52. public bool position, rotation, scale, zPosition = true;
  53. [Range(0f, 1f)]
  54. public float overrideAlpha = 1;
  55. #endregion
  56. [System.NonSerialized] public SkeletonUtility skeletonUtility;
  57. [System.NonSerialized] public Bone bone;
  58. [System.NonSerialized] public bool transformLerpComplete;
  59. [System.NonSerialized] public bool valid;
  60. Transform cachedTransform;
  61. Transform skeletonTransform;
  62. bool incompatibleTransformMode;
  63. public bool IncompatibleTransformMode { get { return incompatibleTransformMode; } }
  64. public void Reset () {
  65. bone = null;
  66. cachedTransform = transform;
  67. valid = skeletonUtility != null && skeletonUtility.skeletonRenderer != null && skeletonUtility.skeletonRenderer.valid;
  68. if (!valid)
  69. return;
  70. skeletonTransform = skeletonUtility.transform;
  71. skeletonUtility.OnReset -= HandleOnReset;
  72. skeletonUtility.OnReset += HandleOnReset;
  73. DoUpdate(UpdatePhase.Local);
  74. }
  75. void OnEnable () {
  76. skeletonUtility = transform.GetComponentInParent<SkeletonUtility>();
  77. if (skeletonUtility == null)
  78. return;
  79. skeletonUtility.RegisterBone(this);
  80. skeletonUtility.OnReset += HandleOnReset;
  81. }
  82. void HandleOnReset () {
  83. Reset();
  84. }
  85. void OnDisable () {
  86. if (skeletonUtility != null) {
  87. skeletonUtility.OnReset -= HandleOnReset;
  88. skeletonUtility.UnregisterBone(this);
  89. }
  90. }
  91. public void DoUpdate (UpdatePhase phase) {
  92. if (!valid) {
  93. Reset();
  94. return;
  95. }
  96. var skeleton = skeletonUtility.skeletonRenderer.skeleton;
  97. if (bone == null) {
  98. if (string.IsNullOrEmpty(boneName)) return;
  99. bone = skeleton.FindBone(boneName);
  100. if (bone == null) {
  101. Debug.LogError("Bone not found: " + boneName, this);
  102. return;
  103. }
  104. }
  105. var thisTransform = cachedTransform;
  106. float skeletonFlipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
  107. if (mode == Mode.Follow) {
  108. switch (phase) {
  109. case UpdatePhase.Local:
  110. if (position)
  111. thisTransform.localPosition = new Vector3(bone.x, bone.y, 0);
  112. if (rotation) {
  113. if (bone.data.transformMode.InheritsRotation()) {
  114. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.rotation);
  115. } else {
  116. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  117. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  118. }
  119. }
  120. if (scale) {
  121. thisTransform.localScale = new Vector3(bone.scaleX, bone.scaleY, 1f);
  122. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  123. }
  124. break;
  125. case UpdatePhase.World:
  126. case UpdatePhase.Complete:
  127. // Use Applied transform values (ax, ay, AppliedRotation, ascale) if world values were modified by constraints.
  128. if (!bone.appliedValid)
  129. bone.UpdateAppliedTransform();
  130. if (position)
  131. thisTransform.localPosition = new Vector3(bone.ax, bone.ay, 0);
  132. if (rotation) {
  133. if (bone.data.transformMode.InheritsRotation()) {
  134. thisTransform.localRotation = Quaternion.Euler(0, 0, bone.AppliedRotation);
  135. } else {
  136. Vector3 euler = skeletonTransform.rotation.eulerAngles;
  137. thisTransform.rotation = Quaternion.Euler(euler.x, euler.y, euler.z + (bone.WorldRotationX * skeletonFlipRotation));
  138. }
  139. }
  140. if (scale) {
  141. thisTransform.localScale = new Vector3(bone.ascaleX, bone.ascaleY, 1f);
  142. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  143. }
  144. break;
  145. }
  146. } else if (mode == Mode.Override) {
  147. if (transformLerpComplete)
  148. return;
  149. if (parentReference == null) {
  150. if (position) {
  151. Vector3 clp = thisTransform.localPosition;
  152. bone.x = Mathf.Lerp(bone.x, clp.x, overrideAlpha);
  153. bone.y = Mathf.Lerp(bone.y, clp.y, overrideAlpha);
  154. }
  155. if (rotation) {
  156. float angle = Mathf.LerpAngle(bone.Rotation, thisTransform.localRotation.eulerAngles.z, overrideAlpha);
  157. bone.Rotation = angle;
  158. bone.AppliedRotation = angle;
  159. }
  160. if (scale) {
  161. Vector3 cls = thisTransform.localScale;
  162. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  163. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  164. }
  165. } else {
  166. if (transformLerpComplete)
  167. return;
  168. if (position) {
  169. Vector3 pos = parentReference.InverseTransformPoint(thisTransform.position);
  170. bone.x = Mathf.Lerp(bone.x, pos.x, overrideAlpha);
  171. bone.y = Mathf.Lerp(bone.y, pos.y, overrideAlpha);
  172. }
  173. if (rotation) {
  174. float angle = Mathf.LerpAngle(bone.Rotation, Quaternion.LookRotation(Vector3.forward, parentReference.InverseTransformDirection(thisTransform.up)).eulerAngles.z, overrideAlpha);
  175. bone.Rotation = angle;
  176. bone.AppliedRotation = angle;
  177. }
  178. if (scale) {
  179. Vector3 cls = thisTransform.localScale;
  180. bone.scaleX = Mathf.Lerp(bone.scaleX, cls.x, overrideAlpha);
  181. bone.scaleY = Mathf.Lerp(bone.scaleY, cls.y, overrideAlpha);
  182. }
  183. incompatibleTransformMode = BoneTransformModeIncompatible(bone);
  184. }
  185. transformLerpComplete = true;
  186. }
  187. }
  188. public static bool BoneTransformModeIncompatible (Bone bone) {
  189. return !bone.data.transformMode.InheritsScale();
  190. }
  191. public void AddBoundingBox (string skinName, string slotName, string attachmentName) {
  192. SkeletonUtility.AddBoundingBoxGameObject(bone.skeleton, skinName, slotName, attachmentName, transform);
  193. }
  194. #if UNITY_EDITOR
  195. void OnDrawGizmos () {
  196. if (IncompatibleTransformMode)
  197. Gizmos.DrawIcon(transform.position + new Vector3(0, 0.128f, 0), "icon-warning");
  198. }
  199. #endif
  200. }
  201. }