{"id":169,"date":"2024-07-03T23:31:32","date_gmt":"2024-07-03T15:31:32","guid":{"rendered":"http:\/\/chenglixue.top\/?p=169"},"modified":"2024-10-31T01:40:10","modified_gmt":"2024-10-30T17:40:10","slug":"169","status":"publish","type":"post","link":"http:\/\/chenglixue.top\/?p=169","title":{"rendered":"unity \u5c4f\u5e55\u7a7a\u95f4\u4f53\u79ef\u5149"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h1>\u601d\u8def<\/h1>\n<ul>\n<li>\u89c2\u5bdf\u4e0b\u9762\u8fd9\u526f\u56fe\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u660e\u4eae\u5904\u5149\u5f88\u660e\u663e\uff0c\u6697\u5904(\u9634\u5f71\u4e2d)\u6ca1\u6709\u660e\u663e\u7684\u5149\uff0c\u4e14\u8d8a\u6697\u5149\u8d8a\u4e0d\u660e\u663e<br \/>\n<img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/65ef0f369f345e8d031ac065.jpg\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/65ef0f369f345e8d031ac065.jpg\" alt=\"\" \/><\/p><\/noscript>\n<\/li>\n<li>\n<p>\u4e3a\u4e86\u8fd8\u539f\u8fd9\u4e00\u73b0\u8c61\uff0c\u53ef\u4ee5\u60f3\u5230\u7684\u662f\u6839\u636e<span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">\u76ee\u6807pixel\u7684\u9634\u5f71\u503c<\/span>\u6765\u8ba1\u7b97\u4eae\u5ea6\u3002\u4f46\u5982\u4f55\u8425\u9020\u5149\u7684\u4f53\u79ef\u611f\u5462\uff1f\u8fd9\u5c31\u9700\u8981\u7528\u5230\u5149\u7ebf\u8ffd\u8e2a\uff01\u7684\u601d\u60f3rayMarching\uff08\u5149\u7ebf\u6b65\u8fdb\uff09<\/p>\n<p>\u4e0e\u5149\u8ffd\u4e0d\u540c\u7684\u662f\uff0c\u5149\u8ffd\u662f\u6bcf\u4e2apixel\uff0c\u5728\u573a\u666f\u4e2d\u53d1\u5c04\u4e00\u6839\u5c04\u7ebf\u5e76\u4e0d\u65ad\u5f39\u5c04\uff0c\u5f53\u5f39\u5c04\u51fa\u573a\u666f\u6216\u8fbe\u5230\u6700\u5927\u5f39\u5c04\u6b21\u6570\u65f6\uff0c\u7d2f\u52a0\u6bcf\u6b21\u5f39\u5c04\u8ba1\u7b97\u5f97\u5230\u7684\u989c\u8272\uff0c\u6700\u7ec8\u8be5pixel\u8fd4\u56de\u8be5\u989c\u8272\u503c\uff1b\u800crayMarching\u7279\u522b\u4e4b\u5904\u5728\u4e8e\uff0c\u5b83\u4e0d\u4f1a\u5f39\u5c04\uff0c\u800c\u662f\u6bcf\u4e2apixel\u53d1\u5c04\u4e00\u6839\u5c04\u7ebf\uff0c\u8be5\u5c04\u7ebf\u6bcf\u6b21\u884c\u8d70\u4e00\u5b9a\u7684\u8ddd\u79bbstep\uff0c\u6bcf\u884c\u8d70\u4e00\u6b21\u8ba1\u7b97\u5f53\u524d\u4f4d\u7f6e\u7684\u9634\u5f71\u503c\u5e76\u7d2f\u52a0\uff0c\u5f53\u78b0\u5230\u906e\u6321\u7269\u4f53\u6216\u8fbe\u5230\u6700\u5927\u8ddd\u79bb\uff0c\u5c31\u7ec8\u6b62\u6b65\u8fdb\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u7ed3\u679c\u5373\u4e3a\u7d2f\u52a0\u7684\u9634\u5f71\u503c<\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u7ea2\u8272\u865a\u7ebf\u4ee3\u8868\u5149\u7ebf\u8d70\u5230\u8fc7\u7684\u4f4d\u7f6e\uff0c\u5f53\u8d70\u5230\u8fd9\u4e9b\u4f4d\u7f6e\u65f6\u5c31\u91c7\u6837\u9634\u5f71\u56fe\u5e76\u5f97\u5230\u5bf9\u5e94\u7684\u9634\u5f71\u503c\uff0c\u6700\u540e\u7d2f\u52a0<br \/>\n<img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/65ef313a9f345e8d0316ed9d.jpg\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/65ef313a9f345e8d0316ed9d.jpg\" alt=\"\" \/><\/p><\/noscript>\n<\/li>\n<\/ul>\n<h1>\u5b9e\u73b0<\/h1>\n<h2>\u91c7\u6837\u9634\u5f71\u56fe<\/h2>\n<ul>\n<li>\n<p>\u5728\u524d\u9762\u8bf4\u8fc7\uff0c\u5f97\u5230\u7684\u5149\u7167\u4e0e\u9634\u5f71\u503c\u6709\u5173\uff0c\u6240\u4ee5\u8fd9\u91cc\u9700\u8981\u91c7\u6837\u9634\u5f71\u56fe<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0<\/p>\n<pre><code class=\"line-numbers\">#pragma multi_compile _ _MAIN_LIGHT_SHADOWS                    \/\/\u63a5\u53d7\u9634\u5f71\n#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE            \/\/\u4ea7\u751f\u9634\u5f71\n#pragma multi_compile _ _SHADOWS_SOFT                         \/\/\u8f6f\u9634\u5f71 \n\nfloat GetShadow(float3 positionWS)\n{\n  float4 shadowUV = TransformWorldToShadowCoord(positionWS);\n  float shadow = MainLightRealtimeShadow(shadowUV);\n\n  return shadow;\n}\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u4e3a\u4e86\u7b80\u4fbf\u4f7f\u7528\u7684\u662furp\u7ba1\u7ebf\u5185\u7f6e\u7684\u9634\u5f71\uff0c\u4f46\u8fd9\u91cc\u662f\u505a\u4e86\u8fb9\u7f18\u8f6f\u5316\u7684\u3002\u4e3a\u4e86\u8282\u7ea6\u6027\u80fd\uff0c\u53ef\u4ee5<strong>\u4ec5\u4ec5\u91c7\u6837\u786c\u9634\u5f71\uff0c\u4e0d\u505a\u8f6f\u5316<\/strong><\/p>\n<\/li>\n<\/ul>\n<h2>\u6563\u5c04<\/h2>\n<ul>\n<li>\n<p>\u5149\u7ebf\u5728\u5927\u6c14\u4e2d\u884c\u8fdb\u662f\u6709\u53ef\u80fd\u53d1\u751f\u6563\u5c04\u7684\uff0c\u4f53\u79ef\u5149\u4e5f\u4e0d\u4f8b\u5916\u3002\u4e3a\u4e86\u7b80\u4fbf\u8fd9\u91cc\u4ec5\u4ec5\u91c7\u7528\u5355\u6b21\u6563\u5c04\uff0c\u4e14\u4e3a\u4e86\u8282\u7701\u6027\u80fd\uff0c\u4e0d\u518d\u8d70\u57fa\u4e8e\u7269\u7406\u7684\u6563\u5c04\u6a21\u578b\uff0c\u4ec5\u4ec5\u91c7\u7528\u76f8\u4f4d\u51fd\u6570\u3001\u5927\u6c14\u6563\u5c04\u5bc6\u5ea6\u3001\u900f\u5149\u7387<\/p>\n<\/li>\n<li>\n<p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\uff0c\u5bf9\u4e8e\u76f8\u4f4d\u51fd\u6570\u91c7\u7528\u8fd1\u4f3c\u7684<\/p>\n<p><span class=\"katex math inline\">\\frac{1 - g^2}{4\u03a0(1 + g^2 - 2gcos\u03b8)^{1.5}}<\/span><\/p>\n<p>\u5176\u4e2dg=0\u662f\u5404\u5411\u540c\u6027\u76f8\u51fd\u6570\uff0cg>0 \u4e3b\u8981\u662f\u524d\u5411\u6563\u5c04\uff0c g &lt; 0\u4e3b\u8981\u662f\u540e\u5411\u6563\u5c04<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0<\/p>\n<pre><code class=\"line-numbers\">\/\/\/ -----------------\n\/\/\/ \u6cbf\u89c6\u7ebf\u65b9\u5411\u6563\u5c04\u7684\u91cf(\u76f8\u4f4d\u51fd\u6570)\n\/\/\/ -----------------\nfloat GetPhase(float cosTheta)\n{\n  float a = 1.f - Pow2(_HGCoff);\n  float b = 4.f * PI * pow(1.f + Pow2(_HGCoff) - 2.f * _HGCoff * cosTheta, 1.5f);\n\n  return a \/ b;\n}\n\n\/\/\/ -----------------\n\/\/\/ \u5927\u6c14\u5bc6\u5ea6\u6bd4\u4f8b\u51fd\u6570\n\/\/\/ -----------------\nfloat GetRho()\n{\n  return exp(-_HeightFromSeaLevel \/ 1200.f);\n}\n\n\/\/\/ -----------------\n\/\/\/ \u6700\u7ec8\u7684\u6563\u5c04\u6bd4\u4f8b\n\/\/\/ -----------------\nfloat GetScatter(float cosTheta)\n{\n  return GetPhase(cosTheta) * GetRho();\n}\n\n\/\/\/ -----------------\n\/\/\/ \u900f\u5149\u7387\n\/\/\/ -----------------\nfloat GetTransmittance(float distance)\n{\n  return exp(-distance * _TransmittanceFactor * GetRho());\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>RayMarching<\/h2>\n<p>\u6309\u5f00\u5934\u6240\u63d0\u7684\u601d\u8def\u8fdb\u884c\u5149\u7ebf\u6b65\u8fdb\u5373\u53ef<\/p>\n<pre><code class=\"line-numbers\">half3 GetLightShaft(float3 viewOrigin, half3 viewDir, float maxDistance, float2 screenPos)\n{\n    float stepLength = maxDistance \/ _StepCount;              \/\/ \u6b65\u957f\n    float3 step = stepLength * viewDir;\n    float3 currPos = viewOrigin;\n    float3 totalLight = 0.f;\n    float3 totalDistance = 0.f;\n\n    float scatterFun = GetScatter(dot(viewDir, _SunDirection));\n\n    UNITY_LOOP\n    for(int i = 0; i &lt; _StepCount; ++i)\n    {\n        float shadow = GetShadow(currPos);\n        if(shadow &gt; 0.f)\n        {\n            \/\/ \u6c42\u5f53\u524dpixel\u7684\u9634\u5f71\u503c\n            totalLight += _Brightness * shadow * scatterFun * GetTransmittance(totalDistance);\n        }\n\n        \/\/ \u6b65\u8fdb\u540e\u65b0\u7684\u4f4d\u7f6e\n        currPos += step;\n        totalDistance += stepLength;\n    }\n\n    half3 result = totalLight * _MainLightColor * _LightShaftColor.rgb * _LightShaftColor.aaa;\n\n    return result;\n}\n<\/code><\/pre>\n<p>\u518d\u548c\u539f\u56fe\u53e0\u52a0\uff0c\u4e0d\u96be\u5f97\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc3dacf21886ccc0525742.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc3dacf21886ccc0525742.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>Dual Blur\u4f18\u5316\u5757\u72b6\u611f<\/h2>\n<ul>\n<li>\u5b58\u5728\u7684\u95ee\u9898\uff1a\u4ed4\u7ec6\u89c2\u770b\u4e0a\u56fe\uff0c\u867d\u7136\u6709\u4f53\u79ef\u5149\u7684\u611f\u89c9\uff0c\u4f46\u662f\u6709\u660e\u663e\u7684\u786c\u7ebf\uff0c\u8fd9\u662f\u56e0\u4e3astep\u7684\u6b65\u957f\u5927\u5c0f\u4e0d\u591f\u5c0f\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e0d\u591f\u7cbe\u51c6\uff08\u548c\u5149\u8ffd\u4e00\u4e2a\u9053\u7406\uff0c\u5f39\u5c04\u6b21\u6570\u8d8a\u591a\u8d8a\u7cbe\u51c6\uff09\u3002\u4f46\u662fstep\u6b65\u957f\u5c0f\u4e86\u5f00\u9500\u53c8\u5f88\u9ad8\uff0c\u771f\u662f\u5934\u75bc\u600e\u4e48\u529e\u5462\uff1f<\/li>\n<li>\u89e3\u51b3\u65b9\u6848\uff1a\u56e0\u4e3a\u4f53\u79ef\u5149\u5c5e\u4e8e\u540e\u5904\u7406\uff0c\u8981\u7528\u9b54\u6cd5\u6253\u8d25\u9b54\u6cd5\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ef\u4ee5\u91c7\u7528<span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">\u6a21\u7cca\u5f31\u5316\u786c\u7ebf<\/span>\u3002\u51fa\u4e8e\u6027\u80fd\u8003\u8651\uff0c\u8fd9\u91cc\u4f7f\u7528\u6027\u80fd\u62d4\u5c16\u7684Dual Blur<\/li>\n<\/ul>\n<p>\u4e0d\u96be\u5f97\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc3eb9f21886ccc0531434.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc3eb9f21886ccc0531434.png\" alt=\"\" \/><\/p><\/noscript>\n<h1>\u6027\u80fd\u4f18\u5316<\/h1>\n<h2>\u68cb\u76d8\u683cClip<\/h2>\n<ul>\n<li>\u5149\u7684\u53d8\u5316\u9891\u7387\u4e0d\u9ad8\uff0c\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u8fdb\u884c<span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">\u90e8\u5206clip<\/span>\uff0c\u4e5f\u4e0d\u4f1a\u5f88\u5bb9\u6613\u88ab\u8bc6\u522b\u51fa\u6765\uff0c\u8fd9\u91cc\u91c7\u7528<span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">\u68cb\u76d8\u683c\u5237\u65b0<\/span>\u7684\u65b9\u5f0f\u6765\u66f4\u65b0<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0<\/p>\n<p>\u5f88\u7b80\u5355\uff0c\u5728ps\u4e2dclip\u5373\u53ef<\/p>\n<pre><code class=\"line-numbers\">float2 channel = floor(i.positionCS);\nclip(channel.y%2 * channel.x%2 + (channel.y+1)%2 * (channel.x+1)%2 - 0.1f);\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>Dither<\/h2>\n<p>\u5bf9\u4e8e\u6bcf\u6b21\u5149\u7ebf\u6b65\u8fdb\u7684\u65b9\u5411\u90fd\u662f\u56fa\u5b9a\u7684\uff0c\u8fd9\u6837\u4e0d\u80fd\u4ee5\u8f83\u4f4e\u7684\u6b65\u8fdb\u6b21\u6570\u8fbe\u5230\u4e0d\u9519\u7684\u6548\u679c\uff0c\u8f83\u4f4e\u7684\u5149\u7ebf\u6b65\u8fdb\u6b65\u6570\u4f1a\u4f7f\u4f53\u79ef\u5149\u6548\u679c\u5e26\u6709\u5e26\u72b6\u7684\u4f2a\u5f71\u4ee5\u53ca\u53d8\u5f97\u5f88\u7cca\u3002\u53ef\u4ee5\u5bf9\u6bcf\u4e2apixel\u90fd\u6296\u52a8\u4e00\u4e0b\u6b65\u8fdb\u65b9\u5411<\/p>\n<ul>\n<li>\u5b9e\u73b0\n<pre><code class=\"line-numbers\">half3 GetLightShaft(float3 viewOrigin, half3 viewDir, float maxDistance, float2 screenPos)\n{\nfloat2 ditherPos = fmod(floor(screenPos.xy), 4.f);\n  float3 ditherDir = _BlueNoiseTex.Sample(Smp_ClampU_ClampV_Linear, ditherPos \/ 4.f + float2(0.5 \/ 4.f, 0.5f \/ 4.f), float2(0, 0));\n\n  float3 step = stepLength * viewDir * ditherDir;\n  \/\/ ...\n}\n<\/code><\/pre>\n<p>\u4e0b\u56fe\u662f\u6b65\u8fdb16\u6b21\u7684\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc3fe0f21886ccc053eefe.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc3fe0f21886ccc053eefe.png\" alt=\"\" \/><\/p><\/noscript>\n<p>\u5373\u4f7f\u6b65\u8fdb\u4ec56\u6b21\u4e5f\u6bd4\u5148\u524d\u7684\u597d\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc402ef21886ccc0542d4c.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc402ef21886ccc0542d4c.png\" alt=\"\" \/><\/p><\/noscript>\n<\/li>\n<\/ul>\n<h2>\u964d\u91c7\u6837<\/h2>\n<p>\u4f53\u79ef\u5149\u8fd9\u79cd\u6bd4\u8f83\u68a6\u5e7b\u7684\u6548\u679c\uff0c\u4e0d\u7528\u4f7f\u7528\u539f\u5206\u8fa8\u7387\u91c7\u7528\uff0c\u53ef\u4ee5\u5148\u964d\u91c7\u6837\uff0c\u8ba1\u7b97\u4f53\u79ef\u5149\u6a21\u7cca\u518d\u5408\u5e76\u5230\u539f\u56fe\u4e0a\uff0c\u5373\u4fbf\u5982\u6b64\u5e76\u4e0d\u4f1a\u9732\u9985<\/p>\n<p>\u4f46\u5f53\u5347\u91c7\u6837\u65f6\uff0c\u7531\u4e8e\u4f1a\u7528\u5230\u53cc\u7ebf\u6027\u63d2\u503c\uff0c\u4f1a\u5bfc\u81f4\u5347\u91c7\u6837\u540e\u7684\u56fe\u50cf\u5e26\u6709\u6a21\u7cca\u4e14\u5bf9\u4e8e\u4f53\u79ef\u5149\u6765\u8bf4\u8fd8\u597d\uff0cSSAO\u5c31\u4f1a\u51fa\u73b0\u4f2a\u5f71\uff0c\u5c24\u5176\u662f\u7269\u4f53\u8fb9\u7f18\uff0c\u8fd9\u6837\u5f97\u5230\u7684\u4f53\u79ef\u5149\u6548\u679c\u5e76\u4e0d\u597d\uff0c\u9700\u8981\u7528\u5230Depth-Aware Upsampling<\/p>\n<p>\u4ee5\u4e0b\u662f\u5347\u91c7\u6837\u5f97\u5230\u7684\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc419ef21886ccc0553d9b.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc419ef21886ccc0553d9b.png\" alt=\"\" \/><\/p><\/noscript>\n<p>\u4ee5\u4e0b\u662f\u5347\u91c7\u6837\u4f46\u7ecf\u8fc7Depth-Aware Upsampling\u540e\u7684\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc420bf21886ccc0558df2.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc420bf21886ccc0558df2.png\" alt=\"\" \/><\/p><\/noscript>\n<ul>\n<li>Depth-Aware Upsampling\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8be5\u7b97\u6cd5\u601d\u8def\u662f\uff0c\u5728\u4e00\u4e2a2x2\u7684\u6838\u4e2d\uff0c\u8ba1\u7b97\u9ad8\u5206\u8fa8\u7387\u56fe\u7684\u76ee\u6807pixel\u5bf9\u5e94\u7684depth\u548c\u8be5pixel\u5728\u4f4e\u5206\u8fa8\u7387\u65c1\u8fb9\u56db\u4e2apixel\u7684depth\u5dee\uff0c\u9009\u62e9\u6700\u63a5\u8fd1\u5168\u5206\u8fa8\u7387\u6df1\u5ea6\u5e76\u8fd4\u56de\u8be5\u6837\u672c\u5bf9\u5e94\u7684\u4f4e\u5206\u8fa8\u7387\u989c\u8272<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0<\/p>\n<p>\u9996\u5148\u9700\u8981\u964d\u4f4e\u5206\u8fa8\u7387<\/p>\n<pre><code class=\"line-numbers\">_descriptor.width  \/= (int)_passSetting.downSample;\n_descriptor.height \/= (int)_passSetting.downSample;\n_descriptor.colorFormat = RenderTextureFormat.R16;\n\n_LowResDepthRT = new RenderTargetIdentifier(_lowResDepthTexID);\ncmd.GetTemporaryRT(_lowResDepthTexID, _descriptor, FilterMode.Point);\n<\/code><\/pre>\n<p>\u7136\u540e\u9700\u8981\u4e00\u4e2a\u989d\u5916\u7684Pass\u6765\u8ba1\u7b97\u4f4e\u5206\u8fa8\u7387\u4e0b\u7684depth<\/p>\n<pre><code class=\"line-numbers\">PSOutput CopyDepth(PSInput i)\n{\n  PSOutput o;\n\n  o.color = Linear01Depth(_CameraDepthTexture.Sample(Smp_ClampU_ClampV_Linear, i.uv), _ZBufferParams);\n\n  return o;\n}\n<\/code><\/pre>\n<p>\u6700\u540e\u8fdb\u884cDepth-Aware Upsampling<\/p>\n<pre><code class=\"line-numbers\">half4 result = 0.h;\n\nfloat highResDepth = _CameraDepthTexture.Sample(Smp_ClampU_ClampV_Linear, i.uv).r;\nhighResDepth = Linear01Depth(highResDepth, _ZBufferParams);\nfloat lowResDepth1 = _LowResDepthTex.Sample(Smp_ClampU_ClampV_Linear, i.uv, int2(0, 0.5f)).r;\nfloat lowResDepth2 = _LowResDepthTex.Sample(Smp_ClampU_ClampV_Linear, i.uv, int2(0, -0.5f)).r;\nfloat lowResDepth3 = _LowResDepthTex.Sample(Smp_ClampU_ClampV_Linear, i.uv, int2(0.5f, 0)).r;\nfloat lowResDepth4 = _LowResDepthTex.Sample(Smp_ClampU_ClampV_Linear, i.uv, int2(-0.5f, 0)).r;\n\nfloat depthDiff1 = abs(highResDepth - lowResDepth1);\nfloat depthDiff2 = abs(highResDepth - lowResDepth2);\nfloat depthDiff3 = abs(highResDepth - lowResDepth3);\nfloat depthDiff4 = abs(highResDepth - lowResDepth4);\n\nfloat depthDiffMin = min(min(depthDiff1, depthDiff2), min(depthDiff3, depthDiff4));\nint index = -1;\nif(depthDiffMin == depthDiff1) index = 0;\nelse if(depthDiffMin == depthDiff2) index = 1;\nelse if(depthDiffMin == depthDiff3) index = 2;\nelse if(depthDiffMin == depthDiff4) index = 3;\n\nswitch(index)\n{\n  case 0:\n      result += _LightShaftTex.Sample(Smp_ClampU_ClampV_Point, i.uv, int2(0, 0.5f));\n      break;\n  case 1:\n      result += _LightShaftTex.Sample(Smp_ClampU_ClampV_Point, i.uv, int2(0, -0.5f));\n      break;\n  case 2:\n      result += _LightShaftTex.Sample(Smp_ClampU_ClampV_Point, i.uv, int2(0.5f, 0));\n      break;\n  case 3:\n      result += _LightShaftTex.Sample(Smp_ClampU_ClampV_Point, i.uv, int2(-0.5f, 0));\n      break;\n  default:\n      result += _LightShaftTex.Sample(Smp_ClampU_ClampV_Point, i.uv);\n      break;\n}\n\nhalf4 sourceTex = SAMPLE_TEXTURE2D(_SourceTex, Smp_ClampU_ClampV_Linear, i.uv);\nresult += sourceTex;\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u91c7\u6837\u6838\u7684\u5927\u5c0f\u6bd4\u8f83\u5c0f\uff0c\u53ef\u80fd\u5bfc\u81f4\u975e\u8fb9\u7f18\u4ea7\u751f\u5757\u72b6\u50cf\u7d20\uff0c\u6240\u4ee5\u53ef\u4ee5\u8ba1\u7b97\u6df1\u5ea6\u5dee\uff0c\u5e76\u5224\u65ad\u8be5\u6df1\u5ea6\u5dee\u662f\u5426\u5927\u4e8e\u8bbe\u5b9a\u7684\u6df1\u5ea6\u9608\u503c\uff0c\u5927\u4e8e\u5219\u91c7\u7528\u70b9\u91c7\u6837\uff0c\u5426\u5219\u53cc\u7ebf\u6027\u63d2\u503c<\/p>\n<\/li>\n<\/ul>\n<p>\u5728\u964d\u4f4e\u56db\u500d\u5206\u8fa8\u7387\u7684\u60c5\u51b5\u4e0b\uff0c\u5f97\u5230\u7684\u4f53\u79ef\u5149\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66fc513bf21886ccc05ff08a.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/66fc513bf21886ccc05ff08a.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>TAA<\/h2>\n<p>\u5f53\u964d\u91c7\u6837\u8fc7\u591a\u540e\uff0c\u7531\u4e8e\u5206\u8fa8\u7387\u8fc7\u4f4e\uff0c\u4f53\u79ef\u5149\u4f1a\u5b58\u5728\u8bb8\u591a\u50cf\u7d20\u70b9\u3002\u5177\u4f53\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/672268afd29ded1a8cd9fb2a.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/672268afd29ded1a8cd9fb2a.png\" alt=\"\" \/><\/p><\/noscript>\n<p>\u8fd9\u65f6\u53ef\u4ee5\u8003\u8651\u7528TAA\u8fdb\u884c\u6296\u52a8\uff0c\u5e76\u5c06\u5f53\u524d\u5e27\u548c\u4e0a\u4e00\u5e27\u6296\u52a8\u540e\u7684\u56fe\u50cf\u8fdb\u884clerp\uff0c\u4ece\u800c\u51cf\u5c11\u566a\u70b9\uff08\u5173\u4e8eTAA\u7b14\u8005\u5148\u524d\u7684\u6587\u7ae0\u4e5f\u6709\u4ecb\u7ecd\uff0c\u8fd9\u91cc\u4e0d\u518d\u63d0\u53ca\uff09\u3002TAA\u540e\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/672268ccd29ded1a8cda29e1.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/672268ccd29ded1a8cda29e1.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u6027\u80fd\u8017\u65f6<\/h2>\n<p>3080\u7684\u914d\u7f6e\u4e0b\u8017\u65f6\uff1a1k\u5206\u8fa8\u7387 + 8\u6b21\u6b65\u8fdb + 8\u500d\u964d\u91c7\u6837 + TAA\uff0c\u603b\u8017\u65f60.557 + 0.108ms<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/67226f10d29ded1a8ce4f6b0.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/67226f10d29ded1a8ce4f6b0.png\" alt=\"\" \/><\/p><\/noscript>\n<h1>\u5f69\u8272\u534a\u900f\u660e\u73bb\u7483<\/h1>\n<p>\u5f53\u5149\u7ebf\u7a7f\u8fc7\u534a\u900f\u660e\u7269\u4f53\u65f6\uff0c\u5149\u7ebf\u4f1a\u53d7\u534a\u900f\u660e\u7269\u4f53\u7684\u989c\u8272\u7684\u5f71\u54cd\uff0c\u4f53\u79ef\u5149\u4e5f\u9700\u652f\u6301\u8be5\u529f\u80fd<\/p>\n<ul>\n<li>\u601d\u8def\n<ol>\n<li>\u5bf9\u4e8e\u534a\u900f\u660e\u7269\u4f53\uff0c\u65b0\u589e\u4e00\u4e2apass\u91c7\u6837\u5b83\u7684\u6df1\u5ea6\uff0c\u8bb0\u4f5ctransparent depth<\/li>\n<li>\u6bcf\u6b21\u6b65\u8fdb\u65f6\uff0c\u989d\u5916\u65b0\u589e\u4e00\u4e2a\u6b65\u8fdb\uff0c\u8be5\u6b65\u8fdb\u671d\u5149\u6e90\u65b9\u5411\u6b65\u8fdb\uff0c\u6bcf\u6b21\u6b65\u8fdb\u540e\u5c06\u8be5\u70b9\u7684depth positionWS\u8f6c\u5230uv\u7a7a\u95f4\uff0c\u4f7f\u7528\u8be5uv\u91c7\u6837transparent depth tex<\/li>\n<li>\u6bd4\u8f83\u6b65\u8fdb\u540e\u7684length(depth positionWS - camera world position)\u548ctransparent depth\u3002\u82e5\u5927\u4e8e\uff0c\u5219\u8bf4\u660e\u8be5\u6b65\u8fdb\u70b9\u4f4d\u4e8e\u534a\u900f\u660e\u7269\u4f53\u540e\uff0c\u4e58\u4e0a\u6b65\u8fdb\u524d\u7684camera color tex<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>\u91c7\u6837Transparent Depth<\/h2>\n<pre><code class=\"line-numbers\">Pass\n{\n    Name \"Sample Linear 01 Depth For Light Shaft\"\n    Tags\n    {\n        \"LightMode\" = \"SampleLinear01Depth\"\n    }\n\n    HLSLINCLUDE\n    #include \"Assets\/Materials\/Common.hlsl\"\n    ENDHLSL\n\n    HLSLPROGRAM\n    #pragma vertex VS\n    #pragma fragment SampleLinearDepth\n\n    PSInput VS(VSInput i)\n    {\n        PSInput o;\n\n        o.posCS = mul(UNITY_MATRIX_MVP, float4(i.posOS, 1.f));\n        o.posWS = mul(UNITY_MATRIX_M, float4(i.posOS, 1.f));\n\n        o.uv = i.uv;\n        #if defined (UNITY_UV_STARTS_AT_TOP)\n            o.uv.y = 1 - o.uv.y;\n        #endif\n\n        return o;\n    }\n\n    PSOutput SampleLinearDepth(PSInput i)\n    {\n        PSOutput o;\n\n        \/\/ \u7528\u4e8e\u540e\u7eed\u6bd4\u8f83\u6b65\u8fdb\n        float viewDepth = length(i.posWS - _WorldSpaceCameraPos) \/ _ProjectionParams.z;\n        o.color.r = viewDepth;\n\n        return o;\n    }\n    ENDHLSL\n}\n<\/code><\/pre>\n<h2>Depth\u7684\u6b65\u8fdb\u4e0e\u6bd4\u8f83<\/h2>\n<pre><code class=\"line-numbers\">float2 TransformWorldToScreen(float3 positionWS)\n{\n    positionWS = (positionWS - _WorldSpaceCameraPos) * (_ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y)) + _WorldSpaceCameraPos;\n    real2 uv = 0;\n    real3 toCam = mul(unity_WorldToCamera, positionWS);\n    real camPosZ = toCam.z;\n    real height = 2 * camPosZ \/ unity_CameraProjection._m11;\n    real width = _TexParams.x \/ _TexParams.y * height;\n    uv.x = (toCam.x + width \/ 2) \/ width;\n    uv.y = (toCam.y + height \/ 2) \/ height;\n\n    return uv;\n    float4 positionCS = TransformWorldToHClip(positionWS);\n\n    return positionCS.xy * 0.5f + 0.5f;\n}\n\nhalf3 GetLightShaft(float3 viewOrigin, half3 viewDir, float maxDistance, float2 screenPos)\n{\n    \/\/...\n    #if defined(_TRANSPARENT_COLOR_ON)\n    float3 depthRayDir = -_SunDirection;\n    float depthStepLength = _TransparentMaxDistance \/ _TransparentStepCounts;\n    float3 depthStep = depthStepLength * depthRayDir;\n    #endif\n\n    UNITY_LOOP\n    for(int i = 0; i &lt; _StepCount; ++i)\n    {\n        float shadow = GetShadow(currPos);\n\n        if(shadow &gt; 0.f)\n        {\n            float3 currColor = _Brightness * shadow * scatterFun * GetTransmittance(totalDistance);\n\n            #if defined(_TRANSPARENT_COLOR_ON)\n            float3 depthCurrPos = currPos + depthRayDir * ditherDir;\n            UNITY_LOOP\n            for(int j = 0; j &lt; _TransparentStepCounts; ++j)\n            {\n                float2 depth_uv = TransformWorldToScreen(depthCurrPos);\n                float distanceCameraToDepth = length(depthCurrPos - _WorldSpaceCameraPos);\n\n                if(depth_uv.x &lt; 0.f || depth_uv.y &lt; 0.f || depth_uv.x &gt; 1.f || depth_uv.y &gt; 1.f)\n                {\n                    break;\n                }\n\n                float transparentDepth = _LinearDepthTex.Sample(Smp_ClampU_ClampV_Linear, depth_uv).r * _ProjectionParams.z;  \/\/ length posws to world camera pos\n\n                \/\/ \u6b65\u8fdbdepth\u70b9\u4f4d\u4e8e\u534a\u900f\u660e\u7269\u4f53\u540e\u9762\n                if(transparentDepth &lt; distanceCameraToDepth)\n                {\n                    float4 sourceColor = _CameraColorTexture.Sample(Smp_ClampU_ClampV_Linear, depth_uv) * _TransparentColorIntensity;\n                    currColor *= sourceColor;\n                }\n\n                depthCurrPos += depthStep;\n            }\n            #endif\n\n            totalLight += saturate(currColor);\n        }\n\n        currPos += step;\n        totalDistance += stepLength;\n    }\n}\n<\/code><\/pre>\n<p>\u6548\u679c\uff1a<br \/>\n<img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/6704d5e4d29ded1a8c5181e7.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" \/><\/p >\n<noscript><img decoding=\"async\" src=\"https:\/\/pic.imgdb.cn\/item\/6704d5e4d29ded1a8c5181e7.png\" alt=\"\" \/><\/p><\/noscript>\n<h1>Reference<\/h1>\n<p>GPU Pro 5<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/21425792\" target=\"_blank\"  rel=\"nofollow\" >\u6e38\u620f\u5f00\u53d1\u76f8\u5173\u5b9e\u65f6\u6e32\u67d3\u6280\u672f\u4e4b\u4f53\u79ef\u5149<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/linjf520\/article\/details\/104761121\" target=\"_blank\"  rel=\"nofollow\" >Unity Shader - \u6839\u636e\u7247\u6bb5\u6df1\u5ea6\u91cd\u5efa\u7247\u6bb5\u7684\u4e16\u754c\u5750\u6807<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/616923712\" target=\"_blank\"  rel=\"nofollow\" >URP\u7ba1\u7ebf\u4e0b\u7684\u9ad8\u6027\u80fd\u79fb\u52a8\u7aef\u4f53\u79ef\u5149<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.alexandre-pestana.com\/volumetric-lights\/\" target=\"_blank\"  rel=\"nofollow\" >Volumetric lights<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/pubs.rsc.org\/en\/content\/articlehtml\/2019\/nr\/c9nr01707k\" target=\"_blank\"  rel=\"nofollow\" >A novel phase function describing light scattering of layers containing colloidal nanospheres<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.cnblogs.com\/anyview\/p\/5040545.html#:~:text=%E9%99%8D%E9%87%87%E6%A0%B7%EF%BC%8C%E5%8D%B3%E6%98%AF%E9%87%87%E6%A0%B7%E7%82%B9%E6%95%B0\" target=\"_blank\"  rel=\"nofollow\" >\u56fe\u50cf\u7684\u964d\u91c7\u6837\u4e0e\u5347\u91c7\u6837\uff08\u4e8c\u7ef4\u63d2\u503c\uff09<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/zhuanlan.zhihu.com\/p\/526598533\" target=\"_blank\"  rel=\"nofollow\" >\u8499\u7279\u5361\u6d1b\u4f53\u6e32\u67d3\uff08\u56db\uff09\uff1a\u76f8\u51fd\u6570Phase Function<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/eleni.mutantstargoat.com\/hikiko\/on-depth-aware-upsampling\/\" target=\"_blank\"  rel=\"nofollow\" >Depth-aware upsampling experiments (Part 1: Nearest depth)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u89c2\u5bdf\u4e0b\u9762\u8fd9\u526f\u56fe\u53ef\u4ee5\u53d1\u73b0\uff0c\u5728\u660e\u4eae\u5904\u5149\u5f88\u660e\u663e\uff0c\u6697\u5904(\u9634\u5f71\u4e2d)\u6ca1\u6709\u660e\u663e\u7684\u5149\uff0c\u4e14\u8d8a\u6697\u5149\u8d8a\u4e0d\u660e\u663e \u4e3a\u4e86\u8fd8\u539f\u8fd9\u4e00\u73b0\u8c61\uff0c\u53ef\u4ee5\u60f3\u5230\u7684\u662f\u6839\u636e\u76ee &#8230;<\/p>","protected":false},"author":1,"featured_media":170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/169"}],"collection":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=169"}],"version-history":[{"count":6,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":270,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions\/270"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/media\/170"}],"wp:attachment":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}