Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active October 22, 2024 12:19
Show Gist options
  • Save smkplus/2a5899bf415e2b6bf6a59726bb1ae2ec to your computer and use it in GitHub Desktop.
Save smkplus/2a5899bf415e2b6bf6a59726bb1ae2ec to your computer and use it in GitHub Desktop.
Controlling fixed function states from materials/scripts in Unity
Shader "Smkgames/Sprite"
{
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" "PerformanceChecks" = "False" }
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/TJHeuvel/c4f8d218a0d774682560a8f348a90dff#comments
//Blending Description
// // 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
@anaanook
Copy link

anaanook commented Nov 7, 2019

hey I just want to say this is exactly what I needed and I love you for putting that helpful graphic together

@smkplus
Copy link
Author

smkplus commented Nov 7, 2019

@anaanook
you're welcome ❤️

@WooshiiDev
Copy link

Thank you so much for making this, fantastic cheat sheet! 👍

@smkplus
Copy link
Author

smkplus commented Sep 2, 2020

Thank you so much for making this, fantastic cheat sheet! 👍

It's my pleasure

@FleshMobProductions
Copy link

This is a great resource, I frequently come back to this! Thanks.
I just noticed your feature request for [ShowIf] and remembered that I worked on something like this a while ago. It works but the performance is likely not optimal:
https://gist.github.com/FleshMobProductions/72cb17da6a9d03cf7aaa83940e26b751

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