I have made the following code:
public class AppearanceDefinition<VertexInfo> where VertexInfo : struct
{
public readonly ShaderProperty<bool> HasBorder; // READONLY VARIABLE
public readonly ShaderProperty<float> BorderSize;
internal readonly Shader[] Shaders;
private readonly string VertexShaderCode;
private readonly string FragmentShaderCode;
public AppearanceDefinition(string vertexCode, string fragmentCode)
{
this.VertexShaderCode = vertexCode;
this.FragmentShaderCode = fragmentCode;
HasBorder = new ShaderProperty<bool>("HasBorder", ResourceKind.UniformBuffer, ShaderStages.Fragment, 0);
BorderSize = new ShaderProperty<float>("BorderSize", ResourceKind.UniformBuffer, ShaderStages.Fragment, 0);
}
}
public class ShaderProperty<T> where T : struct
{
public readonly string Name;
public readonly ResourceKind InternalDataType;
public readonly ShaderStages ShaderStage;
public readonly uint GroupId;
private T _value;
public T Value
{
get
{
return _value;
}
set
{
_value = value;
// requires buffer update.
}
}
public ShaderProperty(string name, ResourceKind internalDataType, ShaderStages stage, uint group)
{
this.Name = name;
this.InternalDataType = internalDataType;
this.ShaderStage = stage;
this.GroupId = group;
this._value = default(T);
}
}
And I use it like so:
class Program
{
static AppearanceDefinition<bool> test;
static void Main(string[] args)
{
test = new AppearanceDefinition<bool>("", "");
test.HasBorder.Value = true;
}
}
This works just fine, the value of HasBorder
changes from false
to true
.
This is great since I don't want people to re-assign the value HasBorder
but I do want people to change the value of HasBorder.Value
. But it feels weird that I in a way can change a readonly variable.
Could some one explain me why this is possible and if there is maybe a better way of doing this?