Unity NGUI UIPanel for particle clipping

Source: Internet
Author: User
Tags mul

Ext.: http://www.cnblogs.com/jietian331/p/5075487.html

When using unity for game development, it is sometimes necessary to use particles under scroll view, but particles are not clipped by uipanel, and I provide script particlesystemclipper to handle this situation. The idea is that the finalclipregion of the UIPanel is converted into a region in the unity3d world coordinate system and then passed to the shader of the particle to be cropped. The code is as follows:

usingSystem;usingUnityengine; [Requirecomponent (typeof(UIPanel))] Public classparticlesystemclipper:monobehaviour{Const stringShadername ="bleach/particles Additive Area Clip"; Const floatClipinterval =0.5f;    UIPanel M_targetpanel;    Shader M_shader; voidStart () {//Find PanelM_targetpanel = getcomponent<uipanel>(); if(M_targetpanel = =NULL)            Throw NewArgumentNullException ("Cann ' t find the right UIPanel"); if(M_targetpanel.clipping! =UIDrawCall.Clipping.SoftClip)Throw NewInvalidOperationException ("Don ' t need to clip"); M_shader=Shader.find (shadername); if(! Isinvoking ("Clip")) invokerepeating ("Clip",0, Clipinterval); } Vector4 Calccliparea () {varClipregion =m_targetpanel.finalclipregion; Vector4 Nguiarea=NewVector4 () {x= CLIPREGION.X-CLIPREGION.Z/2, y= CLIPREGION.Y-CLIPREGION.W/2, Z= clipregion.x + clipregion.z/2, W= CLIPREGION.Y + CLIPREGION.W/2        }; varUiroot = luautils.uiroot.getcomponent<uiroot>(); varpos = m_targetPanel.transform.position-uiRoot.transform.position; floath =2; floattemp = h/Uiroot.manualheight; return NewVector4 () {x= pos.x + nguiarea.x *temp, y= Pos.y + nguiarea.y *temp, Z= pos.x + nguiarea.z *Temp, W= Pos.y + NGUIAREA.W *temp}; }    voidClip () {Vector4 Cliparea=Calccliparea (); varParticlesystems = This. Getcomponentsinchildren<particlesystem>();  for(inti =0; i < particlesystems.length; i++)        {            varPS =Particlesystems[i]; varMat =ps.renderer.material; if(Mat.shader.name! =shadername) Mat.shader=M_shader; Mat. Setvector ("_area", Cliparea); }    }    voidOnDestroy () {Cancelinvoke ("Clip"); }}

The corresponding shader code is as follows:

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. oncolormask RGB cull off Lighting off Zwrite off Fog {Color (0,0,0,0)} subshader {Pass {cgprogram#pragmaVertex vert#pragmaFragment Frag#pragmaMulti_compile_particles#include"Unitycg.cginc"sampler2d _maintex;                Fixed4 _tintcolor;                                FLOAT4 _area; structappdata_t {float4 vertex:position;                    Fixed4 Color:color;                FLOAT2 texcoord:texcoord0;                }; structv2f {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; returno; } fixed4 Frag (v2f i): sv_target {BOOLInarea = i.worldpos.x >= _area.x && i.worldpos.x <= _area.z && i.worldpos.y >= _area.y && I.worldpos.y <=_AREA.W; returnInarea?2.0f* I.color * _tintcolor * tex2d (_maintex, I.texcoord): Fixed4 (0,0,0,0); } ENDCG} }}

Unity NGUI UIPanel for particle clipping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.