Skip to content

Commit

Permalink
close #111; UITransitionEffect: Change transition texture
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Aug 7, 2018
1 parent 4236fcc commit 9a580b2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 29 deletions.
107 changes: 95 additions & 12 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UITransitionEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public enum EffectMode
//################################
[SerializeField] EffectMode m_EffectMode;
[SerializeField][Range(0, 1)] float m_EffectFactor = 1;
[SerializeField] Texture m_TransitionTexture;
[SerializeField] protected EffectArea m_EffectArea;


//################################
Expand All @@ -51,11 +53,66 @@ public float effectFactor
}
}

/// <summary>
/// Transition texture.
/// </summary>
public Texture transitionTexture
{
get { return m_TransitionTexture; }
set
{
if (m_TransitionTexture != value)
{
m_TransitionTexture = value;
if (graphic)
{
ModifyMaterial();
}
}
}
}

/// <summary>
/// Effect mode.
/// </summary>
public EffectMode effectMode { get { return m_EffectMode; } }

/// <summary>
/// Modifies the material.
/// </summary>
public override void ModifyMaterial()
{
ulong hash = (m_TransitionTexture ? (uint)m_TransitionTexture.GetInstanceID() : 0) + (uint)m_EffectMode;
if (_materialCache != null && (_materialCache.hash != hash || !isActiveAndEnabled || !m_EffectMaterial))
{
MaterialCache.Unregister(_materialCache);
_materialCache = null;
}

if (!isActiveAndEnabled || !m_EffectMaterial)
{
graphic.material = null;
}
else if (!m_TransitionTexture)
{
graphic.material = m_EffectMaterial;
}
else if (_materialCache != null && _materialCache.hash == hash)
{
graphic.material = _materialCache.material;
}
else
{
_materialCache = MaterialCache.Register(hash, m_TransitionTexture, () =>
{
var mat = new Material(m_EffectMaterial);
mat.SetTexture("_TransitionTexture", m_TransitionTexture);
return mat;
});
graphic.material = _materialCache.material;
}
}

/// <summary>
/// Modifies the mesh.
/// </summary>
Expand All @@ -66,24 +123,45 @@ public override void ModifyMesh(VertexHelper vh)
return;
}

UIVertex vt;
vh.GetUIVertexStream(tempVerts);

// Pack effect prameters.
Vector2 factor = new Vector2(effectFactor, 0);
// rect.
Rect rect = m_EffectArea.GetEffectArea(vh, graphic);

// Set prameters to vertex.
for (int i = 0; i < tempVerts.Count; i++)
UIVertex vertex = default(UIVertex);
bool effectEachCharacter = graphic is Text && m_EffectArea == EffectArea.Character;
float x, y;
for (int i = 0; i < vh.currentVertCount; i++)
{
vt = tempVerts[i];
vt.uv1 = factor;
tempVerts[i] = vt;
vh.PopulateUIVertex(ref vertex, i);

if (effectEachCharacter)
{
x = splitedCharacterPosition[i%4].x;
y = splitedCharacterPosition[i%4].y;
}
else
{
x = Mathf.Clamp01(vertex.position.x / rect.width + 0.5f);
y = Mathf.Clamp01(vertex.position.y / rect.height + 0.5f);
}
vertex.uv1 = new Vector2(
effectFactor,
Packer.ToFloat (x, y)
);

vh.SetUIVertex(vertex, i);
}
}

vh.Clear();
vh.AddUIVertexTriangleStream(tempVerts);

tempVerts.Clear();
//################################
// Protected Members.
//################################
protected override void OnDisable()
{
MaterialCache.Unregister(_materialCache);
_materialCache = null;
base.OnDisable();
}

#if UNITY_EDITOR
Expand All @@ -98,5 +176,10 @@ protected override Material GetMaterial()
: null;
}
#endif

//################################
// Private Members.
//################################
MaterialCache _materialCache = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Shader "UI/Hidden/UI-Effect-Transition"
_ColorMask ("Color Mask", Float) = 15

[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0

[Header(Transition)]
_TransitionTexture("Transition Texture (A)", 2D) = "white" {}
}

SubShader
Expand Down Expand Up @@ -57,13 +60,14 @@ Shader "UI/Hidden/UI-Effect-Transition"

#include "UnityCG.cginc"
#include "UnityUI.cginc"

#include "UI-Effect.cginc"

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
half factor : TEXCOORD1;
half2 factor : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

Expand All @@ -73,14 +77,15 @@ Shader "UI/Hidden/UI-Effect-Transition"
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 wpos : TEXCOORD1;
half factor : TEXCOORD2;
half3 factor : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};

fixed4 _Color;
fixed4 _TextureSampleAdd;
float4 _ClipRect;
sampler2D _MainTex;
sampler2D _TransitionTexture;

v2f vert(appdata_t IN)
{
Expand All @@ -93,32 +98,32 @@ Shader "UI/Hidden/UI-Effect-Transition"
OUT.texcoord = IN.texcoord;

OUT.color = IN.color * _Color;
OUT.factor = IN.factor;
OUT.factor = half3(IN.factor.x, UnpackToVec2(IN.factor.y));

return OUT;
}


fixed4 frag(v2f IN) : SV_Target
{
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
color.a *= UnityGet2DClipping(IN.wpos.xy, _ClipRect);
half effectFactor = IN.factor.x;
half alpha = tex2D(_TransitionTexture, IN.factor.yz).a;

#ifdef CUTOFF
clip (color.a - 1 + IN.factor * 1.001);
#elif UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd);
color.a *= UnityGet2DClipping(IN.wpos.xy, _ClipRect);

#if MONO
color.rgb = IN.color.rgb;
color.a = color.a * tex2D(_MainTex, IN.texcoord).a + IN.factor * 2 - 1;
color.a = color.a * alpha + (effectFactor * 2 - 1);
color.a *= step(0.001, color.a);
#elif CUTOFF
color.rgb = IN.color.rgb;
color.a = 1;
color.a = step(0.001, color.a * alpha - 1 + effectFactor);
#endif

#if UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif

return color;
return color * IN.color;
}
ENDCG
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a580b2

Please sign in to comment.