Files
UIEffect/Scripts/UIDissolve.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

320 lines
9.4 KiB
C#

using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.Serialization;
using System.Text;
using System.Linq;
using System.IO;
namespace Coffee.UIExtensions
{
/// <summary>
/// Dissolve effect for uGUI.
/// </summary>
[AddComponentMenu("UI/UIEffects/UIDissolve", 3)]
public class UIDissolve : BaseMaterialEffect, IMaterialModifier
{
private const uint k_ShaderId = 0 << 3;
private static readonly ParameterTexture s_ParamTex = new ParameterTexture(8, 128, "_ParamTex");
private static readonly int k_TransitionTexId = Shader.PropertyToID("_TransitionTex");
private bool _lastKeepAspectRatio;
private EffectArea _lastEffectArea;
private static Texture _defaultTransitionTexture;
[Tooltip("Current location[0-1] for dissolve effect. 0 is not dissolved, 1 is completely dissolved.")]
[FormerlySerializedAs("m_Location")]
[SerializeField]
[Range(0, 1)]
float m_EffectFactor = 0.5f;
[Tooltip("Edge width.")] [SerializeField] [Range(0, 1)]
float m_Width = 0.5f;
[Tooltip("Edge softness.")] [SerializeField] [Range(0, 1)]
float m_Softness = 0.5f;
[Tooltip("Edge color.")] [SerializeField] [ColorUsage(false)]
Color m_Color = new Color(0.0f, 0.25f, 1.0f);
[Tooltip("Edge color effect mode.")] [SerializeField]
ColorMode m_ColorMode = ColorMode.Add;
[Tooltip("Noise texture for dissolving (single channel texture).")]
[SerializeField]
[FormerlySerializedAs("m_NoiseTexture")]
Texture m_TransitionTexture;
[Header("Advanced Option")] [Tooltip("The area for effect.")] [SerializeField]
protected EffectArea m_EffectArea;
[Tooltip("Keep effect aspect ratio.")] [SerializeField]
bool m_KeepAspectRatio;
[Header("Effect Player")] [SerializeField]
EffectPlayer m_Player;
[Tooltip("Reverse the dissolve effect.")] [FormerlySerializedAs("m_ReverseAnimation")] [SerializeField]
bool m_Reverse = false;
/// <summary>
/// Effect factor between 0(start) and 1(end).
/// </summary>
public float effectFactor
{
get { return m_EffectFactor; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_EffectFactor, value)) return;
m_EffectFactor = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge width.
/// </summary>
public float width
{
get { return m_Width; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_Width, value)) return;
m_Width = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge softness.
/// </summary>
public float softness
{
get { return m_Softness; }
set
{
value = Mathf.Clamp(value, 0, 1);
if (Mathf.Approximately(m_Softness, value)) return;
m_Softness = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Edge color.
/// </summary>
public Color color
{
get { return m_Color; }
set
{
if (m_Color == value) return;
m_Color = value;
SetEffectParamsDirty();
}
}
/// <summary>
/// Noise texture.
/// </summary>
public Texture transitionTexture
{
get
{
return m_TransitionTexture
? m_TransitionTexture
: defaultTransitionTexture;
}
set
{
if (m_TransitionTexture == value) return;
m_TransitionTexture = value;
SetMaterialDirty();
}
}
private static Texture defaultTransitionTexture
{
get
{
return _defaultTransitionTexture
? _defaultTransitionTexture
: (_defaultTransitionTexture = Resources.Load<Texture>("Default-Transition"));
}
}
/// <summary>
/// The area for effect.
/// </summary>
public EffectArea effectArea
{
get { return m_EffectArea; }
set
{
if (m_EffectArea == value) return;
m_EffectArea = value;
SetVerticesDirty();
}
}
/// <summary>
/// Keep aspect ratio.
/// </summary>
public bool keepAspectRatio
{
get { return m_KeepAspectRatio; }
set
{
if (m_KeepAspectRatio == value) return;
m_KeepAspectRatio = value;
SetVerticesDirty();
}
}
/// <summary>
/// Color effect mode.
/// </summary>
public ColorMode colorMode
{
get { return m_ColorMode; }
set
{
if (m_ColorMode == value) return;
m_ColorMode = value;
SetMaterialDirty();
}
}
/// <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;
var shaderVariantId = (uint) ((int) m_ColorMode << 6);
var resourceId = (uint) transitionTexture.GetInstanceID();
return new Hash128(
(uint) material.GetInstanceID(),
k_ShaderId + shaderVariantId,
resourceId,
0
);
}
public override void ModifyMaterial(Material newMaterial, Graphic graphic)
{
var connector = BaseConnector.FindConnector(graphic);
newMaterial.shader = connector.FindShader("UIDissolve");
SetShaderVariants(newMaterial, m_ColorMode);
newMaterial.SetTexture(k_TransitionTexId, transitionTexture);
paramTex.RegisterMaterial(newMaterial);
}
/// <summary>
/// Modifies the mesh.
/// </summary>
public override void ModifyMesh(VertexHelper vh, Graphic graphic)
{
if (!isActiveAndEnabled)
return;
// bool isText = isTMPro || graphic is Text;
var normalizedIndex = paramTex.GetNormalizedIndex(this);
// rect.
var tex = transitionTexture;
var aspectRatio = m_KeepAspectRatio && tex ? ((float) tex.width) / tex.height : -1;
var rect = m_EffectArea.GetEffectArea(vh, rectTransform.rect, aspectRatio);
// Calculate vertex position.
var vertex = default(UIVertex);
var count = vh.currentVertCount;
for (var i = 0; i < count; i++)
{
vh.PopulateUIVertex(ref vertex, i);
float x;
float y;
connector.GetPositionFactor(m_EffectArea, i, rect, vertex.position, out x, out y);
vertex.uv0 = new Vector2(
Packer.ToFloat(vertex.uv0.x, vertex.uv0.y),
Packer.ToFloat(x, y, normalizedIndex)
);
vh.SetUIVertex(vertex, i);
}
}
protected override void SetEffectParamsDirty()
{
paramTex.SetData(this, 0, m_EffectFactor); // param1.x : location
paramTex.SetData(this, 1, m_Width); // param1.y : width
paramTex.SetData(this, 2, m_Softness); // param1.z : softness
paramTex.SetData(this, 4, m_Color.r); // param2.x : red
paramTex.SetData(this, 5, m_Color.g); // param2.y : green
paramTex.SetData(this, 6, m_Color.b); // param2.z : blue
}
protected override void SetVerticesDirty()
{
base.SetVerticesDirty();
_lastKeepAspectRatio = m_KeepAspectRatio;
_lastEffectArea = m_EffectArea;
}
protected override void OnDidApplyAnimationProperties()
{
base.OnDidApplyAnimationProperties();
if (_lastKeepAspectRatio != m_KeepAspectRatio
|| _lastEffectArea != m_EffectArea)
SetVerticesDirty();
}
/// <summary>
/// Play effect.
/// </summary>
public void Play(bool reset = true)
{
_player.Play(reset);
}
/// <summary>
/// Stop effect.
/// </summary>
public void Stop(bool reset = true)
{
_player.Stop(reset);
}
protected override void OnEnable()
{
base.OnEnable();
_player.OnEnable((f) => effectFactor = m_Reverse ? 1f - f : f);
}
protected override void OnDisable()
{
base.OnDisable();
_player.OnDisable();
}
EffectPlayer _player
{
get { return m_Player ?? (m_Player = new EffectPlayer()); }
}
}
}