shader之——移動端陰影實現

來源:互聯網
上載者:User

標籤:osi   即時   struct   方式   off   write   int   地形   str   

  移動端陰影的實現有很多種方式,用shader實現個人覺得是比較省的方法。原理比較簡單,

將模型的沿著y方向壓扁,然後按照一個方向把zx做延伸,相當於多渲染一次模型,也多了一個dc

但是比起昂貴的即時陰影,還是相當省的。

陰影相當於一個平面,即使是這樣,也可以適應稍有起伏的地形

 

代碼如下:

Shader "Game-X/PlanarShadow" {    Properties {        _Strength ("Strength", Range(0.1, 10)) = 3        _MainTex ("Base (RGB)", 2D) = "white" {}        _Projplane ("_Projplane", Float) = 0        _Lightdir ("_Lightdir", Vector) = (0, 0.707, 0.707)    }    SubShader {                Tags { "Queue" = "Geometry+1" "RenderType" = "Opaque" }                pass {               Tags { "LightMode" = "ForwardBase" }             Blend One SrcAlpha            ZWrite Off            ZTest Off                                CGPROGRAM            #pragma vertex vert             #pragma fragment frag            #pragma multi_compile _BLEND_ALPHA_ON _BLEND_ALPHA_OFF            #pragma multi_compile _BLEND_ADDITIVE_ON _BLEND_ADDITIVE_OFF            #include "UnityCG.cginc"            #include "Lighting.cginc"            struct vsIn            {                float4 vertex   : POSITION;                #if defined (_BLEND_ALPHA_ON) || defined (_BLEND_ADDITIVE_ON)                float2 texcoord : TEXCOORD0;                #endif            };            struct vsOut            {                float4 wpos        : SV_POSITION;                #if defined (_BLEND_ALPHA_ON) || defined (_BLEND_ADDITIVE_ON)                float2 texcoord : TEXCOORD0;                     #endif             };                        float _Strength;            float _Projplane;            float3 _Lightdir;            sampler2D _MainTex;            float4 _MainTex_ST;                        vsOut vert(vsIn In)            {                vsOut o;                float4 wp = mul(unity_ObjectToWorld, In.vertex);                if (wp.y < _Projplane)                    wp.y = _Projplane;                wp.xz = wp.xz - ((wp.y - _Projplane) / _Lightdir.y) * _Lightdir.xz;                 wp.y = _Projplane;                o.wpos = mul(UNITY_MATRIX_VP, wp);                #if defined (_BLEND_ALPHA_ON) || defined (_BLEND_ADDITIVE_ON)                o.texcoord = TRANSFORM_TEX(In.texcoord, _MainTex);                #endif                return o;            }                         float4 frag(vsOut In) : COLOR             {                float4 ambient = UNITY_LIGHTMODEL_AMBIENT * 2;                float k = (ambient.r + ambient.g + ambient.b) / _Strength;                 #ifdef _BLEND_ALPHA_ON                float a = tex2D(_MainTex, In.texcoord).a;                k = lerp(1, k, a);                if (a <= 0)                    clip(-1);                #endif                #ifdef _BLEND_ADDITIVE_ON                float3 t = tex2D(_MainTex, In.texcoord).rgb;                float q = (t.r + t.g + t.b) / 3;                if (q <= 0)                    clip(-1);                #endif                return float4(0,0,0,k);            }                         ENDCG         }   }}

 

shader之——移動端陰影實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.