SkeletonPartsRenderer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public class SkeletonPartsRenderer : MonoBehaviour {
  33. #region Properties
  34. MeshGenerator meshGenerator;
  35. public MeshGenerator MeshGenerator {
  36. get {
  37. LazyIntialize();
  38. return meshGenerator;
  39. }
  40. }
  41. MeshRenderer meshRenderer;
  42. public MeshRenderer MeshRenderer {
  43. get {
  44. LazyIntialize();
  45. return meshRenderer;
  46. }
  47. }
  48. MeshFilter meshFilter;
  49. public MeshFilter MeshFilter {
  50. get {
  51. LazyIntialize();
  52. return meshFilter;
  53. }
  54. }
  55. #endregion
  56. MeshRendererBuffers buffers;
  57. SkeletonRendererInstruction currentInstructions = new SkeletonRendererInstruction();
  58. void LazyIntialize () {
  59. if (buffers == null) {
  60. buffers = new MeshRendererBuffers();
  61. buffers.Initialize();
  62. if (meshGenerator != null) return;
  63. meshGenerator = new MeshGenerator();
  64. meshFilter = GetComponent<MeshFilter>();
  65. meshRenderer = GetComponent<MeshRenderer>();
  66. currentInstructions.Clear();
  67. }
  68. }
  69. public void ClearMesh () {
  70. LazyIntialize();
  71. meshFilter.sharedMesh = null;
  72. }
  73. public void RenderParts (ExposedList<SubmeshInstruction> instructions, int startSubmesh, int endSubmesh) {
  74. LazyIntialize();
  75. // STEP 1: Create instruction
  76. var smartMesh = buffers.GetNextMesh();
  77. currentInstructions.SetWithSubset(instructions, startSubmesh, endSubmesh);
  78. bool updateTriangles = SkeletonRendererInstruction.GeometryNotEqual(currentInstructions, smartMesh.instructionUsed);
  79. // STEP 2: Generate mesh buffers.
  80. var currentInstructionsSubmeshesItems = currentInstructions.submeshInstructions.Items;
  81. meshGenerator.Begin();
  82. if (currentInstructions.hasActiveClipping) {
  83. for (int i = 0; i < currentInstructions.submeshInstructions.Count; i++)
  84. meshGenerator.AddSubmesh(currentInstructionsSubmeshesItems[i], updateTriangles);
  85. } else {
  86. meshGenerator.BuildMeshWithArrays(currentInstructions, updateTriangles);
  87. }
  88. buffers.UpdateSharedMaterials(currentInstructions.submeshInstructions);
  89. // STEP 3: modify mesh.
  90. var mesh = smartMesh.mesh;
  91. if (meshGenerator.VertexCount <= 0) { // Clear an empty mesh
  92. updateTriangles = false;
  93. mesh.Clear();
  94. } else {
  95. meshGenerator.FillVertexData(mesh);
  96. if (updateTriangles) {
  97. meshGenerator.FillTriangles(mesh);
  98. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  99. } else if (buffers.MaterialsChangedInLastUpdate()) {
  100. meshRenderer.sharedMaterials = buffers.GetUpdatedSharedMaterialsArray();
  101. }
  102. meshGenerator.FillLateVertexData(mesh);
  103. }
  104. meshFilter.sharedMesh = mesh;
  105. smartMesh.instructionUsed.Set(currentInstructions);
  106. }
  107. public void SetPropertyBlock (MaterialPropertyBlock block) {
  108. LazyIntialize();
  109. meshRenderer.SetPropertyBlock(block);
  110. }
  111. public static SkeletonPartsRenderer NewPartsRendererGameObject (Transform parent, string name, int sortingOrder = 0) {
  112. var go = new GameObject(name, typeof(MeshFilter), typeof(MeshRenderer));
  113. go.transform.SetParent(parent, false);
  114. var returnComponent = go.AddComponent<SkeletonPartsRenderer>();
  115. returnComponent.MeshRenderer.sortingOrder = sortingOrder;
  116. return returnComponent;
  117. }
  118. }
  119. }