{"id":181,"date":"2024-07-22T13:36:39","date_gmt":"2024-07-22T05:36:39","guid":{"rendered":"http:\/\/chenglixue.top\/?p=181"},"modified":"2024-08-16T22:54:58","modified_gmt":"2024-08-16T14:54:58","slug":"unity-%e9%87%8d%e5%bb%ba%e6%b3%95%e7%ba%bf%e5%9b%be%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/chenglixue.top\/?p=181","title":{"rendered":"unity \u91cd\u5efa\u6cd5\u7ebf\u56fe\u7684\u4e09\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h1>\u4e3a\u4ec0\u4e48\u8981\u91cd\u5efa\u6cd5\u7ebf\u56fe<\/h1>\n<p>\u5728\u540e\u5904\u7406\u4e2d\u4f1a\u4e22\u5931\u573a\u666f\u4e2d\u7684\u9876\u70b9\u4fe1\u606f\u548c\u6cd5\u7ebf\u4fe1\u606f\uff0c\u4f46\u5728\u63cf\u8fb9\u6216\u505a\u4e00\u4e9b\u5c4f\u5e55\u6548\u679c\u65f6\uff0c\u90fd\u9700\u8981\u7528\u5230\u6cd5\u7ebf\u4fe1\u606f<\/p>\n<h1>\u5982\u4f55\u91cd\u5efa\u6cd5\u7ebf\u56fe<\/h1>\n<p>\u4e3b\u8981\u601d\u8def\u662f\u4f7f\u7528\u53c9\u4e58\uff0c\u56e0\u4e3a\u6cd5\u7ebf\u662f\u5782\u76f4\u4e8e\u9762\u7684\u5411\u91cf\uff0c\u6240\u4ee5\u9700\u8981\u5728\u6c42\u5f97\u67d0\u4e2a\u9762\u7684\u4e24\u4e2a\u5411\u91cf\u3002\u4f46\u4ec5\u4ec5\u662f\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f1a\u5b58\u5728\u7455\u75b5\uff0c\u672c\u7bc7\u5c06\u4ecb\u7ecd\u5982\u4f55\u5bf9\u5176\u8fdb\u884c\u4f18\u5316<\/p>\n<h1>\u5b9e\u73b0<\/h1>\n<h2>\u65b9\u6cd5\u4e00<\/h2>\n<p>\u7b2c\u4e00\u79cd\u65b9\u6cd5\u5373\u662f\u6700\u7b80\u5355\u7684\u53c9\u4e58\uff0c\u53ea\u9700\u91c7\u6837\u4e09\u4e2a\u70b9<\/p>\n<pre><code class=\"line-numbers\">float GetDeviceDepth(float2 uv)\n{\n    return _CameraDepthTexture.SampleLevel(sampler_LinearClamp, uv, 0).r;\n}\n\n\/\/ \u9700\u8981\u6ce8\u610fUNITY_MATRIX_I_P\u770b\u5411z\u8f74\u8d1f\u65b9\u5411\uff0c\u91cd\u5efa\u5f97\u5230\u7684\u503c\u5e94\u5927\u90e8\u5206\u4e3a\u84dd\u8272\nfloat3 ReBuildPosVS(float2 positionVP, float depth)\n{\n    float3 positionNDC = float3(positionVP * 2.f - 1.f, depth);\n    #if defined (UNITY_UV_STARTS_AT_TOP)\n    positionNDC.y = - positionNDC.y;\n    #endif\n\n    float4 positionWS = mul(UNITY_MATRIX_I_P, float4(positionNDC, 1.f));\n    positionWS.xyz \/= positionWS.w;\n\n    return positionWS;\n}\n\nfloat3 ReBuildNormalVS_Low(float2 uv)\n{\n    float depth = GetDeviceDepth(uv);\n    float3 positionVS = ReBuildPosVS(uv, depth);\n\n    return SafeNormalize(cross(ddx(positionVS), ddy(positionVS)));\n}\n<\/code><\/pre>\n<p>\u4e0d\u96be\u5f97\u5230\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/669d1ab9d9c307b7e9e8e783.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\/669d1ab9d9c307b7e9e8e783.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u65b9\u6cd5\u4e8c<\/h2>\n<p>\u4e0d\u96be\u770b\u51fa\uff0c\u65b9\u6cd5\u4e00\u5f97\u5230\u7684\u6cd5\u7ebf\u56fe\uff0c\u5728\u8fb9\u7f18\u6709\u5f88\u591a\u7455\u75b5\u4e14\u5b58\u5728artifacts \uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u8fb9\u7f18\u8ba1\u7b97\u65f6\uff0c\u53ef\u80fd\u91c7\u6837\u7684\u70b9\u7684\u6df1\u5ea6\u5dee\u8f83\u5927\uff0c\u5f97\u5230\u7684\u6548\u679c\u4e0d\u591f\u7406\u60f3<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u4e00\u95ee\u9898\uff0c\u9700\u8981\u9009\u53d6\u6df1\u5ea6\u5dee\u66f4\u5c0f\u7684\u70b9<\/p>\n<pre><code class=\"line-numbers\">float3 ReBuildNormalVS_Medium(float2 uv)\n{\n    float3 posVS_C = ReBuildPosVS(positionVP);\n    float3 posVS_T = ReBuildPosVS(positionVP + float2(0, 1 ));\n    float3 posVS_B = ReBuildPosVS(positionVP + float2(0, -1));\n    float3 posVS_R = ReBuildPosVS(positionVP + float2(1, 0 ));\n    float3 posVS_L = ReBuildPosVS(positionVP + float2(-1, 0));\n\n    float3 depthDiffL = posVS_C - posVS_L;\n    float3 depthDiffR = posVS_R - posVS_C;\n    float3 depthDiffT = posVS_T - posVS_C;\n    float3 depthDiffB = posVS_C - posVS_B;\n\n    float3 horizionVec = abs(depthDiffL.z) &lt; abs(depthDiffR.z) ? depthDiffL : depthDiffR;\n    float3 verticalVec = abs(depthDiffB.z) &lt; abs(depthDiffT.z) ? depthDiffB : depthDiffT;\n\n    return SafeNormalize(cross(horizionVec, verticalVec));\n}\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<br \/>\n<img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66bf67b2d9c307b7e9e38c40.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\/66bf67b2d9c307b7e9e38c40.png\" alt=\"\" \/><\/p><\/noscript>\n<h2>\u65b9\u6cd5\u4e09<\/h2>\n<p>\u65b9\u6cd5\u4e8c\u5f97\u5230\u7684\u6548\u679c\u633a\u4e0d\u9519\u7684\uff0c\u5bf9\u4e8e\u8fb9\u7f18\u7684\u5904\u7406\u662f\u5f88\u4e0d\u9519\u7684\uff0c\u4f46\u5728\u67d0\u4e9b\u5730\u65b9\u4ecd\u7136\u5b58\u5728artifacts \uff0c\u63a5\u4e0b\u6765\u4ecb\u7ecd\u4e00\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u5b8c\u7f8e\u7684\u6cd5\u7ebf\uff0c\u4f46\u6d88\u8017\u4e5f\u968f\u4e4b\u589e\u9ad8<\/p>\n<p>\u9996\u5148\u6765\u89e3\u91ca\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u65b9\u6cd5\u4e8c\u4f1a\u6709\u90e8\u5206\u5730\u65b9\u4ecd\u5b58\u5728artifacts \uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p>\u5de6\u56fe\u4e2d\uff0c\u5f53\u91c7\u6837\u7684\u4e2d\u5fc3\u70b9\u4e3ac\u70b9\u65f6\uff0c\u4e0d\u4f1a\u51fa\u73b0artifacts\uff0c\u8fd9\u662f\u56e0\u4e3a<span class=\"katex math inline\">|d - c|<|c - b|<\/span>\uff0cc\u70b9\u662f\u5f88\u6709\u53ef\u80fd\u5728\u5411\u91cfed\u4e0a\uff0c\u800c\u975eab<\/p>\n<p>\u4f46\u53f3\u56fe\u4e2d\uff0c\u5c31\u51fa\u73b0\u95ee\u9898\u4e86\uff0c\u867d\u7136<span class=\"katex math inline\">|d - c| > |c - b|<\/span>\uff0c\u4f46c\u70b9\u662f\u5728\u5411\u91cfed\u4e0a\uff0c\u5b9e\u9645\u4e0a\u4f7f\u7528\u65b9\u6cd5\u4e8c\u8ba1\u7b97\u4f1a\u9009\u53d6bc\u5411\u91cf\uff0c\u8fd9\u5c31\u5bfc\u81f4artifacts\u7684\u51fa\u73b0<\/p>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/669dd6e8d9c307b7e97da1f0.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\/669dd6e8d9c307b7e97da1f0.png\" alt=\"\" \/><\/p><\/noscript>\n<p>\u8981\u60f3\u5f97\u5230\u7edd\u5bf9\u6b63\u786e\u7684\u6cd5\u7ebf\uff0c\u5c31\u9700\u8981<code>\u989d\u5916\u5224\u65adc\u70b9\u5728\u5411\u91cfab\u4e0a\u8fd8\u662fed\u4e0a<\/code>\u3002\u5177\u4f53\u505a\u6cd5\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<ol>\n<li>\u5ef6\u957f\u7ebf\u6bb5ab\u5230\u70b9c\uff0c\u5f97\u5230\u70b9c<span class=\"katex math inline\">_1<\/span><\/li>\n<li>\u5ef6\u8fdf\u7ebf\u6bb5ed\u5230\u70b9c\uff0c\u5f97\u5230\u70b9c<span class=\"katex math inline\">_2<\/span><\/li>\n<li>\u5982\u679c<span class=\"katex math inline\">|c_1 - c|<|c_2 - c|<\/span>\uff0c\u70b9c\u5728\u5411\u91cfab\u4e0a\uff0c\u5426\u5219\u5728\u5411\u91cfde\u4e0a<\/li>\n<\/ol>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/669dd90fd9c307b7e97f13a6.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\/669dd90fd9c307b7e97f13a6.png\" alt=\"\" \/><\/p><\/noscript>\n<p>\u53e6\u5916\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7ecf\u8fc7\u900f\u89c6\u53d8\u6362\u540e\uff0c\u6df1\u5ea6\u662f\u975e\u7ebf\u6027\u7684\uff0c\u5bf9\u4e8e\u5c4f\u5e55\u4e0a\u7b49\u8ddd\u5206\u5e03\u7684\u4e09\u4e2a\u70b9ABC\uff0c\u5f53\u4ed6\u4eec\u5728\u4e16\u754c\u7a7a\u95f4\u4e2d\u5904\u4e8e\u540c\u4e00\u6761\u76f4\u7ebf\u65f6\uff0c\u6df1\u5ea6\u503c\u5927\u5c0f\u7684\u5173\u7cfb\u662f<span class=\"katex math inline\">2 Depth_B = Depth_A + Depth_C<\/span><\/p>\n<pre><code class=\"line-numbers\">float3 ReBuildNormalVS_High(float2 uv)\n{\n    float3 posVS_C = ReBuildPosVS(positionVP);\n    float3 posVS_T = ReBuildPosVS(positionVP + float2(0, 1 ));\n    float3 posVS_B = ReBuildPosVS(positionVP + float2(0, -1));\n    float3 posVS_R = ReBuildPosVS(positionVP + float2(1, 0 ));\n    float3 posVS_L = ReBuildPosVS(positionVP + float2(-1, 0));\n\n    float3 depthDiff_L = posVS_C - posVS_L;\n    float3 depthDiff_R = posVS_R - posVS_C;\n    float3 depthDiff_T = posVS_T - posVS_C;\n    float3 depthDiff_B = posVS_C - posVS_B;\n\n    float centerDepth = GetDeviceDepth(positionVP * _ViewSize.zw);\n    float4 horizionDepth = float4(\n        GetDeviceDepth(positionVP + float2(-1.f, 0.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(1.f, 0.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(-2.f, 0.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(2.f, 0.f) * _ViewSize.zw)\n    );\n    float4 verticalDepth = float4(\n        GetDeviceDepth(positionVP + float2(0.f, -1.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(0.f, 1.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(0.f, -2.f) * _ViewSize.zw),\n        GetDeviceDepth(positionVP + float2(0.f, 2.f) * _ViewSize.zw)\n    );\n\n    float2 horizionDepthDiff = abs(horizionDepth.xy * 2 - horizionDepth.zw - centerDepth);\n    float2 verticalDepthDiff = abs(verticalDepth.xy * 2 - verticalDepth.zw - centerDepth);\n\n    float3 horizionVec = horizionDepthDiff.x &lt; horizionDepthDiff.y ? depthDiff_L : depthDiff_R;\n    float3 verticalVec = verticalDepthDiff.x &lt; verticalDepthDiff.y ? depthDiff_B : depthDiff_T;\n\n    float3 normalVS = SafeNormalize(cross(horizionVec, verticalVec));\n\n    return normalVS;\n}\n<\/code><\/pre>\n<p><img decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic.imgdb.cn\/item\/66bf67d8d9c307b7e9e3af90.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\/66bf67d8d9c307b7e9e3af90.png\" alt=\"\" \/><\/p><\/noscript>\n<h1>\u603b\u7ed3<\/h1>\n<p>\u603b\u7684\u6765\u8bf4\uff0c\u65b9\u6cd5\u4e8c\u548c\u65b9\u6cd5\u4e09\u6548\u679c\u4e0d\u4f1a\u7279\u522b\u5927\uff0c\u5728\u505a\u540e\u5904\u7406\u65f6\uff0c\u53ef\u4ee5\u9488\u5bf9\u8fd9\u4e09\u79cd\u91cd\u5efa\u6cd5\u7ebf\u7684\u65b9\u5f0f\u8fdb\u884c\u753b\u8d28\u5206\u7ea7<\/p>\n<h1>\u4ee3\u7801\u4ed3\u5e93<\/h1>\n<p>https:\/\/github.com\/chenglixue\/Unity-RebuildNomralVS\/tree\/main<\/p>\n<h1>Reference<\/h1>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/wickedengine.net\/2019\/09\/improved-normal-reconstruction-from-depth\/\" target=\"_blank\"  rel=\"nofollow\" >Improved normal reconstruction from depth<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/atyuwen.github.io\/posts\/normal-reconstruction\/\" target=\"_blank\"  rel=\"nofollow\" >Accurate Normal Reconstruction from Depth Buffer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u8981\u91cd\u5efa\u6cd5\u7ebf\u56fe \u5728\u540e\u5904\u7406\u4e2d\u4f1a\u4e22\u5931\u573a\u666f\u4e2d\u7684\u9876\u70b9\u4fe1\u606f\u548c\u6cd5\u7ebf\u4fe1\u606f\uff0c\u4f46\u5728\u63cf\u8fb9\u6216\u505a\u4e00\u4e9b\u5c4f\u5e55\u6548\u679c\u65f6\uff0c\u90fd\u9700\u8981\u7528\u5230\u6cd5\u7ebf\u4fe1\u606f \u5982\u4f55\u91cd\u5efa\u6cd5\u7ebf\u56fe \u4e3b &#8230;<\/p>","protected":false},"author":1,"featured_media":182,"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\/181"}],"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=181"}],"version-history":[{"count":3,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":218,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions\/218"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=\/wp\/v2\/media\/182"}],"wp:attachment":[{"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/chenglixue.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}