1

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

光照贴图结果

我的着色器代码:

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

1 回答 1

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 的帮助。

于 2015-09-17T13:24:56.897 回答