-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathNodeResult.cs
More file actions
113 lines (99 loc) · 2.99 KB
/
NodeResult.cs
File metadata and controls
113 lines (99 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace Editor.ShaderGraph;
public enum NodeResultType
{
Bool,
Float,
Vector2,
Vector3,
Vector4,
[Obsolete( "Use NodeResultType.Vector4 instead", false )]
Color,
Invalid,
}
public struct NodeResult : IValid
{
public delegate NodeResult Func( GraphCompiler compiler );
public NodeResultType ResultType { get; private set; } = NodeResultType.Invalid;
public string Code { get; private set; }
public bool Constant { get; set; }
public string[] Errors { get; private init; }
public readonly bool IsValid => ResultType != NodeResultType.Invalid && !string.IsNullOrWhiteSpace( Code );
public readonly string TypeName => ResultType switch
{
NodeResultType.Bool => "bool",
NodeResultType.Float => "float",
NodeResultType.Vector2 => "float2",
NodeResultType.Vector3 => "float3",
NodeResultType.Vector4 => "float4",
_ => null
};
public int Components => ResultType switch
{
NodeResultType.Bool => 1,
NodeResultType.Float => 1,
NodeResultType.Vector2 => 2,
NodeResultType.Vector3 => 3,
NodeResultType.Vector4 => 4,
_ => 1
};
public readonly Type ComponentType => ResultType switch
{
NodeResultType.Float => typeof( float ),
NodeResultType.Vector2 => typeof( Vector2 ),
NodeResultType.Vector3 => typeof( Vector3 ),
NodeResultType.Vector4 => typeof( Vector4 ),
_ => null,
};
[Obsolete( "Use NodeResult( NodeResultType resultType, string code, bool constant = false ) instead", false )]
public NodeResult( int components, string code, bool constant = false )
{
ResultType = components switch
{
1 => NodeResultType.Float,
2 => NodeResultType.Vector2,
3 => NodeResultType.Vector3,
4 => NodeResultType.Vector4,
_ => NodeResultType.Invalid,
};
Code = code;
Constant = constant;
}
public NodeResult( NodeResultType resultType, string code, bool constant = false )
{
ResultType = resultType;
Code = code;
Constant = constant;
}
public static NodeResult Error( params string[] errors ) => new() { Errors = errors };
public static NodeResult MissingInput( string name ) => Error( $"Missing required input '{name}'." );
/// <summary>
/// "Cast" this result to different float types
/// </summary>
public string Cast( int components, float defaultValue = 0.0f )
{
if ( ResultType == NodeResultType.Bool || ResultType == NodeResultType.Invalid )
{
throw new Exception( $"ResultType `{ResultType}` cannot be cast." );
}
if ( Components == components )
return Code;
if ( Components > components )
{
return $"{Code}.{"xyzw"[..components]}";
}
else if ( Components == 1 )
{
return $"float{components}( {string.Join( ", ", Enumerable.Repeat( Code, components ) )} )";
}
else
{
if ( !string.IsNullOrWhiteSpace( Code ) )
return $"float{components}( {Code}, {string.Join( ", ", Enumerable.Repeat( $"{defaultValue}", components - Components ) )} )";
return $"float{components}( {string.Join( ", ", Enumerable.Repeat( $"{defaultValue}", components ) )} )";
}
}
public override readonly string ToString()
{
return Code;
}
}