VFXExpressionColor.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 VFXExpressionRGBtoHSV : VFXExpression
  9. {
  10. public VFXExpressionRGBtoHSV() : this(VFXValue<Vector3>.Default)
  11. {
  12. }
  13. public VFXExpressionRGBtoHSV(VFXExpression parent) : base(VFXExpression.Flags.None, new[] { parent })
  14. {
  15. }
  16. public override VFXExpressionOperation operation
  17. {
  18. get
  19. {
  20. return VFXExpressionOperation.RGBtoHSV;
  21. }
  22. }
  23. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  24. {
  25. var rgbReduce = constParents[0];
  26. var rgb = rgbReduce.Get<Vector3>();
  27. Color color = new Color(rgb.x, rgb.y, rgb.z, 1.0f);
  28. float h, s, v;
  29. Color.RGBToHSV(color, out h, out s, out v);
  30. return VFXValue.Constant(new Vector3(h, s, v));
  31. }
  32. public override string GetCodeString(string[] parents)
  33. {
  34. return string.Format("RGBtoHSV({0})", parents[0]);
  35. }
  36. }
  37. class VFXExpressionHSVtoRGB : VFXExpression
  38. {
  39. public VFXExpressionHSVtoRGB() : this(VFXValue<Vector3>.Default)
  40. {
  41. }
  42. public VFXExpressionHSVtoRGB(VFXExpression parent) : base(VFXExpression.Flags.None, new VFXExpression[] { parent })
  43. {
  44. }
  45. public override VFXExpressionOperation operation
  46. {
  47. get
  48. {
  49. return VFXExpressionOperation.HSVtoRGB;
  50. }
  51. }
  52. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  53. {
  54. var hsvReduce = constParents[0];
  55. var hsv = hsvReduce.Get<Vector3>();
  56. var rgb = Color.HSVToRGB(hsv.x, hsv.y, hsv.z, true);
  57. return VFXValue.Constant<Vector3>(new Vector3(rgb.r, rgb.g, rgb.b));
  58. }
  59. public override string GetCodeString(string[] parents)
  60. {
  61. return string.Format("HSVtoRGB({0})", parents[0]);
  62. }
  63. }
  64. }