Skip to content

Commit 185fc47

Browse files
gabrieldelacruzjulienf-unity
authored andcommitted
[VFX] Add shader formatting for Infinity and NaN in floats #259
1 parent 58a2b83 commit 185fc47

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXDefines.hlsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
// this is only necessary for the old VFXTarget pathway
4040
// it defines the macro used to access hybrid instanced properties
4141
// (new HDRP/URP Target pathway overrides the type so this is never used)
42+
#define VFX_INFINITY (1.0f/0.0f)
43+
#define VFX_NAN asfloat(~0u)
44+
4245
#define UNITY_ACCESS_HYBRID_INSTANCED_PROP(name, type) name

com.unity.visualeffectgraph/Editor/Compiler/VFXShaderWriter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,40 @@ public static string GetValueString(VFXValueType type, object value)
6161
value = value.ToString().ToLower();
6262
break;
6363
case VFXValueType.Float:
64-
value = ((float)value).ToString("G9", CultureInfo.InvariantCulture);
64+
value = FormatFloat((float)value);
6565
break;
6666
case VFXValueType.Float2:
67-
value = $"({((Vector2)value).x.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector2)value).y.ToString("G9", CultureInfo.InvariantCulture)})";
67+
value = $"({FormatFloat(((Vector2)value).x)}, {FormatFloat(((Vector2)value).y)})";
6868
break;
6969
case VFXValueType.Float3:
70-
value = $"({((Vector3)value).x.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector3)value).y.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector3)value).z.ToString("G9", CultureInfo.InvariantCulture)})";
70+
value = $"({FormatFloat(((Vector3)value).x)}, {FormatFloat(((Vector3)value).y)}, {FormatFloat(((Vector3)value).z)})";
7171
break;
7272
case VFXValueType.Float4:
73-
value = $"({((Vector4)value).x.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector4)value).y.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector4)value).z.ToString("G9", CultureInfo.InvariantCulture)}, {((Vector4)value).w.ToString("G9", CultureInfo.InvariantCulture)})";
73+
value = $"({FormatFloat(((Vector4)value).x)}, {FormatFloat(((Vector4)value).y)}, {FormatFloat(((Vector4)value).z)}, {FormatFloat(((Vector4)value).w)})";
7474
break;
7575
case VFXValueType.Matrix4x4:
7676
{
7777
var matrix = ((Matrix4x4)value).transpose;
7878
value = "(";
7979
for (int i = 0; i < 16; ++i)
80-
value += string.Format(CultureInfo.InvariantCulture, i == 15 ? "{0}" : "{0},", matrix[i].ToString("G9", CultureInfo.InvariantCulture));
80+
value += string.Format(CultureInfo.InvariantCulture, i == 15 ? "{0}" : "{0},", FormatFloat(matrix[i]));
8181
value += ")";
8282
}
8383
break;
8484
}
8585
return string.Format(CultureInfo.InvariantCulture, format, VFXExpression.TypeToCode(type), value);
8686
}
8787

88+
private static string FormatFloat(float f)
89+
{
90+
if (float.IsInfinity(f))
91+
return f > 0.0f ? "VFX_INFINITY" : "-VFX_INFINITY";
92+
else if (float.IsNaN(f))
93+
return "VFX_NAN";
94+
else
95+
return f.ToString("G9", CultureInfo.InvariantCulture);
96+
}
97+
8898
public static string GetMultilineWithPrefix(string str, string linePrefix)
8999
{
90100
if (linePrefix.Length == 0)

com.unity.visualeffectgraph/Editor/Models/Blocks/Implementations/Attribute/AttributeFromCurve.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,9 @@ public override IEnumerable<VFXNamedExpression> parameters
387387
if (SampleMode == CurveSampleMode.BySpeed)
388388
{
389389
var speedRangeComponents = VFXOperatorUtility.ExtractComponents(speedRange).ToArray();
390-
// speedRange.y = 1 / (sign(speedRange.y - speedRange.y) * max(epsilon, abs(speedRange.y - speedRange.y))
390+
// speedRange.y = 1 / (speedRange.y - speedRange.x)
391391
var speedRangeDelta = speedRangeComponents[1] - speedRangeComponents[0];
392-
var denom = new VFXExpressionSign(speedRangeDelta) * new VFXExpressionMax(VFXOperatorUtility.EpsilonExpression[VFXValueType.Float], new VFXExpressionAbs(speedRangeDelta));
393-
speedRangeComponents[1] = VFXOperatorUtility.OneExpression[VFXValueType.Float] / denom;
392+
speedRangeComponents[1] = VFXOperatorUtility.OneExpression[VFXValueType.Float] / speedRangeDelta;
394393
yield return new VFXNamedExpression(new VFXExpressionCombine(speedRangeComponents), "SpeedRange");
395394
}
396395
}

com.unity.visualeffectgraph/Shaders/ParticlePoints/Pass.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ VFX_VARYING_PS_INPUTS vert(uint id : SV_VertexID, vs_input i)
2525
${VFXProcessBlocks}
2626

2727
if (!attributes.alive)
28+
{
29+
o.pos.x = VFX_NAN;
2830
return o;
31+
}
2932

3033
float3 vPos = attributes.position;
3134
o.VFX_VARYING_POSCS = TransformPositionVFXToClip(vPos);

com.unity.visualeffectgraph/Shaders/VFXParticleCommon.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ if (index >= asuint(nbMax) - deadCount)
7575
#if USE_GEOMETRY_SHADER
7676
return; // cull
7777
#else
78+
o.pos.x = VFX_NAN;
7879
return o; // cull
7980
#endif
8081

@@ -88,7 +89,10 @@ ${VFXLoadAttributes}
8889
${VFXLoadAttributes:{alive}}
8990
#if !HAS_STRIPS
9091
if (!attributes.alive)
92+
{
93+
o.pos.x = VFX_NAN;
9194
return o;
95+
}
9296
#endif
9397

9498
${VFXLoadAttributes:{(?!(alive))(\b\w)}}

0 commit comments

Comments
 (0)