{"id":151,"date":"2017-07-14T18:09:12","date_gmt":"2017-07-14T10:09:12","guid":{"rendered":"http:\/\/49.232.131.197\/?p=151"},"modified":"2017-07-14T18:12:34","modified_gmt":"2017-07-14T10:12:34","slug":"shader","status":"publish","type":"post","link":"http:\/\/www.sourcecool.com\/index.php\/2017\/07\/14\/shader\/","title":{"rendered":"\u6cd5\u7ebf\u7684\u6545\u4e8b"},"content":{"rendered":"<p>\u597d\u4e45\u4e0d\u5199shader\u4e86\uff0c\u679c\u7136\u5fd8\u7684\u5f88\u5feb\uff0c\u8fd8\u662f\u9700\u8981\u8bb0\u5f55\u4e00\u4e0b\uff0c\u8fd9\u6837\u624d\u4e0d\u4f1a\u7ecf\u5e38\u9020\u8f66\u8f6e\u3002<\/p>\n<h1>\u6cd5\u7ebf\u7684\u771f\u4eba\u771f\u4e8b<\/h1>\n<p>shader\u91cc\u9762\uff0c\u7ecf\u5e38\u4f1a\u7528\u5230\u6cd5\u7ebf\u5b9e\u73b0\u4e00\u4e9b\u6548\u679c\uff0c\u6cd5\u7ebf\u5230\u4e16\u754c\u5750\u6807\u7cfb\u7684\u8f6c\u6362\u4e5f\u662f\u7ecf\u5e38\u7684\uff0c\u5751\u4e5f\u4e0d\u5c11\uff1a<\/p>\n<div>\n<blockquote>\n<div>mul(IT_M, norm) =&gt; mul(norm, I_M) =&gt; {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}<\/div>\n<\/blockquote>\n<div>\n<div>\u5c06\u70b9\u6216\u8005\u666e\u901a\u7684\u5411\u91cf\u4ece\u672c\u5730\u5750\u6807\u7cfb\u8f6c\u6362\u5230\u4e16\u754c\u5750\u6807\u7cfb\uff0c\u53ea\u8981\u6309\u7167\u4e0a\u9762\u8bf4\u7684\uff0c\u7528\u8be5\u8f6c\u6362\u77e9\u9635\u5de6\u4e58\u539f\u5750\u6807\u503c\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f<strong>\u5bf9\u4e8e\u6cd5\u7ebf\uff0c\u8fd8\u9700\u8981\u4e00\u4e9b\u7279\u6b8a\u7684\u5904\u7406<\/strong>\u3002<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u4e3a\u4ec0\u4e48\uff1f<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff0c\u5c31\u9700\u8981\u642c\u51fa\u6cd5\u7ebf\u7684\u5b9a\u4e49\uff1a<strong>\u4e09\u7ef4\u5e73\u9762\u7684\u6cd5\u7ebf\u662f\u5782\u76f4\u4e8e\u8be5\u5e73\u9762\u7684\u4e09\u7ef4\u5411\u91cf\u3002\u66f2\u9762\u5728\u67d0\u70b9P\u5904\u7684\u6cd5\u7ebf\u4e3a\u5782\u76f4\u4e8e\u8be5\u70b9\u5207\u5e73\u9762\u7684\u5411\u91cf\u3002\u6240\u4ee5\uff0c\u5728\u8f6c\u6362\u4e4b\u540e\uff0c\u8fd8\u9700\u8981\u4fdd\u6301\u539f\u6709\u7684\u5782\u76f4\u5173\u7cfb<\/strong>\u3002<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u6309\u7167\u4e0a\u9762\u4e3e\u4f8b\u4e2d\u7684\u53d8\u6362\u77e9\u9635\uff0c\u5047\u5b9a\u67d0\u6761\u6cd5\u7ebf\u4e3a\uff081\uff0c1\uff0c0\uff09\uff0c\u4e0e\u4e4b\u5782\u76f4\u7684\u4e00\u4e2a\u5411\u91cf\uff081\uff0c-1\uff0c0\uff09\u3002\u5982\u679c\u5de6\u4e58\u53d8\u6362\u77e9\u9635\uff0c\u5219\u6cd5\u7ebf\u53d8\u4e3a\uff083.5\uff0c3.5\uff0c-0.7\uff09\uff0c\u5411\u91cf\u53d8\u4e3a\uff080.7\uff0c-3.5\uff0c-3.5\uff09\u3002\u53d8\u6362\u4e4b\u540e\u7684\u5185\u79ef\u4e3a -7\uff0c\u5373\u5df2\u7ecf\u4e0d\u518d\u5782\u76f4\u3002<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u5176\u5b9e\u4ece\u6570\u5b66\u4e0a\u770b\u8fd9\u662f\u663e\u7136\u7684\uff1a<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-153\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/20170714172828-300x114.png\" alt=\"\" width=\"563\" height=\"214\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/20170714172828-300x114.png 300w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/20170714172828.png 538w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/div>\n<div>\u800c\u4ece\u76f4\u89c2\u4e0a\uff0c\u539f\u56e0\u662f\u8fd9\u6837\u7684\uff1a<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-154\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714172929-300x177.png\" alt=\"\" width=\"300\" height=\"177\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714172929-300x177.png 300w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714172929.png 550w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/div>\n<\/div>\n<div>\u56e0\u6b64\u8981\u6b63\u786e\u7684\u8fdb\u884c\u6cd5\u7ebf\u7684\u53d8\u6362\uff0c\u8981\u4f7f\u5f97\u4fdd\u6301 V \u548c N \u7684\u5185\u79ef\u4e3a0\uff0c\u56e0\u6b64\u8981\u8ba9<strong>\u00a0N \u5de6\u4e58 Trans\u7684\u9006\u8f6c\u7f6e\u77e9\u9635<\/strong>\uff0c\u8fd9\u6837\u6709\uff1a<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-155\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/3222211-300x40.png\" alt=\"\" width=\"413\" height=\"55\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/3222211-300x40.png 300w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/3222211.png 562w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/div>\n<div>\u4f46\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u5e38\u5e38\u89c1\u5230\u8fd9\u6837\u7684\u5199\u6cd5\uff1a<\/div>\n<div>\n<pre class=\"lang:c++ decode:true \">normalize(mul(normal, (float3x3)unity_WorldToObject));<\/pre>\n<p>\u6ce8\u610f\u5230\u8f93\u51fa\u7684vector4\uff0c\u5728\u6570\u636e\u683c\u5f0f\u4e0a\uff0c\u8f6c\u7f6e\u4e0e\u4e0d\u8f6c\u7f6e\u5b9e\u9645\u4e0a\u5e76\u6ca1\u6709\u533a\u522b\uff0c\uff08shader\u4e2d\uff0c\u8f6c\u7f6e\u5b9e\u9645\u4e0a\u53ea\u5bf9\u77e9\u9635\u6709\u6548\u679c\uff09\u3002\u56e0\u6b64\uff0c<strong>mul(V, M) \u5c31\u76f8\u5f53\u4e8e mul( tranpose(M), V )<\/strong>\u66f4\u8fdb\u4e00\u6b65\u7684\uff0c<strong>\u5bf9\u4e8e\u6b63\u4ea4\u77e9\u9635\uff0c\u5176\u8f6c\u7f6e\u77e9\u9635\u7b49\u4e8e\u9006\u77e9\u9635\uff0c\u90a3\u4e48\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u5f97\u5230\u4ed6\u7684\u9006\u77e9\u9635<\/strong>\uff0c\u8fd9\u4e2a\u7279\u6027\u6211\u4eec\u540e\u9762\u8fd8\u4f1a\u770b\u5230\u3002<\/p>\n<h2>\u4f7f\u7528\u6cd5\u7ebf\u7eb9\u7406<\/h2>\n<p>\u4f46\u5176\u5b9e\uff0cUnity\u5728\u80cc\u540e\u505a\u4e86\u5f88\u591a\u3002\u867d\u7136\u6211\u4eec\u4f7f\u7528\u4e86\u540c\u6837\u7684Lighting&lt;Name&gt;\u51fd\u6570\uff0c\u4f46\u5176\u4e2dnormal\u3001lightDir\u3001viewDir\u6240\u5728\u7684\u5750\u6807\u7cfb\u5df2\u7ecf\u88abUnity\u8f6c\u6362\u8fc7\u4e86\u3002\u8fd9\u6b21\uff0c<strong>\u5b83\u4eec\u4f7f\u7528\u7684\u5750\u6807\u7cfb\u662fTangent Space<\/strong>\u3002\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u5b83\uff0c\u6ca1\u5173\u7cfb\u6211\u4eec\u9a6c\u4e0a\u5c31\u4f1a\u8bb2\u8fd9\u4e2a\u5750\u6807\u7cfb\u7684\u7ec6\u8282\u3002<\/p>\n<p>\u4f46\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528UnpackNormal\u8fd9\u4e2a\u51fd\u6570\u3002\u8fd9\u5c31\u7275\u626f\u5230\u6211\u4eec\u7684<strong>\u7b2c\u4e00\u4e2a\u95ee\u9898\uff1a<\/strong><strong>\u4e3a\u4ec0\u4e48\u6cd5\u7ebf\u7eb9\u7406\u901a\u5e38\u90fd\u662f\u504f\u84dd\u8272\u7684\uff1f<\/strong>\u5b83\u91cc\u9762\u5230\u5e95\u662f\u5b58\u50a8\u7684\u4ec0\u4e48\u5462\uff1f\u4f60\u4f1a\u8bf4\uff0c\u5f53\u7136\u662f\u6cd5\u7ebf\u5566\uff01\u90a3\u4e48\u5b83\u7684\u6240\u5728\u5750\u6807\u7cfb\u662f\u4ec0\u4e48\u5462\uff1f\u662fWorld Space\uff1fObject Space\uff1f\u8fd8\u662fView Space\uff1f<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u901a\u5e38\u89c1\u5230\u7684\u8fd9\u79cd\u504f\u84dd\u8272\u7684\u6cd5\u7ebf\u7eb9\u7406\u4e2d\uff0c\u5b58\u50a8\u7684\u662f\u5728Tangent Space\u4e2d\u7684\u9876\u70b9\u6cd5\u7ebf\u65b9\u5411\u3002\u90a3\u4e48\uff0c\u95ee\u9898\u53c8\u6765\u4e86\uff0c\u4ec0\u4e48\u662fTangent Space\uff08\u6709\u65f6\u4e5f\u53ebobject local coordinate system\uff09\uff1f\u770b\u5230\u65b0\u540d\u8bcd\u4e0d\u8981\u6015\uff0c\u5750\u6807\u7cfb\u561b\uff0c\u65e0\u975e\u5c31\u662f\u539f\u70b9+\u4e09\u4e2a\u5750\u6807\u8f74\u51b3\u5b9a\u7684\u4e00\u4e2a\u76f8\u5bf9\u7a7a\u95f4\u561b\uff0c\u6211\u4eec\u53ea\u8981\u641e\u6e05\u695a\u539f\u70b9\u548c\u4e09\u4e2a\u5750\u6807\u8f74\u662f\u4ec0\u4e48\u5c31\u53ef\u4ee5\u4e86\u3002\u5728Tangent Space\u4e2d\uff0c\u5750\u6807\u539f\u70b9\u5c31\u662f\u9876\u70b9\u7684\u4f4d\u7f6e\uff0c\u5176\u4e2dz\u8f74\u662f\u8be5\u9876\u70b9\u672c\u8eab\u7684\u6cd5\u7ebf\u65b9\u5411\uff08N\uff09\u3002\u8fd9\u6837\uff0c\u53e6\u5916\u4e24\u4e2a\u5750\u6807\u8f74\u5c31\u662f\u548c\u8be5\u70b9\u76f8\u5207\u7684\u4e24\u6761\u5207\u7ebf\u3002\u8fd9\u6837\u7684\u5207\u7ebf\u672c\u6765\u6709\u65e0\u6570\u6761\uff0c\u4f46\u6a21\u578b\u4e00\u822c\u4f1a\u7ed9\u5b9a\u8be5\u9876\u70b9\u7684\u4e00\u4e2atangent\uff0c\u8fd9\u4e2atangent\u65b9\u5411\u4e00\u822c\u662f\u4f7f\u7528\u548c\u7eb9\u7406\u5750\u6807\u65b9\u5411\u76f8\u540c\u7684\u90a3\u6761tangent\uff08T\uff09\u3002\u800c\u53e6\u4e00\u4e2a\u5750\u6807\u8f74\u7684\u65b9\u5411\uff08B\uff09\u5c31\u53ef\u4ee5\u901a\u8fc7normal\u548ctangent\u7684\u53c9\u4e58\u5f97\u5230\u3002\u4e0a\u8ff0\u8fc7\u7a0b\u53ef\u4ee5\u5982\u4e0b\u56fe\u6240\u793a\uff08\u6765\u6e90\uff1a<a href=\"http:\/\/www.opengl-tutorial.org\/intermediate-tutorials\/tutorial-13-normal-mapping\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.opengl-tutorial.org\/intermediate-tutorials\/tutorial-13-normal-mapping\/<\/a>\uff09\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-156\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714173737-300x231.png\" alt=\"\" width=\"300\" height=\"231\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714173737-300x231.png 300w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714173737.png 339w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u901a\u5e38\u6211\u4eec\u6240\u89c1\u7684\u6cd5\u7ebf\u7eb9\u7406\u8fd8\u662f\u57fa\u4e8e\u539f\u6cd5\u7ebf\u4fe1\u606f\u6784\u5efa\u7684\u5750\u6807\u7cfb\u6765\u6784\u5efa\u51fa\u6765\u7684\u3002\u90a3\u79cd\u504f\u84dd\u8272\u7684\u6cd5\u7ebf\u7eb9\u7406\u5176\u5b9e\u5c31\u662f\u5b58\u50a8\u4e86\u5728\u6bcf\u4e2a\u9876\u70b9\u5404\u81ea\u7684Tangent Space\u4e2d\uff0c\u6cd5\u7ebf\u7684\u6270\u52a8\u65b9\u5411\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u9876\u70b9\u7684\u6cd5\u7ebf\u65b9\u5411\u4e0d\u53d8\uff0c\u90a3\u4e48\u5728\u5b83\u7684Tangent Space\u4e2d\uff0c\u65b0\u7684normal\u503c\u5c31\u662fz\u8f74\u65b9\u5411\uff0c\u4e5f\u5c31\u662f\u8bf4\u503c\u4e3a(0, 0, 1)\u3002\u4f46\u8fd9\u5e76\u4e0d\u662f\u6cd5\u7ebf\u7eb9\u7406\u4e2d\u5b58\u50a8\u7684\u6700\u7ec8\u503c\uff0c\u56e0\u4e3a\u4e00\u4e2a\u5411\u91cf\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u53d6\u503c\u8303\u56f4\u5728(-1, 1)\uff0c\u800c\u7eb9\u7406\u6bcf\u4e2a\u901a\u9053\u7684\u503c\u8303\u56f4\u5728(0, 1)\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u505a\u4e00\u4e2a\u6620\u5c04\uff0c\u5373pixel = (normal + 1) \/ 2\u3002\u8fd9\u6837\uff0c\u4e4b\u524d\u7684\u6cd5\u7ebf\u503c(0, 0, 1)\u5b9e\u9645\u4e0a\u5bf9\u5e94\u4e86\u6cd5\u7ebf\u7eb9\u7406\u4e2dRGB\u7684\u503c\u4e3a(0.5, 0.5, 1)\uff0c\u800c\u8fd9\u4e2a\u989c\u8272\u4e5f\u5c31\u662f\u6cd5\u7ebf\u7eb9\u7406\u4e2d\u90a3\u5927\u7247\u7684\u84dd\u8272\u3002\u8fd9\u4e9b\u84dd\u8272\u5b9e\u9645\u4e0a\u8bf4\u660e\u9876\u70b9\u7684\u5927\u90e8\u5206\u6cd5\u7ebf\u662f\u548c\u6a21\u578b\u672c\u8eab\u6cd5\u7ebf\u4e00\u6837\u7684\uff0c\u4e0d\u9700\u8981\u6539\u53d8\u3002\u603b\u7ed3\u4e00\u4e0b\u5c31\u662f\uff0c\u6cd5\u7ebf\u7eb9\u7406\u7684RGB\u901a\u9053\u5b58\u50a8\u4e86\u5728\u6bcf\u4e2a\u9876\u70b9\u5404\u81ea\u7684Tangent Space\u4e2d\u7684\u6cd5\u7ebf\u65b9\u5411\u7684\u6620\u5c04\u503c\u3002<\/p>\n<p>\u7b2c\u4e00\u6b21\u63a5\u89e6Tangent Space\u4f1a\u8ba9\u4eba\u89c9\u5f97\u6bd4\u8f83\u96be\u7406\u89e3\uff0c\u800c\u6a21\u578b\u539f\u59cb\u7684\u6cd5\u7ebf\u5176\u5b9e\u662f\u5b9a\u4e49\u5728Object Space\u4e2d\u7684\uff0c\u90a3\u4e3a\u4ec0\u4e48\u6cd5\u7ebf\u7eb9\u7406\u5c31\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5728Object Space\u4e2d\u7684\u65b0\u6cd5\u7ebf\u4fe1\u606f\u5462\uff1f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u5bf9\u5e94\u4e86\u4e24\u79cd\u6cd5\u7ebf\u7eb9\u7406\u2014\u2014<strong>Object-Space Normal Map<\/strong>\u548c<strong>Tangent-Space Normal Map<\/strong>\u3002\u5b83\u4eec\u5206\u522b\u5bf9\u5e94\u4e86\u4e0b\u9762\u4e24\u79cd\u6837\u5b50\u7684\u7eb9\u7406\uff08\u6765\u6e90\uff1a<a href=\"http:\/\/www.surlybird.com\/tutorials\/TangentSpace\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.surlybird.com\/tutorials\/TangentSpace\/<\/a>\uff09\uff1a<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-157\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/ssss.png\" alt=\"\" width=\"256\" height=\"256\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/ssss.png 256w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/ssss-150x150.png 150w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-158\" src=\"http:\/\/49.232.131.197\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714174242.png\" alt=\"\" width=\"256\" height=\"256\" srcset=\"http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714174242.png 256w, http:\/\/www.sourcecool.com\/wp-content\/uploads\/2017\/07\/\u56fe\u724720170714174242-150x150.png 150w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/p>\n<p>\u4ece\u89c6\u89c9\u4e0a\u6765\u8bf4\uff0cObject-Space Normal Map\u4e94\u989c\u516d\u8272\uff0c\u539f\u56e0\u662f\u5b83\u662f\u57fa\u4e8eObject Space\u5b58\u50a8\u7684\uff0c\u65b9\u5411\u5404\u5f02\u3002\u5982\u679c\u6211\u4eec\u628a\u6a21\u578b\u672c\u8eab\u81ea\u5e26\u7684\u6cd5\u7ebf\u6620\u5c04\u5230\u4e00\u5f20\u7eb9\u7406\u4e0a\uff0c\u5c31\u662f\u4e00\u5f20Object-Space Normal Map\uff1b\u800cTangent-Space Normal Map\u5982\u6211\u4eec\u524d\u9762\u6240\u8bf4\uff0c\u662f\u504f\u84dd\u8272\u7684\u3002\u539f\u56e0\u662f\u5b83\u57fa\u4e8e\u6bcf\u4e2a\u9876\u70b9\u7684Tangent Space\uff0c\u5f88\u591a\u9876\u70b9\u6cd5\u7ebf\u53ea\u662f\u5728\u539f\u6cd5\u7ebf\u7684\u57fa\u7840\u4e0a\u7565\u5fae\u6709\u4e9b\u504f\u79fb\u800c\u5df2\u3002<\/p>\n<p>\u603b\u4f53\u6765\u8bf4\uff0cObject-Space Normal Map\u66f4\u7b26\u5408\u6211\u4eec\u4eba\u7c7b\u7684\u76f4\u89c2\u8ba4\u8bc6\uff0c\u800c\u4e14\u6cd5\u7ebf\u7eb9\u7406\u672c\u8eab\u4e5f\u5f88\u76f4\u89c2\uff0c\u5bb9\u6613\u8c03\u6574\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u989c\u8272\u5c31\u4ee3\u8868\u4e86\u4e0d\u540c\u7684\u989c\u8272\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u8fd9\u4e48\u201c\u8e69\u811a\u201d\u7684Tangent Space\u6765\u5b58\u50a8\u6cd5\u7ebf\u7eb9\u7406\u91cc\uff08\u8d77\u7801\u5927\u90e8\u5206\u90fd\u662f\uff09\uff1f\u800c\u4e14Unity\u91cc\u662f\u4ec5\u652f\u6301Tangent-Space Normal Map\u7684\u6cd5\u7ebf\u7eb9\u7406\u7684\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u6cd5\u7ebf\u672c\u8eab\u5b58\u50a8\u5728\u54ea\u4e2a\u5750\u6807\u7cfb\u4e2d\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u4f8b\u5982\u5b58\u50a8\u5728World Space\u3001\u6216\u8005Object Space\u3001\u6216\u8005Tangent Space\u4e2d\u3002\u4f46\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5e76\u4e0d\u662f\u5355\u7eaf\u7684\u60f3\u8981\u5f97\u5230\u6cd5\u7ebf\uff0c\u540e\u7eed\u7684\u5149\u7167\u8ba1\u7b97\u624d\u662f\u6211\u4eec\u7684\u76ee\u7684\u3002\u800c\u9009\u62e9\u54ea\u4e2a\u5750\u6807\u7cfb\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u628a\u5176\u4ed6\u4fe1\u606f\uff08\u4f8b\u5982viewDir\u548clightDir\uff09\u8f6c\u6362\u5230\u76f8\u5e94\u7684\u5750\u6807\u7cfb\u4e2d\u3002\u800c\u7f51\u4e0a\u5173\u4e8e\u8fd9\u4e24\u79cd\u6cd5\u7ebf\u7eb9\u7406\uff08World Space\u4f7f\u7528\u7684\u6bd4\u8f83\u5c11\uff0c\u6682\u65f6\u5ffd\u7565\uff09\u7684\u9009\u62e9\u5404\u79cd\u5404\u6837\uff0c\u6709\u4e9b\u89c2\u70b9\u6211\u4e5f\u89c9\u5f97\u65e0\u6cd5\u7406\u89e3\u3002\u6709\u7684\u4eba\u8bb2\u5e94\u8be5\u53ea\u7528Tangent Space\uff0c\u6709\u7684\u8bb2Object Space\u66f4\u5feb\u66f4\u597d\uff0c\u6709\u7684\u4eba\u8ba4\u4e3aObject Space\u4e0d\u53ef\u4ee5\u7528\u4e8e\u53ef\u53d8\u5f62\u7684\u7269\u4f53\uff0c\u800c\u53e6\u4e00\u4e9b\u4eba\u8bf4\u53ef\u4ee5\uff08\u6211\u4e5f\u8ba4\u4e3a\u4f7f\u7528\u54ea\u79cd\u5750\u6807\u7cfb\u90fd\u53ef\u4ee5\u5728\u6e38\u620f\u91cc\u5f97\u5230\u6b63\u786e\u7684\u6548\u679c\uff0c\u53ea\u8981\u901a\u8fc7\u5408\u9002\u7684\u5750\u6807\u7cfb\u8f6c\u6362\uff09\u3002\u4e0b\u9762\u662f\u603b\u7ed3\u7684\u6211\u6bd4\u8f83\u8ba4\u540c\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<ul>\n<li><strong>\u4f7f\u7528Object-Space\u7684\u4f18\u70b9<\/strong>\n<ul>\n<li>\u5b9e\u73b0\u7b80\u5355\uff0c\u66f4\u52a0\u76f4\u89c2\u3002\u6211\u4eec\u751a\u81f3\u90fd\u4e0d\u9700\u8981\u6a21\u578b\u539f\u59cb\u7684normal\u548ctangent\u7b49\u4fe1\u606f\uff0c\u4e5f\u5c31\u662f\u8bf4\u8ba1\u7b97\u66f4\u5c11\u3002\u751f\u6210\u5b83\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u800c\u5982\u8981\u8981\u751f\u6210Tangent-Space Normal Map\u7684\u8bdd\uff0c\u7531\u4e8e\u5b83\u7684tangent\u662f\u548cUV\u65b9\u5411\u76f8\u540c\uff0c\u56e0\u6b64\u60f3\u8981\u6548\u679c\u6bd4\u8f83\u597d\u7684Normal Map\u7684\u8bdd\u8981\u6c42UV Map\u4e5f\u662f\u8fde\u7eed\u7684\u3002<\/li>\n<li>\u5728UV\u7f1d\u5408\u5904\u548c\u5c16\u9510\u7684\u8fb9\u89d2\u90e8\u5206\uff0c\u53ef\u89c1\u7684\u7a81\u53d8\uff08\u7f1d\u9699\uff09\u8f83\u5c11\uff0c\u53ef\u4ee5\u63d0\u4f9b\u5e73\u6ed1\u7684\u8fb9\u754c\u3002\u8fd9\u662f\u56e0\u4e3aObject-Space Normal Map\u5b58\u50a8\u7684\u662f\u540c\u4e00\u5750\u6807\u7cfb\u4e0b\u7684\u6cd5\u7ebf\u4fe1\u606f\uff0c\u56e0\u6b64\u5728\u8fb9\u754c\u5904\u901a\u8fc7\u63d2\u503c\u5f97\u5230\u7684\u6cd5\u7ebf\u53ef\u4ee5\u5e73\u7f13\u53d8\u6362\u3002\u800cTangent-Space Normal Map\u4e2d\u7684\u6cd5\u7ebf\u4fe1\u606f\u5219\u4f9d\u9760UV\u7684\u65b9\u5411\u548c\u4e09\u89d2\u5316\u7ed3\u679c\uff0c\u53ef\u80fd\u5728\u8fb9\u7f18\u5904\u6216\u5c16\u9510\u7684\u90e8\u5206\u4f1a\u9020\u6210\u66f4\u591a\u53ef\u89c1\u7684\u7f1d\u5408\u8ff9\u8c61\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4f7f\u7528<\/strong><strong>Tangent-Space\u7684\u4f18\u70b9<\/strong>\n<ul>\n<li>\u81ea\u7531\u5ea6\u5f88\u9ad8\u3002Object-Space Normal Map\u8bb0\u5f55\u7684\u662f\u7edd\u5bf9\u6cd5\u7ebf\u4fe1\u606f\uff0c\u4ec5\u53ef\u7528\u4e8e\u521b\u5efa\u5b83\u65f6\u7684\u90a3\u4e2a\u6a21\u578b\uff0c\u800c\u5e94\u7528\u5230\u5176\u4ed6\u6a21\u578b\u4e0a\u6548\u679c\u5c31\u5b8c\u5168\u9519\u8bef\u4e86\u3002\u800cTangent-Space Normal Map\u8bb0\u5f55\u7684\u662f\u76f8\u5bf9\u6cd5\u7ebf\u4fe1\u606f\uff0c\u8fd9\u610f\u5473\u7740\uff0c\u5373\u4fbf\u628a\u8be5\u7eb9\u7406\u5e94\u7528\u5230\u4e00\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u7f51\u683c\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5408\u7406\u7684\u7ed3\u679c\u3002<\/li>\n<li>\u53ef\u8fdb\u884cUV\u52a8\u753b\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u79fb\u52a8\u4e00\u4e2a\u7eb9\u7406\u7684UV\u5750\u6807\u6765\u5b9e\u73b0\u4e00\u4e2a\u51f9\u51f8\u79fb\u52a8\u7684\u6548\u679c\uff0c\u4f46\u4f7f\u7528Object-Space Normal Map\u4f1a\u5f97\u5230\u5b8c\u5168\u9519\u8bef\u7684\u7ed3\u679c\u3002\u539f\u56e0\u540c\u4e0a\u3002\u8fd9\u79cdUV\u52a8\u753b\u5728\u6c34\u6216\u8005\u706b\u5c71\u7194\u5ca9\u8fd9\u79cd\u7c7b\u578b\u7684\u7269\u4f53\u4f1a\u4f1a\u7528\u5230\u3002<\/li>\n<li>\u53ef\u4ee5\u91cd\u7528Normal Map\u3002\u6bd4\u5982\uff0c\u4e00\u4e2a\u7816\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u4ec5\u4f7f\u7528\u4e00\u5f20Normal Map\u5c31\u53ef\u4ee5\u7528\u5230\u6240\u6709\u7684\u516d\u4e2a\u9762\u4e0a\u3002<\/li>\n<li>\u53ef\u538b\u7f29\u3002\u7531\u4e8eTangent-Space Normal Map\u4e2d\u6cd5\u7ebf\u7684Z\u65b9\u5411\u603b\u662f\u6b63\u65b9\u5411\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u4ec5\u5b58\u50a8XY\u65b9\u5411\uff0c\u800c\u63a8\u5bfc\u5f97\u5230Z\u65b9\u5411\u3002\u800cObject-Space Normal Map\u7531\u4e8e\u6bcf\u4e2a\u65b9\u5411\u90fd\u662f\u5b8c\u5168\u53ef\u80fd\u7684\uff0c\u56e0\u6b64\u5fc5\u987b\u5b58\u50a8\u4e09\u4e2a\u65b9\u5411\u7684\u503c\uff0c\u4e0d\u53ef\u538b\u7f29\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Tangent Space\u7684\u524d\u4e24\u4e2a\u4f18\u70b9\u8db3\u4ee5\u8ba9\u5f88\u591a\u4eba\u653e\u5f03Object Space\u800c\u9009\u62e9\u5b83\u4e86\u3002\u4e0b\u9762\u7684\u94fe\u63a5\u91cc\u6709\u66f4\u6df1\u5165\u5730\u8ba8\u8bba\uff08\u4e0d\u4fdd\u8bc1\u89c2\u70b9\u7684\u6b63\u786e\u6027\uff09\uff1a<\/p>\n<ul>\n<li><a href=\"http:\/\/www.valvetime.net\/threads\/tangent-vs-object-vs-world-normal-maps.81018\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.valvetime.net\/threads\/tangent-vs-object-vs-world-normal-maps.81018\/<\/a><\/li>\n<li><a href=\"http:\/\/www.surlybird.com\/tutorials\/TangentSpace\/\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.surlybird.com\/tutorials\/TangentSpace\/<\/a>\uff08\u611f\u89c9\u91cc\u9762\u7684\u8bf4\u6cd5\u4e0d\u662f\u5f88\u51c6\u786e\uff09<\/li>\n<li><a href=\"http:\/\/docs.cryengine.com\/plugins\/servlet\/mobile#TangentSpaceNormalMapping-DrawbacksofTangentSpaceLighting\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/docs.cryengine.com\/plugins\/servlet\/mobile#TangentSpaceNormalMapping-DrawbacksofTangentSpaceLighting<\/a><\/li>\n<li><a href=\"http:\/\/docs.cryengine.com\/display\/SDKDOC4\/Tangent+Space+Normal+Mapping\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/docs.cryengine.com\/display\/SDKDOC4\/Tangent+Space+Normal+Mapping<\/a><\/li>\n<li><a href=\"http:\/\/gamedev.stackexchange.com\/questions\/31499\/what-are-the-advantages-of-tangent-space-normals-over-object-space-normals\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/gamedev.stackexchange.com\/questions\/31499\/what-are-the-advantages-of-tangent-space-normals-over-object-space-normals<\/a><\/li>\n<li><a href=\"http:\/\/www.3dkingdoms.com\/tutorial.htm\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.3dkingdoms.com\/tutorial.htm<\/a><\/li>\n<li><a href=\"https:\/\/www.opengl.org\/discussion_boards\/showthread.php\/173724-Tangent-or-object-space\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.opengl.org\/discussion_boards\/showthread.php\/173724-Tangent-or-object-space<\/a>\uff08\u8ba8\u8bba\u5f88\u6fc0\u70c8\uff0c\u5efa\u8bae\u597d\u597d\u770b\u770b\uff09<\/li>\n<\/ul>\n<p>\u770b\u4e86\u8fd9\u4e48\u591a\uff0c\u603b\u7ed3\u4e00\u4e0b\u4e3a\u4ec0\u4e48Tangent-Space\u4f1a\u8fd9\u4e48\u6d41\u884c\u3002<strong>\u201cIt never fails\uff01\u201d<\/strong>\u4ece\u4e0a\u9762\u7684\u4f18\u70b9\u5217\u8868\u53ef\u4ee5\u770b\u51fa\uff0cTangent-Space\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u90fd\u4f18\u4e8eObject-Space\uff0c\u800c\u4e14\u53ef\u4ee5\u8282\u7701\u5f88\u591a\u7f8e\u672f\u4eba\u5458\u7684\u5de5\u4f5c\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u4e5f\u4e0d\u662f\u8bf4Object-Space Normal Map\u5b8c\u5168\u6ca1\u6709\u7528\u5904\uff0c\u4e00\u4e9b\u4eba\u5c31\u559c\u6b22\u7528Object-Space Normal Map\u4e5f\u662f\u53ef\u4ee5\u7684~<\/p>\n<p>\u8bf4\u4e86\u534a\u5929\uff0c\u4e0d\u7ba1\u4f7f\u7528\u54ea\u4e2a\u5750\u6807\u7cfb\uff0c\u90fd\u9762\u4e34\u7740\u4e00\u4e2a\u9009\u62e9\uff0c\u5c31\u662f<strong>\u6700\u540e\u5149\u7167\u8ba1\u7b97\u4f7f\u7528\u7684\u5750\u6807\u7cfb\u7a76\u7adf\u662f\u54ea\u4e2a<\/strong>\u3002\u5bf9\u4e8eTangent-Space Normal Map\uff0c\u6211\u4eec\u4e00\u822c\u5c31\u662f\u5728Tangent Space\u91cc\u8ba1\u7b97\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u9700\u8981<strong>\u628aviewDir\u3001lightDir\u5728Vertex Shader\u4e2d\u8f6c\u6362\u5230Tangent Space\u4e2d\uff0c\u7136\u540e\u5728Fragment Shader\u5bf9\u6cd5\u7ebf\u7eb9\u7406\u91c7\u6837\u540e\uff0c\u76f4\u63a5\u8fdb\u884c\u5149\u7167\u8ba1\u7b97<\/strong>\u3002\u800c\u5bf9\u4e8eObject-Space Normal Map\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u591a\u79cd\u9009\u62e9\uff0c\u5373\u53ef\u4ee5\u9009\u62e9\u6700\u7ec8\u5728Object-Space\u4e0b\uff0c\u4e5f\u53ef\u4ee5\u5728World Space\u6216\u8005View Space\u4e0b\u3002\u800c\u8fd9\u4e9b\u8ba1\u7b97\uff0c\u6211\u4eec\u4f1a\u5728\u4e0b\u4e00\u8282\u91cc\u9762\u8bb2\u5230\u5177\u4f53\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u4f7f\u7528\u6cd5\u7ebf\u7eb9\u7406\u6765\u91c7\u6837\u6cd5\u7ebf\u4fe1\u606f\uff0c\u6700\u5927\u7684\u95ee\u9898\u540c\u6837\u662f\u5750\u6807\u7cfb\u7684\u8f6c\u6362\u3002\u6211\u4eec\u4e4b\u524d\u8bf4\u8fc7\uff0c\u4e00\u822c\u7684\u505a\u6cd5\u662f\u628a\u6240\u6709\u4fe1\u606f\u8f6c\u6362\u5230Tangent Space\u4e2d\uff0c\u6211\u4eec\u73b0\u5728\u5c31\u6765\u770b\u5982\u4f55\u8f6c\u6362\u3002<\/p>\n<p>\u5982\u679c\u8981\u81ea\u5df1\u7f16\u7801\u5b9e\u73b0\u6cd5\u7ebf\u6620\u5c04\u7684\u76ee\u7684\uff0c\u6700\u4e3b\u8981\u7684\u5c31\u662f\u8981\u8003\u8651\u6700\u7ec8\u5c06\u5149\u7167\u8ba1\u7b97\u8f6c\u6362\u5230\u54ea\u4e2a\u5750\u6807\u7cfb\u4e2d\uff1aModel Space\uff0cWorld Space\uff0cView Space\u8fd8\u662fTangent Space\u3002\u901a\u5e38\uff08\u6ce8\u610f\u662f\u901a\u5e38\uff01\uff09\uff0c<strong>\u5982\u679c\u4f7f\u7528\u6a21\u578b\u81ea\u5e26\u7684\u6cd5\u7ebf\u65f6\uff0c\u6211\u4eec\u4e00\u822c\u628a\u6240\u6709\u4fe1\u606f\u8f6c\u6362\u5230World Space\u4e2d<\/strong>\u3002\u8fd9\u6837\u6700\u5927\u7684\u597d\u5904\u5c31\u662f\u4e00\u5207\u90fd\u5f88\u76f4\u89c2\uff0c\u7b26\u5408\u6211\u4eec\u7684\u4e00\u822c\u8ba4\u8bc6\u3002\u800c<strong>\u5982\u679c\u662f\u4f7f\u7528\u6cd5\u7ebf\u7eb9\u7406\uff0c\u4e00\u822c\u662f\u8f6c\u6362\u5230Tangent Space\u4e2d<\/strong>\u3002\u8fd9\u6837\u505a\u7684\u539f\u56e0\u6709\u4e00\u5b9a\u6027\u80fd\u7684\u8003\u8651\uff0c\u56e0\u4e3a\u771f\u6b63\u7684\u6cd5\u7ebf\u4fe1\u606f\u53ea\u6709\u5230\u4e86Fragment Shader\u9636\u6bb5\u624d\u4f1a\u4ece\u7eb9\u7406\u4e2d\u91c7\u6837\u5f97\u5230\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528Tangent Space\uff0c\u5c31\u9700\u8981\u9010\u50cf\u7d20\u5904\u7406\u6bcf\u4e2a\u6cd5\u7ebf\u4fe1\u606f\uff0c\u800c\u76f8\u53cd\uff0c\u5982\u679c\u4f7f\u7528Tangent Space\uff0c\u6211\u4eec\u5c31\u53ea\u9700\u8981\u5728Vertex Shader\u4e2d\u5bf9\u5149\u7167\u65b9\u5411\u7b49\u4fe1\u606f\u8fdb\u884c\u9010\u9876\u70b9\u5904\u7406\u3002\u800c\u9010\u9876\u70b9\u603b\u662f\u6bd4\u9010\u50cf\u7d20\u7684\u5904\u7406\u6548\u7387\u66f4\u4f18\u3002<\/p>\n<p><strong>\u9700\u8981\u8f6c\u6362\u7684\u4fe1\u606f<\/strong>\u4e3b\u8981\u5305\u542b\u4e86\u4e0b\u9762\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li>\u6cd5\u7ebf\uff08\u5982\u679c\u4f7f\u7528\u81ea\u5e26\u6cd5\u7ebf\u7684\u8bdd\uff09<\/li>\n<li>\u5149\u6e90\u65b9\u5411\u3002\u5982\u679c\u662f\u4f7f\u7528\u5e73\u884c\u5149\uff08\u4f8b\u5982ForwardBase\u4e2d\u4f7f\u7528\u7684\u5149\u7167\uff09\uff0c\u90a3\u4e48\u4e0d\u9700\u8981\u628a\u5149\u6e90\u65b9\u5411\u4f5c\u4e3a\u9876\u70b9\u4fe1\u606f\u4eceVertex Shader\u4e2d\u4f20\u9012\u7ed9Fragment Shader\uff1b\u5982\u679c\u662f\u4f7f\u7528\u70b9\u5149\u6e90\u8fd9\u7c7b\u5149\u6e90\uff0c\u6211\u4eec\u9700\u8981\u9010\u9876\u70b9\u5904\u7406\u5149\u6e90\u65b9\u5411\uff0c\u628a\u4ee5\u6bcf\u4e2a\u9876\u70b9\u4e3a\u51fa\u53d1\u70b9\u7684\u5149\u6e90\u4fe1\u606f\u5b58\u50a8\u5728v2f\u4e2d\uff0c\u4f20\u9012\u7ed9Fragment Shader\u3002<\/li>\n<li>\u89c6\u89d2\u65b9\u5411\u3002\u5982\u679c\u6211\u4eec\u7684Shader\u9700\u8981\u8ba1\u7b97\u548c\u89c6\u89d2\u65b9\u5411\u6709\u5173\u7684\u5149\u7167\u8ba1\u7b97\uff08\u5982\u9ad8\u5149\uff09\u65f6\uff0c\u5c31\u9700\u8981\u628a\u89c6\u89d2\u65b9\u5411\u5728Vertex Shader\u4e2d\u5904\u7406\u540e\u4f20\u9012\u7ed9Fragment Shader\u3002<\/li>\n<\/ul>\n<div><\/div>\n<div>\u7406\u89e3\u4e86\u4e0a\u8ff0\u5185\u5bb9\u540e\uff0c\u6211\u4eec\u6765\u5177\u4f53\u770b\u5982\u679c\u7f16\u7801\u5b9e\u73b0\u3002\u4e3a\u4e86\u5145\u5206\u8bf4\u660e\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\u90fd\u4f7f\u7528\u4e86\u4f20\u7edf\u7684ADS\u6a21\u578b\uff08ambient+diffuse+specular\uff09\uff0c\u4ee5\u7528\u5230\u4e0a\u8ff0\u6240\u6709\u9700\u8981\u8f6c\u6362\u7684\u4fe1\u606f\u3002<\/div>\n<div>\n<pre class=\"lang:c++ decode:true\">\/\/ Declares 3x3 matrix 'rotation', filled with tangent space basis\r\n#define TANGENT_SPACE_ROTATION \\\r\n    float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w; \\\r\n    float3x3 rotation = float3x3( v.tangent.xyz, binormal, v.normal )\r\n\r\nTANGENT_SPACE_ROTATION;\r\n\r\no.wNormal = UnityObjectToWorldNormal(v.normal);\r\no.wViewDir = mul(rotation, ObjSpaceViewDir(v.vertex));\r\no.wLightDir = mul(rotation, ObjSpaceLightDir(v.vertex));<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h2>\u6cd5\u7ebf\u683c\u5f0f<\/h2>\n<p>\u8fd9\u6837\u7684\u8bbe\u7f6e\u53ef\u4ee5\u8ba9Unity\u6839\u636e\u4e0d\u540c\u5e73\u53f0\u5bf9\u7eb9\u7406\u8fdb\u884c\u538b\u7f29\uff0c\u901a\u8fc7UnpackNormal\u51fd\u6570\u5bf9\u6cd5\u7ebf\u7eb9\u7406\u8fdb\u884c\u6b63\u786e\u7684\u91c7\u6837\uff0c\u5373\u201c\u5c06\u628a\u989c\u8272\u901a\u9053\u53d8\u6210\u4e00\u4e2a\u9002\u5408\u4e8e\u5b9e\u65f6\u6cd5\u5411\u6620\u5c04\u7684\u683c\u5f0f\u201d\u3002\u6211\u4eec\u9996\u5148\u6765\u770bUnpackNormal\u51fd\u6570\u7684\u5185\u90e8\u5b9e\u73b0\uff08\u5728UnityCG.cginc\u91cc\uff09\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">inline fixed3 UnpackNormalDXT5nm (fixed4 packednormal)\r\n{\r\nfixed3 normal;\r\nnormal.xy = packednormal.wy * 2 - 1;\r\n#if defined(SHADER_API_FLASH)\r\n\/\/ Flash does not have efficient saturate(), and dot() seems to require an extra register.\r\nnormal.z = sqrt(1 - normal.x*normal.x - normal.y * normal.y);\r\n#else\r\nnormal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));\r\n#endif\r\nreturn normal;\r\n}\r\n\r\ninline fixed3 UnpackNormal(fixed4 packednormal)\r\n{\r\n#if (defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)) &amp;&amp; defined(SHADER_API_MOBILE)\r\nreturn packednormal.xyz * 2 - 1;\r\n#else\r\nreturn UnpackNormalDXT5nm(packednormal);\r\n#endif\r\n}<\/pre>\n<p>\u4ece\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u63a8\u5bfc\u51fa\uff0c\u5bf9\u4e8e\u79fb\u52a8\u5e73\u53f0\u4e0a\uff0cUnity\u6ca1\u6709\u66f4\u6539\u6cd5\u7ebf\u7eb9\u7406\u7684\u5b58\u50a8\u683c\u5f0f\uff0c\u4ecd\u7136\u662fRGB\u901a\u9053\u5bf9\u5e94\u4e86XYZ\u65b9\u5411\u3002\u5bf9\u4e8e\u5176\u4ed6\u5e73\u53f0\u4e0a\uff0c\u5219\u4f7f\u7528\u4e86\u53e6\u4e00\u4e2a\u51fd\u6570UnpackNormalDXT5nm\u3002\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u5dee\u522b\u5bf9\u5f85\u5462\uff1f\u5b9e\u9645\u4e0a\u662f\u56e0\u4e3a\u5bf9\u6cd5\u7ebf\u7eb9\u7406\u7684\u538b\u7f29\u3002\u6309\u6211\u4eec\u4e4b\u524d\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u6cd5\u7ebf\u7eb9\u7406\u88ab\u5f53\u6210\u4e00\u4e2a\u548c\u666e\u901a\u7eb9\u7406\u65e0\u5f02\u7684\u56fe\uff0c\u4f46\u5176\u5b9e\uff0c\u5b83\u53ea\u6709\u4e24\u4e2a\u901a\u9053\u662f\u771f\u6b63\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u56e0\u4e3a\u7b2c\u4e09\u4e2a\u901a\u9053\u7684\u503c\u53ef\u4ee5\u7528\u53e6\u5916\u4e24\u4e2a\u63a8\u5bfc\u51fa\u6765\uff08\u6cd5\u7ebf\u662f\u5355\u4f4d\u5411\u91cf\uff09\u3002\u663e\u7136\uff0cUnity\u91c7\u7528\u7684\u538b\u7f29\u65b9\u5f0f\u662fDXT5nm\u3002\u8fd9\u79cd\u538b\u7f29\u65b9\u5f0f\u7684\u539f\u7406\u6211\u5c31\u4e0d\u8bb2\u4e86\uff08\u5176\u5b9e\u6211\u4e5f\u4e0d\u662f\u5f88\u61c2\u3002\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u770b<a href=\"http:\/\/tech-artists.org\/wiki\/Normal_map_compression\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u7bc7<\/a>\uff09\uff0c\u4f46\u4ece\u901a\u9053\u7684\u5b58\u50a8\u4e0a\uff0c\u5b83\u7684\u7279\u70b9\u662f\uff0c\u539f\u5148\u5b58\u50a8\u5728R\u901a\u9053\u7684\u503c\u4f1a\u88ab\u8f6c\u79fb\u5230A\u901a\u9053\u4e0a\uff0cG\u901a\u9053\u4fdd\u7559\uff0c\u800cRB\u901a\u9053\u4f1a\u4f7f\u7528\u67d0\u79cd\u989c\u8272\u586b\u5145\uff08\u76f8\u5f53\u4e8e\u88ab\u820d\u5f03\u4e86\uff09\u3002\u56e0\u6b64UnpackNormalDXT5nm\u51fd\u6570\u4e2d\uff0c\u771f\u6b63\u6cd5\u7ebf\u7684xy\u503c\u5bf9\u5e94\u4e86\u538b\u7f29\u7eb9\u7406\u7684wy\u503c\uff0c\u800cz\u503c\u662f\u901a\u8fc7xy\u503c\u63a8\u5bfc\u51fa\u6765\u7684\u3002<\/p>\n<h2>\u9488\u5bf9UNITY\u76842\uff0c3\u4e8b<\/h2>\n<p>\u5c31\u662f\u5728Unity 5.0\u4ee5\u524d\uff0c\u5b9e\u9645\u4e0a\u5982\u679c\u6211\u4eec\u5bf9\u4e00\u4e2a\u6a21\u578bA\u8fdb\u884c\u4e86\u975e\u7edf\u4e00\u7f29\u653e\uff0cUnity\u5185\u90e8\u4f1a\u91cd\u65b0\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6a21\u578bB\uff0c\u6a21\u578bB\u7684\u5927\u5c0f\u548c\u7f29\u653e\u540e\u7684A\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5b83\u7684\u7f29\u653e\u7cfb\u6570\u662f\u7edf\u4e00\u7f29\u653e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7unity_Scale.w\u6765\u5f97\u5230\u8fd9\u4e2a\u7edf\u4e00\u7f29\u653e\u7cfb\u6570\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728Unity 5.0\u4ee5\u524d\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u5728shader\u4e2d\u6839\u672c\u4e0d\u9700\u8981\u8003\u8651\u6a21\u578b\u7684\u975e\u7edf\u4e00\u7f29\u653e\u95ee\u9898\uff0c\u56e0\u4e3a\u5728shader\u9636\u6bb5\u975e\u7edf\u4e00\u7f29\u653e\u6839\u672c\u5c31\u4e0d\u5b58\u5728\u4e86\uff01<\/p>\n<p>\u4e0d\u5f97\u4e0d\u8bf4\uff0cUnity\u8fd9\u4e2a\u80cc\u540e\u7684\u5c0f\u52a8\u4f5c\u771f\u7684\u662f\u5f88\u5947\u8469\u3002\u3002\u3002\u6211\u8ba4\u4e3a\u6b63\u662f\u8fd9\u4e2a\u539f\u56e0\uff0c\u5bfc\u81f4\u5728Unity 4.x\u4e2d\u5185\u7f6eshader\u7684\u5bf9\u6cd5\u7ebf\u7684\u8f6c\u6362\u90fd\u5927\u80c6\u5730\u76f4\u63a5\u4f7f\u7528\u4e86_Object2World\u6765\u628a\u6cd5\u7ebf\u8f6c\u6362\u5230\u4e16\u754c\u7a7a\u95f4\u4e0b\uff0c\u5929\u771f\u7684\u6211\u7adf\u7136\u8fd8\u4ee5\u4e3a\u662f\u4ed6\u4eec\u4e0d\u5728\u610f\u3002Too young\uff01\uff01\uff01\u56e0\u6b64\uff0c\u5728Unity 4.x\u4e2d\uff0c\u5b8c\u5168\u53ef\u4ee5\u4e0d\u8003\u8651\u975e\u7edf\u4e00\u7f29\u653e\u5bf9\u6cd5\u7ebf\u53d8\u6362\u7684\u5f71\u54cd\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u539f\u53d8\u6362\u77e9\u9635\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u518d\u4f7f\u7528\u539f\u53d8\u6362\u77e9\u9635\u7684\u9006\u8f6c\u7f6e\u77e9\u9635\u6765\u53d8\u6362\u6cd5\u7ebf\u3002\u8bc4\u8bba\u91cc\u6709\u4e00\u4f4d\u4ec1\u5144\u6307\u51fa\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u7684\u786e\uff0c\u5728\u4e0a\u9762\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u91cc\uff0c\u6211\u5728v2f\u4e2d\u5b58\u50a8\u4ece\u8be5\u9876\u70b9\u7684Tangent Space\u8f6c\u6362\u5230World Space\u7684\u8f6c\u6362\u77e9\u9635\uff0c\u7136\u540e\u5728frag\u51fd\u6570\u4e2d\uff0c\u5229\u7528\u8f6c\u6362\u77e9\u9635\u5148\u628a\u6cd5\u7ebf\u8f6c\u6362\u5230World Space\u4e2d\u3002\u8fd9\u91cc\uff0c\u6211\u76f4\u63a5\u4f7f\u7528\u7684\u5c31\u662f\u4eceTangent Space\u5230World Space\u7684\u53d8\u6362\u77e9\u9635\u6765\u53d8\u6362\u6cd5\u7ebf\u7684\uff0c\u6ca1\u6709\u8003\u8651\u9006\u8f6c\u7f6e\u77e9\u9635\uff0c\u56e0\u6b64\u8fd9\u4f4d\u4ec1\u5144\u770b\u4e0d\u61c2\u662f\u6b63\u5e38\u7684\u3002\u3002\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u6211\u7ed9\u51fa\u5b98\u65b9\u7684\u66f4\u65b0\u8bf4\u660e\uff08\u8be6\u60c5\u8bf7\u89c1\uff1a<a href=\"http:\/\/docs.unity3d.com\/Manual\/UpgradeGuide5-Shaders.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/docs.unity3d.com\/Manual\/UpgradeGuide5-Shaders.html<\/a>\uff09\uff1a<\/p>\n<p><em>\u201cunity_Scale\u201d shader variable has been removed \u00a0<\/em><\/p>\n<p><em>The \u201cunity_Scale\u201d shader property has been removed. In 4.x unity_Scale.w was the 1 \/ uniform Scale of the transform, Unity 4.x only rendered non-scaled or uniformly scaled models. Other scales were performed on the CPU, which was very expensive &amp; had an unexpected memory overhead. \u00a0In Unity 5.0 all this is done on the GPU by simply passing matrices with non-uniform scale to the shaders. Thus unity_Scale has been removed because it can not represent the full scale. In most cases where \u201cunity_Scale\u201d was used we recommend instead transforming to world space first. In the case of transforming normals, you always have to use normalize on the transformed normal now. In some cases this leads to slightly more expensive code in the vertex shader.<\/em><\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0cunity_Scale\u5728Unity 5.x\u4e2d\u5df2\u7ecf\u88ab\u629b\u5f03\u4e86\uff0cUnity\u4e5f\u4e0d\u4f1a\u5728CPU\u91cc\u518d\u5904\u7406\u975e\u7edf\u4e00\u7f29\u653e\u6a21\u578b\u3002\u5e26\u6765\u7684\u95ee\u9898\u5c31\u662f\uff0c\u6211\u4eec\u9700\u8981\u81ea\u884c\u5904\u7406\u975e\u7edf\u4e00\u7f29\u653e\u95ee\u9898\uff0c\u5e76\u8981\u5728\u5fc5\u8981\u65f6\u8fdb\u884c\u5f52\u4e00\u5316\u64cd\u4f5c\u4e86\u3002<\/p>\n<p><strong>\u628a\u201cTexture Type\u201d\u8bbe\u7f6e\u6210\u201cNormal Map\u201d\u540e\uff0c\u6709\u4e00\u4e2a\u590d\u9009\u6846\u662f\u201cCreate from grayscale\u201d\uff0c\u8fd9\u4e2a\u662f\u505a\u4ec0\u4e48\u7528\u7684<\/strong>\u3002\u8fd9\u8981\u4ece\u6cd5\u7ebf\u7eb9\u7406\u7684\u79cd\u7c7b\u8bf4\u8d77\u3002\u6211\u4eec\u4e0a\u8ff0\u63d0\u5230\u7684\u6cd5\u7ebf\u7eb9\u7406\uff0c\u4e5f\u79f0\u201cTangent-Space Normal Map\u201d\u3002\u8fd8\u6709\u4e00\u79cd\u6cd5\u7ebf\u7eb9\u7406\u662f\u4ece\u201cGrayscale Height Map\u201d\u4e2d\u751f\u6210\u7684\u3002\u540e\u9762\u8fd9\u79cd\u7eb9\u7406\u672c\u8eab\u8bb0\u5f55\u7684\u662f\u76f8\u5bf9\u9ad8\u5ea6\uff0c\u662f\u4e00\u5f20\u7070\u5ea6\u56fe\uff0c\u767d\u8272\u8868\u793a\u76f8\u5bf9\u66f4\u9ad8\uff0c\u9ed1\u8272\u8868\u793a\u76f8\u5bf9\u66f4\u4f4e\u3002\u800c\u6cd5\u7ebf\u7eb9\u7406\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8fd9\u5f20\u56fe\u8fdb\u884c\u56fe\u50cf\u6ee4\u6ce2\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u65b9\u6cd5\u53ef\u89c1<a href=\"http:\/\/docs.unity3d.com\/Manual\/HOWTO-bumpmap.html\" target=\"_blank\" rel=\"noopener noreferrer\">\u5b98\u7f51<\/a>\uff0c<a class=\"replace_word\" title=\"\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u5e93\" href=\"http:\/\/lib.csdn.net\/base\/datastructure\" target=\"_blank\" rel=\"noopener noreferrer\">\u7b97\u6cd5<\/a>\u53ef\u89c1<a href=\"http:\/\/forum.unity3d.com\/threads\/bumpmap-grayscale-to-normalmap-rgb.5714\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u8bba\u575b\u8ba8\u8bba<\/a>\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u6b63\u786e\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\u5462\uff1f\u5728Unity 5.x\u4e2d\uff0c<strong>\u5982\u679c\u6211\u4eec\u9700\u8981\u628a\u6cd5\u7ebf\u4ece\u6a21\u578b\u7a7a\u95f4\u53d8\u6362\u5230\u4e16\u754c\u7a7a\u95f4\u4e2d<\/strong>\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570UnityObjectToWorldNormal\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">fixed3\u00a0worldNormal\u00a0=\u00a0UnityObjectToWorldNormal(v.normal)\r\n<\/pre>\n<pre class=\"lang:c++ decode:true\">Shader \"OpenGL Cookbook\/UsingNormalMaps (Unity 5.x)\" {\r\nProperties {\r\n_MainTex (\"Base (RGB)\", 2D) = \"white\" {}\r\n_Bump (\"Bump\", 2D) = \"bump\" {}\r\n_Specular (\"Specular\", Range(1.0, 500.0)) = 250.0\r\n_Gloss (\"Gloss\", Range(0.0, 1.0)) = 0.2\r\n_Cubemap (\"Cubemap\", CUBE) = \"\"{}\r\n_ReflAmount (\"Reflection Amount\", Range(0,1)) = 0.5\r\n}\r\nSubShader {\r\nTags { \"RenderType\"=\"Opaque\" }\r\nLOD 200\r\n\r\nPass {\r\nTags { \"LightMode\" = \"ForwardBase\" }\r\n\r\nCGPROGRAM\r\n\r\n#pragma vertex vert\r\n#pragma fragment frag\r\n\r\n#pragma multi_compile_fwdbase\r\n\r\n#include \"UnityCG.cginc\"\r\n#include \"Lighting.cginc\"\r\n#include \"AutoLight.cginc\"\r\n\r\nsampler2D _MainTex;\r\nsampler2D _Bump;\r\nfloat _Specular;\r\nfloat _Gloss;\r\nsamplerCUBE _Cubemap;\r\nfloat _ReflAmount;\r\n\r\nfloat4 _MainTex_ST;\r\n\r\nstruct a2v {\r\nfloat4 vertex : POSITION;\r\nfixed3 normal : NORMAL;\r\nfixed4 texcoord : TEXCOORD0;\r\nfixed4 tangent : TANGENT;\r\n};\r\n\r\nstruct v2f {\r\nfloat4 pos : POSITION;\r\nfixed2 uv : TEXCOORD0;\r\nfixed3 lightDir: TEXCOORD1;\r\nfixed4 TtoW0 : TEXCOORD2;\r\nfixed4 TtoW1 : TEXCOORD3;\r\nfixed4 TtoW2 : TEXCOORD4;\r\nLIGHTING_COORDS(5, 6)\r\n};\r\n\r\nv2f vert(a2v v) {\r\nv2f o;\r\n\r\no.pos = mul(UNITY_MATRIX_MVP, v.vertex);\r\n\r\no.uv = TRANSFORM_TEX (v.texcoord, _MainTex);\r\n\r\n\/\/Create a rotation matrix for tangent space\r\nTANGENT_SPACE_ROTATION;\r\no.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex));\r\n\r\nfloat3 worldPos = mul(_Object2World, v.vertex).xyz;\r\nfixed3 worldNormal = UnityObjectToWorldNormal(v.normal);\r\nfixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);\r\nfixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;\r\n\/\/ Case 1: The codes used by built-in shaders\r\no.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);\r\no.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);\r\no.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);\r\n\r\n\/\/ Case 2: The codes which I think are correct \r\nfloat3x3 WtoT = mul(rotation, (float3x3)_World2Object);\r\no.TtoW0 = float4(WtoT[0].xyz, worldPos.x);\r\no.TtoW1 = float4(WtoT[1].xyz, worldPos.y);\r\no.TtoW2 = float4(WtoT[2].xyz, worldPos.z);\r\n\r\n\/\/ pass lighting information to pixel shader\r\nTRANSFER_VERTEX_TO_FRAGMENT(o);\r\nreturn o;\r\n}\r\n\r\nfixed4 frag(v2f i) : COLOR {\r\nfixed4 texColor = tex2D(_MainTex, i.uv);\r\nfixed3 norm = UnpackNormal(tex2D(_Bump, i.uv));\r\nfloat3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);\r\nfixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));\r\nfixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));\r\n\/\/ Case 1\r\nhalf3 worldNormal = normalize(half3(dot(i.TtoW0.xyz, norm), dot(i.TtoW1.xyz, norm), dot(i.TtoW2.xyz, norm)));\r\n\/\/ Case 2\r\nworldNormal = normalize(mul(norm, float3x3(i.TtoW0.xyz, i.TtoW1.xyz, i.TtoW2.xyz)));\r\n\r\nfixed atten = LIGHT_ATTENUATION(i);\r\n\r\nfixed3 ambi = UNITY_LIGHTMODEL_AMBIENT.xyz;\r\n\r\nfixed3 diff = _LightColor0.rgb * saturate (dot (normalize(worldNormal), normalize(lightDir)));\r\n\r\nfixed3 lightRefl = reflect(-lightDir, worldNormal);\r\nfixed3 spec = _LightColor0.rgb * pow(saturate(dot(normalize(lightRefl), normalize(worldViewDir))), _Specular) * _Gloss;\r\n\r\nfixed3 worldView = fixed3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);\r\nfixed3 worldRefl = reflect (-worldViewDir, worldNormal);\r\nfixed3 reflCol = texCUBE(_Cubemap, worldRefl).rgb * _ReflAmount;\r\n\r\nfixed4 fragColor;\r\nfragColor.rgb = float3((ambi + (diff + spec) * atten) * texColor) + reflCol;\r\nfragColor.a = 1.0f;\r\n\r\nreturn fragColor;\r\n}\r\n\r\nENDCG\r\n}\r\n} \r\nFallBack \"Diffuse\"\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u4e45\u4e0d\u5199shader\u4e86\uff0c\u679c\u7136\u5fd8&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/posts\/151"}],"collection":[{"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":3,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":160,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/160"}],"wp:attachment":[{"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sourcecool.com\/index.php\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}