SkeletonRendererInstruction.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. // Not for optimization. Do not disable.
  30. #define SPINE_TRIANGLECHECK // Avoid calling SetTriangles at the cost of checking for mesh differences (vertex counts, memberwise attachment list compare) every frame.
  31. //#define SPINE_DEBUG
  32. // Important Note: When disabling this define, also disable the one in MeshGenerator.cs
  33. // For details, see MeshGenerator.cs.
  34. #define SLOT_ALPHA_DISABLES_ATTACHMENT
  35. using System;
  36. using System.Collections.Generic;
  37. using UnityEngine;
  38. namespace Spine.Unity {
  39. /// <summary>Instructions used by a SkeletonRenderer to render a mesh.</summary>
  40. public class SkeletonRendererInstruction {
  41. public readonly ExposedList<SubmeshInstruction> submeshInstructions = new ExposedList<SubmeshInstruction>();
  42. public bool immutableTriangles;
  43. #if SPINE_TRIANGLECHECK
  44. public bool hasActiveClipping;
  45. public int rawVertexCount = -1;
  46. public readonly ExposedList<Attachment> attachments = new ExposedList<Attachment>();
  47. #endif
  48. public void Clear () {
  49. #if SPINE_TRIANGLECHECK
  50. this.attachments.Clear(false);
  51. rawVertexCount = -1;
  52. hasActiveClipping = false;
  53. #endif
  54. this.submeshInstructions.Clear(false);
  55. }
  56. public void Dispose () {
  57. attachments.Clear(true);
  58. }
  59. public void SetWithSubset (ExposedList<SubmeshInstruction> instructions, int startSubmesh, int endSubmesh) {
  60. #if SPINE_TRIANGLECHECK
  61. int runningVertexCount = 0;
  62. #endif
  63. ExposedList<SubmeshInstruction> submeshes = this.submeshInstructions;
  64. submeshes.Clear(false);
  65. int submeshCount = endSubmesh - startSubmesh;
  66. submeshes.Resize(submeshCount);
  67. SubmeshInstruction[] submeshesItems = submeshes.Items;
  68. SubmeshInstruction[] instructionsItems = instructions.Items;
  69. for (int i = 0; i < submeshCount; i++) {
  70. SubmeshInstruction instruction = instructionsItems[startSubmesh + i];
  71. submeshesItems[i] = instruction;
  72. #if SPINE_TRIANGLECHECK
  73. this.hasActiveClipping |= instruction.hasClipping;
  74. submeshesItems[i].rawFirstVertexIndex = runningVertexCount; // Ensure current instructions have correct cached values.
  75. runningVertexCount += instruction.rawVertexCount; // vertexCount will also be used for the rest of this method.
  76. #endif
  77. }
  78. #if SPINE_TRIANGLECHECK
  79. this.rawVertexCount = runningVertexCount;
  80. // assumption: instructions are contiguous. start and end are valid within instructions.
  81. int startSlot = instructionsItems[startSubmesh].startSlot;
  82. int endSlot = instructionsItems[endSubmesh - 1].endSlot;
  83. attachments.Clear(false);
  84. int attachmentCount = endSlot - startSlot;
  85. attachments.Resize(attachmentCount);
  86. Attachment[] attachmentsItems = attachments.Items;
  87. Slot[] drawOrderItems = instructionsItems[0].skeleton.DrawOrder.Items;
  88. for (int i = 0; i < attachmentCount; i++) {
  89. Slot slot = drawOrderItems[startSlot + i];
  90. if (!slot.Bone.Active
  91. #if SLOT_ALPHA_DISABLES_ATTACHMENT
  92. || slot.A == 0f
  93. #endif
  94. ) {
  95. attachmentsItems[i] = null;
  96. continue;
  97. }
  98. attachmentsItems[i] = slot.Attachment;
  99. }
  100. #endif
  101. }
  102. public void Set (SkeletonRendererInstruction other) {
  103. this.immutableTriangles = other.immutableTriangles;
  104. #if SPINE_TRIANGLECHECK
  105. this.hasActiveClipping = other.hasActiveClipping;
  106. this.rawVertexCount = other.rawVertexCount;
  107. this.attachments.Clear(false);
  108. this.attachments.EnsureCapacity(other.attachments.Capacity);
  109. this.attachments.Count = other.attachments.Count;
  110. other.attachments.CopyTo(this.attachments.Items);
  111. #endif
  112. this.submeshInstructions.Clear(false);
  113. this.submeshInstructions.EnsureCapacity(other.submeshInstructions.Capacity);
  114. this.submeshInstructions.Count = other.submeshInstructions.Count;
  115. other.submeshInstructions.CopyTo(this.submeshInstructions.Items);
  116. }
  117. public static bool GeometryNotEqual (SkeletonRendererInstruction a, SkeletonRendererInstruction b) {
  118. #if SPINE_TRIANGLECHECK
  119. #if UNITY_EDITOR
  120. if (!Application.isPlaying)
  121. return true;
  122. #endif
  123. if (a.hasActiveClipping || b.hasActiveClipping) return true; // Triangles are unpredictable when clipping is active.
  124. // Everything below assumes the raw vertex and triangle counts were used. (ie, no clipping was done)
  125. if (a.rawVertexCount != b.rawVertexCount) return true;
  126. if (a.immutableTriangles != b.immutableTriangles) return true;
  127. int attachmentCountB = b.attachments.Count;
  128. if (a.attachments.Count != attachmentCountB) return true; // Bounds check for the looped storedAttachments count below.
  129. // Submesh count changed
  130. int submeshCountA = a.submeshInstructions.Count;
  131. int submeshCountB = b.submeshInstructions.Count;
  132. if (submeshCountA != submeshCountB) return true;
  133. // Submesh Instruction mismatch
  134. SubmeshInstruction[] submeshInstructionsItemsA = a.submeshInstructions.Items;
  135. SubmeshInstruction[] submeshInstructionsItemsB = b.submeshInstructions.Items;
  136. Attachment[] attachmentsA = a.attachments.Items;
  137. Attachment[] attachmentsB = b.attachments.Items;
  138. for (int i = 0; i < attachmentCountB; i++)
  139. if (!System.Object.ReferenceEquals(attachmentsA[i], attachmentsB[i])) return true;
  140. for (int i = 0; i < submeshCountB; i++) {
  141. SubmeshInstruction submeshA = submeshInstructionsItemsA[i];
  142. SubmeshInstruction submeshB = submeshInstructionsItemsB[i];
  143. if (!(
  144. submeshA.rawVertexCount == submeshB.rawVertexCount &&
  145. submeshA.startSlot == submeshB.startSlot &&
  146. submeshA.endSlot == submeshB.endSlot
  147. && submeshA.rawTriangleCount == submeshB.rawTriangleCount &&
  148. submeshA.rawFirstVertexIndex == submeshB.rawFirstVertexIndex
  149. ))
  150. return true;
  151. }
  152. return false;
  153. #else
  154. // In normal immutable triangle use, immutableTriangles will be initially false, forcing the smartmesh to update the first time but never again after that, unless there was an immutableTriangles flag mismatch..
  155. if (a.immutableTriangles || b.immutableTriangles)
  156. return (a.immutableTriangles != b.immutableTriangles);
  157. return true;
  158. #endif
  159. }
  160. }
  161. }