ScrollRect裁剪ParticleSystem粒子(UGUI)

  1. 下载Unity对应版本shader代码(如果链接失效,可以直接去Unity官网找下载),我使用的是unity5.5.1版本
  2. 打开并定位到Particle Add.shader文件,复制一份命名为UIParticleAdditiveClip.shader保存到Asset文件夹中
  3. 修改UIParticleAdditiveClip.shader中的shader名称为“Custom/UI/Particle_Additive_Clip”
  4. 创建一个Material命名为pt,并使用3中创建的shader,选择任意一张图片作为粒子的图片,用pt替换ParticleSystem的默认材质

Shader "Custom/UI/Particle_Additive_Clip" {
Properties {
	_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
	_MainTex ("Particle Texture", 2D) = "white" {}
	_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
}

Category {
	Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
	Blend SrcAlpha One
	ColorMask RGB
	Cull Off Lighting Off ZWrite Off
	
	SubShader {
		Pass {
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_particles
			#pragma multi_compile_fog

			#include "UnityCG.cginc"
			#include "UnityUI.cginc"

			sampler2D _MainTex;
			fixed4 _TintColor;
			
			struct appdata_t {
				float4 vertex : POSITION;
				fixed4 color : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 vertex : SV_POSITION;
				fixed4 color : COLOR;
				float2 texcoord : TEXCOORD0;
				float2 worldPosition : TEXCOORD1;
				UNITY_FOG_COORDS(1)
				#ifdef SOFTPARTICLES_ON
				float4 projPos : TEXCOORD2;
				
				#endif
			};
			
			float4 _MainTex_ST;

			v2f vert (appdata_t v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				#ifdef SOFTPARTICLES_ON
				o.projPos = ComputeScreenPos (o.vertex);
				COMPUTE_EYEDEPTH(o.projPos.z);
				#endif
				o.color = v.color;
				o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
				//world space中的xy坐标保存到o.worldPosition.xy中
				//unity_ObjectToWorld 等同于_Object2World 5.3.8中使用:_Object2World
				o.worldPosition.xy = mul(_Object2World, v.vertex).xy;
				UNITY_TRANSFER_FOG(o,o.vertex);
				return o;
			}

			sampler2D_float _CameraDepthTexture;
			float _InvFade;
			// C#代码需要传入的裁剪区域变量, 这里我们增加一个变量(_UseClipRect)用来标记是否需要裁剪
			float4 _ClipRect;
			float _UseClipRect;
			
			fixed4 frag (v2f i) : SV_Target
			{
				#ifdef SOFTPARTICLES_ON
				float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
				float partZ = i.projPos.z;
				float fade = saturate (_InvFade * (sceneZ-partZ));
				i.color.a *= fade;
				#endif
				
				fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
				UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
				
				float c = UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
				col.a = lerp(col.a, c * col.a, _UseClipRect);
				return col;
			}
			ENDCG 
		}
	}	
}
}

C#脚本:编写C#代码,计算裁剪区域

[RequireComponent(typeof(ParticleSystem))]
public class ParticleMask : MonoBehaviour
{
   public RectMask2D mask;
   public Material mt;
   private void Awake()
   {
       mt = GetComponent<ParticleSystem>().GetComponent<Renderer>().material;
       mask = GetComponentInParent<RectMask2D>();
       // ScrollView位置变化时重新计算裁剪区域
       GetComponentInParent<ScrollRect>().onValueChanged.AddListener((e) => { setClip(); });
       setClip();
   }

   void setClip()
   {
       Vector3[] wc = new Vector3[4];
       mask.GetComponent<RectTransform>().GetWorldCorners(wc);        // 计算world space中的点坐标
       var clipRect = new Vector4(wc[0].x, wc[0].y, wc[2].x, wc[2].y);// 选取左下角和右上角
       mt.SetVector("_ClipRect", clipRect);                           // 设置裁剪区域
       mt.SetFloat("_UseClipRect", 1.0f);                             // 开启裁剪
   }
}

引用:https://www.cnblogs.com/devgl/p/6598781.html

猜你喜欢

转载自my.oschina.net/u/698044/blog/1818030