1.需求:
游戲中粒子特效能實(shí)現(xiàn)非常好的效果,但是由于粒子特效是獨(dú)立的系統(tǒng),Unity自帶的Mask普通的遮罩,遮不住粒子特效。
2.實(shí)現(xiàn)原理:
通過(guò)shader把超出范圍的粒子紋理(Texture),改成透明顏色,以實(shí)現(xiàn)遮住粒子特效的功能。
3.關(guān)鍵Shader代碼
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);
col.a *= (i.vpos.x >= _MinX);
col.a *= (i.vpos.x <= _MaxX);
col.a *= (i.vpos.y >= _MinY);
col.a *= (i.vpos.y <= _MaxY);
col.rgb *= col.a;
UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
return col;
}
4.效果演示:
5.接口易用性:
在需要遮住粒子特效的GameObject上面掛上MaskParticle腳本即可。
設(shè)置遮罩范圍。
MaskParticle.SetMaskRectTr(RectTransform rect)
設(shè)置遮罩跟隨設(shè)置的范圍。
MaskParticle.SetMaskUpdate(RectTransform rect)
6.環(huán)境:
Unity3D 2020.3.48
7.GitHub地址:https://github.com/MyTestProjectYour/Unity3d-ParticleMask/tree/main文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-740619.html
記得給個(gè)星星喲。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-740619.html
到了這里,關(guān)于Unity 粒子特效遮罩(ParticleMask)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!