Can anyone tell me how to write an Unlit Lightmapped Shader which can receive shadows? I tried writing a surface shader with a custom lighting model (for being unlit) but it didn't receive or cast any shadows. Please help me. Code (csharp): Shader "Custom/Lightmapped/Unlit" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _LightMap ("Lightmap (RGB)", 2D) = "black" {} } SubShader { LOD 300 Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf None half4 LightingNone (SurfaceOutput s, half3 lightDir, half atten) { half4 c; c.rgb = s.Albedo; c.a = s.Alpha; return c; } struct Input { float2 uv_MainTex; float2 uv2_LightMap; }; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _LightMap; fixed4 _Color; void surf (Input IN, inout SurfaceOutput o) { half4 c; o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb; half4 lm = tex2D (_LightMap, IN.uv2_LightMap); o.Albedo *= lm.rgb; o.Alpha = lm.a; } ENDCG } FallBack "Legacy Shaders/Lightmapped/Diffuse" }
This might sound facetious, but shadows are complementary to lighting. If you're talking about Unity's projected shadows specifically, all they do is prevent specific lights from affecting specific parts of an object. If something is unlit, where do you expect shadows to come from?
oh, I thought the shadows projected by Unity3d darkened the specific areas on the object. So I was expecting it to work on unlit objects as well. Thanks for clearing that out.
Daniel, you could still just make a lightning function of the surface shader, where the light itself is not taken into consideration (and no dot operations on normals/lightdirs and such), but only the "atten" feed is multiplied on. So it's still a lit shader, just not using the light. Here's something to get you going (The atten parameter is what contains the shadow info, so it is just being multiplied on the color output of the lightning function) You can play around with how to scale this atten property (multiply it by 2, or stretch its range, whatever ): Shader "Custom/Lightmapped/Unlit" { Code (csharp): Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _LightMap ("Lightmap (RGB)", 2D) = "black" {} } SubShader { LOD 300 Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf None half4 LightingNone (SurfaceOutput s, half3 lightDir, half atten) { half4 c; c.rgb = s.Albedo*atten; c.a = s.Alpha; return c; } struct Input { float2 uv_MainTex; float2 uv2_LightMap; }; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _LightMap; fixed4 _Color; void surf (Input IN, inout SurfaceOutput o) { half4 c; o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb*_Color; half4 lm = tex2D (_LightMap, IN.uv2_LightMap); o.Albedo *= lm.rgb; o.Alpha = lm.a; } ENDCG } FallBack "Legacy Shaders/Lightmapped/Diffuse" } EDIT: Just remember that the atten (atten stands for "attenuation") parameter also contains the fallof of lights (spots and points), so you will have more than just shadows in this, so use it carefully to get the result you want
Thanks a lot Kragh. It works properly for me since I'm using only a directional light as its an outdoor scene.
Hi, I'm using it now, I love it, but I'd like the albedo to use its alpha channel, and I can't make it work! Thanks!