{"id":2631,"date":"2022-01-21T15:06:00","date_gmt":"2022-01-21T07:06:00","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=2631"},"modified":"2022-01-23T13:48:26","modified_gmt":"2022-01-23T05:48:26","slug":"unityshader%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=2631","title":{"rendered":"UnityShader\u5185\u7f6e\u51fd\u6570"},"content":{"rendered":"\n<h1>UnityCG.cginc \u5e38\u7528\u6570\u636e\u7ed3\u6784\u4f53<\/h1>\n\n\n\n<h2>\u8f93\u5165<\/h2>\n\n\n\n<h3>appdata_base<\/h3>\n\n\n\n<p>\u9876\u70b9\u7740\u8272\u5668\u8f93\u5165\uff0c\u5305\u542b\u4f4d\u7f6e\u3001\u6cd5\u7ebf\u548c\u4e00\u4e2a\u7eb9\u7406\u5750\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct appdata_base {\n    float4 vertex : POSITION;\n    float3 normal : NORMAL;\n    float4 texcoord : TEXCOORD0;\n    UNITY_VERTEX_INPUT_INSTANCE_ID\n};<\/code><\/pre>\n\n\n\n<h3>appdata_tan<\/h3>\n\n\n\n<p>\u9876\u70b9\u7740\u8272\u5668\u8f93\u5165\uff0c\u5305\u542b\u4f4d\u7f6e\u3001\u6cd5\u7ebf\u3001\u5207\u7ebf\u548c\u4e00\u4e2a\u7eb9\u7406\u5750\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct appdata_tan {\n    float4 vertex : POSITION;\n    float4 tangent : TANGENT;\n    float3 normal : NORMAL;\n    float4 texcoord : TEXCOORD0;\n    UNITY_VERTEX_INPUT_INSTANCE_ID\n};<\/code><\/pre>\n\n\n\n<h3>appdata_full<\/h3>\n\n\n\n<p>\u9876\u70b9\u7740\u8272\u5668\u8f93\u5165\uff0c\u5305\u542b\u4f4d\u7f6e\u3001\u6cd5\u7ebf\u3001\u5207\u7ebf\u3001\u9876\u70b9\u989c\u8272\u548c\u4e24\u4e2a\u7eb9\u7406\u5750\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct appdata_full {\n    float4 vertex : POSITION;\n    float4 tangent : TANGENT;\n    float3 normal : NORMAL;\n    float4 texcoord : TEXCOORD0;\n    float4 texcoord1 : TEXCOORD1;\n    float4 texcoord2 : TEXCOORD2;\n    float4 texcoord3 : TEXCOORD3;\n    fixed4 color : COLOR;\n    UNITY_VERTEX_INPUT_INSTANCE_ID\n};<\/code><\/pre>\n\n\n\n<h3>appdata_img<\/h3>\n\n\n\n<p>\u9876\u70b9\u7740\u8272\u5668\u8f93\u5165\uff0c\u5305\u542b\u4f4d\u7f6e\u548c\u4e00\u4e2a\u7eb9\u7406\u5750\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct appdata_img\n{\n    float4 vertex : POSITION;\n    half2 texcoord : TEXCOORD0;\n    UNITY_VERTEX_INPUT_INSTANCE_ID\n};<\/code><\/pre>\n\n\n\n<h2>\u8f93\u51fa<\/h2>\n\n\n\n<h3>v2f_img<\/h3>\n\n\n\n<p>\u53ef\u7528\u4e8e\u9876\u70b9\u7740\u8272\u5668\u7684\u8f93\u51fa\uff0c\u5305\u542b\u88c1\u526a\u7a7a\u95f4\u4e2d\u7684\u4f4d\u7f6e\uff0c\u7eb9\u7406\u5750\u6807<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct v2f_img\n{\n    float4 pos : SV_POSITION;\n    half2 uv : TEXCOORD0;\n    UNITY_VERTEX_INPUT_INSTANCE_ID\n    UNITY_VERTEX_OUTPUT_STEREO\n};<\/code><\/pre>\n\n\n\n<h1>UnityCG.cginc \u5e38\u7528\u51fd\u6570<\/h1>\n\n\n\n<h2>\u6444\u50cf\u673a\u65b9\u5411\uff08\u89c6\u89d2\u65b9\u5411\uff09<\/h2>\n\n\n\n<h3>float3 WorldSpaceViewDir(float4 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u6a21\u578b\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e16\u754c\u7a7a\u95f4\u4e2d\u4ece\u8be5\u70b9\u5230\u6444\u50cf\u673a\u7684\u89c2\u5bdf\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes world space view direction, from object space position\n\/\/ *Legacy* Please use UnityWorldSpaceViewDir instead\ninline float3 WorldSpaceViewDir( in float4 localPos )\n{\n    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;\n    return UnityWorldSpaceViewDir(worldPos);\n}<\/code><\/pre>\n\n\n\n<h3>float3 UnityWorldSpaceViewDir(float4 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u4e16\u754c\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e16\u754c\u7a7a\u95f4\u4e2d\u4ece\u8be5\u70b9\u5230\u6444\u50cf\u673a\u7684\u89c2\u5bdf\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes world space view direction, from object space position\ninline float3 UnityWorldSpaceViewDir( in float3 worldPos )\n{\n    return _WorldSpaceCameraPos.xyz - worldPos;\n}<\/code><\/pre>\n\n\n\n<h3>float3 ObjSpaceViewDir(float4 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u6a21\u578b\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u6a21\u578b\u7a7a\u95f4\u4e2d\u4ece\u8be5\u5e97\u5230\u6444\u50cf\u673a\u7684\u89c2\u5bdf\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes object space view direction\ninline float3 ObjSpaceViewDir( in float4 v )\n{\n    float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;\n    return objSpaceCameraPos - v.xyz;\n}<\/code><\/pre>\n\n\n\n<h2>\u5149\u6e90\u65b9\u5411<\/h2>\n\n\n\n<p>\u4ec5\u5f53\u4f7f\u7528\u524d\u5411\u6e32\u67d3\uff08ForwardBase \u6216 ForwardAdd \u901a\u9053\u7c7b\u578b\uff09\u65f6\uff0c\u8fd9\u4e9b\u51fd\u6570\u624d\u6709\u7528<\/p>\n\n\n\n<h3>float3 WorldSpaceLightDir(float4 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u6a21\u578b\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e16\u754c\u7a7a\u95f4\u4e2d\u4ece\u8fd9\u4e2a\u70b9\u5230\u5e73\u884c\u5149\u6e90\u7684\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes world space light direction, from object space position\n\/\/ *Legacy* Please use UnityWorldSpaceLightDir instead\ninline float3 WorldSpaceLightDir( in float4 localPos )\n{\n    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;\n    return UnityWorldSpaceLightDir(worldPos);\n}<\/code><\/pre>\n\n\n\n<h3>float3 UnityWorldSpaceLightDir(float3 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u4e16\u754c\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u4e16\u754c\u7a7a\u95f4\u4e2d\u4ece\u8fd9\u4e2a\u70b9\u5230\u5e73\u884c\u5149\u6e90\u7684\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes world space light direction, from world space position\ninline float3 UnityWorldSpaceLightDir( in float3 worldPos )\n{\n    #ifndef USING_LIGHT_MULTI_COMPILE\n        return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;\n    #else\n        #ifndef USING_DIRECTIONAL_LIGHT\n        return _WorldSpaceLightPos0.xyz - worldPos;\n        #else\n        return _WorldSpaceLightPos0.xyz;\n        #endif\n    #endif\n}<\/code><\/pre>\n\n\n\n<h3>float3 ObjSpaceLightDir(float4 v)<\/h3>\n\n\n\n<p>\u8f93\u5165\u4e00\u4e2a\u6a21\u578b\u7a7a\u95f4\u4e2d\u7684\u9876\u70b9\u4f4d\u7f6e\uff0c\u8fd4\u56de\u6a21\u578b\u7a7a\u95f4\u4e2d\u4ece\u8fd9\u4e2a\u70b9\u5230\u5e73\u884c\u5149\u6e90\u7684\u65b9\u5411<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Computes object space light direction\ninline float3 ObjSpaceLightDir( in float4 v )\n{\n    float3 objSpaceLightPos = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;\n    #ifndef USING_LIGHT_MULTI_COMPILE\n        return objSpaceLightPos.xyz - v.xyz * _WorldSpaceLightPos0.w;\n    #else\n        #ifndef USING_DIRECTIONAL_LIGHT\n        return objSpaceLightPos.xyz - v.xyz;\n        #else\n        return objSpaceLightPos.xyz;\n        #endif\n    #endif\n}<\/code><\/pre>\n\n\n\n<h2>\u65b9\u5411\u8f6c\u6362<\/h2>\n\n\n\n<h3>float3 UnityObjectToWorldNormal(float3 norm)<\/h3>\n\n\n\n<p>\u628a\u6cd5\u7ebf\u65b9\u5411\uff0c\u4ece\u6a21\u578b\u7a7a\u95f4\u8f6c\u4e3a\u4e16\u754c\u7a7a\u95f4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Transforms normal from object to world space\ninline float3 UnityObjectToWorldNormal( in float3 norm )\n{\n#ifdef UNITY_ASSUME_UNIFORM_SCALING\n    return UnityObjectToWorldDir(norm);\n#else\n    \/\/ 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)}\n    return normalize(mul(norm, (float3x3)unity_WorldToObject));\n#endif\n}<\/code><\/pre>\n\n\n\n<h3>float3 UnityObjectToWorldDir(float3 dir)<\/h3>\n\n\n\n<p>\u628a\u65b9\u5411\u4ece\u6a21\u578b\u7a7a\u95f4\uff0c\u8f6c\u4e3a\u4e16\u754c\u7a7a\u95f4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Transforms direction from object to world space\ninline float3 UnityObjectToWorldDir( in float3 dir )\n{\n    return normalize(mul((float3x3)unity_ObjectToWorld, dir));\n}<\/code><\/pre>\n\n\n\n<h3>float3 UnityWorldToObjectDir(float3 dir)<\/h3>\n\n\n\n<p>\u628a\u65b9\u5411\u4ece\u4e16\u754c\u7a7a\u95f4\uff0c\u8f6c\u4e3a\u6a21\u578b\u7a7a\u95f4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Transforms direction from world to object space\ninline float3 UnityWorldToObjectDir( in float3 dir )\n{\n    return normalize(mul((float3x3)unity_WorldToObject, dir));\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UnityCG.cginc \u5e38\u7528\u6570\u636e\u7ed3\u6784\u4f53 \u8f93\u5165 appdata_base \u9876\u70b9 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=2631\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[419],"tags":[],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2631"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2631"}],"version-history":[{"count":7,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2631\/revisions"}],"predecessor-version":[{"id":2654,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/2631\/revisions\/2654"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2631"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}