ScrollView中的特效

http://www.ithao123.cn/content-10729390.html


ParticleSystemClipper.cs

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(UIPanel))]
public class ParticleSystemClipper : MonoBehaviour
{
	const string ShaderName = "Bleach/Particles Additive Area Clip";
	const float ClipInterval = 1f;
	
	UIPanel m_targetPanel;
	Shader m_shader;
	
	void Start()
	{
		// find panel
		m_targetPanel = gameObject.GetComponent<UIPanel>();
		m_shader = Shader.Find(ShaderName);
		
		if (!IsInvoking("Clip"))
			InvokeRepeating("Clip", 0, ClipInterval);
	}
	
	Vector4 CalcClipArea()
	{
		Vector4 nguiArea = new Vector4();
		var clipRegion = m_targetPanel.finalClipRegion;
		nguiArea.x = clipRegion.x - clipRegion.z / 2;
		nguiArea.y = clipRegion.y - clipRegion.w / 2;
		nguiArea.z = clipRegion.x + clipRegion.z / 2;
		nguiArea.w = clipRegion.y + clipRegion.w / 2;
		
		UIRoot uiRoot = SanguoHome.instance.gameObject.GetComponent<UIRoot>();
		float h = 2;
		float w = h * Screen.width / Screen.height;
		
		Vector4 clipArea = new Vector4();
		Vector3 pos = m_targetPanel.transform.position - uiRoot.transform.position;
		clipArea.x = pos.x + nguiArea.x * w / uiRoot.manualWidth;
		clipArea.y = pos.y + nguiArea.y * h / uiRoot.manualHeight;
		clipArea.z = pos.x + nguiArea.z * w / uiRoot.manualWidth;
		clipArea.w = pos.y + nguiArea.w * h / uiRoot.manualHeight;
		
		return clipArea;
	}
	
	void Clip()
	{
		Vector4 clipArea = CalcClipArea();
		ParticleSystem[] particleSystems = gameObject.GetComponentsInChildren<ParticleSystem>(true);
		
		for (int i = 0; i < particleSystems.Length; i++)
		{
			ParticleSystem ps = particleSystems[i];
			Material mat = ps.renderer.material;
			
			if (mat.shader.name != ShaderName)
				mat.shader = m_shader;
			
			mat.SetVector("_Area", clipArea);
		}
	}
	void OnDestroy()
	{
		CancelInvoke("Clip");
	}
}

ParticlesAdditiveAreaClip.shader

Shader "Bleach/Particles Additive Area Clip"
{
    Properties
    {
        _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
        _MainTex ("Particle Texture", 2D) = "white" {}
        _Area ("Area", Vector) = (0,0,1,1)
    }

    Category
    {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
        Blend SrcAlpha One
        AlphaTest Greater .01
        ColorMask RGB
        Cull Off
        Lighting Off
        ZWrite Off
        Fog { Color (0,0,0,0) }
        
        SubShader 
        {
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_particles

                #include "UnityCG.cginc"

                sampler2D _MainTex;
                fixed4 _TintColor;
                float4 _Area;
                
                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 worldPos : TEXCOORD1;
                };
                
                float4 _MainTex_ST;

                v2f vert (appdata_t v)
                {
                    v2f o;
                    o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
                    o.color = v.color;
                    o.worldPos = mul(_Object2World, v.vertex).xy;
                    return o;
                }

                fixed4 frag (v2f i) : SV_Target
                {
                    bool inArea = i.worldPos.x >= _Area.x && i.worldPos.x <= _Area.z && i.worldPos.y >= _Area.y && i.worldPos.y <= _Area.w;
                    return inArea? 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord) : fixed4(0,0,0,0);
                }
                ENDCG 
            }
        }    
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nmg10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值