{"id":106,"date":"2020-10-09T00:55:19","date_gmt":"2020-10-08T16:55:19","guid":{"rendered":"https:\/\/meecarlo.wordpress.com\/?p=106"},"modified":"2020-12-28T18:21:12","modified_gmt":"2020-12-28T10:21:12","slug":"shader%e5%85%a5%e9%97%a8-3-%e6%bc%ab%e5%8f%8d%e5%b0%84%e5%85%89%e7%85%a7%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/meecarlo.com\/old\/2020\/10\/09\/shader%e5%85%a5%e9%97%a8-3-%e6%bc%ab%e5%8f%8d%e5%b0%84%e5%85%89%e7%85%a7%e6%a8%a1%e5%9e%8b\/","title":{"rendered":"Shader\u5165\u95e8\u20143.\u6f2b\u53cd\u5c04\u5149\u7167\u6a21\u578b"},"content":{"rendered":"\n<p>        \u5728\u6784\u5efa\u8fd9\u4e2ashader\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u4e00\u4e2a\u5b9a\u5f8b\uff0c\u5170\u4f2f\u7279\u5b9a\u5f8b\uff08Lambert&#8217;s low\uff09:\u53cd\u5c04\u5149\u7ebf\u7684\u5f3a\u5ea6\u4e0e\u8868\u9762\u6cd5\u7ebf\u548c\u5149\u6e90\u65b9\u5411\u4e4b\u95f4\u7684\u5939\u89d2\u7684cos\u6210\u6b63\u6bd4\uff0c\u56e0\u6b64\u6f2b\u53cd\u5c04\u7684\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"65\" src=\"http:\/\/meecarlo.com\/wp-content\/uploads\/2020\/12\/8.png\" alt=\"\" class=\"wp-image-491\" srcset=\"https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/8.png 541w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/8-300x36.png 300w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><figcaption>\u5170\u4f2f\u7279\u5b9a\u5f8b<\/figcaption><\/figure>\n\n\n\n<p>        <em><strong>n<\/strong><\/em>\u662f\u8868\u9762\u6cd5\u7ebf\uff0c<strong><em>l<\/em><\/strong>\u662f\u5149\u6e90\u7684\u5355\u4f4d\u77e2\u91cf\uff0cmax\uff080\uff0cn\u00b7l\uff09\u662f\u53d60\uff0cn\u00b7l\u4e2d\u7684\u6700\u5927\u503c\uff0c<em>c<sub>light<\/sub><\/em> \u662f\u5149\u6e90\u989c\u8272\uff0c<em>m<sub>diffuse<\/sub><\/em> \u662f\u6750\u8d28\u7684\u6f2b\u53cd\u5c04\u989c\u8272\u3002<\/p>\n\n\n\n<p>        \u6240\u4ee5\u5f53\u6211\u4eec\u8981\u8ba1\u7b97\u6f2b\u53cd\u5c04\u7684\u989c\u8272\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u89814\u4e2a\u53c2\u6570\uff0c\u7269\u4f53\u7684\u8868\u9762\u6cd5\u7ebf\u3001\u5149\u6e90\u7684\u5355\u4f4d\u77e2\u91cf\u3001\u5149\u6e90\u989c\u8272\u4e0e\u6750\u8d28\u7684\u6f2b\u53cd\u5c04\u989c\u8272\u3002\u7531\u4e8e\u672c\u7ae0\u6240\u7f16\u5199\u7684\u662f\u9ed8\u8ba4\u5149\u7ebf\u4e0b\u7684\u5149\u7167\u6a21\u578b\uff0c\u6240\u4ee5\u53ea\u8fdb\u884c\u5355\u4e00\u5149\u7167\u7684\u8ba1\u7b97\uff0c\u800c\u8868\u9762\u6cd5\u7ebf\u4e0e\u5149\u6e90\u7684\u5355\u4f4d\u77e2\u91cf\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u53d6\u5f97\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u5b9a\u4e49\u8868\u9762\u6f2b\u53cd\u5c04\u7684\u989c\u8272\u5373\u53ef\u3002\u6240\u4ee5\u5728\u5c5e\u6027\u58f0\u660e\u4e2d\u53ea\u9700\u8981\u8fdb\u884c\u6f2b\u53cd\u5c04\u989c\u8272\u7684\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u6211\u7684shader\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Shader&nbsp;\"Custom\/DiffuseShader\"<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;Properties<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_Diffuse(\"Diffuse&nbsp;Color\",Color)=(1,1,1,1)<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;SubShader<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tags{\"LightModel\"=\"ForwardBase\"}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CGPROGRAM<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#pragma&nbsp;vertex&nbsp;vert<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#pragma&nbsp;fragment&nbsp;frag<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#include&nbsp;\"Lighting.cginc\"<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed4&nbsp;_Diffuse;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;a2v{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float4&nbsp;vertex&nbsp;:&nbsp;POSITION;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float3&nbsp;normal&nbsp;:&nbsp;NORMAL;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;v2f{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float4&nbsp;pos:SV_POSITION;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float3&nbsp;color:COLOR;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v2f&nbsp;vert(a2v&nbsp;v){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v2f&nbsp;o;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.pos=UnityObjectToClipPos(v.vertex);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;worldNormal=normalize(mul(v.normal,(float3x3)unity_WorldToObject));<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;lightNormal=normalize(_WorldSpaceLightPos0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;diffuse=_LightColor0.rgb*_Diffuse.rgb*saturate(dot(worldNormal,lightNormal));<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.color=ambient.rgb+diffuse.rgb;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;o;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed4&nbsp;frag(v2f&nbsp;i):SV_TARGET{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;fixed4(i.color,1.0);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENDCG<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;FallBack&nbsp;\"Diffuse\"<br>}<br><\/code><\/pre>\n\n\n\n<p>        \u6211\u4eec\u7ee7\u7eed\u6765\u9010\u884c\u7ffb\u8bd1\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u662f\u7f16\u5199\u4e86\u4e00\u4e2a\u6f2b\u53cd\u5c04\u6a21\u578b\uff0c\u6240\u4ee5\u6682\u65f6\u4e0d\u5bf9\u8d34\u56fe\u8fdb\u884c\u91c7\u6837\u3002\u6211\u4eec\u5728\u5c5e\u6027\u4e2d\u53ea\u6dfb\u52a0\u4e86\u4e00\u4e2a\u989c\u8272\u5c5e\u6027\u6240\u5bf9\u5e94\u7684\u662f\u6f2b\u53cd\u5c04\u989c\u8272\u3002<\/p>\n\n\n\n<p>       \u6211\u4eec\u5728\u8fdb\u884c\u6f2b\u53cd\u5c04\u8ba1\u7b97\u7684\u65f6\u5019\u8981\u6b63\u786e\u7684\u8bbe\u7f6eLightMode\u624d\u80fd\u5f97\u5230\u6211\u4eec\u63a5\u4e0b\u6765\u9700\u8981\u7528\u5230\u7684_LightColor0\u8fd9\u4e2a\u5185\u7f6e\u53d8\u91cf\uff0cLightMode\u6307\u7684\u662f\u706f\u5149\u7684\u6e32\u67d3\u8def\u5f84\uff0c\u800cForwardBase\u5219\u662f\u8bbe\u7f6e\u706f\u5149\u7684\u6e32\u67d3\u8def\u5f84\u7684\u4e00\u4e2a\u6807\u7b7e\uff0c\u5b83\u51b3\u5b9a\u4e86\u6e32\u67d3\u5668\u5982\u4f55\u6e32\u67d3\u706f\u5149\uff0c\u5176\u4ed6\u7684\u8fd8\u6709ForwardAdd\u7b49\u6807\u7b7e\u3002\u63a5\u7740\u5c31\u662f\u6b63\u5e38\u7684\u6846\u67b6\u642d\u5efa\uff0c\u4e0d\u5728\u8d58\u8ff0\uff0c\u4f46\u6211\u4eec\u8fd9\u91cc\u8fd0\u7528\u5230\u4e86\u4e00\u4e2a\u5934\u6587\u4ef6&#8221;Lighting.cginc&#8221;\uff0c\u8be5\u5934\u6587\u4ef6\u4e3b\u8981\u662f\u6709\u4e00\u4e9b\u5185\u7f6e\u7684\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f9b\u6211\u4eec\u8c03\u7528\uff0c\u6bd4\u5982\u4e4b\u524d\u6240\u8bf4\u7684_LightColor0\u8fd9\u4e2a\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed4 _Diffuse;<\/code><\/pre>\n\n\n\n<p>        _Diffuse\u662f\u989c\u8272\u5c5e\u6027\uff0c\u6211\u4eec\u6309\u7167\u60ef\u4f8b\u76f4\u63a5\u5b9a\u4e49\u6210fixed4\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>        \u6211\u4eec\u4ece\u9876\u70b9\u7740\u8272\u5668\u5f00\u59cb\uff0c\u9996\u5148\u7f16\u5199\u7ed3\u6784\u4f53a2v\uff0c\u6211\u4eec\u8981\u60f3\u4e00\u60f3\u8981\u6784\u5efa\u6f2b\u53cd\u5c04\u6a21\u578b\u9700\u8981\u4ece\u6211\u4eec\u7684\u9876\u70b9\u6570\u636e\u4e2d\u53d6\u5f97\u4ec0\u4e48\u6570\u636e\uff1f\u9876\u70b9\u4f4d\u7f6e\u81ea\u7136\u4e0d\u5fc5\u591a\u8bf4\uff0c\u6211\u4eec\u6765\u56de\u987e\u4e00\u4e0bLambert\u5b9a\u5f8b<em>C<sub>diffuse<\/sub><\/em> =<em>L<sub>color<\/sub><\/em> *<em>M<sub>diffuse<\/sub><\/em> *max(0,<em>n*l<\/em>)\u3002\u73af\u5883\u5149\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u5f97\u5230\uff0c\u6f2b\u53cd\u5c04\u6750\u8d28\u7684\u989c\u8272\u7531\u6211\u4eec\u7684\u610f\u613f\u63a7\u5236\uff0c\u5269\u4e0b\u7684\u5c31\u662f\u8868\u9762\u6cd5\u7ebf\u4e0e\u5149\u7684\u5355\u4f4d\u5411\u91cf\u4e86\u3002\u800c\u5149\u7684\u5355\u4f4d\u5411\u91cf\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Unity\u5185\u7f6e\u7684\u51fd\u6570\u53d6\u5f97\uff0c\u800c\u8868\u9762\u6cd5\u7ebf\u5219\u9700\u8981\u4e0e\u9876\u70b9\u8fdb\u884c\u4ea4\u4e92\u624d\u80fd\u591f\u53d6\u5f97\u6240\u4ee5\uff0c\u6211\u4eec\u8fd9\u91cc\u53c8\u7528\u5230\u4e00\u4e2a\u8bed\u4e49\uff1aNORMAL\uff0c\u83b7\u53d6\u9876\u70b9\u7684\u6cd5\u7ebf\u4fe1\u606f\uff0c\u6240\u4ee5\u6211\u4eec\u7684\u7ed3\u6784\u4f53\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Struct a2v{<br>    float4 vertex:POSITION;<br>    float3 normal:NORMAL;<br>};<\/code><\/pre>\n\n\n\n<p>        \u6211\u4eec\u5c06NORMAL\u8bed\u4e49\u6240\u53d6\u5f97\u7684\u9876\u70b9\u6cd5\u7ebf\u4fe1\u606f\u5b58\u50a8\u5728normal\u8fd9\u4e2a\u53d8\u91cf\u4e2d\uff0c\u53c8\u56e0\u4e3a\u6cd5\u7ebf\u662f\u4e00\u4e2a\u53ea\u5305\u542bXYZ\u7684\u5411\u91cf\u4fe1\u606f\uff0c\u6240\u4ee5\u6211\u4ee5float3\u6765\u8fdb\u884c\u5b9a\u4e49\u3002<\/p>\n\n\n\n<p>        \u4e0b\u9762\u6211\u4eec\u6765\u6784\u5efa\u9876\u70b9\u7740\u8272\u5668\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>v2f vert(a2v v){<br>    v2f o;<br>    o.pos=UnityObjectToClipPos(v.vertex);<br>    ...<br>}<\/code><\/pre>\n\n\n\n<p>        v2f \u5b9a\u4e49\u4e86\u6211\u4eec\u7684\u8f93\u51fa\u53c2\u6570\u662fo\uff0c\u4e4b\u540e\u7684\u5c31\u662f\u4e00\u822c\u8bbe\u7f6e\u3002\u9876\u70b9\u7684\u6cd5\u7ebf\u4fe1\u606f\u662f\u57fa\u4e8e\u6a21\u578b\u5750\u6807\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u9996\u5148\u5c06\u5176\u53d8\u6362\u5230\u7a7a\u95f4\u5750\u6807\u4e2d\uff0c\u8fd9\u91cc\u9700\u8981\u5c06\u9876\u70b9\u7684\u6cd5\u7ebf\u4e0e\u53d8\u6362\u77e9\u9635unity_WorldToObject\uff08\u8001\u7248\u672c\u4e3a\uff1a_World2Object\uff09\u8fdb\u884cmul\u8ba1\u7b97,\u4e4b\u540e\u5c06\u5176\u5f52\u4e00\u5316\uff0c\u6240\u4ee5\u6211\u4eec\u8fd9\u4e48\u5199<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed3 worldNormal=normalize(mul(v.normal,unity_WorldToObject));<\/code><\/pre>\n\n\n\n<p>        \u6ce8\u610f\uff0c\u8fd9\u4e48\u4e9b\u5bf9\u4e48\uff1f\u9996\u5148\u6cd5\u7ebf\u662f\u4e00\u4e2a\u4e09\u7ef4\u77e2\u91cf\uff0c\u800c\u6211\u4eec\u7684unity_WorldToObject\u5219\u662f\u4e00\u4e2a4X4\u7684\u77e9\u9635\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u8fdb\u884c\u53d8\u6362\u7684\u65f6\u5019\u53ea\u9700\u8981\u622a\u53d6\u524d\u4e09\u884c\u4e0e\u524d\u4e09\u5217\u5c31\u884c\u4e86\uff0c\u4fee\u6539\u8fc7\u540e\u7684\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed3 worldNormal=normalize(mul(v.normal,(float3x3)unity_WorldToObject));<\/code><\/pre>\n\n\n\n<p>        \u8fd9\u6837\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u6211\u4eec\u9700\u8981\u7684<em>n<\/em>,\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8fdb\u884c<em>l<\/em>\u7684\u8ba1\u7b97\uff0c\u5173\u4e8e\u5149\u6e90\u7684\u65b9\u5411\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570_WorldSpaceLightPos0\u6765\u53d6\u5f97\uff0c\u4f46\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u51fd\u6570\u53ea\u8ba1\u7b97\u5355\u4e00\u51fd\u6570\uff0c\u6240\u4ee5\u5728\u66f4\u590d\u6742\u7684\u5149\u7ebf\u4e2d\u5e76\u4e0d\u80fd\u8ba1\u7b97\u51fa\u6b63\u771f\u7684\u5149\u7ebf\u4fe1\u606f\uff0c\u4f46\u5728\u8fd9\u4e2a\u6a21\u578b\u4e2d\u662f\u591f\u7528\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed3 worldLight=normalize(_WorldSpaceLightPos0);<\/code><\/pre>\n\n\n\n<p>        \u5982\u6b64\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u6211\u4eec\u9700\u8981\u4e24\u4e2a\u5411\u91cf\u7684\u5f52\u4e00\u5316\uff0cworldNormal\u4e0eworldLight\uff0c\u63a5\u7740\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u5957\u5165\u6211\u4eec\u7684Lambert\u5b9a\u5f8b\u5c31\u884c\u4e86\uff0c\u518d\u56de\u4e00\u4e0bLambert\u5b9a\u5f8b\uff1a<em>C<sub>diffuse<\/sub>=L<sub>color<\/sub>*M<sub>diffuse<\/sub>*<\/em>max(<em>n,l<\/em>)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed3 diffuse=_LightColor0.rgb*_Diffuse.rgb*saturate(worldNormal,worldLight);<\/code><\/pre>\n\n\n\n<p>        \u81f3\u6b64\uff0c\u6211\u4eec\u7684\u6f2b\u53cd\u5c04\u8ba1\u7b97\u7ed3\u675f\uff0c\u4f46\u6574\u4f53\u7684\u9876\u70b9\u7740\u8272\u5668\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u56e0\u4e3a\u5728\u8003\u8651\u6f2b\u53cd\u5c04\u7684\u65f6\u5019\u4e0d\u5f97\u4e0d\u5728\u610f\u73af\u5883\u5149\u3002\u6240\u4ee5\u6211\u4eec\u8fd8\u9700\u8981\u8c03\u7528\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570UNITY_LIGHTMODEL_AMBIENT\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;<\/code><\/pre>\n\n\n\n<p>        \u6211\u4eec\u5f97\u5230\u4e86\u6f2b\u53cd\u5c04\u989c\u8272\u4e0e\u73af\u5883\u5149\uff0c\u5c06\u5176\u76f8\u52a0\u5219\u662f\u6700\u7ec8\u6211\u4eec\u6240\u5f97\u5230\u7684\u6f2b\u53cd\u5c04\u6a21\u578b\u989c\u8272\uff0c\u6700\u540e\u8fd4\u56deo\u5c31\u53ef\u4ee5\u4e86\u3002\u6240\u4ee5\u5b8c\u6574\u7684\u9876\u70b9\u7740\u8272\u5668\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v2f&nbsp;vert(a2v&nbsp;v){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v2f&nbsp;o;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.pos=UnityObjectToClipPos(v.vertex);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;worldNormal=normalize(mul(v.normal,(float3x3)unity_WorldToObject));<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;lightNormal=normalize(_WorldSpaceLightPos0);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fixed3&nbsp;diffuse=_LightColor0.rgb*_Diffuse.rgb*saturate(dot(worldNormal,lightNormal));<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.color=ambient.rgb+diffuse.rgb;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;o;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/code><\/pre>\n\n\n\n<p>        \u606d\u559c\uff0c\u6211\u4eec\u5df2\u7ecf\u5c06\u6700\u56f0\u96be\u7684\u90e8\u5206\u5b8c\u6210\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u7f16\u5199\u7247\u5143\u7740\u8272\u5668\u7684\u7ed3\u6784\u4f53\uff0c\u9996\u5148\u6211\u4eec\u8ddf\u4e4b\u524d\u4e00\u6837\uff0c\u9700\u8981\u5c06\u88c1\u526a\u8fc7\u540e\u7684\u9876\u70b9\u5750\u6807\u4f20\u8f93\u7ed9pos\uff0c\u6211\u4eec\u8fd8\u4ecd\u9700\u8981\u9876\u4e00\u4e2acolor\u53d8\u91cf\uff0ccolor\u662f\u4f5c\u4e3ao\u7684\u8f93\u51fa\u4e0e\u7247\u5143\u4e2d\u7684\u8f93\u5165\uff0c\u6211\u4eec\u53ef\u4ee5\u8d4b\u4e88\u4ed6COLOR\uff0c\u6709\u4e9b\u8d44\u6599\u4e5f\u53ef\u4ee5\u8d4b\u4e88TEXCOORD0\uff0c\u8fd9\u5e76\u4e0d\u662f\u5199\u6b7b\u7684\uff0c\u53ef\u81ea\u7531\u53d1\u6325\uff0c\u6240\u4ee5\u6211\u4eec\u7684\u7ed3\u6784\u4f53\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct v2f{<br>    pos:SV_POSITION;<br>    color:COLOR;<br>};<\/code><\/pre>\n\n\n\n<p>        \u63a5\u7740\u662f\u6211\u4eec\u7684\u7247\u5143\u7740\u8272\u5668\uff0c\u8fd9\u90e8\u5206\u5f88\u7b80\u5355\uff0c\u56e0\u4e3a\u6211\u4eec\u5c06\u6240\u6709\u7684\u5de5\u4f5c\u90fd\u5728\u9876\u70b9\u7740\u8272\u5176\u4e2d\u505a\u5b8c\u4e86\uff0c\u6240\u4ee5\u76f4\u63a5\u8f93\u51fa\u9876\u70b9\u4fe1\u606f\u5c31\u53ef\u4ee5\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fixed4 frag(v2f i):SV_Target{<br>    return fixed4 (i.color,1.0)<br>}<\/code><\/pre>\n\n\n\n<p>        \u6ce8\u610f\uff0c\u7531\u4e8e\u6211\u4eec\u5728\u9876\u70b9\u7740\u8272\u5668\u4e2d\u8f93\u51fa\u7684\u989c\u8272\u662frgb\u662f\u4e00\u4e2a\u4e09\u4f4d\u4fe1\u606f\uff0c\u6240\u4ee5\u6211\u4eec\u6dfb\u52a0\u4e2a1.0\u5c06\u5176\u8865\u5b8c\u3002<\/p>\n\n\n\n<p>        \u81f3\u6b64\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u6211\u4eec\u6574\u4e2a\u7684Lambert\u5149\u7167\u6a21\u578b\uff0c\u4e0b\u4e00\u8282\u6211\u4eec\u5c06\u7ed3\u5408\u8d34\u56fe\u4e0e\u8fd9\u4e2a\u6a21\u578b\u8fdb\u884c\u4e00\u4e2a\u5e26\u6709\u6f2b\u53cd\u5c04\u6027\u8d28\u7684\u8d34\u56fe\u6a21\u578b\u3002<\/p>\n\n\n\n<p>        \u63a5\u4e0b\u6765\u6211\u4eec\u5728shader graph\u5b9e\u73b0\u8fd9\u4e2a\u6f2b\u53cd\u5c04\u6a21\u578b<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"555\" src=\"http:\/\/meecarlo.com\/wp-content\/uploads\/2020\/12\/10-1024x555.png\" alt=\"\" class=\"wp-image-493\" srcset=\"https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10-1024x555.png 1024w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10-300x163.png 300w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10-768x416.png 768w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10-1536x832.png 1536w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10-1568x849.png 1568w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/10.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>shader graph\u4e2d\u7684Lambert\u6f2b\u53cd\u5c04\u6a21\u578b<\/figcaption><\/figure>\n\n\n\n<p>        \u5927\u4f53\u601d\u8def\u8ddf\u6211\u4eec\u5199\u4ee3\u7801\u7684\u601d\u8def\u4e00\u6837\uff0c\u4f46\u503c\u5f97\u63d0\u7684\u4e00\u70b9\u662fshader graph\u4e2d\u5e76\u6ca1\u6709\u9ed8\u8ba4\u5149\u6e90\u4f4d\u7f6e\u7684\u83b7\u53d6\uff0c\u6240\u4ee5\u6211\u4eec\u8981\u6dfb\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8282\u70b9\u6765\u83b7\u53d6\u5149\u7684\u989c\u8272\u4ee5\u53ca\u77e2\u91cf<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"860\" src=\"http:\/\/meecarlo.com\/wp-content\/uploads\/2020\/12\/11.png\" alt=\"\" class=\"wp-image-494\" srcset=\"https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/11.png 941w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/11-300x274.png 300w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/11-768x702.png 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><figcaption>\u81ea\u5b9a\u4e49\u8282\u70b9<\/figcaption><\/figure>\n\n\n\n<p>        <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#if SHADERGRAPH_PREVIEW<br>        <code>Direction=half3(0.5,0.5,0); Color=1;<\/code><br>#else<br>        <code>Light light=GetMainlight(); <\/code><br>        <code>Direction=light.direction; <\/code><br>        <code>Color=light.color;<\/code><br>#endif<\/code><\/pre>\n\n\n\n<p>\u5728\u5b9e\u9645\u7684\u5236\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u4e2a\u4ee3\u7801\u975e\u5e38\u5e38\u7528\uff0c\u6240\u4ee5\u6211\u76f4\u63a5\u8d34\u51fa\u6765\u5427\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5728UE4\u4e2d\u590d\u73b0\u6211\u4eec\u7684\u64cd\u4f5c\u3002SD\u4e3b\u8981\u662f\u8fdb\u884c\u6750\u8d28\u7684\u7f16\u5199\uff0c\u5e76\u4e0d\u6d89\u53ca\u7ba1\u7ebf\u7684\u7f16\u5199\uff0c\u6240\u4ee5\u8fd9\u7bc7\u6211\u4eec\u5c31\u7565\u8fc7\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"http:\/\/meecarlo.com\/wp-content\/uploads\/2020\/12\/2-1-1024x572.png\" alt=\"\" class=\"wp-image-482\" srcset=\"https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/2-1-1024x572.png 1024w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/2-1-300x168.png 300w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/2-1-768x429.png 768w, https:\/\/meecarlo.com\/old\/wp-content\/uploads\/2020\/12\/2-1.png 1256w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>UE4\u4e2d\u7684shader<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>       <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6784\u5efa\u8fd9\u4e2ashader\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u4e00\u4e2a\u5b9a\u5f8b\uff0c\u5170\u4f2f\u7279\u5b9a\u5f8b\uff08Lambert&#8217;s low\uff09:\u53cd\u5c04\u5149 &hellip; <a href=\"https:\/\/meecarlo.com\/old\/2020\/10\/09\/shader%e5%85%a5%e9%97%a8-3-%e6%bc%ab%e5%8f%8d%e5%b0%84%e5%85%89%e7%85%a7%e6%a8%a1%e5%9e%8b\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Shader\u5165\u95e8\u20143.\u6f2b\u53cd\u5c04\u5149\u7167\u6a21\u578b<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":336,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-study-note","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":3,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/106\/revisions\/566"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}