这几天总结一下之前遇到的有意思的问题,之前遇到的时候没时间总结,现在就总结一下。
首先说一下关于特效的裁剪应用的场景主要是scrollview中,当scrollview中的物体超出UIPannel的范围NGUI会对超出的部分的UI进行裁切,其原理是将UIPannel的裁剪区域传递给其自带裁剪shader对超出区域的像素透明度设为完全透明,但是并不会作用于粒子特效,所以会出现拖拽UI控件至超出边界的时候UI被裁减但是特效依然存在。具体大概是这样:
(新的电脑没有装NGUI,就先用UGUI搭一下。。。)可以看到当UI超出边界的时候挂在UI下的粒子特效并没有是随着UI裁剪。
那么就说一下解决思路把,首先在脚本中拿到当前的Pannel的裁剪区域,通过裁剪区域算出上下左右四个边界值,再乘以分辨率的缩放传递给shader处理。在shader中的处理就很简单了,顶点着色器就是变换顶点至世界坐标,在片元处理器中比较顶点位置是否在裁剪区域,如果不再就不渲染。
c#代码如下 :
using System;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(UIPanel))]
public class ParticleSystemClipper : MonoBehaviour
{
UIPanel m_targetPanel=null;
private UIRoot _uiRoot=null;
bool inited = false;
Vector4 _clipArea;
void Init()
{
if(inited)
{
return;
}
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");
_uiRoot = NGUITools.FindInParents<UIRoot&g