2018년 6월 21일 목요일

NGUI Particle Clipping

중국 애들이 만든걸 주워옴

Shader "ParticlesAdditiveAreaClip"
{
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 = UnityObjectToClipPos(v.vertex);
                    o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
                    o.color = v.color;
                    o.worldPos = mul(unity_ObjectToWorld, 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
            }
        }
    }

}

using System;
using UnityEngine;

[RequireComponent(typeof(UIPanel))]
public class CustomUIClipper : MonoBehaviour
{
    private const string ShaderName = "ParticlesAdditiveAreaClip";
    private const float ClipInterval = 0.5f;

    private UIPanel m_targetPanel;
    private Shader m_shader;

    private void Start()
    {
        // find panel
        m_targetPanel = GetComponent<uipanel>();

        if (m_targetPanel == null)
            throw new ArgumentNullException("Cann't find the right UIPanel");
        if (m_targetPanel.clipping != UIDrawCall.Clipping.SoftClip)
            throw new InvalidOperationException("Don't need to clip");

        m_shader = Shader.Find(ShaderName);

        //if (!IsInvoking("Clip"))
        //    InvokeRepeating("Clip", 0, ClipInterval);

        Clip();
    }

    private Vector4 CalcClipArea()
    {
        var clipRegion = m_targetPanel.finalClipRegion;
        var nguiArea = new Vector4()
        {
            x = clipRegion.x - clipRegion.z / 2,
            y = clipRegion.y - clipRegion.w / 2,
            z = clipRegion.x + clipRegion.z / 2,
            w = clipRegion.y + clipRegion.w / 2
        };

        var uiRoot = m_targetPanel.root;
        var pos = m_targetPanel.transform.position;
        var rate1 = (float)Screen.width / (float)Screen.height;
        var rate2 = (float)uiRoot.manualWidth / (float)uiRoot.manualHeight;
        const float h = 2f;
        var w = h * rate1;
        var tempH = h / uiRoot.manualHeight;
        var tempW = w / uiRoot.manualWidth;
        var tempRate = Mathf.Max(tempW, tempH);
        if (rate1 < rate2)
        {
            tempRate = Mathf.Min(tempW, tempH);
        }

        var result =  new Vector4()
        {
            x = pos.x + nguiArea.x * tempRate,
            y = pos.y + nguiArea.y * tempRate,
            z = pos.x + nguiArea.z * tempRate,
            w = pos.y + nguiArea.w * tempRate
        };

        return result;
    }

    private void Clip()
    {
        var clipArea = CalcClipArea();
        var renderers = GetComponentsInChildren<renderer>();
        for (var i = 0; i < renderers.Length; ++i)
        {
            var mat = renderers[i].material;

            if (mat.shader.name != ShaderName)
                mat.shader = m_shader;

            mat.SetVector("_Area", clipArea);
        }
    }
}
네이버 블로그에서 좋은걸 발견함 http://naver.me/5ink6yfx

댓글 3개:

  1. 답글
    1. < 기호가 HTML에서 저 문자로 바뀌나 보네요. 찾아주셔서 감사합니다

      삭제
  2. 작성자가 댓글을 삭제했습니다.

    답글삭제