SkeletonPartsRenderer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. using UnityEngine;
  30. namespace Spine.Unity {
  31. [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
  32. [HelpURL("http://esotericsoftware.com/spine-unity#SkeletonRenderSeparator")]
  33. public class SkeletonPartsRenderer : MonoBehaviour {
  34. #region Properties
  35. MeshGenerator meshGenerator;
  36. public MeshGenerator MeshGenerator {
  37. get {
  38. LazyIntialize();
  39. return meshGenerator;
  40. }
  41. }
  42. MeshRenderer meshRenderer;
  43. public MeshRenderer MeshRenderer {
  44. get {
  45. LazyIntialize();
  46. return meshRenderer;
  47. }
  48. }
  49. MeshFilter meshFilter;
  50. public MeshFilter MeshFilter {
  51. get {
  52. LazyIntialize();
  53. return meshFilter;
  54. }
  55. }
  56. #endregion
  57. #region Callback Delegates
  58. public delegate void SkeletonPartsRendererDelegate (SkeletonPartsRenderer skeletonPartsRenderer);
  59. /// <summary>OnMeshAndMaterialsUpdated is called at the end of LateUpdate after the Mesh and
  60. /// all materials have been updated.</summary>
  61. public event SkeletonPartsRendererDelegate OnMeshAndMaterialsUpdated;
  62. #endregion
  63. MeshRendererBuffers buffers;
  64. SkeletonRendererInstruction currentInstructions = new SkeletonRendererInstruction();
  65. void LazyIntialize () {
  66. if (buffers == null) {
  67. buffers = new MeshRendererBuffers();
  68. buffers.Initialize();
  69. if (meshGenerator != null) return;
  70. meshGenerator = new MeshGenerator();
  71. meshFilter = GetComponent<MeshFilter>();
  72. meshRenderer = GetComponent<MeshRenderer>();
  73. currentInstructions.Clear();
  74. }
  75. }
  76. void OnDestroy () {
  77. if (buffers != null) buffers.Dispose();
  78. }
  79. public void ClearMesh () {
  80. LazyIntialize();
  81. meshFilter.sharedMesh = null;
  82. }
  83. public void RenderParts (ExposedList<SubmeshInstruction> instructions, int startSubmesh, int endSubmesh) {
  84. LazyIntialize();
  85. // STEP 1: Create instruction
  86. MeshRendererBuffers.SmartMesh smartMesh = buffers.GetNextMesh();
  87. currentInstructions.SetWithSubset(instructions, startSubmesh, endSubmesh);
  88. bool updateTriangles = SkeletonRendererInstruction.GeometryNotEqual(currentInstructions, smartMesh.instructionUsed);
  89. // STEP 2: Generate mesh buffers.
  90. SubmeshInstruction[] currentInstructionsSubmeshesItems = currentInstructions.submeshInstructions.Items;
  91. meshGenerator.Begin();
  92. if (currentInstructions.hasActiveClipping) {
  93. for (int i = 0; i < currentInstructions.submeshInstructions.Count; i++)
  94. meshGenerator.AddSubmesh(currentInstructionsSubmeshesItems[i], updateTriangles);
  95. } else {
  96. meshGenerator.BuildMeshWithArrays(currentInstructions, updateTriangles);
  97. }
  98. buffers.UpdateSharedMaterials(currentInstructions.submeshInstructions);
  99. // STEP 3: modify mesh.
  100. Mesh mesh = smartMesh.mesh;
  101. if (meshGenerator.VertexCount <= 0) { // Clear an empty mesh
  102. updateTriangles = false;
  103. mesh.Clear();
  104. } else {
  105. meshGenerator.FillVertexData(mesh);
  106. if (updateTriangles) {
  107. meshGenerator.FillTriangles(mesh);
  108. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  109. } else if (buffers.MaterialsChangedInLastUpdate()) {
  110. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  111. }
  112. meshGenerator.FillLateVertexData(mesh);
  113. }
  114. meshFilter.sharedMesh = mesh;
  115. smartMesh.instructionUsed.Set(currentInstructions);
  116. if (OnMeshAndMaterialsUpdated != null)
  117. OnMeshAndMaterialsUpdated(this);
  118. }
  119. public void SetPropertyBlock (MaterialPropertyBlock block) {
  120. LazyIntialize();
  121. meshRenderer.SetPropertyBlock(block);
  122. }
  123. public static SkeletonPartsRenderer NewPartsRendererGameObject (Transform parent, string name, int sortingOrder = 0) {
  124. GameObject go = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer));
  125. go.transform.SetParent(parent, false);
  126. SkeletonPartsRenderer returnComponent = go.AddComponent<SkeletonPartsRenderer>();
  127. returnComponent.MeshRenderer.sortingOrder = sortingOrder;
  128. return returnComponent;
  129. }
  130. }
  131. }