SkeletonRendererInstruction.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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
  13. * or 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
  27. * THE 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. using UnityEngine;
  33. using System;
  34. using System.Collections.Generic;
  35. namespace Spine.Unity {
  36. /// <summary>Instructions used by a SkeletonRenderer to render a mesh.</summary>
  37. public class SkeletonRendererInstruction {
  38. public readonly ExposedList<SubmeshInstruction> submeshInstructions = new ExposedList<SubmeshInstruction>();
  39. public bool immutableTriangles;
  40. #if SPINE_TRIANGLECHECK
  41. public bool hasActiveClipping;
  42. public int rawVertexCount = -1;
  43. public readonly ExposedList<Attachment> attachments = new ExposedList<Attachment>();
  44. #endif
  45. public void Clear () {
  46. #if SPINE_TRIANGLECHECK
  47. this.attachments.Clear(false);
  48. rawVertexCount = -1;
  49. hasActiveClipping = false;
  50. #endif
  51. this.submeshInstructions.Clear(false);
  52. }
  53. public void Dispose () {
  54. attachments.Clear(true);
  55. }
  56. public void SetWithSubset (ExposedList<SubmeshInstruction> instructions, int startSubmesh, int endSubmesh) {
  57. #if SPINE_TRIANGLECHECK
  58. int runningVertexCount = 0;
  59. #endif
  60. var submeshes = this.submeshInstructions;
  61. submeshes.Clear(false);
  62. int submeshCount = endSubmesh - startSubmesh;
  63. submeshes.Resize(submeshCount);
  64. var submeshesItems = submeshes.Items;
  65. var instructionsItems = instructions.Items;
  66. for (int i = 0; i < submeshCount; i++) {
  67. var instruction = instructionsItems[startSubmesh + i];
  68. submeshesItems[i] = instruction;
  69. #if SPINE_TRIANGLECHECK
  70. this.hasActiveClipping |= instruction.hasClipping;
  71. submeshesItems[i].rawFirstVertexIndex = runningVertexCount; // Ensure current instructions have correct cached values.
  72. runningVertexCount += instruction.rawVertexCount; // vertexCount will also be used for the rest of this method.
  73. #endif
  74. }
  75. #if SPINE_TRIANGLECHECK
  76. this.rawVertexCount = runningVertexCount;
  77. // assumption: instructions are contiguous. start and end are valid within instructions.
  78. int startSlot = instructionsItems[startSubmesh].startSlot;
  79. int endSlot = instructionsItems[endSubmesh - 1].endSlot;
  80. attachments.Clear(false);
  81. int attachmentCount = endSlot - startSlot;
  82. attachments.Resize(attachmentCount);
  83. var attachmentsItems = attachments.Items;
  84. var drawOrderItems = instructionsItems[0].skeleton.drawOrder.Items;
  85. for (int i = 0; i < attachmentCount; i++) {
  86. Slot slot = drawOrderItems[startSlot + i];
  87. if (!slot.bone.active) continue;
  88. attachmentsItems[i] = slot.attachment;
  89. }
  90. #endif
  91. }
  92. public void Set (SkeletonRendererInstruction other) {
  93. this.immutableTriangles = other.immutableTriangles;
  94. #if SPINE_TRIANGLECHECK
  95. this.hasActiveClipping = other.hasActiveClipping;
  96. this.rawVertexCount = other.rawVertexCount;
  97. this.attachments.Clear(false);
  98. this.attachments.EnsureCapacity(other.attachments.Capacity);
  99. this.attachments.Count = other.attachments.Count;
  100. other.attachments.CopyTo(this.attachments.Items);
  101. #endif
  102. this.submeshInstructions.Clear(false);
  103. this.submeshInstructions.EnsureCapacity(other.submeshInstructions.Capacity);
  104. this.submeshInstructions.Count = other.submeshInstructions.Count;
  105. other.submeshInstructions.CopyTo(this.submeshInstructions.Items);
  106. }
  107. public static bool GeometryNotEqual (SkeletonRendererInstruction a, SkeletonRendererInstruction b) {
  108. #if SPINE_TRIANGLECHECK
  109. #if UNITY_EDITOR
  110. if (!Application.isPlaying)
  111. return true;
  112. #endif
  113. if (a.hasActiveClipping || b.hasActiveClipping) return true; // Triangles are unpredictable when clipping is active.
  114. // Everything below assumes the raw vertex and triangle counts were used. (ie, no clipping was done)
  115. if (a.rawVertexCount != b.rawVertexCount) return true;
  116. if (a.immutableTriangles != b.immutableTriangles) return true;
  117. int attachmentCountB = b.attachments.Count;
  118. if (a.attachments.Count != attachmentCountB) return true; // Bounds check for the looped storedAttachments count below.
  119. // Submesh count changed
  120. int submeshCountA = a.submeshInstructions.Count;
  121. int submeshCountB = b.submeshInstructions.Count;
  122. if (submeshCountA != submeshCountB) return true;
  123. // Submesh Instruction mismatch
  124. var submeshInstructionsItemsA = a.submeshInstructions.Items;
  125. var submeshInstructionsItemsB = b.submeshInstructions.Items;
  126. var attachmentsA = a.attachments.Items;
  127. var attachmentsB = b.attachments.Items;
  128. for (int i = 0; i < attachmentCountB; i++)
  129. if (!System.Object.ReferenceEquals(attachmentsA[i], attachmentsB[i])) return true;
  130. for (int i = 0; i < submeshCountB; i++) {
  131. var submeshA = submeshInstructionsItemsA[i];
  132. var submeshB = submeshInstructionsItemsB[i];
  133. if (!(
  134. submeshA.rawVertexCount == submeshB.rawVertexCount &&
  135. submeshA.startSlot == submeshB.startSlot &&
  136. submeshA.endSlot == submeshB.endSlot
  137. && submeshA.rawTriangleCount == submeshB.rawTriangleCount &&
  138. submeshA.rawFirstVertexIndex == submeshB.rawFirstVertexIndex
  139. ))
  140. return true;
  141. }
  142. return false;
  143. #else
  144. // 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..
  145. if (a.immutableTriangles || b.immutableTriangles)
  146. return (a.immutableTriangles != b.immutableTriangles);
  147. return true;
  148. #endif
  149. }
  150. }
  151. }