{"id":1376,"date":"2020-03-14T00:53:32","date_gmt":"2020-03-13T16:53:32","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=1376"},"modified":"2020-08-27T11:33:25","modified_gmt":"2020-08-27T03:33:25","slug":"ms%e7%9a%84pbr%e9%ab%98%e6%95%88%e7%ae%80%e9%99%8b%e5%ae%9e%e7%8e%b0%e7%89%88","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=1376","title":{"rendered":"MS\u7684PBR\u9ad8\u6548\u7b80\u964b\u5b9e\u73b0\u7248"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>Shader \"MSPBRStandard\"\n{\n    Properties\n    {\n        \/\/ Main maps.\n        _Color(\"Color\", Color) = (1.0, 1.0, 1.0, 1.0)\n        _MainTex(\"Albedo\", 2D) = \"white\" {}\n        _Cutoff(\"Alpha Cutoff\", Range(0.0, 1.0)) = 0.5\n        _Metallic(\"Metallic\", Range(0.0, 1.0)) = 0.0\n        _Smoothness(\"Smoothness\", Range(0.0, 1.0)) = 0.5\n        [Toggle(_CHANNEL_MAP)] _EnableChannelMap(\"Enable Channel Map\", Float) = 0.0\n        [NoScaleOffset] _ChannelMap(\"Channel Map\", 2D) = \"white\" {}\n        [Toggle(_NORMAL_MAP)] _EnableNormalMap(\"Enable Normal Map\", Float) = 0.0\n        [NoScaleOffset] _NormalMap(\"Normal Map\", 2D) = \"bump\" {}\n\n        \/\/ Rendering options.\n        [Toggle(_DIRECTIONAL_LIGHT)] _DirectionalLight(\"Directional Light\", Float) = 1.0\n        [Toggle(_SPECULAR_HIGHLIGHTS)] _SpecularHighlights(\"Specular Highlights\", Float) = 1.0\n        [Toggle(_REFLECTIONS)] _Reflections(\"Reflections\", Float) = 0.0\n        [Toggle(_REFRACTION)] _Refraction(\"Refraction\", Float) = 0.0\n        _RefractiveIndex(\"Refractive Index\", Range(0.0, 3.0)) = 0.0\n\n        \/\/ Advanced options.\n        [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend(\"Source Blend\", Float) = 1                 \/\/ \"One\"\n        [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend(\"Destination Blend\", Float) = 0            \/\/ \"Zero\"\n        [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp(\"Blend Operation\", Float) = 0                 \/\/ \"Add\"\n        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest(\"Depth Test\", Float) = 4                \/\/ \"LessEqual\"\n\n        _ZOffsetFactor(\"Depth Offset Factor\", Float) = 0                                             \/\/ \"Zero\"\n        _ZOffsetUnits(\"Depth Offset Units\", Float) = 0                                               \/\/ \"Zero\"\n        [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask(\"Color Write Mask\", Float) = 15 \/\/ \"All\"\n        [Enum(UnityEngine.Rendering.CullMode)] _CullMode(\"Cull Mode\", Float) = 2                     \/\/ \"Back\"\n        _RenderQueueOverride(\"Render Queue Override\", Range(-1.0, 5000)) = -1\n        [Toggle(_STENCIL)] _Stencil(\"Enable Stencil Testing\", Float) = 0.0\n        _StencilReference(\"Stencil Reference\", Range(0, 255)) = 0\n        [Enum(UnityEngine.Rendering.CompareFunction)]_StencilComparison(\"Stencil Comparison\", Int) = 0\n        [Enum(UnityEngine.Rendering.StencilOp)]_StencilOperation(\"Stencil Operation\", Int) = 0\n    }\n\n    SubShader\n    {\n        Pass\n        {\n            Name \"Main\"\n            Tags{ \"RenderType\" = \"Opaque\" \"LightMode\" = \"ForwardBase\" }\n            LOD 100\n            Blend[_SrcBlend][_DstBlend]\n            BlendOp[_BlendOp]\n            ZTest[_ZTest]\n            Cull[_CullMode]\n            Offset[_ZOffsetFactor],[_ZOffsetUnits]\n            ColorMask[_ColorWriteMask]\n\n            Stencil\n            {\n                Ref[_StencilReference]\n                Comp[_StencilComparison]\n                Pass[_StencilOperation]\n            }\n\n            CGPROGRAM\n\n            #pragma vertex vert\n            #pragma fragment frag\n\n            #pragma multi_compile_instancing\n            #pragma multi_compile _ LIGHTMAP_ON\n            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON\n            #pragma shader_feature _CHANNEL_MAP\n            #pragma shader_feature _NORMAL_MAP\n            #pragma shader_feature _DIRECTIONAL_LIGHT\n            #pragma shader_feature _SPECULAR_HIGHLIGHTS\n            #pragma shader_feature _REFLECTIONS\n            #pragma shader_feature _REFRACTION\n\n            #include \"UnityCG.cginc\"\n            #include \"UnityStandardConfig.cginc\"\n            #include \"UnityStandardUtils.cginc\"\n\n#if defined(_DIRECTIONAL_LIGHT) || defined(_REFLECTIONS) \n            #define _NORMAL\n#else\n            #undef _NORMAL\n#endif\n\n#if defined(_NORMAL) \n            #define _WORLD_POSITION\n#else\n            #undef _WORLD_POSITION\n#endif\n\n#if defined(_ALPHATEST_ON)\n            #define _ALPHA_CLIP\n#else\n            #undef _ALPHA_CLIP\n#endif\n\n#if defined(_ALPHABLEND_ON)\n            #define _TRANSPARENT\n            #undef _ALPHA_CLIP\n#else\n            #undef _TRANSPARENT\n#endif\n\n#if defined(_DIRECTIONAL_LIGHT)\n            #define _FRESNEL\n#else\n            #undef _FRESNEL\n#endif\n\n            struct appdata_t\n            {\n                float4 vertex : POSITION;\n                \/\/ The default UV channel used for texturing.\n                float2 uv : TEXCOORD0;\n#if defined(LIGHTMAP_ON)\n                \/\/ Reserved for Unity's light map UVs.\n                float2 uv1 : TEXCOORD1;\n#endif\n                \/\/ Used for smooth normal data (or UGUI scaling data).\n                float4 uv2 : TEXCOORD2;\n                \/\/ Used for UGUI scaling data.\n                float2 uv3 : TEXCOORD3;\n                fixed3 normal : NORMAL;\n#if defined(_NORMAL_MAP)\n                fixed4 tangent : TANGENT;\n#endif\n                UNITY_VERTEX_INPUT_INSTANCE_ID\n            };\n\n            struct v2f \n            {\n                float4 position : SV_POSITION;\n                float2 uv : TEXCOORD0;\n#if defined(LIGHTMAP_ON)\n                float2 lightMapUV : TEXCOORD1;\n#endif\n\n#if defined(_WORLD_POSITION)\n                float3 worldPosition : TEXCOORD2;\n#endif\n#if defined(_NORMAL)\n#if defined(_NORMAL_MAP)\n                fixed3 tangentX : COLOR3;\n                fixed3 tangentY : COLOR4;\n                fixed3 tangentZ : COLOR5;\n#else\n                fixed3 worldNormal : COLOR3;\n#endif\n#endif\n                UNITY_VERTEX_OUTPUT_STEREO\n            };\n\n            fixed4 _Color;\n            sampler2D _MainTex;\n            fixed4 _MainTex_ST;\n\n#if defined(_ALPHA_CLIP)\n            fixed _Cutoff;\n#endif\n\n            fixed _Metallic;\n            fixed _Smoothness;\n\n#if defined(_CHANNEL_MAP)\n            sampler2D _ChannelMap;\n#endif\n\n#if defined(_NORMAL_MAP)\n            sampler2D _NormalMap;\n#endif\n\n#if defined(_DIRECTIONAL_LIGHT)\n            fixed4 _LightColor0;\n#endif\n\n#if defined(_REFRACTION)\n            fixed _RefractiveIndex;\n#endif\n\n#if defined(_DIRECTIONAL_LIGHT)\n            static const fixed _MinMetallicLightContribution = 0.7;\n            static const fixed _IblContribution = 0.1;\n#endif\n\n#if defined(_SPECULAR_HIGHLIGHTS)\n            static const float _Shininess = 800.0;\n#endif\n\n#if defined(_FRESNEL)\n            static const float _FresnelPower = 8.0;\n#endif\n\n            v2f vert(appdata_t v)\n            {\n                v2f o;\n                UNITY_SETUP_INSTANCE_ID(v);\n                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);\n                float4 vertexPosition = v.vertex;\n\n#if defined(_WORLD_POSITION)\n                float3 worldVertexPosition = mul(unity_ObjectToWorld, vertexPosition).xyz;\n#endif\n                fixed3 localNormal = v.normal;\n\n#if defined(_NORMAL)\n                fixed3 worldNormal = UnityObjectToWorldNormal(localNormal);\n#endif\n\n                o.position = UnityObjectToClipPos(vertexPosition);\n\n#if defined(_WORLD_POSITION)\n                o.worldPosition.xyz = worldVertexPosition;\n#endif\n                o.uv = TRANSFORM_TEX(v.uv, _MainTex);\n\n#if defined(LIGHTMAP_ON)\n                o.lightMapUV.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;\n#endif\n\n#if defined(_NORMAL)\n#if defined(_NORMAL_MAP)\n                fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);\n                fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w;\n                fixed3 worldBitangent = cross(worldNormal, worldTangent) * tangentSign;\n                o.tangentX = fixed3(worldTangent.x, worldBitangent.x, worldNormal.x);\n                o.tangentY = fixed3(worldTangent.y, worldBitangent.y, worldNormal.y);\n                o.tangentZ = fixed3(worldTangent.z, worldBitangent.z, worldNormal.z);\n#else\n                o.worldNormal = worldNormal;\n#endif\n#endif\n\n                return o;\n            }\n\n            fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target\n            {\n                \/\/ Texturing.\n                fixed4 albedo = tex2D(_MainTex, i.uv);\n\n#ifdef LIGHTMAP_ON\n                albedo.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lightMapUV));\n#endif\n\n#if defined(_CHANNEL_MAP)\n                fixed4 channel = tex2D(_ChannelMap, i.uv);\n                _Metallic = channel.r;\n                albedo.rgb *= channel.g;\n                _Smoothness = channel.a;\n#else\n#endif\n                albedo *= _Color;\n\n                \/\/ Normal calculation.\n#if defined(_NORMAL)\n                    fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition.xyz));\n    #if defined(_REFLECTIONS) \n                    fixed3 incident = -worldViewDir;\n    #endif\n                    fixed3 worldNormal;\n\n    #if defined(_NORMAL_MAP)\n                    fixed3 tangentNormal = UnpackNormal(tex2D(_NormalMap, i.uv));\n                    worldNormal.x = dot(i.tangentX, tangentNormal);\n                    worldNormal.y = dot(i.tangentY, tangentNormal);\n                    worldNormal.z = dot(i.tangentZ, tangentNormal);\n                    worldNormal = normalize(worldNormal) * facing;\n    #else\n                    worldNormal = normalize(i.worldNormal) * facing;\n    #endif\n#endif\n\n#if defined(_ALPHA_CLIP)\n    #if !defined(_ALPHATEST_ON)\n                    _Cutoff = 0.5;\n    #endif\n                clip(albedo.a - _Cutoff);\n                albedo.a = 1.0;\n#endif\n\n                \/\/ Blinn phong lighting.\n#if defined(_DIRECTIONAL_LIGHT)\n                float4 directionalLightDirection = _WorldSpaceLightPos0;\n                fixed diffuse = max(0.0, dot(worldNormal, directionalLightDirection));\n    #if defined(_SPECULAR_HIGHLIGHTS)\n                    fixed halfVector = max(0.0, dot(worldNormal, normalize(directionalLightDirection + worldViewDir)));\n                    fixed specular = saturate(pow(halfVector, _Shininess * pow(_Smoothness, 4.0)) * (_Smoothness * 2.0) * _Metallic);\n    #else\n                    fixed specular = 0.0;\n    #endif\n#endif\n\n                \/\/ Image based lighting (attempt to mimic the Standard shader).\n#if defined(_REFLECTIONS)\n                fixed3 worldReflection = reflect(incident, worldNormal);\n                fixed4 iblData = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, worldReflection, (1.0 - _Smoothness) * UNITY_SPECCUBE_LOD_STEPS);\n                fixed3 ibl = DecodeHDR(iblData, unity_SpecCube0_HDR);\n    #if defined(_REFRACTION)\n                    fixed4 refractColor = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, refract(incident, worldNormal, _RefractiveIndex));\n                    ibl *= DecodeHDR(refractColor, unity_SpecCube0_HDR);\n    #endif\n#else\n                fixed3 ibl = unity_IndirectSpecColor.rgb;\n#endif\n\n                \/\/ Fresnel lighting.\n#if defined(_FRESNEL)\n                fixed fresnel = 1.0 - saturate(abs(dot(worldViewDir, worldNormal)));\n                fixed3 fresnelColor = unity_IndirectSpecColor.rgb * (pow(fresnel, _FresnelPower) * max(_Smoothness, 0.5));\n#endif\n                \/\/ Final lighting mix.\n                fixed4 output = albedo;\n                fixed3 ambient = glstate_lightmodel_ambient + fixed3(0.25, 0.25, 0.25);\n                fixed minProperty = min(_Smoothness, _Metallic);\n#if defined(_DIRECTIONAL_LIGHT)\n                fixed oneMinusMetallic = (1.0 - _Metallic);\n                output.rgb = lerp(output.rgb, ibl, minProperty);\n                fixed3 directionalLightColor = _LightColor0.rgb;\n                output.rgb *= lerp((ambient + directionalLightColor * diffuse + directionalLightColor * specular) * max(oneMinusMetallic, _MinMetallicLightContribution), albedo, minProperty);\n                output.rgb += (directionalLightColor * albedo * specular) + (directionalLightColor * specular * _Smoothness);\n                output.rgb += ibl * oneMinusMetallic * _IblContribution;\n#elif defined(_REFLECTIONS)\n                output.rgb = lerp(output.rgb, ibl, minProperty);\n                output.rgb *= lerp(ambient, albedo, minProperty);\n#endif\n\n#if defined(_FRESNEL)\n    #if !defined(_REFLECTIONS)\n                    output.rgb += fresnelColor;\n    #else\n                    output.rgb += fresnelColor * (1.0 - minProperty);\n    #endif\n#endif\n                return output;\n            }\n\n            ENDCG\n        }\n\n        Pass\n        {\n            Name \"Meta\"\n            Tags { \"LightMode\" = \"Meta\" }\n            CGPROGRAM\n            #pragma vertex vert\n            #pragma fragment frag\n            #pragma shader_feature EDITOR_VISUALIZATION\n            #pragma shader_feature _CHANNEL_MAP\n            #include \"UnityCG.cginc\"\n            #include \"UnityMetaPass.cginc\"\n\n            struct v2f\n            {\n                float4 vertex : SV_POSITION;\n                float2 uv : TEXCOORD0;\n            };\n\n            float4 _MainTex_ST;\n            v2f vert(appdata_full v)\n            {\n                v2f o;\n                o.vertex = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST);\n                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);\n                return o;\n            }\n\n            sampler2D _MainTex;\n            sampler2D _ChannelMap;\n            fixed4 _Color;\n            fixed4 _LightColor0;\n\n            half4 frag(v2f i) : SV_Target\n            {\n                UnityMetaInput output;\n                UNITY_INITIALIZE_OUTPUT(UnityMetaInput, output);\n                output.Albedo = tex2D(_MainTex, i.uv) * _Color;\n                output.SpecularColor = _LightColor0.rgb;\n                return UnityMetaFragment(output);\n            }\n            ENDCG\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1376"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1376"}],"version-history":[{"count":1,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1376\/revisions"}],"predecessor-version":[{"id":1377,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1376\/revisions\/1377"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}