Skip to content

Commit

Permalink
feat: new effect that synchronize with another effect
Browse files Browse the repository at this point in the history
With UISyncEffect, you can apply the same effect as any other effect component.

Close #159, #158, #143
  • Loading branch information
mob-sakai committed Apr 10, 2020
1 parent 7116ac0 commit 7a2a97c
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,28 @@ public virtual ParameterTexture paramTex
/// <summary>
/// Mark the vertices as dirty.
/// </summary>
protected void SetMaterialDirty()
public void SetMaterialDirty()
{
connector.SetMaterialDirty(graphic);

foreach (var effect in syncEffects)
{
effect.SetMaterialDirty();
// BaseConnector.FindConnector(effect.graphic).SetMaterialDirty(effect.graphic);
}
}

public virtual Hash128 GetMaterialHash(Material baseMaterial)
{
return k_InvalidHash;
}

public virtual Material GetModifiedMaterial(Material baseMaterial)
public Material GetModifiedMaterial(Material baseMaterial)
{
return GetModifiedMaterial(baseMaterial, graphic);
}

public virtual Material GetModifiedMaterial(Material baseMaterial, Graphic graphic)
{
if (!isActiveAndEnabled) return baseMaterial;

Expand All @@ -55,7 +66,7 @@ public virtual Material GetModifiedMaterial(Material baseMaterial)
var modifiedMaterial = baseMaterial;
if (_effectMaterialHash.isValid)
{
modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, ModifyMaterial);
modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, ModifyMaterial, graphic);
}

MaterialCache.Unregister(oldHash);
Expand All @@ -68,7 +79,7 @@ public virtual Material GetModifiedMaterial(Material baseMaterial)
// return material && material.shader && material.shader.name.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal);
// }

public virtual void ModifyMaterial(Material newMaterial)
public virtual void ModifyMaterial(Material newMaterial, Graphic graphic)
{
if (isActiveAndEnabled && paramTex != null)
paramTex.RegisterMaterial(newMaterial);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

Expand Down Expand Up @@ -41,6 +42,8 @@ protected RectTransform rectTransform
get { return _rectTransform ? _rectTransform : _rectTransform = GetComponent<RectTransform>(); }
}

internal readonly List<UISyncEffect> syncEffects = new List<UISyncEffect>(0);

/// <summary>
/// The material for rendering.
/// </summary>
Expand All @@ -63,6 +66,11 @@ public virtual void ModifyMesh(Mesh mesh)
/// </summary>
/// <param name="vh">VertexHelper.</param>
public virtual void ModifyMesh(VertexHelper vh)
{
ModifyMesh(vh, graphic);
}

public virtual void ModifyMesh(VertexHelper vh, Graphic graphic)
{
}

Expand All @@ -73,6 +81,11 @@ protected virtual void SetVerticesDirty()
{
connector.SetVerticesDirty(graphic);

foreach (var effect in syncEffects)
{
effect.SetVerticesDirty();
}

// #if TMP_PRESENT
// if (textMeshPro)
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using UnityEngine;
using System.Text;
using UnityEngine.UI;

namespace Coffee.UIExtensions
{
Expand Down Expand Up @@ -39,7 +40,7 @@ private static void ClearCache()
}
#endif

public static Material Register(Material baseMaterial, Hash128 hash, System.Action<Material> onModifyMaterial)
public static Material Register(Material baseMaterial, Hash128 hash, System.Action<Material, Graphic> onModifyMaterial, Graphic graphic)
{
if (!hash.isValid) return null;

Expand All @@ -54,7 +55,7 @@ public static Material Register(Material baseMaterial, Hash128 hash, System.Acti
},
};

onModifyMaterial(entry.material);
onModifyMaterial(entry.material, graphic);
materialMap.Add(hash, entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ protected override void CloseScope()
{
if (changed)
{
var graphics = targets.OfType<BaseMaterialEffect>()
.Select(x => x.graphic)
.Where(x => x);

foreach (var g in graphics)
foreach (var effect in targets.OfType<BaseMaterialEffect>())
{
BaseConnector.FindConnector(g).SetMaterialDirty(g);
effect.SetMaterialDirty();
}
}

Expand Down
26 changes: 13 additions & 13 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public Texture transitionTexture
}
}

private static Texture defaultTransitionTexture
{
get
{
return _defaultTransitionTexture
? _defaultTransitionTexture
: (_defaultTransitionTexture = Resources.Load<Texture>("Default-Transition"));
}
}

/// <summary>
/// The area for effect.
/// </summary>
Expand Down Expand Up @@ -201,19 +211,9 @@ public override Hash128 GetMaterialHash(Material material)
);
}


private static Texture defaultTransitionTexture
{
get
{
return _defaultTransitionTexture
? _defaultTransitionTexture
: (_defaultTransitionTexture = Resources.Load<Texture>("Default-Transition"));
}
}

public override void ModifyMaterial(Material newMaterial)
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = BaseConnector.FindConnector(graphic);
newMaterial.shader = connector.FindShader("UIDissolve");
SetShaderVariants(newMaterial, m_ColorMode);

Expand All @@ -224,7 +224,7 @@ public override void ModifyMaterial(Material newMaterial)
/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
Expand Down
6 changes: 4 additions & 2 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ public override Hash128 GetMaterialHash(Material material)
);
}

public override void ModifyMaterial(Material newMaterial)
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = BaseConnector.FindConnector(graphic);

newMaterial.shader = connector.FindShader("UIEffect");
SetShaderVariants(newMaterial, m_EffectMode, m_ColorMode, m_BlurMode,
m_AdvancedBlur ? BlurEx.Ex : BlurEx.None);
Expand All @@ -219,7 +221,7 @@ public override void ModifyMaterial(Material newMaterial)
/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
{
Expand Down
110 changes: 0 additions & 110 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Assets/Coffee/UIExtensions/UIEffect/Scripts/UIFlip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool vertical
/// Call used to modify mesh.
/// </summary>
/// <param name="vh">VertexHelper.</param>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled) return;

Expand Down
2 changes: 1 addition & 1 deletion Assets/Coffee/UIExtensions/UIEffect/Scripts/UIGradient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public bool ignoreAspectRatio
/// <summary>
/// Call used to modify mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
Expand Down
6 changes: 4 additions & 2 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIHsvModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ public override Hash128 GetMaterialHash(Material material)
);
}

public override void ModifyMaterial(Material newMaterial)
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = BaseConnector.FindConnector(graphic);

newMaterial.shader = connector.FindShader("UIHsvModifier");
paramTex.RegisterMaterial(newMaterial);
}

public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected override void OnDisable()
/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled || vh.currentVertCount <= 0 || m_Style == ShadowStyle.None)
{
Expand Down
6 changes: 4 additions & 2 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ public override Hash128 GetMaterialHash(Material material)
);
}

public override void ModifyMaterial(Material newMaterial)
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = BaseConnector.FindConnector(graphic);

newMaterial.shader = connector.FindShader("UIShiny");
paramTex.RegisterMaterial(newMaterial);
}

/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh)
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
Expand Down
Loading

0 comments on commit 7a2a97c

Please sign in to comment.