Unity闪亮着色器覆盖2D精灵

时间:2016-10-27 03:45:02

标签: unity3d shader 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 *= c.a;

	return c;
	}
		ENDCG
	}
	}
}
&#13;
&#13;
&#13;

但是使用这段代码,2D精灵的像素就在这个闪亮的着色器后面。现在我只想显示这个着色器覆盖精灵,并且没有显示精灵中的所有像素。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要为使用着色器的渲染器设置SortingLayerOrderInLayer

例如,转到编辑 - &gt;项目设置 - &gt;标签和图层,并定义一个名为覆盖层的图层,并将其放在精灵使用的图层之后,然后:

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