Skip to content

Instantly share code, notes, and snippets.

@aras-p
Created December 9, 2015 14:08
Show Gist options
  • Save aras-p/c0767dc7cb013eea109c to your computer and use it in GitHub Desktop.
Save aras-p/c0767dc7cb013eea109c to your computer and use it in GitHub Desktop.
Shadows and Attenuation Only Shader
Shader "Custom/OnlyShadowsAndAtten"
{
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
// Forward rendering base (main directional light) pass.
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Want regular shader variants for ForwardBase pass, but don't care about
// lightmaps, dynamic GI etc. Just shadows/no-shadows
#pragma multi_compile_fwdbase nolightmap nodynlightmap novertexlight
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
struct v2f
{
float4 pos : SV_POSITION;
SHADOW_COORDS(0) // shadow parameters to pass from vertex
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
return o;
}
fixed4 frag (v2f IN) : SV_Target
{
// will put attenuation+shadows into "atten" variable
// world position not needed in directional lights case, pass zero
UNITY_LIGHT_ATTENUATION(atten, IN, 0)
fixed4 c = atten;
// might want to take light color into account?
// otherwise with no directional light at all, for example, everything will be lit
// (since ForwardBase will still be rendered, just with black light color)
c.rgb *= _LightColor0.rgb;
return c;
}
ENDCG
}
// Forward additive pass (only needed if you care about more lights than 1 directional).
// Can remove if no point/spot light support needed.
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardAdd" }
ZWrite Off Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// Include shadowing support for point/spot
#pragma multi_compile_fwdadd_fullshadows
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
SHADOW_COORDS(1)
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.worldPos = mul(_Object2World, v.vertex).xyz;
TRANSFER_SHADOW(o); // pass shadow coordinates to pixel shader
return o;
}
fixed4 frag (v2f IN) : SV_Target
{
UNITY_LIGHT_ATTENUATION(atten, IN, IN.worldPos)
fixed4 c = atten;
// might want to take light color into account?
c.rgb *= _LightColor0.rgb;
return c;
}
ENDCG
}
// Support for casting shadows from this shader. Remove if not needed.
UsePass "VertexLit/SHADOWCASTER"
}
}
@panaceadev
Copy link

It worked correctly on Unity2017.3.0f3 even I think it would work okay on other Unity versions. Great work!

@OtakuAndFitness
Copy link

Baed on the source code from here , atten is fixed type. Why you convert it to fixed4?

@aras-p
Copy link
Author

aras-p commented Dec 14, 2020

Baed on the source code from here , atten is fixed type. Why you convert it to fixed4?

The pixel shader needs to return a 4-component value (RGBA), here the shader initializes all the four components with atten

@OtakuAndFitness
Copy link

OtakuAndFitness commented Dec 14, 2020

Baed on the source code from here , atten is fixed type. Why you convert it to fixed4?

The pixel shader needs to return a 4-component value (RGBA), here the shader initializes all the four components with atten

OK. I'm used to writing like this😊

fixed4 c = _LightColor0 * atten;
return c;

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