TargetUvMapper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Shatter Toolkit
  2. // Copyright 2015 Gustav Olsson
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace ShatterToolkit
  6. {
  7. public class TargetUvMapper : UvMapper
  8. {
  9. /// <summary>
  10. /// Determines the start of the target area in the texture, in uv-coordinates.
  11. /// </summary>
  12. public Vector2 targetStart = Vector2.zero;
  13. /// <summary>
  14. /// Determines the size of the target area in the texture, in uv-coordinates.
  15. /// </summary>
  16. public Vector2 targetSize = Vector2.one;
  17. /// <summary>
  18. /// If true, the aspect ratio of the vertex uv-coordinates, before being mapped to the target area, is locked to 1.
  19. /// Useful if the desired target area is square and the final texture should keep it's proportions.
  20. /// </summary>
  21. public bool square = false;
  22. /// <summary>
  23. /// If true, the mesh origo (0,0,0) will be the center of the vertex uv-coordinates, before being mapped to the target area.
  24. /// Useful when for example splitting a watermelon multiple times and a coherent mapping is desired.
  25. /// </summary>
  26. public bool centerMeshOrigo = false;
  27. public override void Map(IList<Vector3> points, Vector3 planeNormal, out Vector4[] tangentsA, out Vector4[] tangentsB, out Vector2[] uvsA, out Vector2[] uvsB)
  28. {
  29. // Calculate texture direction vectors
  30. Vector3 u = Vector3.Cross(planeNormal, Vector3.up);
  31. if (u == Vector3.zero)
  32. {
  33. u = Vector3.Cross(planeNormal, Vector3.forward);
  34. }
  35. Vector3 v = Vector3.Cross(u, planeNormal);
  36. u.Normalize();
  37. v.Normalize();
  38. // Set tangents
  39. Vector4 tangentA = new Vector4(u.x, u.y, u.z, 1.0f);
  40. Vector4 tangentB = new Vector4(u.x, u.y, u.z, -1.0f);
  41. tangentsA = new Vector4[points.Count];
  42. tangentsB = new Vector4[points.Count];
  43. for (int i = 0; i < points.Count; i++)
  44. {
  45. tangentsA[i] = tangentA;
  46. tangentsB[i] = tangentB;
  47. }
  48. // Set uvs
  49. Vector2[] uvs = new Vector2[points.Count];
  50. Vector2 min = Vector2.zero;
  51. Vector2 max = Vector2.zero;
  52. for (int i = 0; i < points.Count; i++)
  53. {
  54. Vector3 point = points[i];
  55. uvs[i].x = Vector3.Dot(point, u);
  56. uvs[i].y = Vector3.Dot(point, v);
  57. if (i == 0)
  58. {
  59. min = uvs[i];
  60. max = uvs[i];
  61. }
  62. else
  63. {
  64. min = Vector2.Min(uvs[i], min);
  65. max = Vector2.Max(uvs[i], max);
  66. }
  67. }
  68. Vector2 originalSize = max - min;
  69. if (square)
  70. {
  71. float largestSide = Mathf.Max(originalSize.x, originalSize.y);
  72. Vector2 offset = new Vector2();
  73. offset.x = (largestSide - originalSize.x) * 0.5f;
  74. offset.y = (largestSide - originalSize.y) * 0.5f;
  75. min -= offset;
  76. max += offset;
  77. }
  78. if (centerMeshOrigo)
  79. {
  80. Vector2 largestExtent = new Vector2();
  81. largestExtent.x = Mathf.Max(Mathf.Abs(min.x), Mathf.Abs(max.x));
  82. largestExtent.y = Mathf.Max(Mathf.Abs(min.y), Mathf.Abs(max.y));
  83. min = -largestExtent;
  84. max = largestExtent;
  85. }
  86. Vector2 size = max - min;
  87. Vector2 invSize = new Vector2(1.0f / size.x, 1.0f / size.y);
  88. for (int i = 0; i < points.Count; i++)
  89. {
  90. // Convert uvs to the range [0, 1]
  91. uvs[i].x = (uvs[i].x - min.x) * invSize.x;
  92. uvs[i].y = (uvs[i].y - min.y) * invSize.y;
  93. // Convert uvs to the range [targetStart, targetStart + targetSize]
  94. uvs[i].x = targetStart.x + targetSize.x * uvs[i].x;
  95. uvs[i].y = targetStart.y + targetSize.y * uvs[i].y;
  96. }
  97. uvsA = uvs;
  98. uvsB = uvs;
  99. }
  100. }
  101. }