2016-10-27 3 views
1

Ich habe einen Unity-Shader für 2D-Sprites glänzend:Unity glänzend Shader-Overlay 2D-Sprite

//Kaan Yamanyar,Levent Seckin 
 
Shader "Sprites/ShinyDefault" 
 
{ 
 
\t Properties 
 
\t { 
 
\t \t [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {} 
 
\t _Color("Tint", Color) = (1,1,1,1) 
 
\t \t _ShineLocation("ShineLocation", Range(0,1)) = 0 
 
\t \t _ShineWidth("ShineWidth", Range(0,1)) = 0 
 
\t \t [MaterialToggle] PixelSnap("Pixel snap", Float) = 0 
 
\t } 
 

 
\t \t SubShader 
 
\t { 
 
\t \t Tags 
 
\t { 
 
\t \t "Queue" = "Transparent" 
 
\t \t "IgnoreProjector" = "True" 
 
\t \t "RenderType" = "Transparent" 
 
\t \t "PreviewType" = "Plane" 
 
\t \t "CanUseSpriteAtlas" = "False" 
 
\t } 
 

 
\t \t Cull Off 
 
\t \t Lighting Off 
 
\t \t ZWrite Off 
 
\t \t Blend One OneMinusSrcAlpha 
 

 
\t \t Pass 
 
\t { 
 
\t \t CGPROGRAM 
 
#pragma vertex vert 
 
#pragma fragment frag 
 
#pragma multi_compile _ PIXELSNAP_ON 
 
#include "UnityCG.cginc" 
 

 
\t \t struct appdata_t 
 
\t { 
 
\t \t float4 vertex : POSITION; 
 
\t \t float4 color : COLOR; 
 
\t \t float2 texcoord : TEXCOORD0; 
 
\t }; 
 

 
\t struct v2f 
 
\t { 
 
\t \t float4 vertex : SV_POSITION; 
 
\t \t fixed4 color : COLOR; 
 
\t \t float2 texcoord : TEXCOORD0; 
 
\t }; 
 

 
\t fixed4 _Color; 
 

 
\t v2f vert(appdata_t IN) 
 
\t { 
 
\t \t v2f OUT; 
 
\t \t OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex); 
 
\t \t OUT.texcoord = IN.texcoord; 
 
\t \t OUT.color = IN.color * _Color; 
 
#ifdef PIXELSNAP_ON 
 
\t \t OUT.vertex = UnityPixelSnap(OUT.vertex); 
 
#endif 
 

 
\t \t return OUT; 
 
\t } 
 

 
\t sampler2D _MainTex; 
 
\t sampler2D _AlphaTex; 
 
\t float _AlphaSplitEnabled; 
 
\t float _ShineLocation; 
 
\t float _ShineWidth; 
 

 
\t fixed4 SampleSpriteTexture(float2 uv) 
 
\t { 
 
\t \t fixed4 color = tex2D(_MainTex, uv); 
 

 
#if UNITY_TEXTURE_ALPHASPLIT_ALLOWED 
 
\t \t if (_AlphaSplitEnabled) 
 
\t \t \t color.a = tex2D(_AlphaTex, uv).r; 
 
#endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED 
 

 

 

 

 
\t \t float lowLevel = _ShineLocation - _ShineWidth; 
 
\t \t float highLevel = _ShineLocation + _ShineWidth; 
 
\t \t float currentDistanceProjection = (uv.x + uv.y)/2; 
 
\t \t if (currentDistanceProjection > lowLevel && currentDistanceProjection < highLevel) { 
 
\t \t \t float whitePower = 1 - (abs(currentDistanceProjection - _ShineLocation)/_ShineWidth); 
 
\t \t \t color.rgb += color.a * whitePower; 
 
\t \t } 
 

 
\t \t return color; 
 
\t } 
 

 
\t fixed4 frag(v2f IN) : SV_Target 
 
\t { 
 
\t \t fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color; 
 
\t c.rgb *= c.a; 
 

 
\t return c; 
 
\t } 
 
\t \t ENDCG 
 
\t } 
 
\t } 
 
}

Aber mit diesem Code die Pixel des 2D-Sprite hinter diesem glänzenden Shader sind. Jetzt möchte ich nur dieses Shader-Overlay-Sprite zeigen und alle Pixel im Sprite werden nicht angezeigt.

Wie kann ich das tun?

Antwort

0

Sie müssen SortingLayer oder für den Renderer festlegen, der den Shader verwendet.

zum Beispiel, gehen Sie auf Bearbeiten - Einstellungen> Projekt -> -Tags und Schichten und eine Schicht namens Deckschicht definieren und es nach der von Sprites verwendet Ebene platzieren und dann:

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