Files
UIEffect/Scripts/UIHsvModifier.cs
T
semantic-release-bot 49d01cac3d 4.0.0-preview.1
# [4.0.0-preview.1](https://github.com/mob-sakai/UIEffect/compare/v3.2.0...v4.0.0-preview.1) (2020-04-10)

### Bug Fixes

* adjusting the brightness of the shiny shader ([29b4dad](https://github.com/mob-sakai/UIEffect/commit/29b4dad9296f22b00687a27c6103820b58cbd890))
* the effect is kept even if the component is disabled (UIFlip, UIGradient) ([6124214](https://github.com/mob-sakai/UIEffect/commit/612421432defaf4e38fa97e12392021e9954027f))

### Features

* add asmdef files ([24710fc](https://github.com/mob-sakai/UIEffect/commit/24710fc892265c16a635ebad0940f779e0c9cdcd))
* new effect that synchronize with another effect ([7a2a97c](https://github.com/mob-sakai/UIEffect/commit/7a2a97c89e90cc94c14fb614544f248824e93f3f)), closes [#159](https://github.com/mob-sakai/UIEffect/issues/159) [#158](https://github.com/mob-sakai/UIEffect/issues/158) [#143](https://github.com/mob-sakai/UIEffect/issues/143)
* transfer TextMeshPro support to another package ([be90210](https://github.com/mob-sakai/UIEffect/commit/be90210e6cbf39133bd829e4bf08b9005dd925f4))
* transfer UIEffectCapturedImage support to another package ([c2a2320](https://github.com/mob-sakai/UIEffect/commit/c2a23202e18d103d58d8ebf56851e693f5ac1bb7))
* update docs to support Unity Package Manager ([bdc891a](https://github.com/mob-sakai/UIEffect/commit/bdc891a88ea2b9456276cfe99286c2e799e9c36b)), closes [#141](https://github.com/mob-sakai/UIEffect/issues/141)
* update the effect architecture ([4c08c8c](https://github.com/mob-sakai/UIEffect/commit/4c08c8c1621cc8d3a1f5d834c77d925b51331cd5)), closes [#202](https://github.com/mob-sakai/UIEffect/issues/202) [#198](https://github.com/mob-sakai/UIEffect/issues/198) [#195](https://github.com/mob-sakai/UIEffect/issues/195) [#189](https://github.com/mob-sakai/UIEffect/issues/189) [#173](https://github.com/mob-sakai/UIEffect/issues/173) [#104](https://github.com/mob-sakai/UIEffect/issues/104)
* update the structure of shaders ([db06e68](https://github.com/mob-sakai/UIEffect/commit/db06e68cfbe3a89e7de9d6ceef841f29f99611a5))
* updated the Unity version used for development to 2018.3 ([c5f22c1](https://github.com/mob-sakai/UIEffect/commit/c5f22c1a3915dd9935b983af1971fa6018f2f320)), closes [#169](https://github.com/mob-sakai/UIEffect/issues/169)

### BREAKING CHANGES

* Scenes, prefabs and scripts using UIEffect v3.x may not work properly.
* The recommended Unity version is 2018.1 or later; it will work with Unity 2017.1, but it is not guaranteed to be supported.
* UIEffectCapturedImage will be supported by another package.
* TextMeshPro support has been removed from this package. It will be supported by another package.
2020-04-10 08:31:51 +00:00

171 lines
5.2 KiB
C#

using System;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace Coffee.UIExtensions
{
/// <summary>
/// HSV Modifier.
/// </summary>
[AddComponentMenu("UI/UIEffects/UIHsvModifier", 4)]
public class UIHsvModifier : BaseMaterialEffect
{
private const uint k_ShaderId = 6 << 3;
private static readonly ParameterTexture s_ParamTex = new ParameterTexture(7, 128, "_ParamTex");
[Header("Target")] [Tooltip("Target color to affect hsv shift.")] [SerializeField] [ColorUsage(false)]
Color m_TargetColor = Color.red;
[Tooltip("Color range to affect hsv shift [0 ~ 1].")] [SerializeField] [Range(0, 1)]
float m_Range = 0.1f;
[Header("Adjustment")] [Tooltip("Hue shift [-0.5 ~ 0.5].")] [SerializeField] [Range(-0.5f, 0.5f)]
float m_Hue;
[Tooltip("Saturation shift [-0.5 ~ 0.5].")] [SerializeField] [Range(-0.5f, 0.5f)]
float m_Saturation;
[Tooltip("Value shift [-0.5 ~ 0.5].")] [SerializeField] [Range(-0.5f, 0.5f)]
float m_Value;
/// <summary>
/// Target color to affect hsv shift.
/// </summary>
public Color targetColor
{
get { return m_TargetColor; }
set
{
if (m_TargetColor == value) return;
m_TargetColor = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Color range to affect hsv shift [0 ~ 1].
/// </summary>
public float range
{
get { return m_Range; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_Range, value)) return;
m_Range = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Saturation shift [-0.5 ~ 0.5].
/// </summary>
public float saturation
{
get { return m_Saturation; }
set
{
value = Mathf.Clamp(value, -0.5f, 0.5f);
if (Mathf.Approximately(m_Saturation, value)) return;
m_Saturation = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Value shift [-0.5 ~ 0.5].
/// </summary>
public float value
{
get { return m_Value; }
set
{
value = Mathf.Clamp(value, -0.5f, 0.5f);
if (Mathf.Approximately(m_Value, value)) return;
m_Value = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Hue shift [-0.5 ~ 0.5].
/// </summary>
public float hue
{
get { return m_Hue; }
set
{
value = Mathf.Clamp(value, -0.5f, 0.5f);
if (Mathf.Approximately(m_Hue, value)) return;
m_Hue = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Gets the parameter texture.
/// </summary>
public override ParameterTexture paramTex
{
get { return s_ParamTex; }
}
public override Hash128 GetMaterialHash(Material material)
{
if (!isActiveAndEnabled || !material || !material.shader)
return k_InvalidHash;
return new Hash128(
(uint) material.GetInstanceID(),
k_ShaderId,
0,
0
);
}
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, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
var normalizedIndex = paramTex.GetNormalizedIndex(this);
var vertex = default(UIVertex);
var count = vh.currentVertCount;
for (var i = 0; i < count; i++)
{
vh.PopulateUIVertex(ref vertex, i);
vertex.uv0 = new Vector2(
Packer.ToFloat(vertex.uv0.x, vertex.uv0.y),
normalizedIndex
);
vh.SetUIVertex(vertex, i);
}
}
protected override void SetEffectParamsDirty()
{
float h, s, v;
Color.RGBToHSV(m_TargetColor, out h, out s, out v);
paramTex.SetData(this, 0, h); // param1.x : target hue
paramTex.SetData(this, 1, s); // param1.y : target saturation
paramTex.SetData(this, 2, v); // param1.z : target value
paramTex.SetData(this, 3, m_Range); // param1.w : target range
paramTex.SetData(this, 4, m_Hue + 0.5f); // param2.x : hue shift
paramTex.SetData(this, 5, m_Saturation + 0.5f); // param2.y : saturation shift
paramTex.SetData(this, 6, m_Value + 0.5f); // param2.z : value shift
}
}
}