VFXExpressionExtractComponent.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.VFX;
  4. namespace UnityEditor.VFX
  5. {
  6. class VFXExpressionExtractComponent : VFXExpressionNumericOperation
  7. {
  8. public VFXExpressionExtractComponent() : this(VFXValue<Vector4>.Default, 0) { }
  9. public VFXExpressionExtractComponent(VFXExpression parent, int iChannel)
  10. : base(new VFXExpression[1] { parent })
  11. {
  12. if (parent.valueType == VFXValueType.Float || !IsFloatValueType(parent.valueType))
  13. {
  14. throw new ArgumentException("Incorrect VFXExpressionExtractComponent");
  15. }
  16. m_Operation = VFXExpressionOperation.ExtractComponent;
  17. m_additionnalOperands = new int[] { iChannel, TypeToSize(parent.valueType) };
  18. }
  19. private int channel { get { return m_additionnalOperands[0]; } }
  20. static private float GetChannel(Vector2 input, int iChannel)
  21. {
  22. switch (iChannel)
  23. {
  24. case 0: return input.x;
  25. case 1: return input.y;
  26. }
  27. Debug.LogError("Incorrect channel (Vector2)");
  28. return 0.0f;
  29. }
  30. static private float GetChannel(Vector3 input, int iChannel)
  31. {
  32. switch (iChannel)
  33. {
  34. case 0: return input.x;
  35. case 1: return input.y;
  36. case 2: return input.z;
  37. }
  38. Debug.LogError("Incorrect channel (Vector2)");
  39. return 0.0f;
  40. }
  41. static private float GetChannel(Vector4 input, int iChannel)
  42. {
  43. switch (iChannel)
  44. {
  45. case 0: return input.x;
  46. case 1: return input.y;
  47. case 2: return input.z;
  48. case 3: return input.w;
  49. }
  50. Debug.LogError("Incorrect channel (Vector2)");
  51. return 0.0f;
  52. }
  53. sealed protected override VFXExpression Evaluate(VFXExpression[] reducedParents)
  54. {
  55. float readValue = 0.0f;
  56. var parent = reducedParents[0];
  57. switch (reducedParents[0].valueType)
  58. {
  59. case VFXValueType.Float: readValue = parent.Get<float>(); break;
  60. case VFXValueType.Float2: readValue = GetChannel(parent.Get<Vector2>(), channel); break;
  61. case VFXValueType.Float3: readValue = GetChannel(parent.Get<Vector3>(), channel); break;
  62. case VFXValueType.Float4: readValue = GetChannel(parent.Get<Vector4>(), channel); break;
  63. }
  64. return VFXValue.Constant(readValue);
  65. }
  66. protected override VFXExpression Reduce(VFXExpression[] reducedParents)
  67. {
  68. var parent = reducedParents[0];
  69. if (parent is VFXExpressionCombine)
  70. return parent.parents[channel];
  71. else if (parent.valueType == VFXValueType.Float && channel == 0)
  72. return parent;
  73. else
  74. return base.Reduce(reducedParents);
  75. }
  76. sealed public override string GetCodeString(string[] parents)
  77. {
  78. return string.Format("{0}[{1}]", parents[0], channel);
  79. }
  80. }
  81. }