Skip to content

Commit

Permalink
Implement the unlit shader [closes #11]
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed May 6, 2023
1 parent fda6534 commit 52b5bb2
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 79 deletions.
63 changes: 63 additions & 0 deletions Assets/Samples/RobotLight_Unlit.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: RobotLight_Unlit
m_Shader: {fileID: 4800000, guid: 6b9fe9604cb243afa2ff826745fc8723, type: 3}
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses:
- SHADOWCASTER
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTexture:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _NormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints:
- _ForwardStencilComp: 0
- _ForwardStencilPass: 0
- _ForwardStencilRef: 0
- _ForwardStencilWriteMask: 0
m_Floats:
- _AlphaClipThreshold: 0.5
- _AlphaClipping: 0
- _BlendDst: 1
- _BlendMode: 2
- _BlendSrc: 1
- _OutlinesStencilLayer: 8
- _OverrideRamp: 0
- _OverrideRamp_RimSmoothness: 0.1
- _OverrideRamp_RimThreshold: 0.5
- _OverrideRamp_Smoothness: 0.083
- _OverrideRamp_SpecularSmoothness: 0.005
- _OverrideRamp_SpecularThreshold: 0.995
- _OverrideRamp_Threshold: 0
- _QueueOffset: 0
- _ReceiveBlobShadows: 0
- _RenderFace: 2
- _SurfaceType: 1
- _ZWrite: 0
m_Colors:
- _EmissionColor: {r: 0, g: 0, b: 0, a: 0}
- _MainColor: {r: 4.237095, g: 1.0204521, b: 0.75424725, a: 0.98039216}
- _RimColor: {r: 0, g: 0, b: 0, a: 0}
- _ShadowColor: {r: 0, g: 0, b: 0, a: 0.75}
- _SpecularColor: {r: 0.43527526, g: 0.43527526, b: 0.43527526, a: 1}
m_BuildTextureStacks: []
8 changes: 8 additions & 0 deletions Assets/Samples/RobotLight_Unlit.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions Assets/Samples/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -5169,6 +5169,89 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_radius: 0.5
--- !u!1 &402274048
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 402274049}
- component: {fileID: 402274051}
- component: {fileID: 402274050}
m_Layer: 0
m_Name: Sphere
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &402274049
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 402274048}
m_LocalRotation: {x: -0.279201, y: 0.17167418, z: 0.00782209, w: 0.9447294}
m_LocalPosition: {x: -0.00008, y: 0.00044, z: 0.00552}
m_LocalScale: {x: 0.0025000006, y: 0.0025000006, z: 0.0015180999}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1949102588}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &402274050
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 402274048}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 2067f5d42fef8244692626584e6d14d1, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &402274051
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 402274048}
m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1001 &544974099
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -6249,6 +6332,11 @@ Transform:
m_Father: {fileID: 544974102}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!4 &1949102588 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -3166758660711944165, guid: eaa42215fec37ff42a7fc22c96434a4a, type: 3}
m_PrefabInstance: {fileID: 544974099}
m_PrefabAsset: {fileID: 0}
--- !u!1 &2032681993
GameObject:
m_ObjectHideFlags: 0
Expand Down
6 changes: 3 additions & 3 deletions Assets/Samples/Toon Render Pipeline Asset.asset
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ MonoBehaviour:
ScreenSpace:
Color: {r: 0, g: 0, b: 0, a: 0}
ColorFilter:
Enabled: 1
Enabled: 0
Threshold: 0.35
Smoothness: 0.5
DepthFilter:
Enabled: 1
Threshold: 5.18
Threshold: 0.6
Smoothness: 0.01
NormalsFilter:
Enabled: 1
Threshold: 1.28
Threshold: 3.23
Smoothness: 1
UseFog: 0
MaxDistance: 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,21 @@ namespace DELTation.ToonRP.Editor.ShaderGUI
[UsedImplicitly]
public sealed class ToonRpDefaultShaderGui : ToonRpShaderGuiBase
{
private const string OutlinesStencilLayerPropertyName = "_OutlinesStencilLayer";
private const string ShadowColorPropertyName = "_ShadowColor";
private const string SpecularColorPropertyName = "_SpecularColor";
private const string RimColorPropertyName = "_RimColor";
private const string NormalMapPropertyName = "_NormalMap";

private static readonly int ForwardStencilRefId = Shader.PropertyToID("_ForwardStencilRef");
private static readonly int ForwardStencilWriteMaskId = Shader.PropertyToID("_ForwardStencilWriteMask");
private static readonly int ForwardStencilCompId = Shader.PropertyToID("_ForwardStencilComp");
private static readonly int ForwardStencilPassId = Shader.PropertyToID("_ForwardStencilPass");
private static readonly int ShadowColorId = Shader.PropertyToID(ShadowColorPropertyName);
private static readonly int SpecularColorId = Shader.PropertyToID(SpecularColorPropertyName);
private static readonly int RimColorId = Shader.PropertyToID(RimColorPropertyName);
private static readonly int NormalMapId = Shader.PropertyToID(NormalMapPropertyName);
private static readonly int OutlinesStencilLayerId = Shader.PropertyToID(OutlinesStencilLayerPropertyName);

public override bool OutlinesStencilLayer => true;

protected override void DrawProperties()
{
if (DrawSurfaceProperties())
{
DrawOutlinesStencilLayer();
}
DrawSurfaceProperties();

EditorGUILayout.Space();

Expand Down Expand Up @@ -77,56 +69,12 @@ private void DrawOverrideRamp()
EditorGUI.indentLevel--;
}

private void DrawOutlinesStencilLayer()
{
if (IsCanUseOutlinesStencilLayerMixed())
{
return;
}

EditorGUI.BeginDisabledGroup(!CanUseOutlinesStencilLayer(GetFirstMaterial()));

if (DrawProperty(OutlinesStencilLayerPropertyName))
{
ForEachMaterial(UpdateStencil);
}

EditorGUI.EndDisabledGroup();
}

protected override void OnSetZWrite(bool zWrite)
{
base.OnSetZWrite(zWrite);
ForEachMaterial(UpdateStencil);
}

private void UpdateStencil(Material m)
{
var stencilLayer = (StencilLayer) m.GetFloat(OutlinesStencilLayerId);

var hasOutlinesStencilLayerKeyword = new LocalKeyword(m.shader, "_HAS_OUTLINES_STENCIL_LAYER");
if (stencilLayer != StencilLayer.None && CanUseOutlinesStencilLayer(GetFirstMaterial()))
{
byte reference = stencilLayer.ToReference();
m.SetInteger(ForwardStencilRefId, reference);
m.SetInteger(ForwardStencilWriteMaskId, reference);
m.SetInteger(ForwardStencilCompId, (int) CompareFunction.Always);
m.SetInteger(ForwardStencilPassId, (int) StencilOp.Replace);
m.EnableKeyword(hasOutlinesStencilLayerKeyword);
}
else
{
m.SetInteger(ForwardStencilRefId, 0);
m.SetInteger(ForwardStencilWriteMaskId, 0);
m.SetInteger(ForwardStencilCompId, 0);
m.SetInteger(ForwardStencilPassId, 0);
m.DisableKeyword(hasOutlinesStencilLayerKeyword);
}
}

private static bool CanUseOutlinesStencilLayer(Material m) => IsZWriteOn(m);

private bool IsCanUseOutlinesStencilLayerMixed() => FindProperty(PropertyNames.ZWrite).hasMixedValue;

private void DrawNormalMap()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ namespace DELTation.ToonRP.Editor.ShaderGUI
public abstract class ToonRpShaderGuiBase : UnityEditor.ShaderGUI
{
private const string ShadowCasterPassName = "ShadowCaster";

private const string OutlinesStencilLayerPropertyName = "_OutlinesStencilLayer";
private static readonly Dictionary<string, bool> Foldouts = new();
private static readonly int ForwardStencilRefId = Shader.PropertyToID("_ForwardStencilRef");
private static readonly int ForwardStencilWriteMaskId = Shader.PropertyToID("_ForwardStencilWriteMask");
private static readonly int ForwardStencilCompId = Shader.PropertyToID("_ForwardStencilComp");
private static readonly int ForwardStencilPassId = Shader.PropertyToID("_ForwardStencilPass");
private static readonly int OutlinesStencilLayerId = Shader.PropertyToID(OutlinesStencilLayerPropertyName);
private GUIStyle _headerStyle;
private MaterialEditor _materialEditor;

private MaterialProperty[] Properties { get; set; }

protected Object[] Targets => _materialEditor.targets;

public virtual bool OutlinesStencilLayer => false;

protected void ForEachMaterial(Action<Material> action)
{
foreach (Object target in Targets)
Expand Down Expand Up @@ -196,6 +205,12 @@ protected bool DrawSurfaceProperties()
}

DrawProperty(PropertyNames.RenderFace);

if (OutlinesStencilLayer)
{
DrawOutlinesStencilLayer();
}

return isFoldoutOpen;
}

Expand All @@ -214,6 +229,12 @@ private void SetBlend(UnityBlendMode blendSrc, UnityBlendMode blendDst)
private void SetZWrite(bool zWrite)
{
ForEachMaterial(m => m.SetFloat(PropertyNames.ZWrite, zWrite ? 1.0f : 0.0f));

if (OutlinesStencilLayer)
{
ForEachMaterial(UpdateStencil);
}

OnSetZWrite(zWrite);
}

Expand All @@ -233,5 +254,50 @@ SurfaceType.Opaque when AlphaClippingEnabled() => RenderQueue.AlphaTest,
_ => throw new ArgumentOutOfRangeException(),
};
}

private void DrawOutlinesStencilLayer()
{
if (IsCanUseOutlinesStencilLayerMixed())
{
return;
}

EditorGUI.BeginDisabledGroup(!CanUseOutlinesStencilLayer(GetFirstMaterial()));

if (DrawProperty(OutlinesStencilLayerPropertyName))
{
ForEachMaterial(UpdateStencil);
}

EditorGUI.EndDisabledGroup();
}

protected void UpdateStencil(Material m)
{
var stencilLayer = (StencilLayer) m.GetFloat(OutlinesStencilLayerId);

var hasOutlinesStencilLayerKeyword = new LocalKeyword(m.shader, "_HAS_OUTLINES_STENCIL_LAYER");
if (stencilLayer != StencilLayer.None && CanUseOutlinesStencilLayer(GetFirstMaterial()))
{
byte reference = stencilLayer.ToReference();
m.SetInteger(ForwardStencilRefId, reference);
m.SetInteger(ForwardStencilWriteMaskId, reference);
m.SetInteger(ForwardStencilCompId, (int) CompareFunction.Always);
m.SetInteger(ForwardStencilPassId, (int) StencilOp.Replace);
m.EnableKeyword(hasOutlinesStencilLayerKeyword);
}
else
{
m.SetInteger(ForwardStencilRefId, 0);
m.SetInteger(ForwardStencilWriteMaskId, 0);
m.SetInteger(ForwardStencilCompId, 0);
m.SetInteger(ForwardStencilPassId, 0);
m.DisableKeyword(hasOutlinesStencilLayerKeyword);
}
}

private static bool CanUseOutlinesStencilLayer(Material m) => IsZWriteOn(m);

private bool IsCanUseOutlinesStencilLayerMixed() => FindProperty(PropertyNames.ZWrite).hasMixedValue;
}
}
Loading

0 comments on commit 52b5bb2

Please sign in to comment.