VFXExpressionRandom.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.VFX;
  5. using System.Runtime.CompilerServices;
  6. namespace UnityEditor.VFX
  7. {
  8. struct RandId
  9. {
  10. public RandId(object owner, int id = 0)
  11. {
  12. this.owner = new WeakReference(owner);
  13. this.id = id;
  14. }
  15. public override bool Equals(object obj)
  16. {
  17. if (!(obj is RandId))
  18. return false;
  19. var other = (RandId)obj;
  20. return ReferenceEquals(owner.Target, other.owner.Target) && id == other.id;
  21. }
  22. public override int GetHashCode()
  23. {
  24. // This is not good practice as hashcode will mutate when target gets destroyed but in our case we don't care.
  25. // Any entry in cache will just be lost, but it would have never been accessed anyway (as owner is lost)
  26. return (RuntimeHelpers.GetHashCode(owner.Target) * 397) ^ id;
  27. }
  28. WeakReference owner;
  29. int id;
  30. }
  31. #pragma warning disable 0659
  32. class VFXExpressionRandom : VFXExpression
  33. {
  34. public VFXExpressionRandom(bool perElement, RandId id) : base(perElement ? VFXExpression.Flags.PerElement : VFXExpression.Flags.None)
  35. {
  36. m_Id = id;
  37. }
  38. public override bool Equals(object obj)
  39. {
  40. if (!base.Equals(obj))
  41. return false;
  42. var other = obj as VFXExpressionRandom;
  43. if (other == null)
  44. return false;
  45. return m_Id.Equals(other.m_Id);
  46. }
  47. protected override int GetInnerHashCode()
  48. {
  49. return (base.GetInnerHashCode() * 397) ^ m_Id.GetHashCode();
  50. }
  51. public override VFXExpressionOperation operation { get { return VFXExpressionOperation.GenerateRandom; } }
  52. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  53. {
  54. return VFXValue.Constant(UnityEngine.Random.value);
  55. }
  56. public override string GetCodeString(string[] parents)
  57. {
  58. return string.Format("Rand(attributes.seed)");
  59. }
  60. public override IEnumerable<VFXAttributeInfo> GetNeededAttributes()
  61. {
  62. if (Is(Flags.PerElement))
  63. yield return new VFXAttributeInfo(VFXAttribute.Seed, VFXAttributeMode.ReadWrite);
  64. }
  65. private RandId m_Id;
  66. }
  67. class VFXExpressionFixedRandom : VFXExpression
  68. {
  69. public VFXExpressionFixedRandom() : this(VFXValue<uint>.Default) { }
  70. public VFXExpressionFixedRandom(VFXExpression hash) : base(VFXExpression.Flags.None, hash) { }
  71. public override VFXExpressionOperation operation { get { return VFXExpressionOperation.GenerateFixedRandom; } }
  72. sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
  73. {
  74. var oldState = UnityEngine.Random.state;
  75. UnityEngine.Random.InitState((int)constParents[0].Get<uint>());
  76. var result = VFXValue.Constant(UnityEngine.Random.value);
  77. UnityEngine.Random.state = oldState;
  78. return result;
  79. }
  80. public override string GetCodeString(string[] parents)
  81. {
  82. return string.Format("FixedRand({0})", parents[0]);
  83. }
  84. }
  85. #pragma warning restore 0659
  86. }