1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Runtime . CompilerServices ;
@@ -48,15 +49,42 @@ internal static string Get(int id) =>
48
49
IdToName . TryGetValue ( id , out var name ) ? name : $ "<{ id } >";
49
50
}
50
51
51
- internal abstract class Prop ;
52
+ internal abstract class Prop
53
+ {
54
+ internal abstract void Write ( int id , MaterialPropertyBlock mpb ) ;
55
+ }
52
56
53
- internal class Prop < T > ( T value ) : Prop
57
+ internal abstract class Prop < T > ( T value ) : Prop
54
58
{
55
59
internal T Value = value ;
56
-
57
60
public override string ToString ( ) => Value . ToString ( ) ;
58
61
}
59
62
63
+ internal class PropColor ( Color value ) : Prop < Color > ( value )
64
+ {
65
+ internal override void Write ( int id , MaterialPropertyBlock mpb ) => mpb . SetColor ( id , Value ) ;
66
+ }
67
+
68
+ internal class PropFloat ( float value ) : Prop < float > ( value )
69
+ {
70
+ internal override void Write ( int id , MaterialPropertyBlock mpb ) => mpb . SetFloat ( id , Value ) ;
71
+ }
72
+
73
+ internal class PropInt ( int value ) : Prop < int > ( value )
74
+ {
75
+ internal override void Write ( int id , MaterialPropertyBlock mpb ) => mpb . SetInt ( id , Value ) ;
76
+ }
77
+
78
+ internal class PropTexture ( Texture value ) : Prop < Texture > ( value )
79
+ {
80
+ internal override void Write ( int id , MaterialPropertyBlock mpb ) => mpb . SetTexture ( id , Value ) ;
81
+ }
82
+
83
+ internal class PropVector ( Vector4 value ) : Prop < Vector4 > ( value )
84
+ {
85
+ internal override void Write ( int id , MaterialPropertyBlock mpb ) => mpb . SetVector ( id , Value ) ;
86
+ }
87
+
60
88
public sealed class Props ( int priority )
61
89
{
62
90
public readonly int Priority = priority ;
@@ -79,39 +107,36 @@ public sealed class Props(int priority)
79
107
internal IEnumerable < int > ManagedIds => _props . Keys ;
80
108
81
109
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
82
- private void _internalSet < T > ( int id , T value )
110
+ private void _internalSet < T , TProp > ( int id , T value ) where TProp : Prop < T >
83
111
{
84
- if ( ! _props . TryGetValue ( id , out var prop ) ) {
85
- _props [ id ] = new Prop < T > ( value ) ;
86
- Changed = true ;
87
- return ;
88
- }
112
+ if ( _props . TryGetValue ( id , out var prop ) ) {
113
+ if ( prop is TProp typedProp ) {
114
+ if ( EqualityComparer < T > . Default . Equals ( value , typedProp . Value ) ) return ;
115
+
116
+ typedProp . Value = value ;
117
+ Changed = true ;
118
+ return ;
119
+ }
89
120
90
- if ( prop is not Prop < T > propT ) {
91
121
MaterialPropertyManager . Instance . LogWarning (
92
122
$ "property { PropIdToName . Get ( id ) } has mismatched type; overwriting with { typeof ( T ) . Name } !") ;
93
- _props [ id ] = new Prop < T > ( value ) ;
94
- Changed = true ;
95
- return ;
96
123
}
97
124
98
- if ( EqualityComparer < T > . Default . Equals ( value , propT . Value ) ) return ;
99
-
100
- propT . Value = value ;
125
+ _props [ id ] = ( TProp ) Activator . CreateInstance ( typeof ( TProp ) , value ) ;
101
126
Changed = true ;
102
127
}
103
128
104
129
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
105
- public void SetColor ( int id , Color value ) => _internalSet < Color > ( id , value ) ;
130
+ public void SetColor ( int id , Color value ) => _internalSet < Color , PropColor > ( id , value ) ;
106
131
107
132
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
108
- public void SetFloat ( int id , float value ) => _internalSet < float > ( id , value ) ;
133
+ public void SetFloat ( int id , float value ) => _internalSet < float , PropFloat > ( id , value ) ;
109
134
110
135
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
111
- public void SetInt ( int id , int value ) => _internalSet < int > ( id , value ) ;
136
+ public void SetInt ( int id , int value ) => _internalSet < int , PropInt > ( id , value ) ;
112
137
113
- public void SetTexture ( int id , Texture value ) => _internalSet < Texture > ( id , value ) ;
114
- public void SetVector ( int id , Vector4 value ) => _internalSet < Vector4 > ( id , value ) ;
138
+ public void SetTexture ( int id , Texture value ) => _internalSet < Texture , PropTexture > ( id , value ) ;
139
+ public void SetVector ( int id , Vector4 value ) => _internalSet < Vector4 , PropVector > ( id , value ) ;
115
140
116
141
private bool _internalHas < T > ( int id ) => _props . TryGetValue ( id , out var prop ) && prop is Prop < T > ;
117
142
@@ -121,6 +146,7 @@ private void _internalSet<T>(int id, T value)
121
146
public bool HasTexture ( int id ) => _internalHas < Texture > ( id ) ;
122
147
public bool HasVector ( int id ) => _internalHas < Vector4 > ( id ) ;
123
148
149
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
124
150
internal void Write ( int id , MaterialPropertyBlock mpb )
125
151
{
126
152
if ( ! _props . TryGetValue ( id , out var prop ) ) {
@@ -130,13 +156,7 @@ internal void Write(int id, MaterialPropertyBlock mpb)
130
156
MaterialPropertyManager . Instance . LogDebug (
131
157
$ "writing property { PropIdToName . Get ( id ) } = { prop } ") ;
132
158
133
- switch ( prop ) {
134
- case Prop < Color > c : mpb . SetColor ( id , c . Value ) ; break ;
135
- case Prop < float > f : mpb . SetFloat ( id , f . Value ) ; break ;
136
- case Prop < int > i : mpb . SetInt ( id , i . Value ) ; break ;
137
- case Prop < Texture > t : mpb . SetTexture ( id , t . Value ) ; break ;
138
- case Prop < Vector4 > v : mpb . SetVector ( id , v . Value ) ; break ;
139
- }
159
+ prop . Write ( id , mpb ) ;
140
160
}
141
161
142
162
public override string ToString ( )
0 commit comments