diff --git a/Assets/ShinyEffectForUGUI/ShinyEffectForUGUI.cs b/Assets/ShinyEffectForUGUI/ShinyEffectForUGUI.cs index 3c8a140..fdeee3e 100644 --- a/Assets/ShinyEffectForUGUI/ShinyEffectForUGUI.cs +++ b/Assets/ShinyEffectForUGUI/ShinyEffectForUGUI.cs @@ -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; @@ -74,6 +75,11 @@ public class ShinyEffectForUGUI : BaseMeshEffect /// public float brightness { get { return m_Brightness; } set { m_Brightness = Mathf.Clamp(value, 0, 1); _SetDirty(); } } + /// + /// Highlight factor for shiny effect. + /// + public float highlight { get { return m_Highlight; } set { m_Highlight = Mathf.Clamp(value, 0, 1); _SetDirty(); } } + /// /// Rotation for shiny effect. /// @@ -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); @@ -207,6 +213,16 @@ static float _PackToFloat(float x, float y, float z, float w) + Mathf.FloorToInt(x * PRECISION); } + /// + /// Pack 2 low-precision [0-1] floats values to a float. + /// Each value [0-1] has 4096 steps(12 bits). + /// + static float _PackToFloat(float x, float y) + { + const int PRECISION = (1 << 12) - 1; + return (Mathf.FloorToInt(y * PRECISION) << 12) + + Mathf.FloorToInt(x * PRECISION); + } /// diff --git a/Assets/ShinyEffectForUGUI/UI-Effect-Shiny.shader b/Assets/ShinyEffectForUGUI/UI-Effect-Shiny.shader index 4d7cae4..39c1b74 100644 --- a/Assets/ShinyEffectForUGUI/UI-Effect-Shiny.shader +++ b/Assets/ShinyEffectForUGUI/UI-Effect-Shiny.shader @@ -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; @@ -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; @@ -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; } @@ -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; }