{"id":542,"date":"2010-07-22T12:47:36","date_gmt":"2010-07-22T12:47:36","guid":{"rendered":""},"modified":"2013-02-27T12:05:17","modified_gmt":"2013-02-27T04:05:17","slug":"542","status":"publish","type":"post","link":"https:\/\/1vr.cn\/?p=542","title":{"rendered":"Unity3\u6280\u672f\u4e4b\u8868\u9762\u7740\u8272"},"content":{"rendered":"<p>\u8c01\u90fd\u77e5\u9053\u5728Unity\u4e2d\u53ef\u4ee5\u7f16\u5199shader,\u53ef\u662f\u4ed6\u4eec\u5f88\u96be\u5199,\u5c24\u5176\u5f53\u4f60\u9700\u8981\u6bcf\u4e2a\u50cf\u7d20\u7684\u706f\u5149\u548c\u9634\u5f71\u4ea7\u751f\u4ea4\u4e92.\u5728Unity3\u4e2d,\u9664\u4e86\u8981\u5305\u542b\u65e7\u7684\u652f\u6301,\u4f60\u7684shader\u4e5f\u5fc5\u987b\u652f\u6301\u65b0\u7684\u9012\u5ef6\u7167\u660e\u6e32\u67d3(Deferred Lighting).\u6240\u4ee5\u6211\u4eec\u51b3\u5b9a\u8981\u8ba9shader\u66f4\u5bb9\u6613\u5199!<\/p>\n<p>\u4e00\u5e74\u591a\u4ee5\u524d,\u6211\u66fe\u8ba4\u4e3a&#8221;\u7740\u8272\u5668\u9a6c\u6b7b\u7684\u5feb(\u5475\u5475,\u7740\u8272\u5668\u5fc5\u6b7b,\u76f8\u5173\u7684\u5e16\u5b50<a href=\"http:\/\/aras-p.info\/blog\/2009\/05\/05\/shaders-must-die\/\" target=\"_blank\" rel=\"external\">http:\/\/aras-p.info\/blog\/2009\/05\/05\/shaders-must-die\/<\/a>,<a href=\"http:\/\/aras-p.info\/blog\/2009\/05\/07\/shaders-must-die-part-2\/\" target=\"_blank\" rel=\"external\">http:\/\/aras-p.info\/blog\/2009\/05\/07\/shaders-must-die-part-2\/<\/a>,<a href=\"http:\/\/aras-p.info\/blog\/2009\/05\/10\/shaders-must-die-part-3\/\" target=\"_blank\" rel=\"external\">http:\/\/aras-p.info\/blog\/2009\/05\/10\/shaders-must-die-part-3\/<\/a>)&#8221;.\u800c\u73b0\u5728\u6211\u4eec\u6b63\u5728\u5f00\u53d1Unity3,\u6211\u4eec\u79f0\u4e4b\u4e3a\u8868\u9762\u7740\u8272(Surface Shaders),\u56e0\u4e3a\u6211\u89c9\u5f97&#8221;\u7740\u8272\u5668\u9a6c\u6b7b\u7684\u5feb&#8221;\u505a\u4e2a\u8fd9\u4e2a\u65b0\u7269\u79cd\u7684\u540d\u79f0\u4e0d\u4f1a\u5f97\u5230\u8001\u677f\u8ba4\u540c.<\/p>\n<p>\u4e3b\u9898\u601d\u60f3\u5c31\u662f\u572890%\u7684\u60c5\u51b5\u4e0b\u6211\u4e4b\u9700\u8981\u58f0\u660e\u8868\u9762\u7684\u7279\u6027\u5c31\u53ef\u4ee5\u4e86.\u4ee5\u540e\u5c31\u60f3\u8bf4:<\/p>\n<p>\u563f,\u53cd\u7167\u7387\u6765\u81ea\u8fd9\u4e2a\u7eb9\u7406\u548c\u90a3\u4e2a\u7eb9\u7406\u7684\u6df7\u5408\u4ee5\u53ca\u6cd5\u7ebf\u8d34\u56fe.\u4f7f\u7528Blinn-Phong\u7167\u660e\u6a21\u5f0f,\u522b\u518d\u6765\u70e6\u6211\u4e86\u55f7!<\/p>\n<p>\u50cf\u4e0a\u9762\u8bf4\u7684,\u6211\u6ca1\u6709\u7406\u4f1a\u662f\u5426\u8981\u63d0\u524d\u6216\u8005\u5ef6\u8fdf\u6e32\u67d3,\u6216\u8005\u5982\u4f55\u4f7f\u7528\u5404\u79cd\u706f\u7684\u7c7b\u578b\u53bb\u5904\u7406,\u6216\u8005\u8981\u8bbe\u7f6e\u6709\u591a\u5c11\u6548\u679c\u5c06\u7528\u706f\u5149\u6765\u5448\u73b0,\u6216\u8005\u7528\u5c06\u6709\u591a\u5c11\u4f1a\u4ea7\u751f\u5b9e\u65f6\u9634\u5f71\u7684\u90e8\u5206,\u7b49\u7b49\u90fd\u4e0d\u662f\u6211\u611f\u5174\u8da3\u7684!\u8fd9\u4e9b\u80ae\u810f\u7684\u6e32\u67d3\u5de5\u4f5c\u600e\u4e48\u80fd\u8ba9\u7a0b\u5e8f\u53bb\u505a?!\u5f97\u8ba9\u7a0b\u5e8f\u5458\u89e3\u8131!<\/p>\n<p>\u5f53\u7136\u8fd9\u4e0d\u662f\u4e00\u4e2a\u65b0\u6982\u5ff5.\u5927\u90e8\u5206\u56fe\u5f62\u6e32\u67d3\u7f16\u8f91\u5668\u90fd\u6ca1\u7528\u50cf\u7d20\u8272(pixel color)\u53bb\u4f5c\u4e3a\u6700\u540e\u8f93\u51fa\u7684\u8282\u70b9,\u800c\u662f\u7528\u4e86\u4ed6\u4eec\u7684\u4e00\u4e9b\u53ef\u4ee5\u63cf\u8ff0\u4ed6\u4eec\u7684\u8868\u9762\u53c2\u6570(\u5f25\u6f2b,\u955c\u9762,\u6cd5\u7ebf)\u4f5c\u4e3a\u8282\u70b9,\u6240\u6709\u7684\u706f\u5149\u4ee3\u7801\u4e5f\u4e0d\u662f\u9488\u5bf9\u4f5c\u8272\u5668\u672c\u8eab\u56fe\u5f62.OpenShadingLanguage(<a href=\"http:\/\/code.google.com\/p\/openshadinglanguage\/\" target=\"_blank\" rel=\"external\">http:\/\/code.google.com\/p\/openshadinglanguage\/<\/a>)\u662f\u548c\u6211\u4eec\u60f3\u7684\u7c7b\u4f3c\u7684\u4e00\u4e2a\u60f3\u6cd5,\u4e0d\u8fc7\u56e0\u4e3a\u5b83\u662f\u9488\u5bf9\u7535\u5f71\u7684\u8131\u673a\u6e32\u67d3,\u6240\u6709\u5b83\u66f4\u4e30\u5bcc\u66f4\u590d\u6742.<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50,\u4f46\u5b83\u5f88\u5b8c\u6574,\u6f14\u793a\u4e86Unity3\u7684\u6f2b\u53cd\u5c04\u8d34\u56fe\u548c\u6cd5\u7ebf\u8d34\u56fe\u7684Shader.<br \/><code><br \/> Shader \"Example\/Diffuse Bump\" {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Properties {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_MainTex (\"Texture\", 2D) = \"white\" {}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_BumpMap (\"Bumpmap\", 2D) = \"bump\" {}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;SubShader {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tags { \"RenderType\" = \"Opaque\" }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CGPROGRAM<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#pragma surface surf Lambert<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct Input {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float2 uv_MainTex;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float2 uv_BumpMap;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sampler2D _MainTex;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sampler2D _BumpMap;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void surf (Input IN, inout SurfaceOutput o) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENDCG<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Fallback \"Diffuse\"<br \/>&nbsp;&nbsp;}<\/code><\/p>\n<p>\u5c06\u5b83\u8d4b\u4e88\u7ed9\u6f02\u4eae\u7684\u6a21\u578b\u548c\u8d34\u56fe,\u5b83\u53ef\u4ee5\u4ea7\u751f\u6f02\u4eae\u7684\u56fe\u50cf!\u600e\u80fd\u4e0d\u9177?!<br \/><img decoding=\"async\" src=\"wp-content\/uploads\/attachments\/month_1007\/201007221243050.png\" \/><\/p>\n<p>\u8fd9\u4e2a\u662f\u4e0d\u662f\u771f\u7684\u5f88\u6709\u8da3,\u5c06\u7740\u8272\u5668\u7684\u590d\u6742\u8bed\u6cd5\u7b80\u5355\u5316,\u540e\u9762\u518d\u4e3a\u4e00\u4e9b\u8001\u53e4\u8463\u673a\u5668\u505a\u4e00\u4e9b\u540e\u5907\u58f0\u660e.\u5269\u4e0b\u7684\u53ea\u6709CG\u8bed\u8a00\/HLSL\u7684\u4ee3\u7801,\u7136\u540e\u901a\u8fc7\u81ea\u52a8\u751f\u6210\u6700\u7ec8\u4ee3\u7801\u63d0\u9ad8\u56fe\u50cf\u6548\u679c.<\/p>\n<p>\u6211\u4eec\u6765\u89e3\u6790\u4e00\u4e0b\u8fd9\u6bb5\u7740\u8272\u5668\u4ee3\u7801:<\/p>\n<ul><\/p>\n<li>#pragma surface surf Lambert:\u8fd9\u662f\u4e00\u4e2a\u8868\u9762\u7740\u8272(surf)\u7528\u7684\u4e3b\u8981\u51fd\u6570,\u548c\u7528Lambert\u7167\u660e\u6a21\u5f0f.<\/li>\n<li>struct Input:\u8868\u9762\u4f5c\u8272\u5668\u8f93\u5165\u7684\u6570\u636e.\u8fd9\u53ef\u4ee5\u6709\u591a\u79cd\u5b9a\u4e49,\u7528\u6765\u63cf\u8ff0\u6bcf\u4e2a\u9876\u70b9\u548c\u50cf\u7d20\u7684\u5bf9\u5e94\u5173\u7cfb.\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u91cc\u5b83\u6709\u4e24\u4e2a\u7eb9\u7406\u5750\u6807.<\/li>\n<li>surf\u51fd\u6570,\u8868\u9762\u7684\u5b9e\u9645\u7740\u8272\u4ee3\u7801.\u9700\u8981\u7531SurfaceOutput(\u9884\u5b9a\u4e49\u7ed3\u6784)\u5199\u5165.\u5b83\u53ef\u4ee5\u5199\u5165\u81ea\u5b9a\u4e49\u7ed3\u6784,\u5b9e\u9645\u7684\u4ee3\u7801\u53ea\u662f\u5199\u53cd\u7167\u7387\u548c\u6cd5\u7ebf\u7684\u8f93\u51fa.<\/li>\n<\/ul>\n<p>\u4ec0\u4e48\u662f\u751f\u6210\uff1f<\/p>\n<p>Unity\u7684&#8221;\u8868\u9762\u7740\u8272\u5668\u4ee3\u7801\u751f\u6210\u5668&#8221;\u5c06\u5229\u7528\u8fd9\u4e2a\u65b9\u5f0f\u4ea7\u751f\u5b9e\u9645\u7684\u9876\u70b9\u548c\u50cf\u7d20\u7740\u8272\u5668,\u5e76\u5c06\u5b83\u7f16\u8bd1\u597d\u7ed9\u5404\u79cd\u76ee\u6807\u5e73\u53f0.\u5728Unity3\u4e2d\u5c06\u9ed8\u8ba4\u652f\u6301\u8fd9\u79cd\u7740\u8272\u5668:<\/p>\n<ul><\/p>\n<li>\u63d0\u524d\u6e32\u67d3\u548c\u9012\u5ef6\u6e32\u67d3<\/li>\n<li>\u9884\u8ba1\u7b97\u5bf9\u8c61\u7269\u4f53\u6709\u65e0\u5149\u7167.<\/li>\n<li>\u5e73\u884c\u5149,\u70b9\u5149\u6e90,\u5c04\u706f\u7684cookie\u9634\u5f71\u6709\u65e0,\u5b9e\u65f6\u5149\u5f71\u7684\u6709\u65e0.<\/li>\n<li>\u5bf9\u4e8e\u63d0\u524d\u6e32\u67d3,\u5b83\u4f1a\u5728\u7f16\u8bd1\u65f6\u8ba1\u7b97\u6bcf\u4e2a\u9876\u70b9\u7684\u706f\u7684\u6548\u679c\u53cd\u6620.<\/li>\n<li>\u5ef6\u8fdf\u7167\u660e\u5c06\u4f1a\u4ea7\u751f\u6cd5\u7ebf\u8d34\u56fe,\u955c\u9762\u8d34\u56fe\u6700\u540e\u901a\u8fc7\u706f\u5149\u7684\u53cd\u5c04\u7387\u548c\u4efb\u4f55\u5176\u5b83\u5149\u7167\u5149\u5f71\u7684\u53cd\u6620.<\/li>\n<li>\u5b83\u53ef\u4ee5\u9009\u62e9\u6027\u7684\u751f\u6210\u4e00\u4e2a\u9634\u5f71,\u6bd4\u5982\u4e00\u4e9b\u590d\u6742\u7684\u901a\u9053\u8d34\u56fe\u4ea7\u751f\u7684\u9634\u5f71.<\/li>\n<\/ul>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\u901a\u8fc7\u63d0\u4f9b\u7684\u4e00\u4e2a\u65b9\u5411\u706f,4\u4e2a\u9876\u70b9\u5149\u6e90,\u4e09\u4e2a\u9634\u5f71\u5149\u88ab\u7f16\u8bd1\u4e3a\u4e00\u4e2a\u63d0\u524d\u6e32\u67d3\u7684\u53ef\u9009\u5149\u5f71\u7684shader.\u6211\u5efa\u8bae\u4f60\u6eda\u52a8\u9875\u9762\u5f80\u4e0b\u770b\u770b\u5c31\u597d\u4e86:<\/p>\n<p><code>#pragma vertex vert_surf<br \/>#pragma fragment frag_surf<br \/>#pragma fragmentoption ARB_fog_exp2<br \/>#pragma fragmentoption ARB_precision_hint_fastest<br \/>#pragma multi_compile_fwdbase<br \/>#include \"HLSLSupport.cginc\"<br \/>#include \"UnityCG.cginc\"<br \/>#include \"Lighting.cginc\"<br \/>#include \"AutoLight.cginc\"<br \/>struct Input {<br \/>&#160;&#160;&#160;&#160;float2 uv_MainTex : TEXCOORD0;<br \/>};<br \/>sampler2D _MainTex;<br \/>sampler2D _BumpMap;<br \/>void surf (Input IN, inout SurfaceOutput o)<br \/>{<br \/>&#160;&#160;&#160;&#160;o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;<br \/>&#160;&#160;&#160;&#160;o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_MainTex));<br \/>}<br \/>struct v2f_surf {<br \/>&nbsp;&nbsp;V2F_POS_FOG;<br \/>&nbsp;&nbsp;float2 hip_pack0 : TEXCOORD0;<br \/>&nbsp;&nbsp;#ifndef LIGHTMAP_OFF<br \/>&nbsp;&nbsp;float2 hip_lmap : TEXCOORD1;<br \/>&nbsp;&nbsp;#else<br \/>&nbsp;&nbsp;float3 lightDir : TEXCOORD1;<br \/>&nbsp;&nbsp;float3 vlight : TEXCOORD2;<br \/>&nbsp;&nbsp;#endif<br \/>&nbsp;&nbsp;LIGHTING_COORDS(3,4)<br \/>};<br \/>#ifndef LIGHTMAP_OFF<br \/>float4 unity_LightmapST;<br \/>#endif<br \/>float4 _MainTex_ST;<br \/>v2f_surf vert_surf (appdata_full v) {<br \/>&nbsp;&nbsp;v2f_surf o;<br \/>&nbsp;&nbsp;PositionFog( v.vertex, o.pos, o.fog );<br \/>&nbsp;&nbsp;o.hip_pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);<br \/>&nbsp;&nbsp;#ifndef LIGHTMAP_OFF<br \/>&nbsp;&nbsp;o.hip_lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;<br \/>&nbsp;&nbsp;#endif<br \/>&nbsp;&nbsp;float3 worldN = mul((float3x3)_Object2World, SCALED_NORMAL);<br \/>&nbsp;&nbsp;TANGENT_SPACE_ROTATION;<br \/>&nbsp;&nbsp;#ifdef LIGHTMAP_OFF<br \/>&nbsp;&nbsp;o.lightDir = mul (rotation, ObjSpaceLightDir(v.vertex));<br \/>&nbsp;&nbsp;#endif<br \/>&nbsp;&nbsp;#ifdef LIGHTMAP_OFF<br \/>&nbsp;&nbsp;float3 shlight = ShadeSH9 (float4(worldN,1.0));<br \/>&nbsp;&nbsp;o.vlight = shlight;<br \/>&nbsp;&nbsp;#ifdef VERTEXLIGHT_ON<br \/>&nbsp;&nbsp;float3 worldPos = mul(_Object2World, v.vertex).xyz;<br \/>&nbsp;&nbsp;o.vlight += Shade4PointLights (<br \/>&nbsp;&nbsp;&nbsp;&nbsp;unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;unity_LightColor0, unity_LightColor1, unity_LightColor2, unity_LightColor3,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;unity_4LightAtten0, worldPos, worldN );<br \/>&nbsp;&nbsp;#endif \/\/ VERTEXLIGHT_ON<br \/>&nbsp;&nbsp;#endif \/\/ LIGHTMAP_OFF<br \/>&nbsp;&nbsp;TRANSFER_VERTEX_TO_FRAGMENT(o);<br \/>&nbsp;&nbsp;return o;<br \/>}<br \/>#ifndef LIGHTMAP_OFF<br \/>sampler2D unity_Lightmap;<br \/>#endif<br \/>half4 frag_surf (v2f_surf IN) : COLOR {<br \/>&nbsp;&nbsp;Input surfIN;<br \/>&nbsp;&nbsp;surfIN.uv_MainTex = IN.hip_pack0.xy;<br \/>&nbsp;&nbsp;SurfaceOutput o;<br \/>&nbsp;&nbsp;o.Albedo = 0.0;<br \/>&nbsp;&nbsp;o.Emission = 0.0;<br \/>&nbsp;&nbsp;o.Specular = 0.0;<br \/>&nbsp;&nbsp;o.Alpha = 0.0;<br \/>&nbsp;&nbsp;o.Gloss = 0.0;<br \/>&nbsp;&nbsp;surf (surfIN, o);<br \/>&nbsp;&nbsp;half atten = LIGHT_ATTENUATION(IN);<br \/>&nbsp;&nbsp;half4 c;<br \/>&nbsp;&nbsp;#ifdef LIGHTMAP_OFF<br \/>&nbsp;&nbsp;c = LightingLambert (o, IN.lightDir, atten);<br \/>&nbsp;&nbsp;c.rgb += o.Albedo * IN.vlight;<br \/>&nbsp;&nbsp;#else \/\/ LIGHTMAP_OFF<br \/>&nbsp;&nbsp;half3 lmFull = DecodeLightmap (tex2D(unity_Lightmap, IN.hip_lmap.xy));<br \/>&nbsp;&nbsp;#ifdef SHADOWS_SCREEN<br \/>&nbsp;&nbsp;c.rgb = o.Albedo * min(lmFull, atten*2);<br \/>&nbsp;&nbsp;#else<br \/>&nbsp;&nbsp;c.rgb = o.Albedo * lmFull;<br \/>&nbsp;&nbsp;#endif<br \/>&nbsp;&nbsp;c.a = o.Alpha;<br \/>&nbsp;&nbsp;#endif \/\/ LIGHTMAP_OFF<br \/>&nbsp;&nbsp;return c;<br \/>}<\/code><br \/>\u572890\u884c\u7684\u4ee3\u7801\u4e2d,10\u884c\u662f\u539f\u59cb\u7684\u7740\u8272\u5668\u4ee3\u7801,\u5176\u4f59\u7684\u4ee3\u7801\u5728Unity2\u7684\u65f6\u5019\u51e0\u4e4e\u90fd\u8981\u624b\u5199\u7684.\u522b\u6025,\u8fd9\u53ea\u662f\u5199\u4e86\u4e00\u70b9\u63d0\u524d\u6e32\u67d3\u7684\u90e8\u5206!\u8fd8\u9700\u8981\u5199\u9012\u5ef6\u6e32\u67d3,\u53ef\u9009\u9634\u5f71\u7b49\u7b49\u706f.<\/p>\n<p>\u56e0\u6b64,\u73b0\u5728\u5e94\u8be5\u4f1a\u6bd4\u8f83\u5bb9\u6613\u7684\u5199\u7740\u8272\u5668(\u5bf9\u4e8e\u6211\u6765\u8bf4\u81f3\u5c11).Unity\u7684\u7528\u6237\u4e5f\u613f\u610f\u5c11\u5199\u4ee3\u7801,\u5c11\u4e9b\u81f3\u5c11\u4e09\u500d\u7684\u4ee3\u7801\u4f55\u4e50\u4e0d\u4e3a?\u5b83\u8fd8\u4f1a\u6709\u4e00\u4e9b\u6539\u53d8\u4ee5\u9002\u5e94Unity\u4e0b\u4e00\u6b65\u65b0\u7684\u7167\u660e\u7cfb\u7edf.<\/p>\n<p>\u9884\u5b9a\u4e49\u7684\u8f93\u5165\u503c<\/p>\n<p>\u8f93\u5165\u7ed3\u6784\u53ef\u4ee5\u5305\u542b\u7eb9\u7406\u5750\u6807\u548c\u4e00\u4e9b\u9884\u5b9a\u4e49\u7684\u503c,\u4f8b\u5982\u67e5\u770b\u7684\u65b9\u5411,\u4e16\u754c\u7a7a\u95f4\u7684\u4f4d\u7f6e,\u4e16\u754c\u7a7a\u95f4\u7684\u53cd\u5c04\u77e2\u91cf\u706f.\u4f8b\u5982,\u5982\u679c\u4f60\u5229\u7528\u4e16\u754c\u7a7a\u95f4\u53cd\u5c04\u77e2\u91cf(\u5982\u81ea\u53d1\u5149),\u5728\u6a21\u578b\u8868\u9762\u505a\u4e00\u4e9bCubeMap\u53cd\u5c04\u6548\u679c(\u9012\u5ef6\u6e32\u67d3\u7684\u53cd\u5c04\u5c06\u4e0d\u4f1a\u8ba1\u7b97,\u56e0\u4e3a\u5b83\u4e0d\u4f1a\u4ea7\u751f\u6548\u679c,\u6240\u4ee5\u4e5f\u4e0d\u9700\u8981\u8003\u8651\u53cd\u5c04\u5411\u91cf),<\/p>\n<p><img decoding=\"async\" src=\"wp-content\/uploads\/attachments\/month_1007\/20100722124338l.png\" \/><\/p>\n<p>\u7528\u4e00\u4e2a\u5c0f\u4f8b\u5b50\u8bf4\u660e\u505a\u7b80\u5355\u7684\u706f\u5149\u6e32\u67d3:<\/p>\n<p><code> #pragma surface surf Lambert<br \/>&nbsp;&nbsp;struct Input {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float2 uv_MainTex;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float2 uv_BumpMap;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float3 viewDir;<br \/>&nbsp;&nbsp;};<br \/>&nbsp;&nbsp;sampler2D _MainTex;<br \/>&nbsp;&nbsp;sampler2D _BumpMap;<br \/>&nbsp;&nbsp;float4 _RimColor;<br \/>&nbsp;&nbsp;float _RimPower;<br \/>&nbsp;&nbsp;void surf (Input IN, inout SurfaceOutput o) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;half rim =<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Emission = _RimColor.rgb * pow (rim, _RimPower);<br \/>&nbsp;&nbsp;}<\/code><\/p>\n<p>\u9876\u70b9\u4fee\u6539\u5668<\/p>\n<p>\u5b83\u53ef\u4ee5\u81ea\u5b9a\u4e49\u9876\u70b9\u4fee\u9970\u529f\u80fd,\u7528\u6765\u4fee\u6539\u6216\u8005\u4ea7\u751f\u6bcf\u4e2a\u9876\u70b9\u7684\u6570\u636e.\u50cf\u98ce\u5439\u52a8\u6811\u7684\u52a8\u753b,\u8349\u7684\u6447\u6446\u7b49\u7b49.<\/p>\n<p>\u6211\u6700\u559c\u6b22\u6cbf\u7740\u6cd5\u7ebf\u9876\u70b9\u7684\u65b9\u5411\u53bb\u79fb\u52a8\u9876\u70b9.\u50cf\u6253\u80bf\u4e86\u7684\u6548\u679c\u2026<\/p>\n<p><img decoding=\"async\" src=\"wp-content\/uploads\/attachments\/month_1007\/20100722124352r.png\" \/><\/p>\n<p>\u81ea\u5b9a\u4e49\u5149\u7167\u6a21\u578b<\/p>\n<p>\u6709\u4e00\u4e2a\u5185\u7f6e\u7684\u7b80\u5355\u7684\u5149\u7167\u6a21\u578b,\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4f60\u81ea\u5df1\u7684.A\u7167\u660e\u6a21\u5f0f\u53ea\u4e0d\u8fc7\u5c06\u5e38\u7528\u7684\u5149\u7684\u53c2\u6570,\u65b9\u5411\u8870\u51cf,\u800c\u6ca1\u5305\u542b\u4e00\u4e9b\u63d0\u524d\u6e32\u67d3\u548c\u5ef6\u8fdf\u6e32\u67d3\u4ee5\u53ca\u4e00\u4e9b\u4e0d\u957f\u7528\u5230\u7684\u65b9\u6cd5.\u56e0\u6b64,\u5bf9\u4e8e\u4efb\u4f55\u5947\u7279\u7684\u6548\u679c\u4f60\u90fd\u8981\u901a\u8fc7\u63d0\u4f9b\u81ea\u5b9a\u4e49\u5149\u7167\u6a21\u578b\u6765\u5b8c\u6210.<\/p>\n<p><img decoding=\"async\" src=\"wp-content\/uploads\/attachments\/month_1007\/20100722124406y.png\" \/><\/p>\n<p>\u4f8b\u5982wrapped-Lambert\u5149\u7167\u6a21\u5f0f:<br \/><code>&nbsp;&nbsp;#pragma surface surf WrapLambert<br \/>&nbsp;&nbsp;half4 LightingWrapLambert (SurfaceOutput s, half3 dir, half atten) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dir = normalize(dir);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;half NdotL = dot (s.Normal, dir);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;half diff = NdotL * 0.5 + 0.5;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;half4 c;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.a = s.Alpha;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return c;<br \/>&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;struct Input {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float2 uv_MainTex;<br \/>&nbsp;&nbsp;};<br \/>&nbsp;&nbsp;sampler2D _MainTex;<br \/>&nbsp;&nbsp;void surf (Input IN, inout SurfaceOutput o) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;<br \/>&nbsp;&nbsp;}<\/code><\/p>\n<p>\u5e55\u540e<\/p>\n<p>\u6211\u4eec\u4f7f\u7528Ryan Gordon\u7684mojoshder(<a href=\"http:\/\/hg.icculus.org\/icculus\/mojoshader\/\" target=\"_blank\" rel=\"external\">http:\/\/hg.icculus.org\/icculus\/mojoshader\/<\/a>)\u4e2d\u7684HLSL\u89e3\u6790\u5668\u6765\u5206\u6790\u539f\u59cb\u7684\u8868\u9762\u7740\u8272\u5668\u4ee3\u7801.\u627e\u51fa\u6709\u54ea\u4e9b\u6210\u5458\u7ed3\u6784,\u6709\u54ea\u4e9b\u51fd\u6570\u539f\u578b\u7b49\u7b49.\u5728\u8fd9\u4e2a\u9636\u6bb5\u505a\u7684\u4e00\u4e9b\u9519\u8bef\u68c0\u67e5\u662f\u4e3a\u4e86\u544a\u8bc9\u7528\u6237\u5b83\u7684\u9762\u51fd\u6570\u539f\u578b\u662f\u9519\u8bef\u6216\u8005\u4ed6\u7684\u7ed3\u6784\u7f3a\u5c11\u5fc5\u8981\u7684\u6210\u5458.<\/p>\n<p>\u4e3a\u4e86\u5f04\u6e05\u695a\u7740\u8272\u5668\u5b9e\u9645\u7684\u7167\u660e\u5173\u7cfb,\u6211\u4eec\u505a\u4e86\u50cf\u7d20\u7740\u8272\u5668\u7684\u5c0f\u5de5\u5177\u6765\u7f16\u8bd1CG\u8bed\u8a00\u548c\u4f7f\u7528CG\u8bed\u8a00\u7684API\u67e5\u8be2\u4f7f\u7528\u4e86\u54ea\u4e9b\u8f93\u5165\u8f93\u51fa.\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u7b97\u51fa\u4f8b\u5982\u4e00\u4e2a\u6cd5\u7ebf\u8d34\u56fe\u5b9e\u9645\u4e0d\u7528\u9012\u5ef6\u7167\u660e\u5e76\u4e5f\u8282\u7701\u4e86\u4e00\u4e9b\u9876\u70b9\u7740\u8272\u5668\u6307\u4ee4\u548c\u4e00\u4e2a\u7eb9\u7406\u5750\u6807\u503c.<\/p>\n<p>\u751f\u6210\u7684\u4ee3\u7801\u6700\u7ec8\u6839\u636e\u76ee\u6807\u5e73\u53f0\u88ab\u7f16\u8bd1.Cg\u4e3aWindows\/Mac,XDK HLSL\u4e3aXbox 360,PS3 Cg\u4e3aPS3,\u548c\u6211\u4eec\u81ea\u5df1\u521b\u9020\u7684HLSL2GLSL\u4e3aiPhone, Android\u548c\u5373\u5c06\u9000\u51fa\u7684Unity NativeClient(<a href=\"http:\/\/blogs.unity3d.com\/2010\/05\/19\/google-android-and-the-future-of-games-on-the-web\/\" target=\"_blank\" rel=\"external\">http:\/\/blogs.unity3d.com\/2010\/05\/19\/google-android-and-the-future-of-games-on-the-web\/<\/a>).<\/p>\n<p>\u5c31\u8fd9\u6837,\u6211\u4eec\u4f1a\u7ee7\u7eed\u505a\u4e0b\u53bb,\u7b49\u5230Unity3\u53d1\u5e03\u7684\u65f6\u5019,\u6211\u5e0c\u671b\u66f4\u591a\u7684\u4eba\u4f1a\u5c1d\u8bd5\u5199\u7740\u8272\u5668!<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea1Vr.Cn,\u539f\u6587\u5730\u5740:<a href=\"http:\/\/blogs.unity3d.com\/2010\/07\/17\/unity-3-technology-surface-shaders\/\" target=\"_blank\" rel=\"external\">http:\/\/blogs.unity3d.com\/2010\/07\/17\/unity-3-technology-surface-shaders\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8c01\u90fd\u77e5\u9053\u5728Unity\u4e2d\u53ef\u4ee5\u7f16\u5199shader,\u53ef\u662f\u4ed6\u4eec\u5f88\u96be\u5199,\u5c24\u5176\u5f53\u4f60\u9700\u8981\u6bcf\u4e2a\u50cf\u7d20\u7684\u706f\u5149\u548c\u9634\u5f71\u4ea7\u751f\u4ea4\u4e92.\u5728Uni &hellip; <a href=\"https:\/\/1vr.cn\/?p=542\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unity3\u6280\u672f\u4e4b\u8868\u9762\u7740\u8272<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","without-featured-image"],"_links":{"self":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=542"}],"version-history":[{"count":1,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions\/728"}],"wp:attachment":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}