MeshRendererBuffers.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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>A double-buffered Mesh, and a shared material array, bundled for use by Spine components that need to push a Mesh and materials to a Unity MeshRenderer and MeshFilter.</summary>
  37. public class MeshRendererBuffers : IDisposable {
  38. DoubleBuffered<SmartMesh> doubleBufferedMesh;
  39. internal readonly ExposedList<Material> submeshMaterials = new ExposedList<Material>();
  40. internal Material[] sharedMaterials = new Material[0];
  41. public void Initialize () {
  42. if (doubleBufferedMesh != null) {
  43. doubleBufferedMesh.GetNext().Clear();
  44. doubleBufferedMesh.GetNext().Clear();
  45. submeshMaterials.Clear();
  46. } else {
  47. doubleBufferedMesh = new DoubleBuffered<SmartMesh>();
  48. }
  49. }
  50. /// <summary>Returns a sharedMaterials array for use on a MeshRenderer.</summary>
  51. /// <returns></returns>
  52. public Material[] GetUpdatedSharedMaterialsArray () {
  53. if (submeshMaterials.Count == sharedMaterials.Length)
  54. submeshMaterials.CopyTo(sharedMaterials);
  55. else
  56. sharedMaterials = submeshMaterials.ToArray();
  57. return sharedMaterials;
  58. }
  59. /// <summary>Returns true if the materials were modified since the buffers were last updated.</summary>
  60. public bool MaterialsChangedInLastUpdate () {
  61. int newSubmeshMaterials = submeshMaterials.Count;
  62. var sharedMaterials = this.sharedMaterials;
  63. if (newSubmeshMaterials != sharedMaterials.Length) return true;
  64. var submeshMaterialsItems = submeshMaterials.Items;
  65. for (int i = 0; i < newSubmeshMaterials; i++)
  66. if (!Material.ReferenceEquals(submeshMaterialsItems[i], sharedMaterials[i])) return true; //if (submeshMaterialsItems[i].GetInstanceID() != sharedMaterials[i].GetInstanceID()) return true;
  67. return false;
  68. }
  69. /// <summary>Updates the internal shared materials array with the given instruction list.</summary>
  70. public void UpdateSharedMaterials (ExposedList<SubmeshInstruction> instructions) {
  71. int newSize = instructions.Count;
  72. { //submeshMaterials.Resize(instructions.Count);
  73. if (newSize > submeshMaterials.Items.Length)
  74. Array.Resize(ref submeshMaterials.Items, newSize);
  75. submeshMaterials.Count = newSize;
  76. }
  77. var submeshMaterialsItems = submeshMaterials.Items;
  78. var instructionsItems = instructions.Items;
  79. for (int i = 0; i < newSize; i++)
  80. submeshMaterialsItems[i] = instructionsItems[i].material;
  81. }
  82. public SmartMesh GetNextMesh () {
  83. return doubleBufferedMesh.GetNext();
  84. }
  85. public void Clear () {
  86. sharedMaterials = new Material[0];
  87. submeshMaterials.Clear();
  88. }
  89. public void Dispose () {
  90. if (doubleBufferedMesh == null) return;
  91. doubleBufferedMesh.GetNext().Dispose();
  92. doubleBufferedMesh.GetNext().Dispose();
  93. doubleBufferedMesh = null;
  94. }
  95. ///<summary>This is a Mesh that also stores the instructions SkeletonRenderer generated for it.</summary>
  96. public class SmartMesh : IDisposable {
  97. public Mesh mesh = SpineMesh.NewSkeletonMesh();
  98. public SkeletonRendererInstruction instructionUsed = new SkeletonRendererInstruction();
  99. public void Clear () {
  100. mesh.Clear();
  101. instructionUsed.Clear();
  102. }
  103. public void Dispose () {
  104. if (mesh != null) {
  105. #if UNITY_EDITOR
  106. if (Application.isEditor && !Application.isPlaying)
  107. UnityEngine.Object.DestroyImmediate(mesh);
  108. else
  109. UnityEngine.Object.Destroy(mesh);
  110. #else
  111. UnityEngine.Object.Destroy(mesh);
  112. #endif
  113. }
  114. mesh = null;
  115. }
  116. }
  117. }
  118. }