欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity Custom PBR材质

程序员文章站 2022-06-10 23:26:55
...

自定义 PBR(MetallicBased) 材质

说明:

    带有法线贴图,带有metallic based PBR map。基于物理渲染,可完善的使用mixedlighting。
包含三张贴图

Albedo:

  • RGBA

Materialmap:

  • R=预留
  • G=roughness
  • B=预留
  • A=metallic

NormalMap:

  • RGB=NormalMap

代码块:

物理材质:

Shader "WH40K/Static/Static_Metal_200" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.7
        _MaterialMap ("Material", 2D) = "white" {}
        _BumpMap ("NormalMap", 2D) ="bump"{}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _MaterialMap;
        sampler2D _BumpMap;

        struct Input {
            float2 uv_MainTex;
            float2 uv_MaterialMap;
            float2 uv_BumpMap;
        };


        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_CBUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_CBUFFER_END

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            fixed4 m = tex2D (_MaterialMap, IN.uv_MaterialMap);
            o.Albedo = c.rgb;
            o.Smoothness = (1-m.g)*_Glossiness;
            o.Metallic = m.a;
            o.Alpha = c.a;
            o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));

        }
        ENDCG
    }
    FallBack "Diffuse"
}

非物理材质

Shader "WH40K/Static/Static_Metal_100" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.7
        _MaterialMap ("Material", 2D) = "white" {}
        _BumpMap ("NormalMap", 2D) ="bump"{}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 100

        CGPROGRAM
        #pragma surface surf WH40KBlinnPhong exclude_path:prepass  noforwardadd halfasview interpolateview
        //定义变量
        sampler2D _MainTex;
        sampler2D _MaterialMap;
        sampler2D _BumpMap;
        fixed _Glossiness;
        fixed _Gloss;
        half _Specular;
        fixed4 _Color;
        //定义光照模型
        inline fixed4 WH40KBlinnPhongLight (SurfaceOutput s, half3 viewDir, UnityLight light)
        {
            half3 h = normalize (light.dir + viewDir);

            fixed diff = max (0, dot (s.Normal, light.dir));

            float nh = max (0, dot (s.Normal, h));
            float spec = pow (nh, s.Specular*128.0) * s.Gloss;

            fixed4 c;
            //加0.1来对冲没有环境光的色差
            c.rgb = s.Albedo * light.color * diff + light.color  * spec+0.1;
            c.a = s.Alpha;

            return c;
        }

        inline fixed4 LightingWH40KBlinnPhong (SurfaceOutput s, half3 viewDir, UnityGI gi)
        {
            fixed4 c;
            c = WH40KBlinnPhongLight (s, viewDir, gi.light);

            #if defined(DIRLIGHTMAP_SEPARATE)
                #ifdef LIGHTMAP_ON
                    c += WH40KBlinnPhongLight (s, viewDir, gi.light2);
                #endif
                #ifdef DYNAMICLIGHTMAP_ON
                    c += WH40KBlinnPhongLight (s, viewDir, gi.light3);
                #endif
            #endif

            #ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT
                c.rgb += s.Albedo * gi.indirect.diffuse;
            #endif

            return c;
        }
        inline void LightingWH40KBlinnPhong_GI (
            SurfaceOutput s,
            UnityGIInput data,
            inout UnityGI gi)
        {
            gi = UnityGlobalIllumination (data, 1.0, s.Normal);
        }

        inline fixed4 LightingWH40KBlinnPhong_PrePass (SurfaceOutput s, half4 light)
        {
            fixed spec = light.a * s.Gloss;

            fixed4 c;
            c.rgb = (s.Albedo * light.rgb + light.rgb * _SpecColor.rgb * spec);
            c.a = s.Alpha;
            return c;
        }




        // Use shader model 3.0 target, to get nicer looking lighting
        //#pragma target 3.0

        //定义 Input 结构体
        struct Input {
            float2 uv_MainTex;
            float2 uv_BumpMap;
        };

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_CBUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_CBUFFER_END

        void surf (Input IN, inout SurfaceOutput o) {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            fixed4 m = tex2D (_MaterialMap, IN.uv_MainTex);
            o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
            o.Specular = m.g;
            o.Gloss = (1-m.g)*_Glossiness;
            o.Alpha = c.a;
            o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
        }
        ENDCG
    }
    FallBack "Diffuse"
}
相关标签: unity shader