Skip to content

Commit

Permalink
close #16 : Improve Visual quality
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed May 24, 2018
1 parent c6e41cb commit f9b9a70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
18 changes: 17 additions & 1 deletion Assets/ShinyEffectForUGUI/ShinyEffectForUGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ShinyEffectForUGUI : BaseMeshEffect
[FormerlySerializedAs("m_Alpha")]
[SerializeField][Range(0, 1)] float m_Brightness = 1f;
[SerializeField][Range(-180, 180)] float m_Rotation;
[SerializeField][Range(0, 1)] float m_Highlight = 1;
[SerializeField] Material m_EffectMaterial;


Expand Down Expand Up @@ -74,6 +75,11 @@ public class ShinyEffectForUGUI : BaseMeshEffect
/// </summary>
public float brightness { get { return m_Brightness; } set { m_Brightness = Mathf.Clamp(value, 0, 1); _SetDirty(); } }

/// <summary>
/// Highlight factor for shiny effect.
/// </summary>
public float highlight { get { return m_Highlight; } set { m_Highlight = Mathf.Clamp(value, 0, 1); _SetDirty(); } }

/// <summary>
/// Rotation for shiny effect.
/// </summary>
Expand Down Expand Up @@ -175,7 +181,7 @@ public override void ModifyMesh(VertexHelper vh)

vertex.uv1 = new Vector2(
_PackToFloat(Mathf.Clamp01(nomalizedPos.y), softness, width, brightness),
location
_PackToFloat(location, highlight)
);

vh.SetUIVertex(vertex, i);
Expand Down Expand Up @@ -207,6 +213,16 @@ static float _PackToFloat(float x, float y, float z, float w)
+ Mathf.FloorToInt(x * PRECISION);
}

/// <summary>
/// Pack 2 low-precision [0-1] floats values to a float.
/// Each value [0-1] has 4096 steps(12 bits).
/// </summary>
static float _PackToFloat(float x, float y)
{
const int PRECISION = (1 << 12) - 1;
return (Mathf.FloorToInt(y * PRECISION) << 12)
+ Mathf.FloorToInt(x * PRECISION);
}


/// <summary>
Expand Down
27 changes: 22 additions & 5 deletions Assets/ShinyEffectForUGUI/UI-Effect-Shiny.shader
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Shader "UI/Hidden/UI-Effect-Shiny"
UNITY_VERTEX_OUTPUT_STEREO

half4 effectFactor : TEXCOORD2;
half location : TEXCOORD3;
half2 effectFactor2 : TEXCOORD3;
};

fixed4 _Color;
Expand Down Expand Up @@ -104,6 +104,19 @@ Shader "UI/Hidden/UI-Effect-Shiny"
return color;
}

half2 UnpackToVec2(float value)
{
const int PACKER_STEP = 4096;
const int PRECISION = PACKER_STEP - 1;
fixed4 color;

color.x = (value % PACKER_STEP) / PRECISION;
value = floor(value / PACKER_STEP);

color.y = (value % PACKER_STEP) / PRECISION;
return color;
}

v2f vert(appdata_t IN)
{
v2f OUT;
Expand All @@ -118,7 +131,9 @@ Shader "UI/Hidden/UI-Effect-Shiny"
OUT.color = IN.color * _Color;

OUT.effectFactor = UnpackToVec4(IN.uv1.x);
OUT.location = IN.uv1.y * 2 - 0.5;
OUT.effectFactor2 = UnpackToVec2(IN.uv1.y);

OUT.effectFactor2.x = OUT.effectFactor2.x * 2 - 0.5;
return OUT;
}

Expand All @@ -133,11 +148,13 @@ Shader "UI/Hidden/UI-Effect-Shiny"
clip (color.a - 0.001);
#endif

half pos = IN.effectFactor.x - IN.location;
half pos = IN.effectFactor.x - IN.effectFactor2.x;

half normalized = 1 - saturate(abs(pos / IN.effectFactor.z));
half shinePower = smoothstep(0, IN.effectFactor.y, normalized);
half shinePower = smoothstep(0, IN.effectFactor.y*2, normalized);
half3 reflectColor = lerp(1, color.rgb * 10, IN.effectFactor2.y);

color.rgb += originAlpha * (shinePower / 2) * IN.effectFactor.w;
color.rgb += originAlpha * (shinePower / 2) * IN.effectFactor.w * reflectColor;
return color;
}

Expand Down

0 comments on commit f9b9a70

Please sign in to comment.