VFXExpressionFlow.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using UnityEngine.VFX;
  3. namespace UnityEditor.VFX
  4. {
  5. // Must match enum in C++
  6. enum VFXCondition
  7. {
  8. Equal,
  9. NotEqual,
  10. Less,
  11. LessOrEqual,
  12. Greater,
  13. GreaterOrEqual,
  14. }
  15. class VFXExpressionCondition : VFXExpression
  16. {
  17. public VFXExpressionCondition()
  18. : this(VFXValueType.Float, VFXCondition.Equal, VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
  19. { }
  20. public VFXExpressionCondition(VFXValueType type, VFXCondition cond, VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
  21. {
  22. if (type != left.valueType || type != right.valueType)
  23. throw new InvalidOperationException(string.Format("Unexpected value type in condition expression : {0}/{1} (expected {2})", left.valueType, right.valueType, type));
  24. if (type != VFXValueType.Float && type != VFXValueType.Uint32 && type != VFXValueType.Int32)
  25. throw new NotImplementedException("This type is not handled by condition expression: " + type);
  26. condition = cond;
  27. this.type = type;
  28. }
  29. public override VFXExpressionOperation operation
  30. {
  31. get
  32. {
  33. return VFXExpressionOperation.Condition;
  34. }
  35. }
  36. private VFXValue<bool> Evaluate<T>(VFXExpression[] constParents) where T : IComparable<T>
  37. {
  38. T left = constParents[0].Get<T>();
  39. T right = constParents[1].Get<T>();
  40. int comp = left.CompareTo(right);
  41. bool res = false;
  42. switch (condition)
  43. {
  44. case VFXCondition.Equal: res = comp == 0; break;
  45. case VFXCondition.NotEqual: res = comp != 0; break;
  46. case VFXCondition.Less: res = comp < 0; break;
  47. case VFXCondition.LessOrEqual: res = comp <= 0; break;
  48. case VFXCondition.Greater: res = comp > 0; break;
  49. case VFXCondition.GreaterOrEqual: res = comp >= 0; break;
  50. default: throw new NotImplementedException("Invalid VFXCondition: " + condition);
  51. }
  52. return VFXValue.Constant<bool>(res);
  53. }
  54. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  55. {
  56. switch (type)
  57. {
  58. case VFXValueType.Float: return Evaluate<float>(constParents);
  59. case VFXValueType.Int32: return Evaluate<int>(constParents);
  60. case VFXValueType.Uint32: return Evaluate<uint>(constParents);
  61. default: throw new NotImplementedException("This type is not handled by condition expression: " + type);
  62. }
  63. }
  64. public override string GetCodeString(string[] parents)
  65. {
  66. string comparator = null;
  67. switch (condition)
  68. {
  69. case VFXCondition.Equal: comparator = "=="; break;
  70. case VFXCondition.NotEqual: comparator = "!="; break;
  71. case VFXCondition.Less: comparator = "<"; break;
  72. case VFXCondition.LessOrEqual: comparator = "<="; break;
  73. case VFXCondition.Greater: comparator = ">"; break;
  74. case VFXCondition.GreaterOrEqual: comparator = ">="; break;
  75. }
  76. return string.Format("{0} {1} {2}", parents[0], comparator, parents[1]);
  77. }
  78. protected override VFXExpression Reduce(VFXExpression[] reducedParents)
  79. {
  80. var newExpression = (VFXExpressionCondition)base.Reduce(reducedParents);
  81. newExpression.condition = condition;
  82. newExpression.type = type;
  83. return newExpression;
  84. }
  85. protected override int[] additionnalOperands { get { return new int[] { (int)type, (int)condition }; } }
  86. private VFXValueType type;
  87. private VFXCondition condition;
  88. }
  89. class VFXExpressionBranch : VFXExpression
  90. {
  91. public VFXExpressionBranch()
  92. : this(VFXValue.Constant(true), VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
  93. { }
  94. public VFXExpressionBranch(VFXExpression pred, VFXExpression trueExp, VFXExpression falseExp)
  95. : base(VFXExpression.Flags.None, new VFXExpression[] { pred, trueExp, falseExp })
  96. {
  97. if (parents[1].valueType != parents[2].valueType)
  98. throw new ArgumentException("both branch expressions must be of the same types");
  99. }
  100. public override VFXExpressionOperation operation
  101. {
  102. get
  103. {
  104. return VFXExpressionOperation.Branch;
  105. }
  106. }
  107. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  108. {
  109. bool pred = constParents[0].Get<bool>();
  110. return pred ? constParents[1] : constParents[2];
  111. }
  112. public override string GetCodeString(string[] parents)
  113. {
  114. return string.Format("{0} ? {1} : {2}", parents);
  115. }
  116. protected override VFXExpression Reduce(VFXExpression[] reducedParents)
  117. {
  118. if (reducedParents[0].Is(VFXExpression.Flags.Constant)) // detect static branching
  119. return Evaluate(reducedParents);
  120. return base.Reduce(reducedParents);
  121. }
  122. protected override int[] additionnalOperands { get { return new int[] { (int)parents[1].valueType }; } }
  123. }
  124. }