Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Unity 5 seems to be able to calculate colored shadows from objects mapped with standart shader and legacy shaders.

What should I do to make my custom shader to cast colored shadows as well?

enter image description here

share|improve this question
1  
I think the red glow you see here is not a colored shadow but light emitted in form of Emission. But I don't know enough about shader programming to tell you how to implement it yourself. –  Philipp Sep 7 at 9:05
1  
The red glow is indirect lighting baked by Unity while calculating lightmap. Unity must somehow capture color of a surface before bouncing photon off of it. And I have no idea why it can capture color of surface shaded by legacy diffuse shader, but unable to do so with my own custom diffuse shader. –  game development germ Sep 7 at 9:23
    
This would be a good question on the Computer Graphics SE –  Blue Sep 15 at 10:25
    
Thanks, I'll post it there too. –  game development germ Sep 17 at 7:11

1 Answer 1

up vote 0 down vote accepted

The solution is to add UsePass "Mobile/Diffuse/META" to all LODs of lightmapped shaders.

Here is fixed version of my shader:

Shader "Opaque/Lightmap"  
{
Properties 
{
    _MainTex ("Base (RGB)", 2D)     = "white" {}
}

SubShader 
{
    Tags { "Queue" = "Geometry" "RenderType" = "Opaque"}
    LOD 100

    Pass
    {   
        Name "FORWARD"
        Tags { "LIGHTMODE"="ForwardBase" "SHADOWSUPPORT"="false" "RenderType"="Opaque" }

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma fragmentoption ARB_precision_hint_fastest
        #pragma exclude_renderers d3d11_9x d3d11
        #pragma multi_compile_fog
        #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON

        #include "UnityCG.cginc"

        sampler2D   _MainTex;

        struct appdata 
        {
            float4 vertex : POSITION;
            half2 texcoord   : TEXCOORD0;
            half2 texcoord1  : TEXCOORD1;
        };

        struct v2f {
            float4 pos : SV_POSITION;
            half2 uv : TEXCOORD0;
            half2 lightmapuv : TEXCOORD1;
            UNITY_FOG_COORDS(2)
        };

        v2f vert (appdata v) 
        {
            v2f o;
            o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
            o.uv = v.texcoord;
            o.lightmapuv = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;

            UNITY_TRANSFER_FOG(o,o.pos);
            return o;
        }

        fixed4 frag (v2f i) : COLOR
        {
            fixed4 color = tex2D(_MainTex, i.uv);

            #ifndef LIGHTMAP_OFF
            fixed3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightmapuv));
            color.rgb *= lightMap;
            #endif

            UNITY_APPLY_FOG(i.fogCoord, color);
            return color;
        }
        ENDCG
    } 

    // to fix diffuse component bounce in the lightmaps
    UsePass "Mobile/Diffuse/META"
}
}

Thanks to Michael Steliaros for help.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.