Skip to content

Commit 3e771e2

Browse files
julienf-unityGitHub Enterprise
authored andcommitted
int and uint support in Compare (#53)
* Add int and uint to Compare node and condition expression * minor fixes * Change SerializeType so that tests are correct against System.Type and null * Fix switch * Update changelog
1 parent a4c7bf3 commit 3e771e2

File tree

8 files changed

+101
-68
lines changed

8 files changed

+101
-68
lines changed

TestProjects/VisualEffectGraph_HDRP/Assets/AllTests/Editor/Tests/VFXExpressionTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ private static void TestConditions(float f0, float f1)
114114
var f0Exp = VFXValue.Constant(f0);
115115
var f1Exp = VFXValue.Constant(f1);
116116

117-
var equalExp = new VFXExpressionCondition(VFXCondition.Equal, f0Exp, f1Exp);
118-
var notEqualExp = new VFXExpressionCondition(VFXCondition.NotEqual, f0Exp, f1Exp);
119-
var lessExp = new VFXExpressionCondition(VFXCondition.Less, f0Exp, f1Exp);
120-
var lessOrEqualExp = new VFXExpressionCondition(VFXCondition.LessOrEqual, f0Exp, f1Exp);
121-
var greater = new VFXExpressionCondition(VFXCondition.Greater, f0Exp, f1Exp);
122-
var greaterOrEqual = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, f0Exp, f1Exp);
117+
var equalExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Equal, f0Exp, f1Exp);
118+
var notEqualExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.NotEqual, f0Exp, f1Exp);
119+
var lessExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Less, f0Exp, f1Exp);
120+
var lessOrEqualExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.LessOrEqual, f0Exp, f1Exp);
121+
var greater = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Greater, f0Exp, f1Exp);
122+
var greaterOrEqual = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, f0Exp, f1Exp);
123123

124124
var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation);
125125
var resultA = context.Compile(equalExp);

com.unity.visualeffectgraph/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The version number for this package has increased due to a version update of a r
4444
- Compute culling of particle which have their alive attribute set to false in output
4545
- Mesh and lit mesh outputs can now have up to 4 differents meshes that can be set per Particle (Experimental)
4646
- Screen space per particle LOD on mesh and lit mesh outputs (Experimental)
47+
- Compare operator can take int and uint as inputs
4748

4849
### Fixed
4950
- Moved VFX Event Tester Window visibility to Component Play Controls SceneView Window

com.unity.visualeffectgraph/Editor/Core/VFXSerializer.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static implicit operator SerializableType(Type value)
2121

2222
public static implicit operator Type(SerializableType value)
2323
{
24-
return value != null ? value.m_Type : null;
24+
return !ReferenceEquals(value, null) ? value.m_Type : null;
2525
}
2626

2727
private SerializableType() {}
@@ -48,42 +48,43 @@ public virtual void OnAfterDeserialize()
4848

4949
public static Type GetType(string name)
5050
{
51-
Type type = Type.GetType(name);
51+
return Type.GetType(name);
52+
}
5253

53-
if (type == null && !string.IsNullOrEmpty(name)) // if type wasn't found, resolve the assembly (to use VFX package assembly name instead)
54-
{
55-
string[] splitted = name.Split(',');
56-
// Replace the assembly with the one containing VFXGraph type which will be either "Unity.VisualEffect.Graph.Editor" or "Unity.VisualEffect.Graph.Editor-testable"
57-
splitted[1] = typeof(VFXGraph).Assembly.GetName().Name;
54+
public override bool Equals(object obj)
55+
{
56+
if (ReferenceEquals(this, obj))
57+
return true;
5858

59-
name = string.Join(",", splitted);
59+
Type otherType = null;
60+
if (obj is SerializableType)
61+
otherType = ((SerializableType)obj)?.m_Type;
62+
else if (obj is Type)
63+
otherType = (Type)obj;
64+
else if (!ReferenceEquals(obj, null))
65+
return false;
6066

61-
type = Type.GetType(name);
67+
return m_Type == otherType;
68+
}
6269

63-
if (type == null) // resolve runtime type if editor assembly didnt work
64-
{
65-
splitted[1] = splitted[1].Replace(".Editor", ".Runtime");
66-
name = string.Join(",", splitted);
67-
type = Type.GetType(name);
68-
}
70+
public static bool operator==(SerializableType left, SerializableType right)
71+
{
72+
if (!ReferenceEquals(left, null))
73+
return left.Equals(right);
74+
if (!ReferenceEquals(right, null))
75+
return right.Equals(left);
6976

70-
// If from here we still haven't found the type, try a last time with the name only.
71-
if (type == null)
72-
{
73-
AppDomain currentDomain = AppDomain.CurrentDomain;
74-
foreach (Assembly assembly in currentDomain.GetAssemblies())
75-
{
76-
type = assembly.GetType(splitted[0]);
77-
if (type != null)
78-
return type;
79-
}
80-
}
77+
return true; // both null
78+
}
8179

82-
if (type == null)
83-
Debug.LogErrorFormat("Cannot get Type from name: {0}", name);
84-
}
80+
public static bool operator !=(SerializableType left, SerializableType right)
81+
{
82+
return !(left == right);
83+
}
8584

86-
return type;
85+
public override int GetHashCode()
86+
{
87+
return m_Type != null ? m_Type.GetHashCode() : 0;
8788
}
8889

8990
public string text

com.unity.visualeffectgraph/Editor/Expressions/VFXExpressionFlow.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ enum VFXCondition
1717
class VFXExpressionCondition : VFXExpression
1818
{
1919
public VFXExpressionCondition()
20-
: this(VFXCondition.Equal, VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
20+
: this(VFXValueType.Float, VFXCondition.Equal, VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
2121
{}
2222

23-
public VFXExpressionCondition(VFXCondition cond, VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
23+
public VFXExpressionCondition(VFXValueType type, VFXCondition cond, VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
2424
{
2525
condition = cond;
26+
this.type = type;
2627
}
2728

2829
public override VFXExpressionOperation operation
@@ -33,25 +34,38 @@ public override VFXExpressionOperation operation
3334
}
3435
}
3536

36-
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
37+
private VFXValue<bool> Evaluate<T>(VFXExpression[] constParents) where T : IComparable<T>
3738
{
38-
bool res = false;
39-
float left = constParents[0].Get<float>();
40-
float right = constParents[1].Get<float>();
39+
T left = constParents[0].Get<T>();
40+
T right = constParents[1].Get<T>();
41+
int comp = left.CompareTo(right);
4142

43+
bool res = false;
4244
switch (condition)
4345
{
44-
case VFXCondition.Equal: res = left == right; break;
45-
case VFXCondition.NotEqual: res = left != right; break;
46-
case VFXCondition.Less: res = left < right; break;
47-
case VFXCondition.LessOrEqual: res = left <= right; break;
48-
case VFXCondition.Greater: res = left > right; break;
49-
case VFXCondition.GreaterOrEqual: res = left >= right; break;
46+
case VFXCondition.Equal: res = comp == 0; break;
47+
case VFXCondition.NotEqual: res = comp != 0; break;
48+
case VFXCondition.Less: res = comp < 0; break;
49+
case VFXCondition.LessOrEqual: res = comp <= 0; break;
50+
case VFXCondition.Greater: res = comp > 0; break;
51+
case VFXCondition.GreaterOrEqual: res = comp >= 0; break;
52+
default: throw new NotImplementedException("Invalid VFXCondition: " + condition);
5053
}
5154

5255
return VFXValue.Constant<bool>(res);
5356
}
5457

58+
sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
59+
{
60+
switch(type)
61+
{
62+
case VFXValueType.Float: return Evaluate<float>(constParents);
63+
case VFXValueType.Int32: return Evaluate<int>(constParents);
64+
case VFXValueType.Uint32: return Evaluate<uint>(constParents);
65+
default: throw new NotImplementedException("This type is not handled by condition expression: " + type);
66+
}
67+
}
68+
5569
public override string GetCodeString(string[] parents)
5670
{
5771
string comparator = null;
@@ -75,7 +89,8 @@ protected override VFXExpression Reduce(VFXExpression[] reducedParents)
7589
return newExpression;
7690
}
7791

78-
protected override int[] additionnalOperands { get { return new int[] { (int)condition }; } }
92+
protected override int[] additionnalOperands { get { return new int[] { (int)type, (int)condition }; } }
93+
private VFXValueType type;
7994
private VFXCondition condition;
8095
}
8196

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using UnityEngine;
45
using UnityEngine.VFX;
56

67
namespace UnityEditor.VFX.Operator
78
{
89
[VFXInfo(category = "Logic")]
9-
class Condition : VFXOperator
10+
class Condition : VFXOperatorDynamicType
1011
{
1112
[VFXSetting, SerializeField, Tooltip("Specifies the comparison condition between the Left and Right operands.")]
1213
protected VFXCondition condition = VFXCondition.Equal;
1314

14-
public class InputProperties
15-
{
16-
[Tooltip("Sets the left operand which will be compared to the right operand based on the specified condition.")]
17-
public float left = 0.0f;
18-
[Tooltip("Sets the right operand which will be compared to the left operand based on the specified condition.")]
19-
public float right = 0.0f;
20-
}
21-
2215
public class OutputProperties
2316
{
2417
[Tooltip("The result of the comparison.")]
@@ -27,9 +20,33 @@ public class OutputProperties
2720

2821
override public string name { get { return "Compare"; } }
2922

23+
protected override IEnumerable<VFXPropertyWithValue> inputProperties
24+
{
25+
get
26+
{
27+
yield return new VFXPropertyWithValue(
28+
new VFXProperty(GetOperandType(), "left", new TooltipAttribute("Sets the left operand which will be compared to the right operand based on the specified condition.")),
29+
GetDefaultValueForType(GetOperandType()));
30+
yield return new VFXPropertyWithValue(
31+
new VFXProperty(GetOperandType(), "right", new TooltipAttribute("Sets the right operand which will be compared to the left operand based on the specified condition.")),
32+
GetDefaultValueForType(GetOperandType()));
33+
}
34+
}
35+
36+
public override IEnumerable<int> staticSlotIndex => Enumerable.Empty<int>();
37+
38+
public override IEnumerable<Type> validTypes => new[]
39+
{
40+
typeof(float),
41+
typeof(uint),
42+
typeof(int),
43+
};
44+
45+
protected override Type defaultValueType => typeof(float);
46+
3047
protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression)
3148
{
32-
return new[] { new VFXExpressionCondition(condition, inputExpression[0], inputExpression[1]) };
49+
return new[] { new VFXExpressionCondition(VFXExpression.GetVFXValueTypeFromType(GetOperandType()), condition, inputExpression[0], inputExpression[1]) };
3350
}
3451
}
3552
}

com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/PositionDepth.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ protected override VFXExpression[] BuildExpression(VFXExpression[] inputExpressi
214214

215215
VFXExpression depthRange = inputExpression[inputSlots.IndexOf(inputSlots.LastOrDefault(o => o.name == "DepthRange")) + _customCameraOffset];
216216

217-
VFXExpression nearRangeCheck = new VFXExpressionCondition(VFXCondition.Less, depth, depthRange.x);
218-
VFXExpression farRangeCheck = new VFXExpressionCondition(VFXCondition.Greater, depth, depthRange.y);
217+
VFXExpression nearRangeCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Less, depth, depthRange.x);
218+
VFXExpression farRangeCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Greater, depth, depthRange.y);
219219
VFXExpression logicOr = new VFXExpressionLogicalOr(nearRangeCheck, farRangeCheck);
220220
isAlive = new VFXExpressionBranch(logicOr, VFXValue.Constant(false), VFXValue.Constant(true));
221221
break;
222222

223223
case CullMode.FarPlane:
224-
VFXExpression farPlaneCheck = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, depth, VFXValue.Constant(1f) - VFXValue.Constant(Mathf.Epsilon));
224+
VFXExpression farPlaneCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, depth, VFXValue.Constant(1f) - VFXValue.Constant(Mathf.Epsilon));
225225
isAlive = new VFXExpressionBranch(farPlaneCheck, VFXValue.Constant(false), VFXValue.Constant(true));
226226
break;
227227
}

com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/ProbabilitySampling.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
136136
var compare = new VFXExpression[m_EntryCount - 1];
137137
for (int i = 0; i < m_EntryCount - 1; i++)
138138
{
139-
compare[i] = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, prefixedProbablities[i], rand);
139+
compare[i] = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, prefixedProbablities[i], rand);
140140
}
141141
;
142142

com.unity.visualeffectgraph/Editor/Models/Operators/Implementations/Switch.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected sealed override IEnumerable<VFXPropertyWithValue> inputProperties
7878
{
7979
var prefix = i.ToString();
8080
if (i != m_EntryCount && m_CustomCaseValue)
81-
yield return new VFXPropertyWithValue(new VFXProperty(typeof(int), "Case " + prefix), (int)i);
81+
yield return new VFXPropertyWithValue(new VFXProperty(typeof(uint), "Case " + prefix), i);
8282
var name = (i == m_EntryCount) ? "default" : "Value " + prefix;
8383
yield return new VFXPropertyWithValue(new VFXProperty((Type)GetOperandType(), name), defaultValue);
8484
}
@@ -96,10 +96,10 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
9696
newInputExpression[0] = inputExpression[0];
9797
int offsetWrite = 1;
9898
int offsetRead = 1;
99-
for (int i = 0; i < m_EntryCount + 1; ++i)
99+
for (uint i = 0; i < m_EntryCount + 1; ++i)
100100
{
101101
if (i != m_EntryCount)
102-
newInputExpression[offsetWrite++] = new VFXValue<int>(i);
102+
newInputExpression[offsetWrite++] = new VFXValue<uint>(i);
103103
for (int sub = 0; sub < expressionCountPerUniqueSlot; ++sub)
104104
{
105105
newInputExpression[offsetWrite++] = inputExpression[offsetRead++];
@@ -109,7 +109,6 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
109109
}
110110

111111
var referenceValue = inputExpression.First();
112-
referenceValue = new VFXExpressionCastUintToFloat(referenceValue);
113112

114113
var startCaseOffset = 1;
115114
var stride = expressionCountPerUniqueSlot + 1;
@@ -120,7 +119,7 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
120119
for (uint i = 0; i < m_EntryCount; i++)
121120
{
122121
valueStartIndex[i] = offsetCase + 1;
123-
compare[i] = new VFXExpressionCondition(VFXCondition.Equal, referenceValue, new VFXExpressionCastIntToFloat(inputExpression[offsetCase]));
122+
compare[i] = new VFXExpressionCondition(VFXValueType.Uint32, VFXCondition.Equal, referenceValue, inputExpression[offsetCase]);
124123
offsetCase += stride;
125124
}
126125

0 commit comments

Comments
 (0)