Skip to content

Commit

Permalink
Added stencil options.
Browse files Browse the repository at this point in the history
Also added options for ZWrite and ColorMask.
  • Loading branch information
AkaiMage authored Mar 19, 2019
1 parent 5d5a645 commit e061029
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Cancerspace.shader
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

[Enum(UnityEngine.Rendering.CullMode)] _CullMode ("Cull Mode", Float) = 0
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Int) = 4
[Enum(Off, 0, On, 1)] _ZWrite ("ZWrite", Int) = 1
_ColorMask ("Color Mask", Int) = 15

_StencilRef ("Ref", Int) = 0
[Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp ("Compare Function", Int) = 8
[Enum(UnityEngine.Rendering.StencilOp)] _StencilPassOp ("Pass Operation", Int) = 0
[Enum(UnityEngine.Rendering.StencilOp)] _StencilFailOp ("Fail Operation", Int) = 0
[Enum(UnityEngine.Rendering.StencilOp)] _StencilZFailOp ("ZFail Operation", Int) = 0
_StencilReadMask ("Read Mask", Int) = 255
_StencilWriteMask ("Write Mask", Int) = 255

_Puffiness ("Puffiness", Float) = 0

Expand Down Expand Up @@ -52,8 +62,20 @@
SubShader {
Tags { "Queue" = "Transparent+3" }

Stencil {
Ref [_StencilRef]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
Comp [_StencilComp]
Pass [_StencilPassOp]
Fail [_StencilFailOp]
ZFail [_StencilZFailOp]
}

Cull [_CullMode]
ZTest [_ZTest]
ZWrite [_ZWrite]
ColorMask [_ColorMask]

GrabPass { "_Garb" }

Expand Down
72 changes: 72 additions & 0 deletions Editor/CancerspaceInspector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using System;
using System.IO;
using System.Text.RegularExpressions;
Expand All @@ -26,6 +27,7 @@ public static class Styles {
public static string overlaySettingsTitle = "Overlay";
public static string screenColorAdjustmentsTitle = "Screen Color Adjustment";
public static string screenTransformTitle = "Screen Transformations";
public static string stencilTitle = "Stencil Testing";
public static string miscSettingsTitle = "Misc";
public static string renderQueueExportTitle = "Custom Render Queue Exporter";
public static string customRenderQueueSliderText = "Custom Render Queue";
Expand All @@ -50,6 +52,16 @@ public CSCategory(string nname, GUIStyle sstyle, CSCategorySetup ssetupDelegate)

protected MaterialProperty cullMode;
protected MaterialProperty zTest;
protected MaterialProperty zWrite;
protected MaterialProperty colorMask;

protected MaterialProperty stencilRef;
protected MaterialProperty stencilComp;
protected MaterialProperty stencilPass;
protected MaterialProperty stencilFail;
protected MaterialProperty stencilZFail;
protected MaterialProperty stencilReadMask;
protected MaterialProperty stencilWriteMask;

protected MaterialProperty puffiness;

Expand Down Expand Up @@ -103,6 +115,16 @@ public void FindProperties(MaterialProperty[] props) {

cullMode = FindProperty("_CullMode", props);
zTest = FindProperty("_ZTest", props);
zWrite = FindProperty("_ZWrite", props);
colorMask = FindProperty("_ColorMask", props);

stencilRef = FindProperty("_StencilRef", props);
stencilComp = FindProperty("_StencilComp", props);
stencilPass = FindProperty("_StencilPassOp", props);
stencilFail = FindProperty("_StencilFailOp", props);
stencilZFail = FindProperty("_StencilZFailOp", props);
stencilReadMask = FindProperty("_StencilReadMask", props);
stencilWriteMask = FindProperty("_StencilWriteMask", props);

puffiness = FindProperty("_Puffiness", props);

Expand Down Expand Up @@ -219,9 +241,21 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
me.ShaderProperty(puffiness, puffiness.displayName);
}),

new CSCategory(Styles.stencilTitle, defaultStyle, me => {
DisplayIntSlider(me, stencilRef, 0, 255);
me.ShaderProperty(stencilComp, stencilComp.displayName);
me.ShaderProperty(stencilPass, stencilPass.displayName);
me.ShaderProperty(stencilFail, stencilFail.displayName);
me.ShaderProperty(stencilZFail, stencilZFail.displayName);
DisplayIntSlider(me, stencilReadMask, 0, 255);
DisplayIntSlider(me, stencilWriteMask, 0, 255);
}),

new CSCategory(Styles.miscSettingsTitle, defaultStyle, me => {
me.ShaderProperty(cullMode, cullMode.displayName);
me.ShaderProperty(zTest, zTest.displayName);
me.ShaderProperty(zWrite, zWrite.displayName);
ShowColorMaskFlags(me, colorMask);
me.ShaderProperty(mirrorReflectionMode, mirrorReflectionMode.displayName);
}),

Expand Down Expand Up @@ -318,4 +352,42 @@ void DisplayVec3Field(MaterialEditor materialEditor, MaterialProperty property)
}
EditorGUI.showMixedValue = false;
}

void DisplayIntField(MaterialEditor materialEditor, MaterialProperty property) {
EditorGUI.showMixedValue = property.hasMixedValue;
int v = (int) property.floatValue;
EditorGUI.BeginChangeCheck();
v = EditorGUILayout.IntField(property.displayName, v);
if (EditorGUI.EndChangeCheck()) {
materialEditor.RegisterPropertyChangeUndo(property.displayName);
property.floatValue = (float) v;
}
EditorGUI.showMixedValue = false;
}

void DisplayIntSlider(MaterialEditor materialEditor, MaterialProperty property, int min, int max) {
EditorGUI.showMixedValue = property.hasMixedValue;
int v = (int) property.floatValue;
EditorGUI.BeginChangeCheck();
v = EditorGUILayout.IntSlider(property.displayName, v, min, max);
if (EditorGUI.EndChangeCheck()) {
materialEditor.RegisterPropertyChangeUndo(property.displayName);
property.floatValue = (float) v;
}
EditorGUI.showMixedValue = false;
}

void ShowColorMaskFlags(MaterialEditor materialEditor, MaterialProperty property) {
EditorGUI.showMixedValue = property.hasMixedValue;
ColorWriteMask v = (ColorWriteMask) ((int) property.floatValue);
EditorGUI.BeginChangeCheck();
v = (ColorWriteMask) EditorGUILayout.EnumFlagsField(property.displayName, v);
if (EditorGUI.EndChangeCheck()) {
materialEditor.RegisterPropertyChangeUndo(property.displayName);
int x = (int) v;
if (x == -1) x = 15;
property.floatValue = (float) x;
}
EditorGUI.showMixedValue = false;
}
}

0 comments on commit e061029

Please sign in to comment.