{"id":981,"date":"2023-12-26T11:12:53","date_gmt":"2023-12-26T03:12:53","guid":{"rendered":"http:\/\/meecarlo.com\/?p=981"},"modified":"2023-12-26T11:13:27","modified_gmt":"2023-12-26T03:13:27","slug":"%e5%8a%a8%e6%80%81%e5%b9%b3%e9%9d%a2%e9%98%b4%e5%bd%b1","status":"publish","type":"post","link":"https:\/\/meecarlo.com\/old\/2023\/12\/26\/%e5%8a%a8%e6%80%81%e5%b9%b3%e9%9d%a2%e9%98%b4%e5%bd%b1\/","title":{"rendered":"\u52a8\u6001\u5e73\u9762\u9634\u5f71"},"content":{"rendered":"\n<figure class=\"wp-block-video\"><video height=\"852\" style=\"aspect-ratio: 496 \/ 852;\" width=\"496\" controls src=\"http:\/\/meecarlo.com\/wp-content\/uploads\/2023\/12\/PlannerShadow.mp4\"><\/video><\/figure>\n\n\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nShader &quot;fake_shadow&quot;\n{\n    Properties\n    {\n        \/\/\u6750\u8d28\u5c5e\u6027\u9762\u677f\n        _MainTex (&quot;\u4e3b\u8d34\u56fe&quot;,2D) = &quot;white&quot;{}\n\n        _GroundY (&quot;\u5730\u9762Y\u9ad8\u5ea6 (\u5916\u90e8\u4f20\u5165)&quot;,float) = 0\n        _Shadow_Color(&quot;\u5f71\u5b50\u989c\u8272&quot;,Color) = (1,1,1,1)\n        _Shadow_Length(&quot;\u5f71\u5b50\u957f\u5ea6&quot;,float) = 0\n        _Shadow_Rotated(&quot;\u5f71\u5b50\u65cb\u8f6c\u89d2\u5ea6&quot;,float) = 0\n    }\n\n    SubShader\n    {\n        Tags\n        {\n            &quot;Queue&quot; = &quot;Transparent+5&quot; \/*&quot;RenderType&quot;=&quot;Transparence&quot;  &quot;RenderPipeline&quot; = &quot;UniversalPipeline&quot; *\/\/\/\u6ce8\u610f\u8fd9\u91cc\u5f88\u91cd\u8981,\u56e0\u4e3a\u5f71\u5b50\u662f\u8981\u7ed8\u5236\u5728\u5730\u9762\u4e0a,\u6240\u4ee5\u5730\u9762\u5fc5\u987b\u5e94\u8be5\u5148\u7ed8\u5236,\u5426\u5219blend\u6df7\u5408\u7684\u65f6\u5019\u5c31\u662f\u548c\u80cc\u540e\u7684skybox\u8fdb\u884c\u6df7\u5408\u4e86\n        }\n\n        pass\n        {\n            Stencil{\n\n                Ref 1\n                \/\/Comp\u53d6\u503c\u4f9d\u6b21\u4e3a  0:Disabled  1:Never  2:Less  3:Equal  4:LessEqual  5:Greater  6:NotEqual  7:GreaterEqual  8:Always\n                Comp NotEqual \/\/\u6216\u8005\u6539\u6210NotEqual\n                \/\/Pass\u53d6\u503c\u4f9d\u6b21\u4e3a  0:Keep  1:Zero  2:Replace  3:IncrementSaturate  4:DecrementSaturate  5:Invert  6:IncrementWrap  7:DecrementWrap\n                Pass Replace\n            }\n\n            Blend SrcAlpha oneMinusSrcAlpha   \n\n            \/\/\u56e0\u4e3a\u548c\u5730\u9762\u91cd\u53e0\u6240\u4ee5\u505a\u4e2a\u504f\u79fb\n            \/\/\u4e5f\u53ef\u4ee5\u4e0d\u505a\u504f\u79fb,\u5c06\u4f20\u5165\u7684\u5730\u9762\u9ad8\u5ea6\u62ac\u9ad8\u4e00\u70b9\u5373\u53ef\n            Offset -2,-2\n\n            CGPROGRAM\n\n            #pragma vertex vert\n            #pragma fragment frag\n            #include &quot;UnityCG.cginc&quot;\n\n            struct appdata\n            {\n                float4 vertex : POSITION;\n            };\n\n            struct v2f\n            {\n                float4 pos : SV_POSITION;\n                \/\/\u8fd9\u91ccworldPos\u4e00\u5b9a\u662ffloat4,\u56e0\u4e3avert()\u4e2d\u5b9e\u9645\u662f\u624b\u52a8\u4e24\u6b21\u7a7a\u95f4\u53d8\u6362\u5982\u679c\u662ffloat3\u4f1a\u5bfc\u81f4w\u5206\u91cf\u4e22\u5931,\u900f\u89c6\u9664\u6cd5\u4f1a\u51fa\u9519\n                \/\/\u5982\u679c\u4e0d\u53c2\u4e0e\u53d8\u6362,\u53ea\u662f\u4f20\u5230frag()\u4e2d\u4f7f\u7528\u7684\u8bdd,\u6bd4\u5982\u8fdb\u884cBlinn-Phong\u5149\u7167\u8ba1\u7b97V\u5411\u91cf\u90a3\u4e48float3\u5c31\u591f\u4e86\n                float4 worldPos : TEXCOORD0;\n                \/\/\u505a\u9634\u5f71\u63d2\u503c\u548cClip\u5730\u9762\u4ee5\u4e0b\u9634\u5f71\u7528\n                float cacheWorldY : TEXCOORD1;\n                float worldPosY : TEXCOORD2;\n};\n \/\/           CBUFFER_START\nCBUFFER_START(UnityPerMaterial)\n            half _GroundY;\n            half4 _Shadow_Color;   \n            half _Shadow_Length;     \n            half _Shadow_Rotated;\nCBUFFER_END\n\/\/            CBUFFER_END\n            \n            v2f vert(appdata v)\n            {\n                v2f o = (v2f)0;\n\n                \/\/\u83b7\u53d6\u4e16\u754c\u7a7a\u95f4\u7684\u4f4d\u7f6e\n                o.worldPos = mul(unity_ObjectToWorld,v.vertex);\n                \/\/\u7f13\u5b58\u4e16\u754c\u7a7a\u95f4\u4e0b\u7684y\u5206\u91cf,\u540e\u7eed\u4e24\u70b9\u4f5c\u7528\n                \/\/\u7b2c\u4e00\u70b9 : \u505a\u63d2\u503c\u7528\u505a\u8ba1\u7b97xz\u7684\u504f\u79fb\u91cf\u7684\u591a\u5c11\n                \/\/\u7b2c\u4e8c\u70b9 : \u9632\u6b62\u5728\u5730\u9762\u4ee5\u4e0b\n                o.cacheWorldY = o.worldPos.y;\n    \n                o.worldPosY = UnityObjectToWorldNormal(v.vertex).y;\n\n                \/\/\u8bbe\u7f6e\u4e16\u754c\u7a7a\u95f4\u4e0by\u7684\u503c\u5168\u90e8\u90fd\u8bbe\u7f6e\u4e3a\u4f20\u5165\u7684\u5730\u9762\u9ad8\u5ea6\u503c\n                o.worldPos.y = _GroundY;\n\n                \/\/\u6839\u636e\u4e16\u754c\u7a7a\u95f4\u4e0b\u6a21\u578by\u503c\u51cf\u53bb\u4f20\u5165\u7684\u5730\u9762\u9ad8\u5ea6\u503c_GroundY\n                \/\/\u4ee5\u8fd9\u4e2a\u503c\u4e3a\u4f20\u5165 lerp(0,_Shadow_Length) \u8fdb\u884c\u7ebf\u6027\u63d2\u503c\n                \/\/\u6700\u540e\u83b7\u53d6\u5230\u6a21\u578by\u503c\u7531\u4f4e\u5230\u9ad8\u7684\u63d2\u503clerpVal\n                \/\/\u8fd9\u4e2amax()\u51fd\u6570 \u5047\u8bbe\u817f\u90e8\u5728\u5730\u9762\u4ee5\u4e0b\u5219\u88c1\u5207\u6389\u817f\u90e8\u9634\u5f71,\u540e\u7eed\u4f7f\u7528clip\u540e\u65e0\u9700Max\n                \/\/half lerpVal = lerp(0,_Shadow_Length,max(0,o.cacheWorldY-_GroundY));\n                half lerpVal = lerp(0,_Shadow_Length,o.cacheWorldY-_GroundY);\n                \n\n                \/\/\u5e38\u91cfPI\n                \/\/const float PI = 3.14159265;\n                \/\/\u89d2\u5ea6\u8f6c\u6362\u6210\u5f27\u5ea6\n                half radian = _Shadow_Rotated \/ 180.0 * UNITY_PI;\n\n                \/\/\u65cb\u8f6c\u77e9\u9635,\u5bf9(0,1)\u5411\u91cf\u8fdb\u884c\u65cb\u8f6c,\u8ba1\u7b97\u65cb\u8f6c\u540e\u7684\u5411\u91cf,\u8be5\u5411\u91cf\u5c31\u662f\u9634\u5f71\u65b9\u5411\n                \/\/2D\u65cb\u8f6c\u77e9\u9635\u5982\u4e0b\n                \/\/ [x]        [ cos\u03b8 , -sin\u03b8 ]\n                \/\/ [ ]  \u4e58\u4ee5  \n                \/\/ [y]        [ sin\u03b8 , cos\u03b8  ]\n                \/\/ x' = xcos\u03b8 - ysin\u03b8\n                \/\/ y' = xsin\u03b8 + ycos\u03b8\n                half2 ratatedAngle = half2((0*cos(radian)-1*sin(radian)),(0*sin(radian)+1*cos(radian)));\n                \n                \/\/\u7528\u4ee5y\u8f74\u9ad8\u5ea6\u4e3a\u53c2\u8003\u8ba1\u7b97\u7684\u63d2\u503c lerpVal \u53bb \u4e58\u4ee5\u4e00\u4e2a\u65cb\u8f6c\u540e\u7684\u65b9\u5411\u5411\u91cf,\u4f5c\u4e3a\u9634\u5f71\u7684\u65b9\u5411\n                \/\/\u6700\u7ec8\u5f97\u5230\u504f\u79fb\u540e\u7684\u9634\u5f71\u4f4d\u7f6e\n                o.worldPos.xz += lerpVal * ratatedAngle;\n                \n                \/\/\u53d8\u6362\u5230\u88c1\u526a\u7a7a\u95f4\n                o.pos = mul(UNITY_MATRIX_VP,o.worldPos);\n\n                return o;\n            }\n\n            fixed4 frag(v2f i) : SV_TARGET\n            {\n                \/\/\u5254\u9664\u4f4e\u4e8e\u5730\u9762\u90e8\u5206\u7684\u7247\u6bb5\n                clip(i.cacheWorldY - _GroundY);\n                \/\/\u7528\u4f5c\u9634\u5f71\u7684Pass\u76f4\u63a5\u8f93\u51fa\u989c\u8272\u5373\u53ef\n                return _Shadow_Color;\n            }\n\n            ENDCG\n        }\n    }\n}\n<\/pre>\n\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing UnityEngine;\n\npublic class SetShadowRotationAndHeight : MonoBehaviour\n{\n\n    public int materialIndex = 2; \/\/ \u8bbe\u7f6e\u6750\u8d28\u7d22\u5f15\n\n    [SerializeField]\n    private Transform rootTransform = null;          \/\/\u521b\u5efa\u6a21\u578b\u65f6\u8bbe\u7f6e\n\n    private Renderer rend = null;\n    private bool isFindRend = false;\n    private float extend = 30f;\n\n\n    private Transform lightTrans=null;\n\n    void Awake()\n    {\n        if (GetComponent&lt;MeshFilter&gt;() != null)\n        {\n            Mesh mesh = GetComponent&lt;MeshFilter&gt;().mesh;\n            Bounds bd = mesh.bounds;\n            bd.Expand(extend);\n            mesh.bounds = bd;\n        }\n        else if(GetComponent&lt;SkinnedMeshRenderer&gt;() != null)\n        {\n            \/\/SkinnedMeshRenderer skinnedMesh = GetComponent&lt;SkinnedMeshRenderer&gt;();\n            \/\/Bounds bd = skinnedMesh.bounds;\n            \/\/bd.Expand(extend);\n            \/\/skinnedMesh.bounds = bd;\n        }\n    }\n\n    public void SetRootTransFrom(Transform root)\n    {\n        rootTransform = root;\n\n        \/\/GameObject mainLightObj = GameObject.Find(&quot;MainLight_Day&quot;);\n        \/\/if(mainLightObj==null)\n        \/\/{\n        \/\/    mainLightObj = GameObject.Find(&quot;FillLight_Day&quot;);\n        \/\/}\n        \/\/if (mainLightObj != null)\n        \/\/    lightTrans = mainLightObj.transform;\n    }\n\n    public Renderer GetTransformRenderer()\n    {\n        if (rend != null)\n            return rend;\n        if (isFindRend)\n            return null;\n        rend= GetComponent&lt;Renderer&gt;();\n        isFindRend = true;\n        return rend;\n    }\n\n    bool isDebug = false;\n\n    void Update()\n    {\n        \/\/if (lightTrans == null)\n        \/\/{\n        \/\/    if(!isDebug)\n        \/\/    {\n        \/\/        Debug.Log(&quot;\u627e\u4e0d\u5230\u5149\u6e90&quot;);\n        \/\/        isDebug = true;\n        \/\/    }\n        \/\/    return;\n        \/\/}\n            \n\n        if (rootTransform == null)\n        {\n            rootTransform = transform;\n        }\n        Renderer tempRend = GetTransformRenderer();\n\n        if (tempRend == null)\n            return;\n\n        \/\/ \u786e\u4fdd\u6307\u5b9a\u7684\u6750\u8d28\u7d22\u5f15\u6709\u6548\n        if (materialIndex &lt; 0 || materialIndex &gt;= tempRend.materials.Length)\n        {\n            Debug.LogError(transform.name + &quot;Invalid material index!&quot;);\n            return;\n        }\n\n        Material mat = tempRend.materials[materialIndex]; \/\/ \u83b7\u53d6\u6307\u5b9a\u7d22\u5f15\u7684\u6750\u8d28\n\n        \/\/ \u83b7\u53d6\u7236\u8282\u70b9\u7684 Y \u5750\u6807\n        float parentY = rootTransform.position.y + 0.2f;\n\n        \/\/ \u5c06\u7236\u8282\u70b9\u7684 Y \u5750\u6807\u4f20\u9012\u7ed9\u6307\u5b9a\u7d22\u5f15\u7684\u6750\u8d28\u7684 _GroundY \u5c5e\u6027\n        mat.SetFloat(&quot;_GroundY&quot;, parentY);\n\n        \/\/ \u83b7\u53d6\u4e3b\u5149\u6e90\u7684\u65b9\u5411\n\n        if (RenderSettings.sun == null)\n            return;\n\n        Vector3 lightDirection = -RenderSettings.sun.transform.forward;\n        \/\/Vector3 lightDirection = -lightTrans.forward;\n\n        \/\/ \u8ba1\u7b97\u4e3b\u5149\u6e90\u7684\u65cb\u8f6c\u89d2\u5ea6\u7684 Y \u503c\uff08\u4f7f\u7528 Atan2 \u51fd\u6570\uff09\n        float shadowRotationY = Mathf.Atan2(lightDirection.x, lightDirection.z) * Mathf.Rad2Deg;\n\n        \/\/ \u5c06\u4e3b\u5149\u6e90\u7684\u65cb\u8f6c\u89d2\u5ea6\u7684 Y \u503c\u4f20\u9012\u7ed9\u6307\u5b9a\u7d22\u5f15\u7684\u6750\u8d28\u7684 _Shadow_Rotated \u5c5e\u6027\n        mat.SetFloat(&quot;_Shadow_Rotated&quot;, shadowRotationY);\n    }\n}\n\n<\/pre>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":942,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-981","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/981","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=981"}],"version-history":[{"count":1,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":983,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/posts\/981\/revisions\/983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/media\/942"}],"wp:attachment":[{"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meecarlo.com\/old\/wp-json\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}