VFXExpressionCombine.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using UnityEngine.VFX;
  6. namespace UnityEditor.VFX
  7. {
  8. class VFXExpressionCombine : VFXExpressionNumericOperation
  9. {
  10. public VFXExpressionCombine() : this(VFXValue<float>.Default, VFXValue<float>.Default)
  11. { }
  12. public VFXExpressionCombine(params VFXExpression[] parents)
  13. : base(parents)
  14. {
  15. if (parents.Length <= 1 || parents.Length > 4 || parents.Any(o => !IsFloatValueType(o.valueType)))
  16. {
  17. throw new ArgumentException("Incorrect VFXExpressionCombine");
  18. }
  19. switch (parents.Length)
  20. {
  21. case 2:
  22. m_Operation = VFXExpressionOperation.Combine2f;
  23. break;
  24. case 3:
  25. m_Operation = VFXExpressionOperation.Combine3f;
  26. break;
  27. case 4:
  28. m_Operation = VFXExpressionOperation.Combine4f;
  29. break;
  30. }
  31. }
  32. sealed protected override VFXExpression Evaluate(VFXExpression[] reducedParents)
  33. {
  34. var constParentFloat = reducedParents.Cast<VFXValue<float>>().Select(o => o.Get()).ToArray();
  35. if (constParentFloat.Length != parents.Length)
  36. {
  37. throw new ArgumentException("Incorrect VFXExpressionCombine.ExecuteConstantOperation");
  38. }
  39. switch (parents.Length)
  40. {
  41. case 2: return VFXValue.Constant(new Vector2(constParentFloat[0], constParentFloat[1]));
  42. case 3: return VFXValue.Constant(new Vector3(constParentFloat[0], constParentFloat[1], constParentFloat[2]));
  43. case 4: return VFXValue.Constant(new Vector4(constParentFloat[0], constParentFloat[1], constParentFloat[2], constParentFloat[3]));
  44. }
  45. return null;
  46. }
  47. sealed public override string GetCodeString(string[] parents)
  48. {
  49. return string.Format("{0}({1})", TypeToCode(valueType), parents.Aggregate((a, b) => string.Format("{0}, {1}", a, b)));
  50. }
  51. }
  52. }