SkeletonPartsRenderer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. 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. public void ClearMesh () {
  77. LazyIntialize();
  78. meshFilter.sharedMesh = null;
  79. }
  80. public void RenderParts (ExposedList<SubmeshInstruction> instructions, int startSubmesh, int endSubmesh) {
  81. LazyIntialize();
  82. // STEP 1: Create instruction
  83. var smartMesh = buffers.GetNextMesh();
  84. currentInstructions.SetWithSubset(instructions, startSubmesh, endSubmesh);
  85. bool updateTriangles = SkeletonRendererInstruction.GeometryNotEqual(currentInstructions, smartMesh.instructionUsed);
  86. // STEP 2: Generate mesh buffers.
  87. var currentInstructionsSubmeshesItems = currentInstructions.submeshInstructions.Items;
  88. meshGenerator.Begin();
  89. if (currentInstructions.hasActiveClipping) {
  90. for (int i = 0; i < currentInstructions.submeshInstructions.Count; i++)
  91. meshGenerator.AddSubmesh(currentInstructionsSubmeshesItems[i], updateTriangles);
  92. } else {
  93. meshGenerator.BuildMeshWithArrays(currentInstructions, updateTriangles);
  94. }
  95. buffers.UpdateSharedMaterials(currentInstructions.submeshInstructions);
  96. // STEP 3: modify mesh.
  97. var mesh = smartMesh.mesh;
  98. if (meshGenerator.VertexCount <= 0) { // Clear an empty mesh
  99. updateTriangles = false;
  100. mesh.Clear();
  101. } else {
  102. meshGenerator.FillVertexData(mesh);
  103. if (updateTriangles) {
  104. meshGenerator.FillTriangles(mesh);
  105. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  106. } else if (buffers.MaterialsChangedInLastUpdate()) {
  107. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  108. }
  109. meshGenerator.FillLateVertexData(mesh);
  110. }
  111. meshFilter.sharedMesh = mesh;
  112. smartMesh.instructionUsed.Set(currentInstructions);
  113. if (OnMeshAndMaterialsUpdated != null)
  114. OnMeshAndMaterialsUpdated(this);
  115. }
  116. public void SetPropertyBlock (MaterialPropertyBlock block) {
  117. LazyIntialize();
  118. meshRenderer.SetPropertyBlock(block);
  119. }
  120. public static SkeletonPartsRenderer NewPartsRendererGameObject (Transform parent, string name, int sortingOrder = 0) {
  121. var go = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer));
  122. go.transform.SetParent(parent, false);
  123. var returnComponent = go.AddComponent<SkeletonPartsRenderer>();
  124. returnComponent.MeshRenderer.sortingOrder = sortingOrder;
  125. return returnComponent;
  126. }
  127. }
  128. }