Skip to content

Commit

Permalink
int and uint support in Compare (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienf-unity committed Nov 16, 2020
1 parent 1f08958 commit b7b8af8
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ private static void TestConditions(float f0, float f1)
var f0Exp = VFXValue.Constant(f0);
var f1Exp = VFXValue.Constant(f1);

var equalExp = new VFXExpressionCondition(VFXCondition.Equal, f0Exp, f1Exp);
var notEqualExp = new VFXExpressionCondition(VFXCondition.NotEqual, f0Exp, f1Exp);
var lessExp = new VFXExpressionCondition(VFXCondition.Less, f0Exp, f1Exp);
var lessOrEqualExp = new VFXExpressionCondition(VFXCondition.LessOrEqual, f0Exp, f1Exp);
var greater = new VFXExpressionCondition(VFXCondition.Greater, f0Exp, f1Exp);
var greaterOrEqual = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, f0Exp, f1Exp);
var equalExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Equal, f0Exp, f1Exp);
var notEqualExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.NotEqual, f0Exp, f1Exp);
var lessExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Less, f0Exp, f1Exp);
var lessOrEqualExp = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.LessOrEqual, f0Exp, f1Exp);
var greater = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Greater, f0Exp, f1Exp);
var greaterOrEqual = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, f0Exp, f1Exp);

var context = new VFXExpression.Context(VFXExpressionContextOption.CPUEvaluation);
var resultA = context.Compile(equalExp);
Expand Down
3 changes: 3 additions & 0 deletions com.unity.visualeffectgraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [8.3.1] - 2020-07-23

### Added
- Compare operator can take int and uint as inputs

### Fixed
- Fix for node window staying when clicking elsewhere
- Prefab highlight missing for initial event name toggle [Case 1263012](https://issuetracker.unity3d.com/product/unity/issues/guid/1263012/)
Expand Down
61 changes: 31 additions & 30 deletions com.unity.visualeffectgraph/Editor/Core/VFXSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SerializableType : ISerializationCallbackReceiver

public static implicit operator Type(SerializableType value)
{
return value != null ? value.m_Type : null;
return !ReferenceEquals(value, null) ? value.m_Type : null;
}

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

public static Type GetType(string name)
{
Type type = Type.GetType(name);
return Type.GetType(name);
}

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

name = string.Join(",", splitted);
Type otherType = null;
if (obj is SerializableType)
otherType = ((SerializableType)obj)?.m_Type;
else if (obj is Type)
otherType = (Type)obj;
else if (!ReferenceEquals(obj, null))
return false;

type = Type.GetType(name);
return m_Type == otherType;
}

if (type == null) // resolve runtime type if editor assembly didnt work
{
splitted[1] = splitted[1].Replace(".Editor", ".Runtime");
name = string.Join(",", splitted);
type = Type.GetType(name);
}
public static bool operator==(SerializableType left, SerializableType right)
{
if (!ReferenceEquals(left, null))
return left.Equals(right);
if (!ReferenceEquals(right, null))
return right.Equals(left);

// If from here we still haven't found the type, try a last time with the name only.
if (type == null)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
foreach (Assembly assembly in currentDomain.GetAssemblies())
{
type = assembly.GetType(splitted[0]);
if (type != null)
return type;
}
}
return true; // both null
}

if (type == null)
Debug.LogErrorFormat("Cannot get Type from name: {0}", name);
}
public static bool operator !=(SerializableType left, SerializableType right)
{
return !(left == right);
}

return type;
public override int GetHashCode()
{
return m_Type != null ? m_Type.GetHashCode() : 0;
}

public string text
Expand Down
41 changes: 28 additions & 13 deletions com.unity.visualeffectgraph/Editor/Expressions/VFXExpressionFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ enum VFXCondition
class VFXExpressionCondition : VFXExpression
{
public VFXExpressionCondition()
: this(VFXCondition.Equal, VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
: this(VFXValueType.Float, VFXCondition.Equal, VFXValue.Constant(0.0f), VFXValue.Constant(0.0f))
{}

public VFXExpressionCondition(VFXCondition cond, VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
public VFXExpressionCondition(VFXValueType type, VFXCondition cond, VFXExpression left, VFXExpression right) : base(VFXExpression.Flags.None, new VFXExpression[] { left, right })
{
condition = cond;
this.type = type;
}

public override VFXExpressionOperation operation
Expand All @@ -33,25 +34,38 @@ public override VFXExpressionOperation operation
}
}

sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
private VFXValue<bool> Evaluate<T>(VFXExpression[] constParents) where T : IComparable<T>
{
bool res = false;
float left = constParents[0].Get<float>();
float right = constParents[1].Get<float>();
T left = constParents[0].Get<T>();
T right = constParents[1].Get<T>();
int comp = left.CompareTo(right);

bool res = false;
switch (condition)
{
case VFXCondition.Equal: res = left == right; break;
case VFXCondition.NotEqual: res = left != right; break;
case VFXCondition.Less: res = left < right; break;
case VFXCondition.LessOrEqual: res = left <= right; break;
case VFXCondition.Greater: res = left > right; break;
case VFXCondition.GreaterOrEqual: res = left >= right; break;
case VFXCondition.Equal: res = comp == 0; break;
case VFXCondition.NotEqual: res = comp != 0; break;
case VFXCondition.Less: res = comp < 0; break;
case VFXCondition.LessOrEqual: res = comp <= 0; break;
case VFXCondition.Greater: res = comp > 0; break;
case VFXCondition.GreaterOrEqual: res = comp >= 0; break;
default: throw new NotImplementedException("Invalid VFXCondition: " + condition);
}

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

sealed protected override VFXExpression Evaluate(VFXExpression[] constParents)
{
switch(type)
{
case VFXValueType.Float: return Evaluate<float>(constParents);
case VFXValueType.Int32: return Evaluate<int>(constParents);
case VFXValueType.Uint32: return Evaluate<uint>(constParents);
default: throw new NotImplementedException("This type is not handled by condition expression: " + type);
}
}

public override string GetCodeString(string[] parents)
{
string comparator = null;
Expand All @@ -75,7 +89,8 @@ protected override VFXExpression Reduce(VFXExpression[] reducedParents)
return newExpression;
}

protected override int[] additionnalOperands { get { return new int[] { (int)condition }; } }
protected override int[] additionnalOperands { get { return new int[] { (int)type, (int)condition }; } }
private VFXValueType type;
private VFXCondition condition;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.VFX;

namespace UnityEditor.VFX.Operator
{
[VFXInfo(category = "Logic")]
class Condition : VFXOperator
class Condition : VFXOperatorDynamicType
{
[VFXSetting, SerializeField, Tooltip("Specifies the comparison condition between the Left and Right operands.")]
protected VFXCondition condition = VFXCondition.Equal;

public class InputProperties
{
[Tooltip("Sets the left operand which will be compared to the right operand based on the specified condition.")]
public float left = 0.0f;
[Tooltip("Sets the right operand which will be compared to the left operand based on the specified condition.")]
public float right = 0.0f;
}

public class OutputProperties
{
[Tooltip("The result of the comparison.")]
Expand All @@ -27,9 +20,33 @@ public class OutputProperties

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

protected override IEnumerable<VFXPropertyWithValue> inputProperties
{
get
{
yield return new VFXPropertyWithValue(
new VFXProperty(GetOperandType(), "left", new TooltipAttribute("Sets the left operand which will be compared to the right operand based on the specified condition.")),
GetDefaultValueForType(GetOperandType()));
yield return new VFXPropertyWithValue(
new VFXProperty(GetOperandType(), "right", new TooltipAttribute("Sets the right operand which will be compared to the left operand based on the specified condition.")),
GetDefaultValueForType(GetOperandType()));
}
}

public override IEnumerable<int> staticSlotIndex => Enumerable.Empty<int>();

public override IEnumerable<Type> validTypes => new[]
{
typeof(float),
typeof(uint),
typeof(int),
};

protected override Type defaultValueType => typeof(float);

protected override sealed VFXExpression[] BuildExpression(VFXExpression[] inputExpression)
{
return new[] { new VFXExpressionCondition(condition, inputExpression[0], inputExpression[1]) };
return new[] { new VFXExpressionCondition(VFXExpression.GetVFXValueTypeFromType(GetOperandType()), condition, inputExpression[0], inputExpression[1]) };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ protected override VFXExpression[] BuildExpression(VFXExpression[] inputExpressi

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

VFXExpression nearRangeCheck = new VFXExpressionCondition(VFXCondition.Less, depth, depthRange.x);
VFXExpression farRangeCheck = new VFXExpressionCondition(VFXCondition.Greater, depth, depthRange.y);
VFXExpression nearRangeCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Less, depth, depthRange.x);
VFXExpression farRangeCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.Greater, depth, depthRange.y);
VFXExpression logicOr = new VFXExpressionLogicalOr(nearRangeCheck, farRangeCheck);
isAlive = new VFXExpressionBranch(logicOr, VFXValue.Constant(false), VFXValue.Constant(true));
break;

case CullMode.FarPlane:
VFXExpression farPlaneCheck = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, depth, VFXValue.Constant(1f) - VFXValue.Constant(Mathf.Epsilon));
VFXExpression farPlaneCheck = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, depth, VFXValue.Constant(1f) - VFXValue.Constant(Mathf.Epsilon));
isAlive = new VFXExpressionBranch(farPlaneCheck, VFXValue.Constant(false), VFXValue.Constant(true));
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
var compare = new VFXExpression[m_EntryCount - 1];
for (int i = 0; i < m_EntryCount - 1; i++)
{
compare[i] = new VFXExpressionCondition(VFXCondition.GreaterOrEqual, prefixedProbablities[i], rand);
compare[i] = new VFXExpressionCondition(VFXValueType.Float, VFXCondition.GreaterOrEqual, prefixedProbablities[i], rand);
}
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected sealed override IEnumerable<VFXPropertyWithValue> inputProperties
{
var prefix = i.ToString();
if (i != m_EntryCount && m_CustomCaseValue)
yield return new VFXPropertyWithValue(new VFXProperty(typeof(int), "Case " + prefix), (int)i);
yield return new VFXPropertyWithValue(new VFXProperty(typeof(uint), "Case " + prefix), i);
var name = (i == m_EntryCount) ? "default" : "Value " + prefix;
yield return new VFXPropertyWithValue(new VFXProperty((Type)GetOperandType(), name), defaultValue);
}
Expand All @@ -96,10 +96,10 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
newInputExpression[0] = inputExpression[0];
int offsetWrite = 1;
int offsetRead = 1;
for (int i = 0; i < m_EntryCount + 1; ++i)
for (uint i = 0; i < m_EntryCount + 1; ++i)
{
if (i != m_EntryCount)
newInputExpression[offsetWrite++] = new VFXValue<int>(i);
newInputExpression[offsetWrite++] = new VFXValue<uint>(i);
for (int sub = 0; sub < expressionCountPerUniqueSlot; ++sub)
{
newInputExpression[offsetWrite++] = inputExpression[offsetRead++];
Expand All @@ -109,7 +109,6 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
}

var referenceValue = inputExpression.First();
referenceValue = new VFXExpressionCastIntToFloat(referenceValue);

var startCaseOffset = 1;
var stride = expressionCountPerUniqueSlot + 1;
Expand All @@ -120,7 +119,7 @@ protected sealed override VFXExpression[] BuildExpression(VFXExpression[] inputE
for (uint i = 0; i < m_EntryCount; i++)
{
valueStartIndex[i] = offsetCase + 1;
compare[i] = new VFXExpressionCondition(VFXCondition.Equal, referenceValue, new VFXExpressionCastIntToFloat(inputExpression[offsetCase]));
compare[i] = new VFXExpressionCondition(VFXValueType.Uint32, VFXCondition.Equal, referenceValue, inputExpression[offsetCase]);
offsetCase += stride;
}

Expand Down

0 comments on commit b7b8af8

Please sign in to comment.