VertexMaterialData.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace TheraBytes.BetterUi
  7. {
  8. [Serializable]
  9. public class VertexMaterialData
  10. {
  11. #region Property Types
  12. [Serializable]
  13. public abstract class Property<T>
  14. {
  15. public string Name;
  16. public T Value;
  17. public abstract void SetValue(ref float uvX, ref float uvY, ref float tangentW);
  18. public abstract Property<T> Clone();
  19. }
  20. [Serializable]
  21. public class FloatProperty : Property<float>
  22. {
  23. public enum Mapping
  24. {
  25. TexcoordX, TexcoordY, TangentW,
  26. }
  27. public Mapping PropertyMap;
  28. public float Min, Max;
  29. public bool IsRestricted { get { return Min < Max; } }
  30. public override void SetValue(ref float uvX, ref float uvY, ref float tangentW)
  31. {
  32. switch (PropertyMap)
  33. {
  34. case Mapping.TexcoordX:
  35. uvX = Value;
  36. break;
  37. case Mapping.TexcoordY:
  38. uvY = Value;
  39. break;
  40. case Mapping.TangentW:
  41. tangentW = Value;
  42. break;
  43. default:
  44. throw new ArgumentException();
  45. }
  46. }
  47. public override Property<float> Clone()
  48. {
  49. return new FloatProperty()
  50. {
  51. Name = this.Name,
  52. Value = this.Value,
  53. Min = this.Min,
  54. Max = this.Max,
  55. PropertyMap = this.PropertyMap,
  56. };
  57. }
  58. }
  59. #endregion
  60. public FloatProperty[] FloatProperties = new FloatProperty[0];
  61. public void Apply(ref float uvX, ref float uvY, ref float tangentW)
  62. {
  63. VertexMaterialData.Apply(FloatProperties, ref uvX, ref uvY, ref tangentW);
  64. }
  65. private static void Apply<T>(IEnumerable<Property<T>> prop,
  66. ref float uvX, ref float uvY, ref float tangentW)
  67. {
  68. if (prop == null)
  69. return;
  70. foreach (var item in prop)
  71. {
  72. item.SetValue(ref uvX, ref uvY, ref tangentW);
  73. }
  74. }
  75. public void Clear()
  76. {
  77. FloatProperties = new FloatProperty[0];
  78. }
  79. public void CopyTo(VertexMaterialData target)
  80. {
  81. target.FloatProperties = CloneArray<FloatProperty, float>(this.FloatProperties);
  82. }
  83. public VertexMaterialData Clone()
  84. {
  85. VertexMaterialData result = new VertexMaterialData();
  86. this.CopyTo(result);
  87. return result;
  88. }
  89. static T[] CloneArray<T, TValue>(T[] array)
  90. where T : Property<TValue>
  91. {
  92. T[] result = new T[array.Length];
  93. for(int i = 0; i < array.Length; i++)
  94. {
  95. result[i] = array[i].Clone() as T;
  96. }
  97. return result;
  98. }
  99. }
  100. }