Unity 5 unlit shadow cutout cast receive shaders

Unity unlit shaders with shadow and cutout – it can be useful:

Unlit shadow cast

Shader "UnlitShadows/UnlitShadowCast" {
Properties{ _MainTex("Base (RGB)", 2D) = "white" {} }
SubShader{ Pass{ SetTexture[_MainTex] } } FallBack "VertexLit" }

Unlit shadow cast cutout

Shader "UnlitShadows/UnlitShadowCastCutout" {
Properties{ _Color("Main Color", Color) = (1,1,1,1) _MainTex("Base (RGB)", 2D) = "white" {} _Cutoff("Cutout", Range(0,1)) = 0.5 }
SubShader{ Pass{ Alphatest Greater[_Cutoff] SetTexture[_MainTex] } } Fallback "Transparent/Cutout/VertexLit" }

Unlit shadow receive

Shader "UnlitShadows/UnlitShadowReceive" {
Properties{ _MainTex("Base (RGB)", 2D) = "white" {} }
SubShader{ Pass{ SetTexture[_MainTex] } Pass { Blend DstColor Zero Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#include "AutoLight.cginc"
struct v2f {
float4 pos : SV_POSITION; LIGHTING_COORDS(0,1) };
v2f vert(appdata_base v) {
v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); TRANSFER_VERTEX_TO_FRAGMENT(o);
return o; }
fixed4 frag(v2f i) : COLOR{
float attenuation = LIGHT_ATTENUATION(i);
return attenuation;
} ENDCG } } Fallback "VertexLit" }

Unlit shadow receive cutout

Shader "UnlitShadows/UnlitShadowReceive" {
Properties{ _Color("Main Color", Color) = (1,1,1,1) _MainTex("Base (RGB)", 2D) = "white" {}	_Cutoff("Cutout", Range(0,1)) = 0.5 }
SubShader{ Pass{ Alphatest Greater[_Cutoff] SetTexture[_MainTex] }	Pass{ Blend DstColor Zero Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#include "AutoLight.cginc"
struct v2f {
float4 pos : SV_POSITION; LIGHTING_COORDS(0,1) };
v2f vert(appdata_base v) {
v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); TRANSFER_VERTEX_TO_FRAGMENT(o);
return o; }
fixed4 frag(v2f i) : COLOR{
float attenuation = LIGHT_ATTENUATION(i);
return attenuation;
} ENDCG } } Fallback "Transparent/Cutout/VertexLit" } 
Posted in abuse by admin

Spec BRDF ref

////////////////////////
//LD - Light direction//
//VD - View direction //
//N  - Normal         //
//a  - Roughness      //	
////////////////////////
float pi				= 3.14159265358979323;
float a 				= roughness * roughness;
float3 H				= normalize(LD + VD);
float HdotN	  			= dot( N, H );
float VdotH				= dot( VD, H );
float VdotN				= saturate(dot( N, VD ));
float LdotN				= saturate( dot( LD, N ));
float LdotH				= saturate(dot( N, H ));

//Normal Distribution Function//
////////////BECKMANN////////////
float NDF_Beckmann = 1.0 / ( pi * a * a ( pow( NdotH, 4.0f ) ) ) * exp ( ( pow( NdotH, 2.0f) - 1.0f ) / a * a * ( pow( NdotH, 2.0f ) ) );

////////////////////////////////
//////////BLINN_PHONG///////////
float NDF_BlinnPhong = ( 1.0f / ( pi * a * a ) ) * ( pow( NdotH, 2.0 / ( a * a ) - 2.0f ) );

////////////////////////////////
////GGX(TROWBRIDGE-REITZ)///////
float NDF_TrowReitz = ( a * a ) / pi * ( pow( (pow( NdotH, 2.0f ) * ( a * a - 1.0f ) + 1.0f ), 2.0) );


////////////////////////////////
////////////G-TERM//////////////
//////SIMPLE(Implicit)//////////
float G_Simple = LdotN * VdotN;

////////////////////////////////
//////////KELEMEN///////////////
float G_Kelemen = ( LdotN * VdotN ) / ( pow( VdotH, 2.0f ) );

////////////////////////////////
////////////NEUMANN/////////////
float G_Neumann = ( LdotN * VdotN ) / max( LdotN, VdotN );

////////////////////////////////
////////BECKMANN////////////////
float G_Beckmann = 0.0;
float c = VdotN / ( a * sqrt( 1.0f - ( pow( VdotN, 2.0f ) ) ) );
if ( c < 1.6) {
	G_Beckmann = ( 3.535f * c + 2.181 * pow(c, 2.0f ) ) / ( 1.0f + 2.276 * c + 2.577 * pow(c, 2.0f ) );
}
else {
	G_Beckmann = 1.0f;
}

//////////////////////////////// 
////////COOK-TORRANCE///////////
float G_CookTorr = min( 1.0f, min( ( 2.0f * HdotN * VdotH ) / VdotH, ( 2.0f * HdotN * LdotN ) / VdotH ) );

////////////////////////////////
/////////GGX(SMITH-WALTER)////////////
float G_GGXL = ( 2.0f * LdotN ) / ( LdotN + sqrt(a * a + ( 1.0f - a * a ) * ( pow(LdotN, 2.0f ) ) ) );
float G_GGXV = ( 2.0f * VdotN ) / ( VdotN + sqrt(a * a + ( 1.0f - a * a ) * ( pow(VdotN, 2.0f ) ) ) );
float G_GGX_SmithWalter = G_GGXV * G_GGXL; 

////////////////////////////////
/////GGX(SMITH-BECKMANN)////////
float G_GGXL = LdotN / ( a * sqrt(1.0f - ( pow(LdotN, 2.0f ) ) ) );
float G_GGXV = VdotN / ( a * sqrt(1.0f - ( pow(VdotN, 2.0f ) ) ) );
float G_GGX_SmithBeckmann = G_GGXV * G_GGXL; 

////////////////////////////////
/////GGX(SMITH-SCHLICK)/////////
float k = a * sqrt( 2.0f / pi );
float G_GGXL = LdotN / ( LdotN * ( 1.0f - k ) + k );
float G_GGXV = VdotN / ( VdotN * ( 1.0f - k ) + k );
float G_GGX_SmithSchlick = G_GGXV * G_GGXL; 

////////////////////////////////
///////////FRESNEL//////////////
//////////SHLICK////////////////
float F0 = FRESNEL; //Reflectance at normal incidence
float F_Schlick = F0 + ( 1.0f - F0) * pow(1.0f - VdotH, 5.0f );

////////////////////////////////
/////////COOK-TORRANCE//////////
float Eta = ( 1.0f + sqrt(F0 ) ) / ( 1.0f - sqrt(F0 ) );
float c = VdotH;
float g = sqrt(pow(Eta, 2.0f ) + pow(c, 2.0f ) - 1.0f );
float F = 0.5f * (pow( ( g - c ) / ( g + c ) , 2.0f ) * ( 1.0f + (pow( ( ( g + c ) * c - 1.0f ) / ( ( g - c ) * c + 1.0f ), 2.0f ) ) ) );

Different decals depends on smoothmap

When it’s raining we need to have a different drops depends on material. To do this we can use the smoothmap to control surface, more smoother surface we have, more differen drops we will have.

PuddleSmooth1 PuddleSmoothMap

This is the result without smoothmap using. We see the same type drops everywhere.

PuddleSmooth2

Here we set the smoothmap to to determine where surface is rough and where no. Also we need to make a dual texture.

texture

PuddleSmooth3

And here is the finished result, now we can see that on the asphalt we have own drops and on the puddle own.

PuddleSmooth4

puddle2

Notepad ++ problem

Hi guys, I don’t know do you use this free Notepad++ editor or not, but I found it very friendly for me. So, what this all about – you may use your desire style configuration, as an example it will be the C++ here, and you want to auto pick this style for your HLSL shaders, you know that they have typical .ps file extension.

Move to Style Configuration in Settings.

Notepad++_style_configurator

Choose you desire Language, as I pointed above, let it be C++, and add “ps” in User ext. (without quotes). You can add more extensions separated by a space. OK, now save and close.

Notepad++_language_ext

Try to open the .ps file with Notepad++, you should open the file with the chosen style automatically BUT THIS DOESN’T HAPPEN. Why?

The problem is that Notepad++ already have this extension “ps” attached to PostScript files and you need to exclude this var. To do this just open %APPDATA%\Notepad++ location folder, specifically langs.xml.

Notepad++_lang_xml

Find this line name=”postscript” and near you can see the ext=”ps” – this is what we looking for.

You may change here the “ext” property “ps” to other, whatever you need or you can delete the postscript language at all. Select all the text from Language name=”postscript” to close </Language>, delete and save lang.xml file. That’s all, now you can open the .ps files with chosen style.