Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of how to make your shader accessible in C#.
using UnityEngine;
/** This class communicates directly with the shader to draw grids. The shader's
properties look like this:
Shader "SDF/Grid"{
Properties{
[KeywordEnum(Grid, Cross, Ticks)]
_GridKind ("Kind", Int) = 0
_InsideColor("Inside Color", Color) = (.5, 0, 0, 1)
_OutsideColor("Outside Color", Color) = (0, .5, 0, 1)
_LineThickness("Major Line Thickness", Range(0, 0.1)) = 0.05
// _LineDistance("Major Line Distance", Range(0, 1)) = 0.1
_SubLineThickness("Proportion thickness of in between lines", Range(0, 1)) = 0.25
[IntRange] _SubLines("Number of lines between major lines", Range(0, 9)) = 1
_Offset("Offset", Vector) = (0, 0, 0, 0)
_LineDistance("Major Line Distance", Vector) = (1, 1, 0.05, 0.05)
}
*/
public class GridShader {
public enum Kind {
Grid = 0,
Cross,
Ticks
}
public class PropertyID {
public int gridKind;
public int insideColor;
public int outsideColor;
public int lineThickness;
public int lineDistance;
public int subLineThickness;
public int subLines;
public int offset;
}
/** We only need to get the properties once. */
private static PropertyID _propertyID;
public static PropertyID propertyID {
get {
if (_propertyID == null) {
_propertyID = new PropertyID {
gridKind = Shader.PropertyToID("_GridKind"),
insideColor = Shader.PropertyToID("_InsideColor"),
outsideColor = Shader.PropertyToID("_OutsideColor"),
lineThickness = Shader.PropertyToID("_LineThickness"),
lineDistance = Shader.PropertyToID("_LineDistance"),
subLineThickness = Shader.PropertyToID("_SubLineThickness"),
subLines = Shader.PropertyToID("_SubLines"),
offset = Shader.PropertyToID("_Offset")
};
}
return _propertyID;
}
}
// protected MaterialPropertyBlock material;
// public GridShader(MaterialPropertyBlock material) {
// this.material = material;
// }
protected Material material;
public GridShader(Material material) {
this.material = material;
}
public Color insideColor {
get => material.GetColor(propertyID.insideColor);
set => material.SetColor(propertyID.insideColor, value);
}
public Color outsideColor {
get => material.GetColor(propertyID.outsideColor);
set => material.SetColor(propertyID.outsideColor, value);
}
public float lineThickness {
get => material.GetFloat(propertyID.lineThickness);
set => material.SetFloat(propertyID.lineThickness, value);
}
public Vector4 lineDistance {
get => material.GetVector(propertyID.lineDistance);
set => material.SetVector(propertyID.lineDistance, value);
}
public float subLineThickness {
get => material.GetFloat(propertyID.subLineThickness);
set => material.SetFloat(propertyID.subLineThickness, value);
}
public int subLines {
get => material.GetInt(propertyID.subLines);
set => material.SetInt(propertyID.subLines, value);
}
public Vector4 offset {
get => material.GetVector(propertyID.offset);
set => material.SetVector(propertyID.offset, value);
}
// public static Kind GetKind(Material material)
// => (Kind) material.GetInt("_GridKind");
// public static void SetKind(Material material, Kind value) {
// EnableKeyword(material, "_GRIDKIND_GRID", value == Kind.Grid);
// EnableKeyword(material, "_GRIDKIND_CROSS", value == Kind.Cross);
// EnableKeyword(material, "_GRIDKIND_TICKS", value == Kind.Ticks);
// }
/** Enable or disable a shader keyword based on a boolean value. */
static void EnableKeyword(/* this */ Material material, string keyword, bool enable) {
if (enable)
material.EnableKeyword(keyword);
else
material.DisableKeyword(keyword);
}
// Would like to do this but can't do it with a material block.
public Kind kind {
get => (Kind) material.GetInt("_GridKind");
set {
EnableKeyword(material, "_GRIDKIND_GRID", value == Kind.Grid);
EnableKeyword(material, "_GRIDKIND_CROSS", value == Kind.Cross);
EnableKeyword(material, "_GRIDKIND_TICKS", value == Kind.Ticks);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment