SkeletonRenderTextureBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated September 24, 2021. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2022, 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. #if UNITY_2017_2_OR_NEWER
  30. #define HAS_VECTOR2INT
  31. #endif
  32. using System;
  33. using UnityEngine;
  34. using UnityEngine.Rendering;
  35. namespace Spine.Unity.Examples {
  36. public abstract class SkeletonRenderTextureBase : MonoBehaviour {
  37. #if HAS_VECTOR2INT
  38. public Color color = Color.white;
  39. public int maxRenderTextureSize = 1024;
  40. public GameObject quad;
  41. protected Mesh quadMesh;
  42. public RenderTexture renderTexture;
  43. public Camera targetCamera;
  44. protected CommandBuffer commandBuffer;
  45. protected Vector2Int requiredRenderTextureSize;
  46. protected Vector2Int allocatedRenderTextureSize;
  47. protected Vector3 worldCornerNoDistortion0;
  48. protected Vector3 worldCornerNoDistortion1;
  49. protected Vector3 worldCornerNoDistortion2;
  50. protected Vector3 worldCornerNoDistortion3;
  51. protected Vector2 uvCorner0;
  52. protected Vector2 uvCorner1;
  53. protected Vector2 uvCorner2;
  54. protected Vector2 uvCorner3;
  55. protected virtual void Awake () {
  56. commandBuffer = new CommandBuffer();
  57. }
  58. void OnDestroy () {
  59. if (renderTexture)
  60. RenderTexture.ReleaseTemporary(renderTexture);
  61. }
  62. protected void PrepareTextureMapping (out Vector3 screenSpaceMin, out Vector3 screenSpaceMax,
  63. Vector3 screenCorner0, Vector3 screenCorner1, Vector3 screenCorner2, Vector3 screenCorner3) {
  64. screenSpaceMin =
  65. Vector3.Min(screenCorner0, Vector3.Min(screenCorner1,
  66. Vector3.Min(screenCorner2, screenCorner3)));
  67. screenSpaceMax =
  68. Vector3.Max(screenCorner0, Vector3.Max(screenCorner1,
  69. Vector3.Max(screenCorner2, screenCorner3)));
  70. // ensure we are on whole pixel borders
  71. screenSpaceMin.x = Mathf.Floor(screenSpaceMin.x);
  72. screenSpaceMin.y = Mathf.Floor(screenSpaceMin.y);
  73. screenSpaceMax.x = Mathf.Ceil(screenSpaceMax.x);
  74. screenSpaceMax.y = Mathf.Ceil(screenSpaceMax.y);
  75. // inverse-map screenCornerN to screenSpaceMin/screenSpaceMax area to get UV coordinates
  76. uvCorner0 = MathUtilities.InverseLerp(screenSpaceMin, screenSpaceMax, screenCorner0);
  77. uvCorner1 = MathUtilities.InverseLerp(screenSpaceMin, screenSpaceMax, screenCorner1);
  78. uvCorner2 = MathUtilities.InverseLerp(screenSpaceMin, screenSpaceMax, screenCorner2);
  79. uvCorner3 = MathUtilities.InverseLerp(screenSpaceMin, screenSpaceMax, screenCorner3);
  80. requiredRenderTextureSize = new Vector2Int(
  81. Math.Min(maxRenderTextureSize, Math.Abs((int)screenSpaceMax.x - (int)screenSpaceMin.x)),
  82. Math.Min(maxRenderTextureSize, Math.Abs((int)screenSpaceMax.y - (int)screenSpaceMin.y)));
  83. PrepareRenderTexture();
  84. }
  85. protected void PrepareRenderTexture () {
  86. Vector2Int textureSize = new Vector2Int(
  87. Mathf.NextPowerOfTwo(requiredRenderTextureSize.x),
  88. Mathf.NextPowerOfTwo(requiredRenderTextureSize.y));
  89. if (textureSize != allocatedRenderTextureSize) {
  90. if (renderTexture)
  91. RenderTexture.ReleaseTemporary(renderTexture);
  92. renderTexture = RenderTexture.GetTemporary(textureSize.x, textureSize.y);
  93. renderTexture.filterMode = FilterMode.Point;
  94. allocatedRenderTextureSize = textureSize;
  95. }
  96. }
  97. protected void AssignAtQuad () {
  98. Transform quadTransform = quad.transform;
  99. quadTransform.position = this.transform.position;
  100. quadTransform.rotation = this.transform.rotation;
  101. quadTransform.localScale = this.transform.localScale;
  102. Vector3 v0 = quadTransform.InverseTransformPoint(worldCornerNoDistortion0);
  103. Vector3 v1 = quadTransform.InverseTransformPoint(worldCornerNoDistortion1);
  104. Vector3 v2 = quadTransform.InverseTransformPoint(worldCornerNoDistortion2);
  105. Vector3 v3 = quadTransform.InverseTransformPoint(worldCornerNoDistortion3);
  106. Vector3[] vertices = new Vector3[4] { v0, v1, v2, v3 };
  107. quadMesh.vertices = vertices;
  108. int[] indices = new int[6] { 0, 1, 2, 2, 1, 3 };
  109. quadMesh.triangles = indices;
  110. Vector3[] normals = new Vector3[4] {
  111. -Vector3.forward,
  112. -Vector3.forward,
  113. -Vector3.forward,
  114. -Vector3.forward
  115. };
  116. quadMesh.normals = normals;
  117. float maxU = (float)requiredRenderTextureSize.x / (float)allocatedRenderTextureSize.x;
  118. float maxV = (float)requiredRenderTextureSize.y / (float)allocatedRenderTextureSize.y;
  119. Vector2[] uv = new Vector2[4] {
  120. new Vector2(uvCorner0.x * maxU, uvCorner0.y * maxV),
  121. new Vector2(uvCorner1.x * maxU, uvCorner1.y * maxV),
  122. new Vector2(uvCorner2.x * maxU, uvCorner2.y * maxV),
  123. new Vector2(uvCorner3.x * maxU, uvCorner3.y * maxV),
  124. };
  125. quadMesh.uv = uv;
  126. AssignMeshAtRenderer();
  127. }
  128. protected abstract void AssignMeshAtRenderer ();
  129. #endif // HAS_VECTOR2INT
  130. }
  131. }