MeshGeneratorUIElements.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2024, 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. #if UNITY_2018_1_OR_NEWER
  30. #define HAS_NATIVE_COLLECTIONS
  31. #endif
  32. using System;
  33. using System.Collections.Generic;
  34. using Unity.Collections;
  35. using UnityEngine;
  36. namespace Spine.Unity {
  37. /// <summary>Holds several methods to prepare and generate a UnityEngine mesh based on a skeleton. Contains buffers needed to perform the operation, and serializes settings for mesh generation.</summary>
  38. [System.Serializable]
  39. public class MeshGeneratorUIElements : MeshGenerator {
  40. #if HAS_NATIVE_COLLECTIONS
  41. public void FillVertexData (ref NativeSlice<UnityEngine.UIElements.Vertex> uiVertices) {
  42. var vertexBufferItems = vertexBuffer.Items;
  43. var uvBufferItems = uvBuffer.Items;
  44. var colorBufferItems = colorBuffer.Items;
  45. int vertexBufferLength = vertexBufferItems.Length;
  46. int vertexCount = vertexBuffer.Count;
  47. // Zero the extra.
  48. {
  49. var vector3zero = Vector3.zero;
  50. for (int i = vertexCount; i < vertexBufferLength; i++)
  51. vertexBufferItems[i] = vector3zero;
  52. }
  53. // Set the vertex buffer.
  54. {
  55. for (int i = 0; i < vertexCount; i++) {
  56. UnityEngine.UIElements.Vertex uiVertex = uiVertices[i];
  57. uiVertex.position = vertexBufferItems[i];
  58. uiVertex.tint = colorBufferItems[i];
  59. uiVertex.uv = uvBufferItems[i];
  60. uiVertices[i] = uiVertex;
  61. }
  62. }
  63. }
  64. public void FillTrianglesSingleSubmesh (ref NativeSlice<ushort> uiIndices, int submeshIndex = 0) {
  65. if (submeshes.Count == 0)
  66. return;
  67. var srcIndices32List = submeshes.Items[submeshIndex];
  68. int[] srcIndices32 = srcIndices32List.Items;
  69. int srcIndicesCount = srcIndices32List.Count;
  70. int dstIndicesCount = uiIndices.Length;
  71. for (int i = 0; i < srcIndicesCount; ++i)
  72. uiIndices[i] = (ushort)srcIndices32[i];
  73. for (int i = srcIndicesCount; i < dstIndicesCount; ++i)
  74. uiIndices[i] = 0;
  75. }
  76. #endif // HAS_NATIVE_COLLECTIONS
  77. }
  78. }