VFXExpressionMapper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using UnityEngine.VFX;
  6. namespace UnityEditor.VFX
  7. {
  8. struct VFXNamedExpression
  9. {
  10. public VFXNamedExpression(VFXExpression exp, string name)
  11. {
  12. this.exp = exp;
  13. this.name = name;
  14. }
  15. public VFXExpression exp;
  16. public string name;
  17. }
  18. class VFXExpressionMapper
  19. {
  20. public struct Data
  21. {
  22. public string fullName { get { return id == -1 ? name : string.Format("{0}_{1}", name, VFXCodeGeneratorHelper.GeneratePrefix((uint)id)); } }
  23. public string name;
  24. public int id;
  25. }
  26. public VFXExpressionMapper()
  27. {
  28. }
  29. public IEnumerable<VFXExpression> expressions { get { return m_ExpressionsData.Keys; } }
  30. public void AddExpressionsFromSlotContainer(IVFXSlotContainer slotContainer, int blockId)
  31. {
  32. foreach (var master in slotContainer.inputSlots)
  33. AddExpressionsFromSlot(master, blockId);
  34. }
  35. public void AddExpressionsFromSlot(VFXSlot masterSlot, int blockId)
  36. {
  37. foreach (var slot in masterSlot.GetExpressionSlots())
  38. {
  39. var exp = slot.GetExpression();
  40. if (!Contains(exp))
  41. AddExpression(exp, slot.fullName, blockId);
  42. }
  43. }
  44. public static VFXExpressionMapper FromBlocks(IEnumerable<VFXBlock> blocks)
  45. {
  46. var mapper = new VFXExpressionMapper();
  47. int cpt = 0;
  48. foreach (var block in blocks)
  49. {
  50. mapper.AddExpressions(block.parameters, cpt++);
  51. }
  52. return mapper;
  53. }
  54. public static VFXExpressionMapper FromContext(VFXContext context)
  55. {
  56. var mapper = FromBlocks(context.activeFlattenedChildrenWithImplicit);
  57. mapper.AddExpressionsFromSlotContainer(context, -1);
  58. return mapper;
  59. }
  60. public ReadOnlyCollection<Data> GetData(VFXExpression exp)
  61. {
  62. List<Data> data;
  63. if (m_ExpressionsData.TryGetValue(exp, out data))
  64. {
  65. return data.AsReadOnly();
  66. }
  67. return (new List<Data>()).AsReadOnly();
  68. }
  69. public bool Contains(VFXExpression exp)
  70. {
  71. return m_ExpressionsData.ContainsKey(exp);
  72. }
  73. public void AddExpression(VFXExpression exp, Data data)
  74. {
  75. AddExpression(exp, data.name, data.id);
  76. }
  77. public VFXExpression FromNameAndId(string name, int id)
  78. {
  79. foreach (var expression in m_ExpressionsData)
  80. {
  81. if (expression.Value.Any(o => o.name == name && o.id == id))
  82. {
  83. return expression.Key;
  84. }
  85. }
  86. return null;
  87. }
  88. public IEnumerable<VFXNamedExpression> CollectExpression(int id, bool fullname = true)
  89. {
  90. foreach (var expressionData in m_ExpressionsData)
  91. {
  92. foreach (var data in expressionData.Value)
  93. {
  94. if (data.id == id)
  95. {
  96. yield return new VFXNamedExpression(expressionData.Key, fullname ? data.fullName : data.name);
  97. }
  98. }
  99. }
  100. }
  101. public void AddExpression(VFXExpression exp, string name, int id)
  102. {
  103. if (exp == null || name == null)
  104. throw new ArgumentNullException();
  105. if (m_ExpressionsData.SelectMany(o => o.Value).Any(o => o.name == name && o.id == id))
  106. throw new ArgumentException(string.Format("{0}_{1} has been added twice: {2}", name, id, exp));
  107. var data = new Data();
  108. data.name = name;
  109. data.id = id;
  110. if (!m_ExpressionsData.ContainsKey(exp))
  111. {
  112. m_ExpressionsData.Add(exp, new List<Data>());
  113. }
  114. m_ExpressionsData[exp].Add(data);
  115. }
  116. public void AddExpressions(IEnumerable<VFXNamedExpression> expressions, int id)
  117. {
  118. foreach (var exp in expressions)
  119. AddExpression(exp.exp, exp.name, id);
  120. }
  121. private Dictionary<VFXExpression, List<Data>> m_ExpressionsData = new Dictionary<VFXExpression, List<Data>>();
  122. }
  123. }