0%

U3D 基于NGUI写的 listView特效裁剪

修改UIDrawCall文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public void UpdateSubMaterials(UIFilterDCWidget uiFilter)
{
UIPanel currentPanel = panel;

if (currentPanel.hasClipping)
{
float angle = 0f;
Vector4 cr = currentPanel.drawCallClipRange;

// Clipping regions past the first one need additional math
if (currentPanel != panel)
{
Vector3 pos = currentPanel.cachedTransform.InverseTransformPoint(panel.cachedTransform.position);
cr.x -= pos.x;
cr.y -= pos.y;

Vector3 v0 = panel.cachedTransform.rotation.eulerAngles;
Vector3 v1 = currentPanel.cachedTransform.rotation.eulerAngles;
Vector3 diff = v1 - v0;

diff.x = NGUIMath.WrapAngle(diff.x);
diff.y = NGUIMath.WrapAngle(diff.y);
diff.z = NGUIMath.WrapAngle(diff.z);

if (Mathf.Abs(diff.x) > 0.001f || Mathf.Abs(diff.y) > 0.001f)
Debug.LogWarning("Panel can only be clipped properly if X and Y rotation is left at 0", panel);

angle = diff.z;
}
var renderers = uiFilter.gameObject.GetComponentsInChildren<Renderer>();
for (int k = 0; k < renderers.Length; k++)
{
if (renderers[k].sharedMaterial != null)
{
// Pass the clipping parameters to the shader
SetClipping(0, cr, currentPanel.clipSoftness, angle, renderers[k].sharedMaterial);
renderers[k].sharedMaterial.SetMatrix(property_UIWorldtoUIRoot, currentPanel.gameObject.transform.worldToLocalMatrix);
}
}
}
else
{
var renderers = uiFilter.gameObject.GetComponentsInChildren<Renderer>();
for (int k = 0; k < renderers.Length; k++)
{
if (renderers[k].sharedMaterial != null)
{
renderers[k].sharedMaterial.SetVector(ClipRange[0], new Vector4(0, 0, 0, 0));
renderers[k].sharedMaterial.SetVector(ClipArgs[0], new Vector4(1000, 1000, 0, 0));
renderers[k].sharedMaterial.SetMatrix(property_UIWorldtoUIRoot, currentPanel.gameObject.transform.worldToLocalMatrix);
}
}
}
}

Shader代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Shader "GameUI/Particles/Transparent Particle Add 1"
{
Properties
{
_InitColor("InitColor", Color) = (1,1,1,1)
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
_AlphaTex ("Alpha Texture",2D) = "red" {}

}

SubShader
{
LOD 100

Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"RenderPipeline" = "UniversalPipeline"
}

Pass
{
Cull Off
Lighting Off
ZWrite Off
Offset -1, -1
Fog { Mode Off }
ColorMask RGB
Blend One One

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _AlphaTex;
float4 _ClipRange0 = float4(0.0, 0.0, 1.0, 1.0);
float2 _ClipArgs0 = float2(1000.0, 1000.0);
half4 _InitColor;

float4x4 _UIWorldtoUIRoot;

struct appdata_t
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 worldPos : TEXCOORD1;
};

v2f o;

v2f vert (appdata_t v)
{
o.vertex = UnityObjectToClipPos(v.vertex);
float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
worldPos = mul(_UIWorldtoUIRoot, worldPos);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.worldPos = worldPos.xy * _ClipRange0.zw + _ClipRange0.xy;
return o;
}

half4 frag (v2f IN) : COLOR
{
// Softness factor
float2 factor = (float2(1.0, 1.0) - abs(IN.worldPos)) * _ClipArgs0;
log2(factor);
// Sample the texture
//half4 col = GreyUI(_MainTex,_AlphaTex,IN.texcoord,IN.color);
half4 col = tex2D(_MainTex, IN.texcoord) * IN.color;
col.a = tex2D(_AlphaTex, IN.texcoord).r * IN.color.a;
col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
col *= _InitColor;
col.rgb *= col.a;

return col;
}
ENDCG
}
}
}