- Home /
Getting Light Attenuation in shaders
I'm currently porting our Shaders from unity 2.6 to 3.3 and I have a question for the esteemed unity graphics gurus.
I did a "Unlit with shadows" shader by hacking the per-pixel light pass in the normal-diffuse shader.
The result is an unlit texture that still "receives" shadows by directly blending the shadow map info.
Here is the base 2.6 shader, I have multiple variants for alpha and lightmapped sources
Shader "Unlit/Shadows" {
Properties {
_ShadowIntensity ("Shadow Intensity", Range (0, 1)) = 0.6
_MainTex ("Base (RGB)", 2D) = "white" {}
}
Category { Tags { "RenderType"="Opaque" } LOD 200 Blend Off
// ------------------------------------------------------------------
// ARB fragment program
SubShader {
// Ambient pass
Pass {
Name "BASE"
Tags {"LightMode" = "PixelOrNone"}
SetTexture [_MainTex]
}
Pass {
Name "BASE"
Tags {"LightMode" = "Vertex"}
SetTexture [_MainTex]
}
// Pixel lights
Pass {
Blend SrcAlpha OneMinusSrcAlpha
Name "PPL"
Tags { "LightMode" = "Pixel" }
CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_builtin #pragma fragmentoption ARB_fog_exp2 #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" #include "AutoLight.cginc"
struct v2f { V2F_POS_FOG; LIGHTING_COORDS float2 uv; float3 normal; float3 lightDir; };
uniform float4 _MainTex_ST;
v2f vert (appdata_base v) { v2f o; PositionFog( v.vertex, o.pos, o.fog ); o.normal = v.normal; o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); o.lightDir = ObjSpaceLightDir( v.vertex ); TRANSFER_VERTEX_TO_FRAGMENT(o); return o; }
uniform sampler2D _MainTex; uniform float _ShadowIntensity;
float4 frag (v2f i) : COLOR { float atten = LIGHT_ATTENUATION(i);
half4 c;
c.rgb = 0;
c.a = (1-atten) * _ShadowIntensity;
return c;
} ENDCG } }
}
Fallback "Unlit/Simple"
}
When porting to 3.3 unity comments the whole per-pixel pass, how can I implement this in the new shaderlab? The Surface shader pass might seem overkill since I don't think I need the normals and such.
I'll also glady accept any info on how to improve this shader :) Feel free to copy/hack/use it as you wish
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613133007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shadow Support in Custom Shader 0 Answers
How would I grab the lighting pass in a post process shader for deferred lighting? 0 Answers
Pass ShaderLab properties to Standard cginc 2 Answers
Shader compiler: internal error compiling shader snippet type=0 platform=0: 1 Answer
[Unity3] How can I fix this shader 'texture' syntax error under 3.0? 1 Answer