{"id":2528,"date":"2022-01-07T14:37:49","date_gmt":"2022-01-07T06:37:49","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2528"},"modified":"2022-01-07T14:37:51","modified_gmt":"2022-01-07T06:37:51","slug":"unity%e9%9d%9e%e6%8f%92%e4%bb%b6%e5%ae%9e%e7%8e%b0bloom%e6%95%88%e6%9e%9c","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2528","title":{"rendered":"Unity\u975e\u63d2\u4ef6\u5b9e\u73b0Bloom\u6548\u679c"},"content":{"rendered":"\n<h2>\u6548\u679c<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-30-38.jpg\" data-lightbox=\"image_lg\"><img title=\"Unity\u975e\u63d2\u4ef6\u5b9e\u73b0Bloom\u6548\u679c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u975e\u63d2\u4ef6\u5b9e\u73b0Bloom\u6548\u679c - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" width=\"639\" height=\"374\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-30-38.jpg\" alt=\"\" class=\"wp-image-2529\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-30-38.jpg 639w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-30-38-300x176.jpg 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/a><\/figure>\n\n\n\n<h2>\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\n&#91;ExecuteInEditMode]\n&#91;ImageEffectAllowedInSceneView]\npublic class Bloom : MonoBehaviour\n{\n    #region constData\n    const int firstPass = 0;\n    const int downPass = 1;\n    const int upPass = 2;\n    const int mixPass = 3;\n    #endregion\n\n\n    public Shader bloom;\n    private Material material;\n    &#91;Range(0,8)]\n    public int Interations;\n    &#91;Range(1,10)]\/\/ \u5df2\u7ecf\u9650\u5b9a\u4ece1\u5f00\u59cb\uff0c \u53ea\u6709\u5f00\u542fHDR\u7684\u624d\u4f1a\u5f00\u542fbloom\n    public float Threshold =1;\n    &#91;Range(0,1)]\n    public float SoftThreshold = 0.5f;\n    &#91;Range(0,10)]\n    public float Intensity = 1;\n    RenderTexture&#91;] textures = new RenderTexture&#91;8];\n    private void OnRenderImage(RenderTexture source, RenderTexture destination)\n    {\n        if (!material)\n        {\n            material = new Material(bloom);\n            material.hideFlags = HideFlags.HideAndDontSave;\n        }\n        material.SetFloat(\"_Threshold\", Threshold);\n        material.SetFloat(\"_Intensity\", Intensity);\n        float knee = Threshold - SoftThreshold;\n        Vector4 filter;\n        filter.x = Threshold;\n        filter.y = Threshold - knee;\n        filter.z = 2 * knee;\n        filter.w = 4 * knee + 0.00001f;\n        \/\/ \u5c06\u53c2\u6570\uff0c\u4ee5V4\u7684\u5f62\u5f0f\u4f20\u9012\u8fdbshader,\u5c06\u8ba1\u7b97\u91cf\u7559\u5728CPU,\u51cf\u5c11GPU\u7684\u8ba1\u7b97\n        material.SetVector(\"_Filter\", filter);\n\n\n        int width = source.width;\n        int height = source.height;\n        width \/= 2;\n        height \/= 2;\n        RenderTextureFormat format = source.format;\n        RenderTexture currentDestination = textures&#91;0] = RenderTexture.GetTemporary(width, height, 0, format);\n        Graphics.Blit(source, currentDestination,material,firstPass);\n        RenderTexture currentSource = currentDestination;\n        int i = 1;\n        \/\/ \u5411\u4e0b\u91c7\u6837\n        for (; i &lt; Interations; i++)\n        {\n            width \/= 2;\n            height \/= 2;\n            if (height&lt;2)\n            {\n                break;\n            }\n            currentDestination = textures&#91;i] = RenderTexture.GetTemporary(width, height, 0, format);\n            Graphics.Blit(currentSource, currentDestination,material,downPass);\n            currentSource = currentDestination;\n        }\n        \/\/\u5411\u4e0a\u91c7\u6837\n        for (i-=2; i&gt;=0; i--)\n        {\n            currentDestination = textures&#91;i];\n            textures&#91;i] = null;            \n            Graphics.Blit(currentSource, currentDestination,material,upPass);\n            RenderTexture.ReleaseTemporary(currentSource);\n            currentSource = currentDestination;\n        }\n        material.SetTexture(\"_SourceTex\", source);\n        \/\/\u6df7\u5408\n        Graphics.Blit(currentDestination, destination,material,mixPass);\n        RenderTexture.ReleaseTemporary(currentDestination);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2>Shader<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Shader \"custom\/Bloom\"\n{\n    Properties\n    {\n        _MainTex (\"Texture\", 2D) = \"white\" {}\n        \/\/_SourceTex(\"SourceTex\",2D) = \"white\"{}\n        \/\/_Threshold(\"_Threshold\",float) = 1\n        \/\/_Filter(\"_Filter\",Vector) = (0,0,0,0)\n    }\n\n\tCGINCLUDE\n            #include \"UnityCG.cginc\"\n            float _Threshold;\n            struct appdata\n            {\n                float4 vertex : POSITION;\n                float2 uv : TEXCOORD0;\n            };\n\n            struct v2f\n            {\n                float2 uv : TEXCOORD0;\n                float4 vertex : SV_POSITION;\n            };\n\n            v2f vert (appdata v)\n            {\n                v2f o;\n                o.vertex = UnityObjectToClipPos(v.vertex);\n                o.uv = v.uv;\n                return o;\n            }\n\n            sampler2D _MainTex;\n            float4 _MainTex_TexelSize;\n            sampler2D _SourceTex;\n            float4 _SourceTex_ST; \n            vector _Filter;  \n\t\t\tfloat _Intensity;\n            half3 Sample(float2 uv)\n            {\n                return tex2D(_MainTex,uv).rgb;\n            }\n            \/\/\n            half3 SampleBox(float2 uv,float detail)\n            {\n                float4 o = _MainTex_TexelSize.xyxy * float4(-detail,-detail,detail,detail);\n                half3 c = Sample(uv+ o.xy) + Sample(uv + o.xz) + Sample(uv+o.zw) +Sample(uv+o.zx);\n                return c*0.25;\n            }\n\n            \/\/ half3 Prefilter(half3 c)\n            \/\/ {\n            \/\/     half brightness = max(c.r,max(c.g,c.b));\n            \/\/     half contribution = max(0,brightness - _Threshold);\n            \/\/     contribution \/= max(brightness,0.00001);\n            \/\/     return contribution;\n            \/\/ }\n            half3 Prefilter(half3 c)\n            {\n                half brightness = max(c.r,max(c.g,c.b));\n                half soft = brightness - _Filter.y;\n                soft = clamp(soft,0,_Filter.z);\n                soft = soft * soft\/_Filter.w;\n                half contribution = max(soft,brightness- _Filter.x);\n                contribution \/= max(brightness,0.00001);\n                return contribution*c;\n            }\n\n\tENDCG\n    SubShader\n    {\n        \/\/ No culling or depth\n        Cull Off ZWrite Off ZTest Always\n        Pass\n        {        \/\/\u50cf\u7d20\u7684\u9884\u7b5b\u9009\n            CGPROGRAM\n            #pragma vertex vert\n            #pragma fragment frag        \n            fixed4 frag (v2f i) : SV_Target\n            {\n                half3 col = Prefilter(SampleBox(i.uv,1));\n                return fixed4(col,1);\n            }\n            ENDCG\n        }\n\n        Pass\n        {\n            \/\/\u4e0b\u91c7\u6837\n            CGPROGRAM\n            #pragma vertex vert\n            #pragma fragment frag          \n\n            fixed4 frag (v2f i) : SV_Target\n            {\n                \n                half3 col = SampleBox(i.uv,1);\n                return fixed4(col,1);\n            }\n            ENDCG\n        }\n        Pass\n        {\n            Blend One One\n            \/\/ \u4e0a\u91c7\u6837\n            CGPROGRAM\n            #pragma vertex vert\n            #pragma fragment frag          \n\n            fixed4 frag (v2f i) : SV_Target\n            {\n                \n                half3 col = SampleBox(i.uv,0.5);\n                return fixed4(col,1);\n            }\n            ENDCG\n        }\n        Pass\n        {\n            \/\/\u6df7\u5408\n            CGPROGRAM\n            #pragma vertex vert\n            #pragma fragment frag  \n            \n                 \n\n            fixed4 frag (v2f i) : SV_Target\n            {\n                \n                half3 col = SampleBox(i.uv,0.5) + tex2D(_SourceTex,i.uv).rgb * _Intensity;\n                \/\/half3 col = SampleBox(i.uv,1);\n                return fixed4(col,1);\n            }\n            ENDCG\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u5c06\u811a\u672c\u6302\u8f7d\u5230\u76f8\u673a\u4e0a\uff0c\u7136\u540e\u5c06\u62d6\u5165shader<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-33-17.jpg\" data-lightbox=\"image_lg\"><img title=\"Unity\u975e\u63d2\u4ef6\u5b9e\u73b0Bloom\u6548\u679c - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Unity\u975e\u63d2\u4ef6\u5b9e\u73b0Bloom\u6548\u679c - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" width=\"441\" height=\"348\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-33-17.jpg\" alt=\"\" class=\"wp-image-2530\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-33-17.jpg 441w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2022\/01\/Snipaste_2022-01-07_14-33-17-300x237.jpg 300w\" sizes=\"(max-width: 441px) 100vw, 441px\" \/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6548\u679c \u4ee3\u7801 Shader \u4f7f\u7528\u65b9\u6cd5 \u5c06\u811a\u672c\u6302\u8f7d\u5230\u76f8\u673a\u4e0a\uff0c\u7136\u540e\u5c06\u62d6\u5165shader<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[421],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2528"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2528"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2528\/revisions"}],"predecessor-version":[{"id":2534,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2528\/revisions\/2534"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2528"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}