簡體   English   中英

Unity閃亮着色器覆蓋2D精靈

[英]Unity shiny shader overlay 2D sprite

我有一個Unity着色器可以為2D精靈閃亮:

 //Kaan Yamanyar,Levent Seckin Shader "Sprites/ShinyDefault" { Properties { [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {} _Color("Tint", Color) = (1,1,1,1) _ShineLocation("ShineLocation", Range(0,1)) = 0 _ShineWidth("ShineWidth", Range(0,1)) = 0 [MaterialToggle] PixelSnap("Pixel snap", Float) = 0 } SubShader { Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "False" } Cull Off Lighting Off ZWrite Off Blend One OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile _ PIXELSNAP_ON #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; }; fixed4 _Color; v2f vert(appdata_t IN) { v2f OUT; OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); OUT.texcoord = IN.texcoord; OUT.color = IN.color * _Color; #ifdef PIXELSNAP_ON OUT.vertex = UnityPixelSnap(OUT.vertex); #endif return OUT; } sampler2D _MainTex; sampler2D _AlphaTex; float _AlphaSplitEnabled; float _ShineLocation; float _ShineWidth; fixed4 SampleSpriteTexture(float2 uv) { fixed4 color = tex2D(_MainTex, uv); #if UNITY_TEXTURE_ALPHASPLIT_ALLOWED if (_AlphaSplitEnabled) color.a = tex2D(_AlphaTex, uv).r; #endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED float lowLevel = _ShineLocation - _ShineWidth; float highLevel = _ShineLocation + _ShineWidth; float currentDistanceProjection = (uv.x + uv.y) / 2; if (currentDistanceProjection > lowLevel && currentDistanceProjection < highLevel) { float whitePower = 1 - (abs(currentDistanceProjection - _ShineLocation) / _ShineWidth); color.rgb += color.a * whitePower; } return color; } fixed4 frag(v2f IN) : SV_Target { fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color; c.rgb *= ca; return c; } ENDCG } } } 

但是使用這段代碼,2D精靈的像素就在這個閃亮的着色器后面。 現在我想只顯示這個着色器覆蓋精靈,並且沒有顯示精靈中的所有像素。

我怎樣才能做到這一點?

您需要為使用着色器的渲染器設置SortingLayerOrderInLayer

例如,轉到編輯 - >項目設置 - >標簽和圖層,並定義一個名為覆蓋層的圖層,並將其放在精靈使用的圖層之后,然后:

[shaderedObject].GetComponent<Renderer>().sortingLayerName = "overlay layer";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM