具有自定义着色器的对象在光照贴图时投射灰度阴影

时间:2015-09-01 09:12:40

标签: unity3d unity3d-5

Unity 5似乎能够从标准着色器和传统着色器计算彩色阴影。我该怎么做才能使我的自定义着色器投射彩色阴影?

Lightmapping results

我的着色器代码:

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
    } 
}
}

1 个答案:

答案 0 :(得分:0)

解决方案是将UsePass "Mobile/Diffuse/META"添加到光照贴图着色器的所有LOD

以下是我的着色器的修复版本:

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"
}
}

感谢Michael Steliaros的帮助。