Skip to content

Commit 9759f7b

Browse files
committed
Improve GoWrapper clipping
1 parent 5e59a60 commit 9759f7b

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

Assets/Scripts/Core/GoWrapper.cs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class GoWrapper : DisplayObject
2121
protected Canvas _canvas;
2222
protected bool _cloneMaterial;
2323
protected bool _shouldCloneMaterial;
24-
protected bool _supportMask;
2524

2625
protected struct RendererInfo
2726
{
@@ -85,7 +84,6 @@ public void SetWrapTarget(GameObject target, bool cloneMaterial)
8584

8685
_canvas = null;
8786
_wrapTarget = target;
88-
_supportMask = false;
8987
_shouldCloneMaterial = false;
9088
_renderers.Clear();
9189

@@ -176,10 +174,6 @@ void CloneMaterials()
176174
if (shouldSetRQ && mat.renderQueue != 3000) //Set the object rendering in Transparent Queue as UI objects
177175
mat.renderQueue = 3000;
178176

179-
if (mat.HasProperty(ShaderConfig.ID_Stencil) || mat.HasProperty(ShaderConfig.ID_Stencil2)
180-
|| mat.HasProperty(ShaderConfig.ID_ClipBox))
181-
_supportMask = true;
182-
183177
//确保相同的材质不会复制两次
184178
Material newMat;
185179
if (!_materialsBackup.TryGetValue(mat, out newMat))
@@ -291,31 +285,48 @@ override public void Update(UpdateContext context)
291285
if (_shouldCloneMaterial)
292286
CloneMaterials();
293287

294-
if (_supportMask)
288+
ApplyClipping(context);
289+
290+
base.Update(context);
291+
}
292+
293+
private List<Material> helperMaterials = new List<Material>();
294+
virtual protected void ApplyClipping(UpdateContext context)
295+
{
296+
#if UNITY_2018_2_OR_NEWER
297+
int cnt = _renderers.Count;
298+
for (int i = 0; i < cnt; i++)
295299
{
296-
int cnt = _renderers.Count;
297-
for (int i = 0; i < cnt; i++)
300+
_renderers[i].renderer.GetMaterials(helperMaterials);
301+
302+
int cnt2 = helperMaterials.Count;
303+
for (int j = 0; j < cnt2; j++)
298304
{
299-
RendererInfo ri = _renderers[i];
300-
Material[] mats = ri.materials;
301-
if (mats != null)
302-
{
303-
int cnt2 = mats.Length;
304-
for (int j = 0; j < cnt2; j++)
305-
{
306-
Material mat = mats[j];
307-
if (mat != null)
308-
context.ApplyClippingProperties(mat, false);
309-
}
305+
Material mat = helperMaterials[j];
306+
if (mat != null)
307+
context.ApplyClippingProperties(mat, false);
308+
}
310309

311-
if (cnt2 > 0 && _cloneMaterial && ri.renderer != null
312-
&& !Material.ReferenceEquals(ri.renderer.sharedMaterial, mats[0]))
313-
ri.renderer.sharedMaterials = mats;
310+
helperMaterials.Clear();
311+
}
312+
#else
313+
int cnt = _renderers.Count;
314+
for (int i = 0; i < cnt; i++)
315+
{
316+
RendererInfo ri = _renderers[i];
317+
Material[] mats = ri.materials;
318+
if (mats != null)
319+
{
320+
int cnt2 = mats.Length;
321+
for (int j = 0; j < cnt2; j++)
322+
{
323+
Material mat = mats[j];
324+
if (mat != null)
325+
context.ApplyClippingProperties(mat, false);
314326
}
315327
}
316328
}
317-
318-
base.Update(context);
329+
#endif
319330
}
320331

321332
public override void Dispose()

Assets/Scripts/Core/ShaderConfig.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public static class ShaderConfig
5353
public static int ID_BlendDstFactor;
5454
public static int ID_ColorOption;
5555

56-
public static int ID_StencilComp2;
5756
public static int ID_Stencil2;
5857

5958
static ShaderConfig()
@@ -73,7 +72,6 @@ static ShaderConfig()
7372
ID_ColorOption = Shader.PropertyToID("_ColorOption");
7473

7574
ID_Stencil2 = Shader.PropertyToID("_StencilRef");
76-
ID_StencilComp2 = Shader.PropertyToID("_StencilComp");
7775
}
7876

7977
/// <summary>

Assets/Scripts/Core/UpdateContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public void ApplyClippingProperties(Material mat, bool isStdMaterial)
228228
{
229229
mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Equal);
230230
mat.SetInt(ShaderConfig.ID_Stencil, stencilCompareValue);
231+
mat.SetInt(ShaderConfig.ID_Stencil2, stencilCompareValue);
231232
mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep);
232233
mat.SetInt(ShaderConfig.ID_StencilReadMask, stencilReferenceValue | (stencilReferenceValue - 1));
233234
mat.SetInt(ShaderConfig.ID_ColorMask, 15);
@@ -236,6 +237,7 @@ public void ApplyClippingProperties(Material mat, bool isStdMaterial)
236237
{
237238
mat.SetInt(ShaderConfig.ID_StencilComp, (int)UnityEngine.Rendering.CompareFunction.Always);
238239
mat.SetInt(ShaderConfig.ID_Stencil, 0);
240+
mat.SetInt(ShaderConfig.ID_Stencil2, 0);
239241
mat.SetInt(ShaderConfig.ID_StencilOp, (int)UnityEngine.Rendering.StencilOp.Keep);
240242
mat.SetInt(ShaderConfig.ID_StencilReadMask, 255);
241243
mat.SetInt(ShaderConfig.ID_ColorMask, 15);

0 commit comments

Comments
 (0)