Version: 2021.2
光照贴图:技术信息
光照贴图 UV

光照贴图和着色器 Meta Pass

A GameObject must use a Material that has a Meta Pass in its Shader in order to receive or contribute to lighting calculated by the Progressive Lightmapper or Enlighten Baked or Realtime Global Illumination.

A Meta Pass is a Shader pass that provides albedo and emission values to the Global Illumination system. These values are separate from those used in real-time rendering, meaning that you can use the Meta Pass to control how a GameObject looks from the point of view of the lighting baking system without affecting its appearance at runtime.

An example of when this would be useful is if you wanted the green moss on a cliff to generate exaggerated green indirect light in your lightmaps, but you didn’t want to recolor the terrain in the real-time pass of shader.

Unity 的所有内置材质都具有 Meta Pass,并且标准着色器也包含 Meta Pass。如果使用了它们,则无需执行任何操作便会启用 Meta Pass。如果使用的是自定义着色器,您可以添加自己的 Meta Pass。

具有 Meta pass 的示例着色器

通过下面的着色器,可以指定仅由光照烘焙系统使用的反照率颜色和反照率纹理,而不会影响运行时的材质外观。在这个例子中,发光是从 UV 中获取的;但是可以使用任何值来控制发光。

Shader "Custom/metaPassShader"{

    Properties {
        _Color ("Color", Color)=(1,1,1,1)
        _MainTex ("Albedo (RGB)",2D)="white"{}
        _Glossiness ("Smoothness", Range(0,1))=0.5
        _Metallic ("Metallic", Range(0,1))=0.0

        _GIAlbedoColor ("Color Albedo (GI)", Color)=(1,1,1,1)
        _GIAlbedoTex ("Albedo (GI)",2D)="white"{}
    }

    SubShader {
    // ------------------------------------------------------------------
    // Extracts information for lightmapping, GI (emission, albedo, ...)
    // This pass is not used during regular rendering.
        Pass
        {
            Name "META"
            Tags {"LightMode"="Meta"}
            Cull Off
            CGPROGRAM

            #include"UnityStandardMeta.cginc"

            sampler2D _GIAlbedoTex;
            fixed4 _GIAlbedoColor;
            float4 frag_meta2 (v2f_meta i): SV_Target
            {
                // We're interested in diffuse & specular colors
                // and surface roughness to produce final albedo.

                FragmentCommonData data = UNITY_SETUP_BRDF_INPUT (i.uv);
                UnityMetaInput o;
                UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o);
                fixed4 c = tex2D (_GIAlbedoTex, i.uv);
                o.Albedo = fixed3(c.rgb * _GIAlbedoColor.rgb);
                o.Emission = Emission(i.uv.xy);
                return UnityMetaFragment(o);
            }

            #pragma vertex vert_meta
            #pragma fragment frag_meta2
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP
            #pragma shader_feature ___ _DETAIL_MULX2
            ENDCG
        }

        Tags {"RenderType"="Opaque"}
        LOD 200

        CGPROGRAM
        // Physically-based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows nometa
        // Use Shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN,inout SurfaceOutputStandard o){
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex)* _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }

    FallBack "Diffuse"
}

技术信息

Enlighten Realtime Global Illumination and lightmapping use Unity’s Meta Pass to extract albedo values from surfaces and handle diffuse transport themselves by using surface albedo on each bounce.

Metallic surfaces with a black (or almost black) albedo bounce very little light diffusely. Because the lightmappers handle only diffuse light transport, this means that you may see very little bounced light from these types of surfaces. Unity’s built-in Meta Passes account for this by providing a boosted version of the metal hue color instead of a physically correct albedo. This means that you get some bounce even from metallic materials. If you want a different behaviour, you can create a custom meta pass.

The built-in Meta Passes do not handle spectral specular reflectance.

Note: If you are using Enlighten Realtime Global Illumination, the Meta pass in the Player is not as fast as DynamicGI.SetEmissive, but it is more flexible because you are not limited to a single color.

光照贴图:技术信息
光照贴图 UV