Skip to content

Instantly share code, notes, and snippets.

@ipud2
Forked from smkplus/UnityShaderCheatSheet.md
Created January 3, 2018 03:47
Show Gist options
  • Save ipud2/10b8a2a88c08f3d6ce55eaef66452913 to your computer and use it in GitHub Desktop.
Save ipud2/10b8a2a88c08f3d6ce55eaef66452913 to your computer and use it in GitHub Desktop.
Controlling fixed function states from materials/scripts in Unity

16999105_467532653370479_4085466863356780898_n

Shader "MaterialPropertyDrawer"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
 
[HideInInspector] _MainTex2("Hide Texture", 2D) = "white" {}
 
[NoScaleOffset] _MainTex3("No Scale/Offset Texture", 2D) = "white" {}
 
[PerRendererData] _MainTex4("PerRenderer Texture", 2D) = "white" {}
 
[Normal] _MainTex5("Normal Texture", 2D) = "white" {}
 
_Color("Color", Color) = (1,0,0,1)
 
[HDR] _HDRColor("HDR Color", Color) = (1,0,0,1)
 
_Vector("Vector", Vector) = (0,0,0,0)
 
//Can't go below zero
[Gamma] _GVector("Gamma Vector", Vector) = (0,0,0,0)
 
// Header creates a header text before the shader property.
[Header(A group of things)]
 
// Will set "_INVERT_ON" shader keyword when set
[Toggle] _Invert("Auto keyword toggle", Float) = 0
 
// Will set "ENABLE_FANCY" shader keyword when set.
[Toggle(ENABLE_FANCY)] _Fancy("Keyword toggle", Float) = 0
 
// Will show when ENABLE_FANCY is true //Feature request
//[ShowIf(ENABLE_FANCY)] _ShowIf("Show If", Float) = 0
 
// Blend mode values
[Enum(UnityEngine.Rendering.BlendMode)] _Blend("Blend mode Enum", Float) = 1
 
// A subset of blend mode values, just "One" (value 1) and "SrcAlpha" (value 5).
[Enum(One,1,SrcAlpha,5)] _Blend2("Blend mode subset", Float) = 1
 
// Each option will set _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY shader keywords.
[KeywordEnum(None, Add, Multiply)] _Overlay("Keyword Enum", Float) = 0
// ...later on in CGPROGRAM code:
//#pragma multi_compile _OVERLAY_NONE, _OVERLAY_ADD, _OVERLAY_MULTIPLY
// ...
 
// A slider with 3.0 response curve
[PowerSlider(3.0)] _Shininess("Power Slider", Range(0.01, 1)) = 0.08
 
// An integer slider for specified range (0 to 255)
[IntRange] _Alpha("Int Range", Range(0, 255)) = 100
 
// Default small amount of space.
[Space] _Prop1("Small amount of space", Float) = 0
 
// Large amount of space.
[Space(50)] _Prop2("Large amount of space", Float) = 0
 
}

https://cmwdexint.com/2017/05/06/materialpropertydrawer-in-shader-gui-without-creating-shadergui/

Controlling fixed function states from materials


Shader "Smkgames/Sprites"
{
    Properties
    {
        [Header(Main Color)]
        [Toggle] _UseColor("Enabled?", Float) = 1
        _Color("Main Color", Color) = (1,1,1,1)
        [Space(5)]

        [Header(Base(RGB))]
        [Toggle] _UseMainTex("Enabled?", Float) = 1
        _MainTex("Base (RGB)", 2D) = "white" {}
		//[NoScaleOffset] _MainTex("Base (RGB)", 2D) = "white" {}
        [Space(5)]

        [Header(Blend State)]
        [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("SrcBlend", Float) = 1 //"One"
        [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("DestBlend", Float) = 0 //"Zero"
        [Space(5)]

        [Header(Other)]
        [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 2 //"Back"
        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4 //"LessEqual"
        [Enum(Off,0,On,1)] _ZWrite("ZWrite", Float) = 1.0 //"On"
        [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("ColorWriteMask", Float) = 15 //"All"
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 100
        Blend[_SrcBlend][_DstBlend]
        ZTest[_ZTest]
        ZWrite[_ZWrite]
        Cull[_Cull]
        ColorMask[_ColorWriteMask]
Pass
{

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

struct appdata_t
{
float4 vertex   : POSITION;
float4 color    : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
half2 texcoord  : TEXCOORD0;
float4 vertex   : SV_POSITION;
fixed4 color    : COLOR;
};

sampler2D _MainTex;
fixed4 _Color;
float _Speed;
float _UseColor;
float _UseMainTex;


v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color;
return OUT;
}

float4 frag (v2f i) : COLOR
{

float2 uv = i.texcoord.xy;
    float4 tex = lerp(float4(1,1,1,1),tex2D(_MainTex, uv)*i.color,_UseMainTex);

return lerp(tex,tex*_Color,_UseColor);
}
ENDCG
}
}
Fallback "Sprites/Default"
}

//Culling
//https://gist.github.com/aras-p/b2a0952161cb0c2b2cc0#file-foo-md

//Blending

//			// Normal
//			Blend SrcAlpha OneMinusSrcAlpha
//			
//			// Soft Additive
//			Blend OneMinusDstColor One
//			
//			// Multiply
//			Blend DstColor Zero
//			
//			// 2x Multiply
//			Blend DstColor SrcColor
//			
//			// Darken
//			BlendOp Min
//			Blend One One	// When using Min operation, these factors are ignored
//			
//			//  Lighten
//			BlendOp Max
//			Blend One One // When using Max operation, these factors are ignored
//			
//			// Screen
//			Blend OneMinusDstColor One
			// Or
//			Blend One OneMinusSrcColor
//			
//			// Linear Dodge
//			Blend One One

multi_compile

https://qiita.com/Es_Program/items/79edf9f8fca786b365aa

https://qiita.com/r-ngtm/items/7ec3e3a9dfcc752cd02c

Unity Material Property Drawer

https://tedsieblog.wordpress.com/2017/03/02/unity-material-property-drawer/

https://qiita.com/r-ngtm/items/7ec3e3a9dfcc752cd02c

Shade More Efficiently

normaltoggleinspector2

http://www.martinpalko.com/muli-compile-unity/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment