{"id":2313,"date":"2020-09-10T21:34:30","date_gmt":"2020-09-10T13:34:30","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=2313"},"modified":"2020-09-17T00:09:35","modified_gmt":"2020-09-16T16:09:35","slug":"urp-%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=2313","title":{"rendered":"[Unity]URP\u5b66\u4e60\u8bb0\u5f55\uff081\uff09"},"content":{"rendered":"\n<p>\u539f\u6587\u94fe\u63a5\uff1a<br><a href=\"https:\/\/cyangamedev.wordpress.com\/2020\/06\/05\/urp-shader-code\/\">https:\/\/cyangamedev.wordpress.com\/2020\/06\/05\/urp-shader-code\/<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>URP\u4e2d\uff0cRenderType\u53ef\u80fd\u4e0d\u592a\u91cd\u8981\u4e86\uff0c\u5728\u5185\u7f6e\u7ba1\u7ebf\u4e2d\uff0c\u662f\u7528\u6765\u505aReplacementShaders\u7528\u7684<br> \u4f46\u662f\u5728URP\u4e2d\u4e0d\u652f\u6301ReplacementShader\uff0c\u5c3d\u7ba1\u6709\u4e2aForwardRenderer\u7684overrideMaterail<br> \u6bcf\u4e2aPass\u6807\u7b7e\u90fd\u9700\u8981\u6807\u8bb0\u7279\u5b9a\u7684LightMode\uff0cURP\u4f7f\u7528 single-pass forward renderer.<br> \u6240\u6709\u53ea\u6709\u4e00\u4e2a\u201cUniversalFoward\u201d\u7684Pass\uff0c\u4e5f\u4e0d\u80fd\u540c\u65f6\u6e32\u67d3\u591a\u4e2a\u5bf9\u8c61\u3002<br> \u4e5f\u53ef\u4ee5\u4e0d\u52a0Tag\uff0c\u4f46\u662f\u4f1a\u7834\u574fSRP Batcher<br> \u5efa\u8bae\u5728\u5355\u72ec\u7684MeshRender\u4e0a\u4f7f\u7528\u72ec\u7acb\u7684Shader\u6216\u8005\u6750\u8d28<br> \u6216\u8005\u4f7f\u7528Forward Renderer\u4e0aoverrideMaterial\u7684\u201cRender Objects \u7279\u6027 <\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p> URP LightMode Tags\uff1a<br> Tags{&#8220;LightMode&#8221; = &#8220;XXX&#8221;}<br> UniversalForward\uff1a\u524d\u5411\u6e32\u67d3\u7269\u4ef6\u4e4b\u7528<br> ShadowCaster\uff1a \u6295\u5c04\u9634\u5f71\u4e4b\u7528<br> DepthOnly\uff1a\u53ea\u7528\u6765\u4ea7\u751f\u6df1\u5ea6\u56fe<br> Mata\uff1a\u6765\u7528\u70d8\u7119\u5149\u7167\u56fe\u4e4b\u7528<br> Universal2D \uff1a\u505a2D\u6e38\u620f\u7528\u7684\uff0c\u7528\u6765\u66ff\u4ee3\u524d\u5411\u6e32\u67d3<br> UniversalGBuffer \uff1a \u8c8c\u4f3c\u4e0e\u5ef6\u8fdf\u6e32\u67d3\u76f8\u5173\uff08\u5f00\u53d1\u4e2d\uff09 <\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p> Unity\u60f3\u5f03\u7528CG\uff0c\u63a8\u8350\u4f7f\u7528HLSL(High level shading language-\u9ad8\u7ea7\u7740\u8272\u8bed\u8a00)<br> \u4e0d\u518d\u6709fixed\u7c7b\u578b,\u53ea\u6709half\u548cfloat<br>Cg\u548cHLSL\u88ab\u89c6\u4e3a\u76f8\u540c\u7684\u8bed\u8a00<br>\u5982\u679c\u5728URP\u4e2d\u4f7f\u7528CG\u7684\u6807\u7b7e\uff0c\u5c06\u4e0eURPShaderLibrary\u51b2\u7a81<br> \u56e0\u4e3a\u53d8\u91cf\u548c\u51fd\u6570\u4f1a\u88ab\u91cd\u590d\u5b9a\u4e49<br>\u73b0\u5728\u8bf7\u4f7f\u7528 HLSLPROGRAM, HLSLINCLUDE, ENDHLSL<br> \u4e0d\u63a8\u8350\u7528CGPROGRAM, ENDCG, CGINCLUDE <\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Pass\u7684Name\uff0c\u5e94\u8be5\u5168\u5927\u5199<br> \u53ef\u4ee5\u7528UsePass\u6765\u5f15\u7528<br> \u4f8b\u5982\uff1aUsePass &#8220;Custom\/UnlitShaderExample\/MyShader&#8221;<br> \u4e3a\u4e86\u4e0eSPRBatcher\u517c\u5bb9\uff0c\u6240\u6709\u4f20\u9012\u5fc5\u987b\u5171\u4eab\u76f8\u540c\u7684UnityPerMaterial CBUFFER<br> \u5982\u679c\u4e0d\u5339\u914d\uff0c\u5219\u51fa\u9519<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HLSL\u6570\u636e\u7c7b\u578b1 &#8211; \u57fa\u7840\u6570\u636e<br> bool \u2013 true \/ false.<br> float \u2013 32\u4f4d\u6d6e\u70b9\u6570\uff0c\u7528\u5728\u6bd4\u5982\u4e16\u754c\u5750\u6807\uff0c\u7eb9\u7406\u5750\u6807\uff0c\u590d\u6742\u7684\u51fd\u6570\u8ba1\u7b97<br> half \u2013 16\u4f4d\u6d6e\u70b9\u6570\uff0c\u7528\u4e8e\u77ed\u5411\u91cf\u3001\u65b9\u5411\u3001\u989c\u8272\uff0c\u6a21\u578b\u7a7a\u95f4\u4f4d\u7f6e<br> double \u2013 64\u4f4d\u6d6e\u70b9\u6570\uff0c\u4e0d\u80fd\u7528\u4e8e\u8f93\u5165\u8f93\u51fa\uff0c\u8981\u4f7f\u7528double\uff0c\u5f97\u58f0\u660e\u4e3a\u4e00\u5bf9unit\u518d\u7528asuint\u628adouble\u6253\u5305\u5230uint\u5bf9\u4e2d\uff0c\u518d\u7528asdouble\u51fd\u6570\u89e3\u5305<br> fixed \u2013 \u53ea\u80fd\u7528\u4e8e\u5185\u5efa\u7ba1\u7ebf\uff0cURP\u4e0d\u652f\u6301\uff0c\u7528half\u66ff\u4ee3<br> real \u2013 \u597d\u50cf\u53ea\u7528\u4e8eURP\uff0c\u5982\u679c\u5e73\u53f0\u6307\u5b9a\u4e86\u7528half\uff08#define PREFER_HALF 0\uff09\uff0c\u5426\u5219\u5c31\u662ffloat\u7c7b\u578b<br> int \u2013 32\u4f4d\u6709\u7b26\u53f7\u6574\u5f62<br> uint \u2013 32\u4f4d\u65e0\u7b26\u53f7\u6574\u5f62(GLES2\u4e0d\u652f\u6301\uff0c\u4f1a\u7528int\u66ff\u4ee3)<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HLSL\u6570\u636e\u7c7b\u578b2 &#8211; \u5411\u91cf<br>vector\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u5728\u57fa\u7840\u6570\u636e\u540e\u6dfb\u52a0\u7ef4\u5ea6<br>\u4f8b\u5982\uff1afloat4, half3, int2 \u2026<br>\u8bbf\u95ee\u53ef\u4ee5\u7528xyzw\u6216\u8005rgba\u8bbf\u95ee<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HLSL\u6570\u636e\u7c7b\u578b3 &#8211; \u77e9\u9635<br> matrix\u7c7b\u578b\u53ef\u4ee5\u76f4\u63a5\u5728\u57fa\u7840\u6570\u636e\u540e\u6dfb\u52a0 \u7ef4\u5ea6 x \u7ef4\u5ea6<br> \u4f8b\u5982\uff1afloat4x4, int4x3, half2x1<br> \u5373\u8868\u8fbe 4\u884c4\u5217\u7684float\uff0c4\u884c3\u5217\u7684int\uff0c2\u884c1\u5217\u7684half<br> float3x3 m = { 0, 1, 2, 3, 4, 5, 6, 7, 8};<br> float3 row0 = m[0]; \/\/ 0, 1, 2<br> float r1c2 = m[1][2]; \/\/ 5<br> \u4e58\u6cd5\uff0c\u4f7f\u7528mul\u8fdb\u884c<br> mul(m, row0)<br> \u7b2c1\u4e2a\u5217\u6570\u5fc5\u987b\u4e0e\u7b2c2\u4e2a\u884c\u6570\u76f8\u540c<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HLSL\u6570\u636e\u7c7b\u578b4 &#8211; \u6570\u7ec4<br> ShaderLab\u6750\u8d28\u5c5e\u6027\u9762\u677fProperties\u4e0d\u652f\u6301\u6570\u7ec4\uff0c\u53ea\u80fd\u4eceC#\u4e2d\u8bbe\u7f6e<br> \u5fc5\u987b\u5728Shader\u4e2d\u6307\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u4f8b\u5982<br> float array[10];<br> float4x4 array2[10];<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7eb9\u7406\u548c\u91c7\u6837<br> \u5b9a\u4e49\uff1a<br> TEXTURE2D(textureName);<br> SAMPLER(sampler_textureName);<br> \u7f13\u5b58\u533a\uff1a<br> \u4eceC#\u4e2d\u4f7f\u7528material.SetBuffer\u6216\u8005 Shader.SetGlobalBuffer<br> \u4f8b\u5982\uff1aStructuredBuffer buffer;<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5b8f(macro)<br> define MUL2(x,y)  ((x)*(y))<br> \u53ef\u4ee5\u505a\u4e00\u4e9b\u8bed\u6cd5\u7cd6\uff0c\u4f8b\u5982\uff1a<br> define TRANSFORM_TEX(tex, name) (tex.xy* name##_ST.xy + name##_ST.zw)<br> o.uv = TRANSFORM_TEX(in.uv, _MainTex) =><br> o.uv = (in.uv.xy * _MainTex_ST.xy + _MainTex.ST_zw)<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u6bcf\u4e2aPass, UnityPerMaterial CBUFFER\u90fd\u662f\u76f8\u540c\u7684<br> CBUFFER\u9700\u8981\u5305\u542b\u6240\u6709\u516c\u7528\u7684\u5c5e\u6027\uff08\u5373\u4e0eShaderLab\u4e2d\u7684Properties\u76f8\u540c\uff09<br> \u5b83\u4e0d\u80fd\u5305\u542b\u5176\u5b83\u672a\u516c\u5f00\u7684\u5c5e\u6027\u4ee5\u53ca\u7eb9\u7406\u91c7\u6837\u5668<br> \u867d\u7136\u4e0d\u9700\u8981\u901a\u8fc7C# material.SetColor\/SetFloat\u7b49<br> \u4f46\u591aMaterial\u5b9e\u4f8b\u5177\u6709\u4e0d\u540c\u7684\u503c\uff0c\u8fd9\u4f1a\u4ea7\u751f\u95ee\u9898\uff0cSRP Batcher\u4f1a\u5c06\u4ed6\u4eec\u4e00\u8d77\u6279\u5904\u7406<br> 1\u3001\u5982\u679c\u60a8\u6709\u672a\u516c\u5f00\u7684\u53d8\u91cf\uff0c\u8bf7\u59cb\u7ec8\u4f7f\u7528Shader.SetGlobalColor \/ Float<br> \u4ee5\u4f7f\u5b83\u4eec\u5728\u6240\u6709\u6750\u8d28\u5b9e\u4f8b\u4e2d\u4fdd\u6301\u4e0d\u53d8\u3002<br> 2\u3001\u5982\u679c\u6bcf\u4e2a\u6750\u6599\u90fd\u4e0d\u540c\uff0c\u5219\u901a\u8fc7Shaderlab\u5c5e\u6027\u5757\u5c06\u5b83\u4eec\u516c\u5f00\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230CBUFFER\u4e2d<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>HLSLINCLUDE<br>     #include &#8220;Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/Core.hlsl&#8221;<br> <code>CBUFFER_START(UnityPerMaterial)     float4 _BaseMap_ST;     float4 _BaseColor; CBUFFER_END<\/code><br> ENDHLSL<br> Core.hlsl\u6587\u4ef6\u662fURP\u7684\u5185\u7f6e\u6838\u5fc3\u6587\u4ef6<br> \u6bd4\u5982\u5982\u679c\u8981\u4f7f\u7528\u706f\u5149\uff0c\u5219\u6dfb\u52a0 Lighting.hlsl\u6587\u4ef6<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Vertex Shader \u9636\u6bb5<br> \u5728\u5185\u7f6eShader\u4e2d\uff0c\u4f7f\u7528UnityObjectToClipPos\u5c06\u6a21\u578b\u7a7a\u95f4\u8f6c\u6362\u5230\u88c1\u526a\u7a7a\u95f4<br> URP\u4e2d\u4f7f\u7528TransformObjectToHClip\u51fd\u6570\uff08\u5728SpaceTransforms.hlsl\u4e2d\u6709\u5b9a\u4e49\uff09<br> URP\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528GetVertexPositionInputs\u51fd\u6570\uff08\u5728Core.hlsh\u4e2d\u6709\u5b9a\u4e49\uff09<br> \u5f97\u5230\u7684VertexPositionInputs \u7ed3\u6784\u4f53\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9<br> positionWS = positionWorldSpace<br> positionVS = positionViewSpace<br> positionCS = positionClipSpace<br> positionNDC = position in Normalised Device Coordinates<br> \u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5373\u4fbf\u542b\u6709\u672a\u4f7f\u7528\u5230\u7684\u53d8\u91cf<br> \u56e0\u4e3aShader\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\u3002<br> \u6cd5\u7ebf&amp;\u5207\u7ebf<br> VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);<br> GetVertexNormalInputs\u5c06\u6a21\u578b\u7a7a\u95f4\u6cd5\u7ebf\u548c\u5207\u7ebf\u8f6c\u6362\u4e3a\u4e16\u754c\u7a7a\u95f4<br> \u5305\u542bnormalWS, tangentWS, bitangetWS\u3002<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>Fragment Shader \u9636\u6bb5\n\u53ef\u4ee5\u4f7f\u7528 SAMPLE_TEXTURE2D \u5b8f\u8fdb\u884c\u91c7\u6837\n\n------------------------------------------------------------------\n\n\u5149\u71671\n\nURP\u4e0d\u652f\u6301SurfaceShader\nURP\u7684\u4e00\u4e9b\u5149\u7167\u6587\u4ef6\u5728 Lighting.hlsl \u4e2d\n\u53ef\u4ee5\u7528\u6765\u5904\u7406\u4f8b\u5982 UniversalFragmentPBR \n\n\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u9634\u5f71\u76f8\u5173\u7684\u5b8f\n#pragma multi_compile _ _MAIN_LIGHT_SHADOWS\n#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE\n#pragma multi_compile _ _SHADOWS_SOFT\n#include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/Lighting.hlsl\"\n\n------------------------------------------------------------------\n\n\u5149\u71672 - \u793a\u4f8b\n\nstruct Attributes {\n    ...\n    float4 normalOS     : NORMAL;\n};\n \nstruct Varyings {\n    ...\n    float3 normalWS     : NORMAL;\n    float3 positionWS   : TEXCOORD2;\n};\n...\nVaryings vert(Attributes IN) {\n    Varyings OUT;\n    VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);\n    ...\n    OUT.positionWS = positionInputs.positionWS;\n \n    VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);\n    OUT.normalWS = normalInputs.normalWS;\n \n    return OUT;\n}\n\nhalf4 frag(Varyings IN) : SV_Target {\n    half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);\n    half4 color = baseMap * _BaseColor * IN.color;\n \n    float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS.xyz);\n    Light light = GetMainLight(shadowCoord);\n \n    half3 diffuse = LightingLambert(light.color, light.direction, IN.normalWS);\n \n    return half4(color.rgb * diffuse * light.shadowAttenuation, color.a);\n}\n\n\n------------------------------------------------------------------\n\n\u5149\u7167-3\n\u5982\u679c\u8981\u7ee7\u7eed\u6269\u5c55\u4f8b\u5982 ambient\u3001bakedGI \u4ee5\u53ca \u591a\u5149\u6e90\n\u53ef\u4ee5\u53c2\u8003\nhttps:\/\/github.com\/Unity-Technologies\/Graphics\/blob\/fcacf7661c3976921d4f5dfbdc17569db7ea77a3\/com.unity.render-pipelines.universal\/ShaderLibrary\/Lighting.hlsl#L832\n\uff08UniversalFragmentBlinnPhong method in Lighting.hlsl \uff09\n\n------------------------------------------------------------------\n\nPBR \u5149\u7167\n\u5728URP\u4e2d\u4f7f\u7528\u201dLit\u201dShader\n\u5728shadergraph\u4e2d\u4f7f\u7528PBR Master \u8282\u70b9\n\nURP\u4e0d\u652f\u6301SurfaceShader\uff0c\u6240\u4ee5\u53ea\u80fd\u4f7f\u7528ShaderLibrary\u4e2d\u7684\u51fd\u6570\uff08\u5728Lighting.hlsl\u4e2d\uff09\n\u91cd\u70b9\u4e3aUniversalFragmentPBR \n\n\n\n------------------------------------------------------------------\n\nPBR \u793a\u4f8b\n\nProperties {\n    _BaseMap (\"Base Texture\", 2D) = \"white\" {}\n    _BaseColor (\"Example Colour\", Color) = (0, 0.66, 0.73, 1)\n    _Smoothness (\"Smoothness\", Float) = 0.5\n \n    [Toggle(_ALPHATEST_ON)] _EnableAlphaTest(\"Enable Alpha Cutoff\", Float) = 0.0\n    _Cutoff (\"Alpha Cutoff\", Float) = 0.5\n \n    [Toggle(_NORMALMAP)] _EnableBumpMap(\"Enable Normal\/Bump Map\", Float) = 0.0\n    _BumpMap (\"Normal\/Bump Texture\", 2D) = \"bump\" {}\n    _BumpScale (\"Bump Scale\", Float) = 1\n \n    [Toggle(_EMISSION)] _EnableEmission(\"Enable Emission\", Float) = 0.0\n    _EmissionMap (\"Emission Texture\", 2D) = \"white\" {}\n    _EmissionColor (\"Emission Colour\", Color) = (0, 0, 0, 0)\n    }\n...\n\/\/ And need to adjust the CBUFFER to include these too\nCBUFFER_START(UnityPerMaterial)\n    float4 _BaseMap_ST; \/\/ Texture tiling &amp; offset inspector values\n    float4 _BaseColor;\n    float _BumpScale;\n    float4 _EmissionColor;\n    float _Smoothness;\n    float _Cutoff;\nCBUFFER_END\n\n\/\/ Material Keywords\n#pragma shader_feature _NORMALMAP\n#pragma shader_feature _ALPHATEST_ON\n#pragma shader_feature _ALPHAPREMULTIPLY_ON\n#pragma shader_feature _EMISSION\n\/\/#pragma shader_feature _METALLICSPECGLOSSMAP\n\/\/#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A\n\/\/#pragma shader_feature _OCCLUSIONMAP\n \n\/\/#pragma shader_feature _SPECULARHIGHLIGHTS_OFF\n\/\/#pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF\n\/\/#pragma shader_feature _SPECULAR_SETUP\n#pragma shader_feature _RECEIVE_SHADOWS_OFF\n \n\/\/ URP Keywords\n#pragma multi_compile _ _MAIN_LIGHT_SHADOWS\n#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE\n#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS\n#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS\n#pragma multi_compile _ _SHADOWS_SOFT\n#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE\n \n\/\/ Unity defined keywords\n#pragma multi_compile _ DIRLIGHTMAP_COMBINED\n#pragma multi_compile _ LIGHTMAP_ON\n#pragma multi_compile_fog\n \n\/\/ Some added includes, required to use the Lighting functions\n#include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/Lighting.hlsl\"\n\/\/ And this one for the SurfaceData struct and albedo\/normal\/emission sampling functions.\n\/\/ Note : It also defines the _BaseMap, _BumpMap and _EmissionMap textures for us, so we should use these as Shaderlab Properties too.\n#include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/SurfaceInput.hlsl\"\n \nstruct Attributes {\n    float4 positionOS   : POSITION;\n    float3 normalOS     : NORMAL;\n    float4 tangentOS    : TANGENT;\n    float4 color        : COLOR;\n    float2 uv           : TEXCOORD0;\n    float2 lightmapUV   : TEXCOORD1;\n};\n \nstruct Varyings {\n    float4 positionCS               : SV_POSITION;\n    float4 color                    : COLOR;\n    float2 uv                       : TEXCOORD0;\n    DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);\n    \/\/ Note this macro is using TEXCOORD1\n#ifdef REQUIRES_WORLD_SPACE_POS_INTERPOLATOR\n    float3 positionWS               : TEXCOORD2;\n#endif\n    float3 normalWS                 : TEXCOORD3;\n#ifdef _NORMALMAP\n    float4 tangentWS                : TEXCOORD4;\n#endif\n    float3 viewDirWS                : TEXCOORD5;\n    half4 fogFactorAndVertexLight   : TEXCOORD6;\n    \/\/ x: fogFactor, yzw: vertex light\n#ifdef REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR\n    float4 shadowCoord              : TEXCOORD7;\n#endif\n};\n \n\/\/TEXTURE2D(_BaseMap);\n\/\/SAMPLER(sampler_BaseMap);\n\/\/ Removed, since SurfaceInput.hlsl now defines the _BaseMap for us\n\n\n#if SHADER_LIBRARY_VERSION_MAJOR &lt; 9\n    \/\/ This function was added in URP v9.x.x versions\n    \/\/ If we want to support URP versions before, we need to handle it instead.\n    \/\/ Computes the world space view direction (pointing towards the viewer).\n    float3 GetWorldSpaceViewDir(float3 positionWS) {\n        if (unity_OrthoParams.w == 0) {\n            \/\/ Perspective\n            return _WorldSpaceCameraPos - positionWS;\n        } else {\n            \/\/ Orthographic\n            float4x4 viewMat = GetWorldToViewMatrix();\n            return viewMat[2].xyz;\n        }\n    }\n#endif\n \nVaryings vert(Attributes IN) {\n    Varyings OUT;\n \n    \/\/ Vertex Position\n    VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);\n    OUT.positionCS = positionInputs.positionCS;\n#ifdef REQUIRES_WORLD_SPACE_POS_INTERPOLATOR\n    OUT.positionWS = positionInputs.positionWS;\n#endif\n    \/\/ UVs &amp; Vertex Colour\n    OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);\n    OUT.color = IN.color;\n \n    \/\/ View Direction\n    OUT.viewDirWS = GetWorldSpaceViewDir(positionInputs.positionWS);\n \n    \/\/ Normals &amp; Tangents\n    VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS, IN.tangentOS);\n    OUT.normalWS =  normalInputs.normalWS;\n#ifdef _NORMALMAP\n    real sign = IN.tangentOS.w * GetOddNegativeScale();\n    OUT.tangentWS = half4(normalInputs.tangentWS.xyz, sign);\n#endif\n \n    \/\/ Vertex Lighting &amp; Fog\n    half3 vertexLight = VertexLighting(positionInputs.positionWS, normalInputs.normalWS);\n    half fogFactor = ComputeFogFactor(positionInputs.positionCS.z);\n    OUT.fogFactorAndVertexLight = half4(fogFactor, vertexLight);\n \n    \/\/ Baked Lighting &amp; SH (used for Ambient if there is no baked)\n    OUTPUT_LIGHTMAP_UV(IN.lightmapUV, unity_LightmapST, OUT.lightmapUV);\n    OUTPUT_SH(OUT.normalWS.xyz, OUT.vertexSH);\n \n    \/\/ Shadow Coord\n#ifdef REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR\n    OUT.shadowCoord = GetShadowCoord(positionInputs);\n#endif\n    return OUT;\n}\n\n\nInputData InitializeInputData(Varyings IN, half3 normalTS){\n    InputData inputData = (InputData)0;\n \n#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)\n    inputData.positionWS = IN.positionWS;\n#endif\n                 \n    half3 viewDirWS = SafeNormalize(IN.viewDirWS);\n#ifdef _NORMALMAP\n    float sgn = IN.tangentWS.w; \/\/ should be either +1 or -1\n    float3 bitangent = sgn * cross(IN.normalWS.xyz, IN.tangentWS.xyz);\n    inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(IN.tangentWS.xyz, bitangent.xyz, IN.normalWS.xyz));\n#else\n    inputData.normalWS = IN.normalWS;\n#endif\n \n    inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);\n    inputData.viewDirectionWS = viewDirWS;\n \n#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)\n    inputData.shadowCoord = IN.shadowCoord;\n#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)\n    inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);\n#else\n    inputData.shadowCoord = float4(0, 0, 0, 0);\n#endif\n \n    inputData.fogCoord = IN.fogFactorAndVertexLight.x;\n    inputData.vertexLighting = IN.fogFactorAndVertexLight.yzw;\n    inputData.bakedGI = SAMPLE_GI(IN.lightmapUV, IN.vertexSH, inputData.normalWS);\n    return inputData;\n}\n \nSurfaceData InitializeSurfaceData(Varyings IN){\n    SurfaceData surfaceData = (SurfaceData)0;\n    \/\/ Note, we can just use SurfaceData surfaceData; here and not set it.\n    \/\/ However we then need to ensure all values in the struct are set before returning.\n    \/\/ By casting 0 to SurfaceData, we automatically set all the contents to 0.\n         \n    half4 albedoAlpha = SampleAlbedoAlpha(IN.uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));\n    surfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);\n    surfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb * IN.color.rgb;\n \n    \/\/ Not supporting the metallic\/specular map or occlusion map\n    \/\/ for an example of that see : https:\/\/github.com\/Unity-Technologies\/Graphics\/blob\/master\/com.unity.render-pipelines.universal\/Shaders\/LitInput.hlsl\n \n    surfaceData.smoothness = _Smoothness;\n    surfaceData.normalTS = SampleNormal(IN.uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);\n    surfaceData.emission = SampleEmission(IN.uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));\n    surfaceData.occlusion = 1;\n    return surfaceData;\n}\n \nhalf4 frag(Varyings IN) : SV_Target {\n    SurfaceData surfaceData = InitializeSurfaceData(IN);\n    InputData inputData = InitializeInputData(IN, surfaceData.normalTS);\n                 \n    \/\/ In URP v10+ versions we could use this :\n    \/\/ half4 color = UniversalFragmentPBR(inputData, surfaceData);\n \n    \/\/ But for other versions, we need to use this instead.\n    \/\/ We could also avoid using the SurfaceData struct completely, but it helps to organise things.\n    half4 color = UniversalFragmentPBR(inputData, surfaceData.albedo, surfaceData.metallic, \n      surfaceData.specular, surfaceData.smoothness, surfaceData.occlusion, \n      surfaceData.emission, surfaceData.alpha);\n                 \n    color.rgb = MixFog(color.rgb, inputData.fogCoord);\n \n    \/\/ color.a = OutputAlpha(color.a);\n    \/\/ Not sure if this is important really. It's implemented as :\n    \/\/ saturate(outputAlpha + _DrawObjectPassData.a);\n    \/\/ Where _DrawObjectPassData.a is 1 for opaque objects and 0 for alpha blended.\n    \/\/ But it was added in URP v8, and versions before just didn't have it.\n    \/\/ And I'm writing thing for v7.3.1 currently\n    \/\/ We could still saturate the alpha to ensure it doesn't go outside the 0-1 range though :\n    color.a = saturate(color.a);\n \n    return color;\n}\n\n\n------------------------------------------------------------------\n\n\u9634\u5f71 Pass\n\u6dfb\u52a0 \"LightMode\" = \"ShadowCaster\" \n\n\u4f7f\u7528\u793a\u4f8bShadowCast\nPass {\n    Name \"ShadowCaster\"\n    Tags { \"LightMode\"=\"ShadowCaster\" }\n \n    ZWrite On\n    ZTest LEqual\n \n    HLSLPROGRAM\n    \/\/ Required to compile gles 2.0 with standard srp library\n    #pragma prefer_hlslcc gles\n    #pragma exclude_renderers d3d11_9x gles\n    \/\/#pragma target 4.5\n \n    \/\/ Material Keywords\n    #pragma shader_feature _ALPHATEST_ON\n    #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A\n \n    \/\/ GPU Instancing\n    #pragma multi_compile_instancing\n    #pragma multi_compile _ DOTS_INSTANCING_ON\n             \n    #pragma vertex ShadowPassVertex\n    #pragma fragment ShadowPassFragment\n     \n    #include \"Packages\/com.unity.render-pipelines.core\/ShaderLibrary\/CommonMaterial.hlsl\"\n    #include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/SurfaceInput.hlsl\"\n    #include \"Packages\/com.unity.render-pipelines.universal\/Shaders\/ShadowCasterPass.hlsl\"\n \n    ENDHLSL\n}\n\n\u6216\u8005\u81ea\u5df1\u5199\n\n#pragma vertex vert\n\n\/\/ function copied from ShadowCasterPass and edited slightly.\nVaryings vert(Attributes input) {\n    Varyings output;\n    UNITY_SETUP_INSTANCE_ID(input);\n     \/\/ Example Displacement\n    input.positionOS += float4(0, _SinTime.y, 0, 0);\n     output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);\n    output.positionCS = GetShadowPositionHClip(input);\n    return output;\n}\n\n------------------------------------------------------------------\n\u6df1\u5ea6 Pass \u6dfb\u52a0 \"LightMode\" = \"DepthOnly\"\n\n\u793a\u4f8b\uff1a\n\nPass {\n    Name \"DepthOnly\"\n    Tags { \"LightMode\"=\"DepthOnly\" }\n \n    ZWrite On\n    ColorMask 0\n \n    HLSLPROGRAM\n    \/\/ Required to compile gles 2.0 with standard srp library\n    #pragma prefer_hlslcc gles\n    #pragma exclude_renderers d3d11_9x gles\n    \/\/#pragma target 4.5\n \n    \/\/ Material Keywords\n    #pragma shader_feature _ALPHATEST_ON\n    #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A\n \n    \/\/ GPU Instancing\n    #pragma multi_compile_instancing\n    #pragma multi_compile _ DOTS_INSTANCING_ON\n             \n    #pragma vertex DepthOnlyVertex\n    #pragma fragment DepthOnlyFragment\n             \n    #include \"Packages\/com.unity.render-pipelines.core\/ShaderLibrary\/CommonMaterial.hlsl\"\n    #include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/SurfaceInput.hlsl\"\n    #include \"Packages\/com.unity.render-pipelines.universal\/Shaders\/DepthOnlyPass.hlsl\"\n \n    \/\/ Again, using this means we also need _BaseMap, _BaseColor and _Cutoff shader properties\n    \/\/ Also including them in cbuffer, except _BaseMap as it's a texture.\n \n    ENDHLSL\n}\n\n\n\n------------------------------------------------------------------\n\n\nBuilt-in\u4e2d\nfloat4 lightColor = _LightColor0;\nfloat3 lightDir = WorldSpaceLightDir(worldPos);\nUNITY_LIGHT_ATTENUATION(atten, i, worldPos.xyz);\n\nURP\u4e2d\nLighting.hlsl:\n    GetMainLight();\n        _MainLightPosition; \nInput.hlsl:\n    \/\/Constant Buffers\n    float4 _MainLightPosition; \/\/ \u4e3b\u5149\u65b9\u5411(dir)\n    half4 _MainLightColor; \/\/ \u4e3b\u5149\u989c\u8272\n\n\n\n------------------------------------------------------------------\n\nGI\n\nBuilt-in\u4e2d\nfloat sh = 0;\nfloat3 SH = ShadeSHPerVertex(worldNormal, sh);\n\nURP\u4e2d\nhalf3 SH = SampleSH(normalWS);\nhalf3 SH = SampleSHVertex(normalWS);\nhalf3 SH = SampleSHPixel(L2Term, normalWS);\n\n------------------------------------------------------------------\n\n\n\u5e38\u7528\u9876\u70b9\u51fd\u6570\u7684\u6539\u52a8\nVertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);\nVertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);\nhalf3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;\nhalf3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);\nhalf fogFactor = ComputeFogFactor(vertexInput.positionCS.z);\noutput.fogFactorAndVertexLight = half4(fogFactor, vertexLight);\ninputData.fogCoord = input.fogFactorAndVertexLight.x;\noutput.normal = half4(normalInput.normalWS, viewDirWS.x);\noutput.tangent = half4(normalInput.tangentWS, viewDirWS.y);\noutput.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);\noutput.normalWs = NormalizeNormalPerVertex (normalInput.normalws);\n\n------------------------------------------------------------------\n\n\n\u5e38\u7528\u91c7\u6837\u7684\u6539\u52a8\n\nhalf4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex, uv) ;\n\n\n\n\n------------------------------------------------------------------\n\u603b\u7ed3\n\n\n1\u3001Use the \u201cRenderPipeline\u201d=\u201dUniversalPipeline\u201d tag on the Subshader block\n\nURP uses the following \u201cLightMode\u201d tags :\nUniversalForward\nShadowCaster\nDepthOnly\nMeta\nUniversal2D\n\n\n2\u3001\n\nURP uses a single-pass forward rendered, \nso only the first supported UniversalForward pass will be rendered. \nWhile you can achieve multi-pass shaders with other passes untagged, \nbe aware that it will break batching with the SRP Batcher. \nIt is instead recommended to use separate shaders\/materials, \neither on separate MeshRenderers or use the Render Objects feature on the Forward Renderer\n\n\nThe RenderObjects Forward Renderer feature can be used to re-render \nobjects on a specific layer with an overrideMaterial \n(which is similar to replacement shaders, but the values of properties \nis not retained \u2013 Unless you use a material property block, \nbut that also breaks batching with the SRP Batcher).\n\n You can also override stencil and ztest values on the feature. \n See here for examples of the RenderObjects feature being used \n (Toon \u201cinverted hull\u201d style outlines and object xray\/occlusion effects).\n\nYou can also write Custom Forward Renderer features, \nfor example a Blit feature like the one here (Blit.cs and BlitPass.cs), \ncan be used to achieve custom post processing effects \n(as URP\u2019s post-processing solution currently doesn\u2019t include custom effects).\nhttps:\/\/github.com\/Unity-Technologies\/UniversalRenderingExamples\/tree\/master\/Assets\/Scripts\/Runtime\/RenderPasses\n\nAlways use HLSLPROGRAM (or HLSLINCLUDE) and ENDHLSL, \nnot the CG versions, otherwise there will be conflicts with the URP ShaderLibrary.\n\nInstead of including UnityCG.cginc, use the URP ShaderLibrary. The main one to include is :\n#include \"Packages\/com.unity.render-pipelines.universal\/ShaderLibrary\/Core.hlsl\"\n\n\nYour shaders need to have a UnityPerMaterial CBUFFER to be compatible with the SRP Batcher. \n(A UnityPerDraw is also required but the URP ShaderLibrary handles this).\n This buffer must be the same for every pass in the shader, \n so it\u2019s usually a good idea to put it in a HLSLINCLUDE in the Subshader. \n It should include every exposed Property which will be used in the shader functions, \n except textures which don\u2019t have to be in there\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/cyangamedev.wordpress.com\/2020\/06\/05\/urp-s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2313"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2313"}],"version-history":[{"count":11,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2313\/revisions"}],"predecessor-version":[{"id":2423,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2313\/revisions\/2423"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}