diff --git a/Assets/Coffee.meta b/Assets/Coffee.meta index 36461ece..154c0043 100644 --- a/Assets/Coffee.meta +++ b/Assets/Coffee.meta @@ -1,9 +1,8 @@ fileFormatVersion: 2 guid: b3793e133930146e397ceaa2825b0e59 folderAsset: yes -timeCreated: 1527743850 -licenseType: Pro DefaultImporter: + externalObjects: {} userData: assetBundleName: assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts.meta index d4dca943..8397f4c7 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts.meta +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts.meta @@ -1,9 +1,8 @@ fileFormatVersion: 2 guid: a75d49073646347b1955a9f9df36cc45 folderAsset: yes -timeCreated: 1527743733 -licenseType: Pro DefaultImporter: + externalObjects: {} userData: assetBundleName: assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMaterialEffect.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMaterialEffect.cs new file mode 100644 index 00000000..9e195d5a --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMaterialEffect.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using UnityEngine; +using UnityEngine.UI; + +namespace Coffee.UIExtensions +{ + /// + /// Abstract effect base for UI. + /// + [DisallowMultipleComponent] + public abstract class BaseMaterialEffect : BaseMeshEffect, IParameterTexture, IMaterialModifier + { + protected static readonly Hash128 k_InvalidHash = new Hash128(); + protected static readonly List s_TempVerts = new List(); + private static readonly StringBuilder s_StringBuilder = new StringBuilder(); + + Hash128 _effectMaterialHash; + + /// + /// Gets or sets the parameter index. + /// + public int parameterIndex { get; set; } + + /// + /// Gets the parameter texture. + /// + public virtual ParameterTexture paramTex + { + get { return null; } + } + + /// + /// Mark the vertices as dirty. + /// + protected void SetMaterialDirty() + { + connector.SetMaterialDirty(graphic); + } + + public virtual Hash128 GetMaterialHash(Material baseMaterial) + { + return k_InvalidHash; + } + + public virtual Material GetModifiedMaterial(Material baseMaterial) + { + if (!isActiveAndEnabled) return baseMaterial; + + var oldHash = _effectMaterialHash; + _effectMaterialHash = GetMaterialHash(baseMaterial); + var modifiedMaterial = baseMaterial; + if (_effectMaterialHash.isValid) + { + modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, ModifyMaterial); + } + + MaterialCache.Unregister(oldHash); + + return modifiedMaterial; + } + + // protected bool isTMProMobile (Material material) + // { + // return material && material.shader && material.shader.name.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal); + // } + + public virtual void ModifyMaterial(Material newMaterial) + { + if (isActiveAndEnabled && paramTex != null) + paramTex.RegisterMaterial(newMaterial); + } + + protected void SetShaderVariants(Material newMaterial, params object[] variants) + { + // Set shader keywords as variants + var keywords = variants.Where(x => 0 < (int) x) + .Select(x => x.ToString().ToUpper()) + .Concat(newMaterial.shaderKeywords) + .Distinct() + .ToArray(); + newMaterial.shaderKeywords = keywords; + + // Add variant name + s_StringBuilder.Length = 0; + s_StringBuilder.Append(Path.GetFileName(newMaterial.shader.name)); + foreach (var keyword in keywords) + { + s_StringBuilder.Append("-"); + s_StringBuilder.Append(keyword); + } + + newMaterial.name = s_StringBuilder.ToString(); + } + +#if UNITY_EDITOR + protected override void Reset() + { + if (!isActiveAndEnabled) return; + SetMaterialDirty(); + SetVerticesDirty(); + SetEffectParamsDirty(); + } + + protected override void OnValidate() + { + if (!isActiveAndEnabled) return; + SetVerticesDirty(); + SetEffectParamsDirty(); + } +#endif + + /// + /// This function is called when the object becomes enabled and active. + /// + protected override void OnEnable() + { + if (paramTex != null) + { + paramTex.Register(this); + } + + SetVerticesDirty(); + SetMaterialDirty(); + SetEffectParamsDirty(); + + // foreach (var mr in GetComponentsInChildren ()) + // { + // mr.GetComponent ().SetMaterialDirty (); + // mr.GetComponent ().SetVerticesDirty (); + // } + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + protected override void OnDisable() + { + SetVerticesDirty(); + SetMaterialDirty(); + + if (paramTex != null) + { + paramTex.Unregister(this); + } + + MaterialCache.Unregister(_effectMaterialHash); + _effectMaterialHash = k_InvalidHash; + } + + // protected override void OnDidApplyAnimationProperties() + // { + // SetEffectParamsDirty(); + // } + + // protected override void OnTextChanged (UnityEngine.Object obj) + // { + // base.OnTextChanged (obj); + // + // + // foreach (var sm in GetComponentsInChildren ()) + // { + // if(!sm.GetComponent()) + // { + // var mr = sm.gameObject.AddComponent (); + // + // targetGraphic.SetAllDirty (); + // //targetGraphic.SetVerticesDirty (); + // + // //mr.GetComponent ().SetMaterialDirty (); + // //mr.GetComponent ().SetVerticesDirty (); + // + // + // } + // } + // } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMaterialEffect.cs.meta similarity index 100% rename from Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs.meta rename to Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMaterialEffect.cs.meta diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMeshEffect.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMeshEffect.cs index edb9adce..e7d8be5d 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMeshEffect.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BaseMeshEffect.cs @@ -2,564 +2,224 @@ using UnityEngine.EventSystems; using UnityEngine.UI; -#if TMP_PRESENT -using System.Collections.Generic; -using TMPro; -#if UNITY_EDITOR -using UnityEditor; -using System.IO; -using System.Linq; -#endif -#endif - namespace Coffee.UIExtensions { - /// - /// Base class for effects that modify the generated Mesh. - /// It works well not only for standard Graphic components (Image, RawImage, Text, etc.) but also for TextMeshPro and TextMeshProUGUI. - /// - [ExecuteInEditMode] - public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier - { - //################################ - // Constant or Static Members. - //################################ -#if TMP_PRESENT - static readonly List s_Uv0 = new List (4096); - static readonly List s_Uv1 = new List (4096); - #if UNITY_2017_1_OR_NEWER - static readonly List s_Uv2 = new List (4096); - static readonly List s_Uv3 = new List (4096); - #endif - static readonly List s_Vertices = new List (4096); - static readonly List s_Indices = new List (4096); - static readonly List s_Normals = new List (4096); - static readonly List s_Tangents = new List (4096); - static readonly List s_Colors = new List (4096); - static readonly VertexHelper s_VertexHelper = new VertexHelper (); - static readonly List s_SubMeshUIs = new List (4096); - static readonly List s_Meshes = new List (4096); - static readonly List s_UIVertices = new List (4096); - static readonly List s_TmpEffects = new List(4); -#endif - static readonly Material [] s_EmptyMaterials = new Material [0]; - - - //################################ - // Public Members. - //################################ - /// - /// The Graphic attached to this GameObject. - /// - public Graphic graphic { get { Initialize (); return _graphic; } } - - /// - /// The CanvasRenderer attached to this GameObject. - /// - public CanvasRenderer canvasRenderer { get { Initialize (); return _canvasRenderer; } } - -#if TMP_PRESENT - /// - /// The TMP_Text attached to this GameObject. - /// - public TMP_Text textMeshPro { get { Initialize (); return _textMeshPro; } } -#endif - - /// - /// The RectTransform attached to this GameObject. - /// - public RectTransform rectTransform { get { Initialize (); return _rectTransform; } } - -#if UNITY_5_6_OR_NEWER - /// - /// Additional canvas shader channels to use this component. - /// - public virtual AdditionalCanvasShaderChannels requiredChannels { get { return AdditionalCanvasShaderChannels.None; } } -#endif - - /// - /// Is TextMeshPro or TextMeshProUGUI attached to this GameObject? - /// - public bool isTMPro - { - get - { -#if TMP_PRESENT - return textMeshPro != null; -#else - return false; -#endif - } - } - - /// - /// The material for rendering. - /// - public virtual Material material - { - get - { - -#if TMP_PRESENT - if (textMeshPro) - { - return textMeshPro.fontSharedMaterial; - } - else -#endif - if (graphic) - { - return graphic.material; - } - else - { - return null; - } - } - set - { -#if TMP_PRESENT - if (textMeshPro) - { - textMeshPro.fontMaterial = value; - } - else -#endif - if (graphic) - { - graphic.material = value; - } - } - } - - public virtual Material[] materials - { - get - { - -#if TMP_PRESENT - if (textMeshPro) - { - return textMeshPro.fontSharedMaterials ?? s_EmptyMaterials; - } - else -#endif - if (graphic) - { - _materials [0] = graphic.material; - return _materials; - } - else - { - return s_EmptyMaterials; - } - } - } - - /// - /// Call used to modify mesh. (legacy) - /// - /// Mesh. - public virtual void ModifyMesh (Mesh mesh) - { - } - - /// - /// Call used to modify mesh. - /// - /// VertexHelper. - public virtual void ModifyMesh (VertexHelper vh) - { - } - - /// - /// Mark the vertices as dirty. - /// - public virtual void SetVerticesDirty () - { -#if TMP_PRESENT - if (textMeshPro) - { - foreach (var info in textMeshPro.textInfo.meshInfo) - { - var mesh = info.mesh; - if (mesh) - { - mesh.Clear (); - mesh.vertices = info.vertices; - mesh.uv = info.uvs0; - mesh.uv2 = info.uvs2; - mesh.colors32 = info.colors32; - mesh.normals = info.normals; - mesh.tangents = info.tangents; - mesh.triangles = info.triangles; - } - } - - if (canvasRenderer) - { - canvasRenderer.SetMesh (textMeshPro.mesh); - - GetComponentsInChildren (false, s_SubMeshUIs); - foreach (var sm in s_SubMeshUIs) - { - sm.canvasRenderer.SetMesh (sm.mesh); - } - s_SubMeshUIs.Clear (); - } - textMeshPro.havePropertiesChanged = true; - } - else -#endif - if (graphic) - { - graphic.SetVerticesDirty (); - } - } - - public void ShowTMProWarning (Shader shader, Shader mobileShader, Shader spriteShader, System.Action onCreatedMaterial) - { -#if UNITY_EDITOR && TMP_PRESENT - if(!textMeshPro || !textMeshPro.fontSharedMaterial) - { - return; - } - - // Is the material preset for dissolve? - Material m = textMeshPro.fontSharedMaterial; - if (m.shader != shader && m.shader != mobileShader) - { - EditorGUILayout.BeginHorizontal (); - EditorGUILayout.HelpBox (string.Format("{0} requires '{1}' or '{2}' as a shader for material preset.", GetType().Name, shader.name, mobileShader.name), MessageType.Warning); - if(GUILayout.Button ("Fix")) - { - var correctShader = m.shader.name.Contains ("Mobile") ? mobileShader : shader; - textMeshPro.fontSharedMaterial = ModifyTMProMaterialPreset (m, correctShader, onCreatedMaterial); - } - EditorGUILayout.EndHorizontal (); - return; - } - - // Is the sprite asset for dissolve? - TMP_SpriteAsset spriteAsset = textMeshPro.spriteAsset ?? TMP_Settings.GetSpriteAsset (); - // Sprite asset might not exist at all - if(spriteAsset == null) { - return; - } - - m = spriteAsset.material; - if (m && m.shader != spriteShader && textMeshPro.richText && textMeshPro.text.Contains(" ().Select (x => x.gameObject).ToList ().ForEach (DestroyImmediate); - GetComponentsInChildren ().Select (x => x.gameObject).ToList ().ForEach (DestroyImmediate); - textMeshPro.spriteAsset = ModifyTMProSpriteAsset (m, spriteShader, onCreatedMaterial); - } - EditorGUILayout.EndHorizontal (); - return; - } - } - - Material ModifyTMProMaterialPreset (Material baseMaterial, Shader shader, System.Action onCreatedMaterial) - { - string path = AssetDatabase.GetAssetPath (baseMaterial); - string filename = Path.GetFileNameWithoutExtension (path) + " (" + GetType ().Name + ")"; - Material mat = Resources.Load (TMP_Settings.defaultFontAssetPath + filename); - if (!mat) - { - mat = new Material (baseMaterial) - { - shaderKeywords = baseMaterial.shaderKeywords, - shader = shader, - }; - onCreatedMaterial (mat); - AssetDatabase.CreateAsset (mat, Path.GetDirectoryName (path) + "/" + filename + ".mat"); - - EditorUtility.FocusProjectWindow (); - EditorGUIUtility.PingObject (mat); - } - else - { - mat.shader = shader; - } - EditorUtility.SetDirty (mat); - return mat; - } - - TMP_SpriteAsset ModifyTMProSpriteAsset (Material baseMaterial, Shader shader, System.Action onCreatedMaterial) - { - string path = AssetDatabase.GetAssetPath (baseMaterial); - string filename = Path.GetFileNameWithoutExtension (path) + " (" + this.GetType ().Name + ")"; - TMP_SpriteAsset spriteAsset = Resources.Load (TMP_Settings.defaultSpriteAssetPath + filename); - if (!spriteAsset) - { - AssetDatabase.CopyAsset (path, Path.GetDirectoryName (path) + "/" + filename + ".mat"); - spriteAsset = Resources.Load (TMP_Settings.defaultSpriteAssetPath + filename); - spriteAsset.material.shader = shader; - spriteAsset.material.name = shader.name; - onCreatedMaterial (spriteAsset.material); - - EditorUtility.FocusProjectWindow (); - EditorGUIUtility.PingObject (spriteAsset); - } - else - { - spriteAsset.material.shader = shader; - } - EditorUtility.SetDirty (spriteAsset); - return spriteAsset; -#endif - } - - - //################################ - // Protected Members. - //################################ - /// - /// Should the effect modify the mesh directly for TMPro? - /// - protected virtual bool isLegacyMeshModifier { get { return false; } } - - - protected virtual void Initialize () - { - if (!_initialized) - { - _initialized = true; - _graphic = _graphic ?? GetComponent (); - _canvasRenderer = _canvasRenderer ?? GetComponent (); - _rectTransform = _rectTransform ?? GetComponent (); -#if TMP_PRESENT - _textMeshPro = _textMeshPro ?? GetComponent (); -#endif - } - } - - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable () - { - _initialized = false; - SetVerticesDirty (); -#if TMP_PRESENT - if (textMeshPro) - { - TMPro_EventManager.TEXT_CHANGED_EVENT.Add (OnTextChanged); - } -#endif - -#if UNITY_EDITOR && TMP_PRESENT - if (graphic && textMeshPro) - { - GraphicRebuildTracker.TrackGraphic (graphic); - } -#endif - -#if UNITY_5_6_OR_NEWER - if (graphic) - { - AdditionalCanvasShaderChannels channels = requiredChannels; - var canvas = graphic.canvas; - if (canvas && (canvas.additionalShaderChannels & channels) != channels) - { - Debug.LogWarningFormat (this, "Enable {1} of Canvas.additionalShaderChannels to use {0}.", GetType ().Name, channels); - } - } -#endif - } - - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - protected override void OnDisable () - { -#if TMP_PRESENT - TMPro_EventManager.TEXT_CHANGED_EVENT.Remove (OnTextChanged); -#endif - SetVerticesDirty (); - -#if UNITY_EDITOR && TMP_PRESENT - if (graphic && textMeshPro) - { - GraphicRebuildTracker.UnTrackGraphic (graphic); - } -#endif - } - - - /// - /// LateUpdate is called every frame, if the Behaviour is enabled. - /// - protected virtual void LateUpdate () - { -#if TMP_PRESENT - if (textMeshPro) - { - if (textMeshPro.havePropertiesChanged || _isTextMeshProActive != textMeshPro.isActiveAndEnabled) - { - SetVerticesDirty (); - } - _isTextMeshProActive = textMeshPro.isActiveAndEnabled; - } -#endif - } - - /// - /// Callback for when properties have been changed by animation. - /// - protected override void OnDidApplyAnimationProperties () - { - SetVerticesDirty (); - } + /// + /// Base class for effects that modify the generated Mesh. + /// It works well not only for standard Graphic components (Image, RawImage, Text, etc.) but also for TextMeshPro and TextMeshProUGUI. + /// + [RequireComponent(typeof(Graphic))] + [RequireComponent(typeof(RectTransform))] + [ExecuteInEditMode] + public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier + { + RectTransform _rectTransform; + Graphic _graphic; + BaseConnector _connector; + + /// + /// The Graphic attached to this GameObject. + /// + protected BaseConnector connector + { + get { return _connector ?? (_connector = BaseConnector.FindConnector(graphic)); } + } + + /// + /// The Graphic attached to this GameObject. + /// + public Graphic graphic + { + get { return _graphic ? _graphic : _graphic = GetComponent(); } + } + + /// + /// The RectTransform attached to this GameObject. + /// + protected RectTransform rectTransform + { + get { return _rectTransform ? _rectTransform : _rectTransform = GetComponent(); } + } + + /// + /// The material for rendering. + /// + // public virtual Material material + // { + // get { return connector.GetMaterial(graphic); } + // set { connector.SetMaterial(graphic, value); } + // } + + /// + /// Call used to modify mesh. (legacy) + /// + /// Mesh. + public virtual void ModifyMesh(Mesh mesh) + { + } + + /// + /// Call used to modify mesh. + /// + /// VertexHelper. + public virtual void ModifyMesh(VertexHelper vh) + { + } + + /// + /// Mark the vertices as dirty. + /// + protected virtual void SetVerticesDirty() + { + connector.SetVerticesDirty(graphic); + +// #if TMP_PRESENT +// if (textMeshPro) +// { +// foreach (var info in textMeshPro.textInfo.meshInfo) +// { +// var mesh = info.mesh; +// if (mesh) +// { +// mesh.Clear(); +// mesh.vertices = info.vertices; +// mesh.uv = info.uvs0; +// mesh.uv2 = info.uvs2; +// mesh.colors32 = info.colors32; +// mesh.normals = info.normals; +// mesh.tangents = info.tangents; +// mesh.triangles = info.triangles; +// } +// } +// +// if (canvasRenderer) +// { +// canvasRenderer.SetMesh(textMeshPro.mesh); +// +// GetComponentsInChildren(false, s_SubMeshUIs); +// foreach (var sm in s_SubMeshUIs) +// { +// sm.canvasRenderer.SetMesh(sm.mesh); +// } +// +// s_SubMeshUIs.Clear(); +// } +// +// textMeshPro.havePropertiesChanged = true; +// } +// else +// #endif +// if (graphic) +// { +// graphic.SetVerticesDirty(); +// } + } + + + //################################ + // Protected Members. + //################################ + /// + /// Should the effect modify the mesh directly for TMPro? + /// + // protected virtual bool isLegacyMeshModifier + // { + // get { return false; } + // } +// protected virtual void Initialize() +// { +// if (_initialized) return; +// +// _initialized = true; +// _graphic = _graphic ? _graphic : GetComponent(); +// +// _connector = BaseConnector.FindConnector(_graphic); +// +// // _canvasRenderer = _canvasRenderer ?? GetComponent (); +// _rectTransform = _rectTransform ? _rectTransform : GetComponent(); +// // #if TMP_PRESENT +// // _textMeshPro = _textMeshPro ?? GetComponent (); +// // #endif +// } + + /// + /// This function is called when the object becomes enabled and active. + /// + protected override void OnEnable() + { + connector.OnEnable(graphic); + SetVerticesDirty(); + + // SetVerticesDirty(); +// #if TMP_PRESENT + // if (textMeshPro) + // { + // TMPro_EventManager.TEXT_CHANGED_EVENT.Add (OnTextChanged); + // } + // #endif + // + // #if UNITY_EDITOR && TMP_PRESENT + // if (graphic && textMeshPro) + // { + // GraphicRebuildTracker.TrackGraphic (graphic); + // } + // #endif + // + // #if UNITY_5_6_OR_NEWER + // if (graphic) + // { + // AdditionalCanvasShaderChannels channels = requiredChannels; + // var canvas = graphic.canvas; + // if (canvas && (canvas.additionalShaderChannels & channels) != channels) + // { + // Debug.LogWarningFormat (this, "Enable {1} of Canvas.additionalShaderChannels to use {0}.", GetType ().Name, channels); + // } + // } + // #endif + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + protected override void OnDisable() + { + connector.OnDisable(graphic); + SetVerticesDirty(); + } + + /// + /// Mark the effect parameters as dirty. + /// + protected virtual void SetEffectParamsDirty() + { + if (!isActiveAndEnabled) return; + SetVerticesDirty(); + } + + /// + /// Callback for when properties have been changed by animation. + /// + protected override void OnDidApplyAnimationProperties() + { + if (!isActiveAndEnabled) return; + SetEffectParamsDirty(); + } #if UNITY_EDITOR - /// - /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). - /// - protected override void OnValidate () - { - SetVerticesDirty (); - } -#endif - - - //################################ - // Private Members. - //################################ - bool _initialized; - CanvasRenderer _canvasRenderer; - RectTransform _rectTransform; - Graphic _graphic; - Material [] _materials = new Material [1]; - -#if TMP_PRESENT - bool _isTextMeshProActive; - TMP_Text _textMeshPro; - - /// - /// Called when any TextMeshPro generated the mesh. - /// - /// TextMeshPro object. - protected virtual void OnTextChanged (Object obj) - { - // Skip if the object is different from the current object or the text is empty. - var textInfo = textMeshPro.textInfo; - if (textMeshPro != obj || textInfo.characterCount - textInfo.spaceCount <= 0) - { - return; - } - - GetComponents(s_TmpEffects); - for (int i = 0; i < s_TmpEffects.Count; i++) - { - if (s_TmpEffects[i].enabled) - { - if (s_TmpEffects[i] == this) - break; - else - return; - } - } - - // Collect the meshes. - s_Meshes.Clear (); - foreach (var info in textInfo.meshInfo) - { - s_Meshes.Add (info.mesh); - } - - // Modify the meshes. - foreach (var e in s_TmpEffects) - { - if (!e.enabled) - continue; - - if (e.isLegacyMeshModifier) - { - // Legacy mode: Modify the meshes directly. - foreach (var m in s_Meshes) - { - if (m) - { - e.ModifyMesh(m); - } - } - } - else - { - // Convert meshes to VertexHelpers and modify them. - foreach (var m in s_Meshes) - { - if (m) - { - FillVertexHelper(s_VertexHelper, m); - e.ModifyMesh(s_VertexHelper); - s_VertexHelper.FillMesh(m); - } - } - } - } - - // Set the modified meshes to the CanvasRenderers (for UI only). - if (canvasRenderer) - { - canvasRenderer.SetMesh (textMeshPro.mesh); - GetComponentsInChildren (false, s_SubMeshUIs); - foreach (var sm in s_SubMeshUIs) - { - sm.canvasRenderer.SetMesh (sm.mesh); - } - s_SubMeshUIs.Clear (); - } - - // Clear. - s_Meshes.Clear (); - } - - void FillVertexHelper (VertexHelper vh, Mesh mesh) - { - vh.Clear (); - - mesh.GetVertices (s_Vertices); - mesh.GetColors (s_Colors); - mesh.GetUVs (0, s_Uv0); - mesh.GetUVs (1, s_Uv1); - mesh.GetNormals (s_Normals); - mesh.GetTangents (s_Tangents); - mesh.GetIndices (s_Indices, 0); - - #if UNITY_2017_1_OR_NEWER - mesh.GetUVs (2, s_Uv2); - mesh.GetUVs (3, s_Uv3); - bool useUv2 = 0 < s_Uv2.Count; - bool useUv3 = 0 < s_Uv3.Count; - #endif - - s_UIVertices.Clear(); - UIVertex v = default(UIVertex); - for (int i = 0; i < s_Vertices.Count; i++) - { - v.position = s_Vertices[i]; - v.color = s_Colors[i]; - v.uv0 = s_Uv0[i]; - v.uv1 = s_Uv1[i]; - #if UNITY_2017_1_OR_NEWER - if (useUv2 && i < s_Uv2.Count) - v.uv2 = s_Uv2[i]; - if (useUv3 && i < s_Uv3.Count) - v.uv3 = s_Uv3[i]; - #endif - v.normal = s_Normals[i]; - v.tangent = s_Tangents[i]; - - s_UIVertices.Add(v); - } - s_VertexHelper.AddUIVertexStream(s_UIVertices, s_Indices); - } -#endif - } -} \ No newline at end of file + protected override void Reset() + { + if (!isActiveAndEnabled) return; + SetVerticesDirty(); + } + + /// + /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). + /// + protected override void OnValidate() + { + if (!isActiveAndEnabled) return; + SetEffectParamsDirty(); + } +#endif + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectPlayer.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectPlayer.cs index f2b11de4..ad2376f0 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectPlayer.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/EffectPlayer.cs @@ -4,153 +4,151 @@ namespace Coffee.UIExtensions { - /// - /// Effect player. - /// - [Serializable] - public class EffectPlayer - { - //################################ - // Public Members. - //################################ - /// - /// Gets or sets a value indicating whether is playing. - /// - [Header("Effect Player")] - [Tooltip("Playing.")] - public bool play = false; - - /// - /// Gets or sets the delay before looping. - /// - [Tooltip("Initial play delay.")] - [Range(0f, 10f)] - public float initialPlayDelay = 0; - - /// - /// Gets or sets the duration. - /// - [Tooltip("Duration.")] - [Range(0.01f,10f)] - public float duration = 1; - - /// - /// Gets or sets a value indicating whether can loop. - /// - [Tooltip("Loop.")] - public bool loop = false; - - /// - /// Gets or sets the delay before looping. - /// - [Tooltip("Delay before looping.")] - [Range(0f,10f)] - public float loopDelay = 0; - - /// - /// Gets or sets the update mode. - /// - [Tooltip("Update mode")] - public AnimatorUpdateMode updateMode = AnimatorUpdateMode.Normal; - - static List s_UpdateActions; - - /// - /// Register player. - /// - public void OnEnable(Action callback = null) - { - - if (s_UpdateActions == null) - { - s_UpdateActions = new List(); - Canvas.willRenderCanvases += () => - { - var count = s_UpdateActions.Count; - for (int i = 0; i < count; i++) - { - s_UpdateActions[i].Invoke(); - } - }; - } - s_UpdateActions.Add(OnWillRenderCanvases); - - if (play) - { - _time = -initialPlayDelay; - } - else - { - _time = 0; - } - _callback = callback; - } - - /// - /// Unregister player. - /// - public void OnDisable() - { - _callback = null; - s_UpdateActions.Remove(OnWillRenderCanvases); - } - - /// - /// Start playing. - /// - public void Play(bool reset, Action callback = null) - { - if (reset) - { - _time = 0; - } - play = true; - if (callback != null) - { - _callback = callback; - } - } - - /// - /// Stop playing. - /// - public void Stop(bool reset) - { - if (reset) - { - _time = 0; - if (_callback != null) - { - _callback(_time); - } - } - play = false; - } - - //################################ - // Private Members. - //################################ - float _time = 0; - Action _callback; - - void OnWillRenderCanvases() - { - if (!play || !Application.isPlaying || _callback == null) - { - return; - } - - _time += updateMode == AnimatorUpdateMode.UnscaledTime - ? Time.unscaledDeltaTime - : Time.deltaTime; - var current = _time / duration; - - if (duration <= _time) - { - play = loop; - _time = loop ? -loopDelay : 0; - } - _callback(current); - } - } -} \ No newline at end of file + /// + /// Effect player. + /// + [Serializable] + public class EffectPlayer + { + //################################ + // Public Members. + //################################ + /// + /// Gets or sets a value indicating whether is playing. + /// + [Header("Effect Player")] [Tooltip("Playing.")] + public bool play = false; + + /// + /// Gets or sets the delay before looping. + /// + [Tooltip("Initial play delay.")] [Range(0f, 10f)] + public float initialPlayDelay = 0; + + /// + /// Gets or sets the duration. + /// + [Tooltip("Duration.")] [Range(0.01f, 10f)] + public float duration = 1; + + /// + /// Gets or sets a value indicating whether can loop. + /// + [Tooltip("Loop.")] public bool loop = false; + + /// + /// Gets or sets the delay before looping. + /// + [Tooltip("Delay before looping.")] [Range(0f, 10f)] + public float loopDelay = 0; + + /// + /// Gets or sets the update mode. + /// + [Tooltip("Update mode")] public AnimatorUpdateMode updateMode = AnimatorUpdateMode.Normal; + + static List s_UpdateActions; + + /// + /// Register player. + /// + public void OnEnable(Action callback = null) + { + if (s_UpdateActions == null) + { + s_UpdateActions = new List(); + Canvas.willRenderCanvases += () => + { + var count = s_UpdateActions.Count; + for (int i = 0; i < count; i++) + { + s_UpdateActions[i].Invoke(); + } + }; + } + + s_UpdateActions.Add(OnWillRenderCanvases); + + if (play) + { + _time = -initialPlayDelay; + } + else + { + _time = 0; + } + + _callback = callback; + } + + /// + /// Unregister player. + /// + public void OnDisable() + { + _callback = null; + s_UpdateActions.Remove(OnWillRenderCanvases); + } + + /// + /// Start playing. + /// + public void Play(bool reset, Action callback = null) + { + if (reset) + { + _time = 0; + } + + play = true; + if (callback != null) + { + _callback = callback; + } + } + + /// + /// Stop playing. + /// + public void Stop(bool reset) + { + if (reset) + { + _time = 0; + if (_callback != null) + { + _callback(_time); + } + } + + play = false; + } + + //################################ + // Private Members. + //################################ + float _time = 0; + Action _callback; + + void OnWillRenderCanvases() + { + if (!play || !Application.isPlaying || _callback == null) + { + return; + } + + _time += updateMode == AnimatorUpdateMode.UnscaledTime + ? Time.unscaledDeltaTime + : Time.deltaTime; + var current = _time / duration; + + if (duration <= _time) + { + play = loop; + _time = loop ? -loopDelay : 0; + } + + _callback(current); + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialCache.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialCache.cs index 7a235df0..5f2ffc5b 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialCache.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialCache.cs @@ -6,9 +6,11 @@ namespace Coffee.UIExtensions { - public class MaterialRepository + public class MaterialCache { - class MaterialEntry + static Dictionary materialMap = new Dictionary(); + + private class MaterialEntry { public Material material; public int referenceCount; @@ -19,43 +21,41 @@ public void Release() { UnityEngine.Object.DestroyImmediate(material, false); } + material = null; } } #if UNITY_EDITOR [UnityEditor.InitializeOnLoadMethod] - static void ClearCache() + private static void ClearCache() { foreach (var entry in materialMap.Values) { entry.Release(); } + materialMap.Clear(); } #endif - static Dictionary materialMap = new Dictionary(); - - public static Material Register(Material material, Hash128 hash, System.Action onModifyMaterial) + public static Material Register(Material baseMaterial, Hash128 hash, System.Action onModifyMaterial) { - if(!hash.isValid) - return null; + if (!hash.isValid) return null; MaterialEntry entry; if (!materialMap.TryGetValue(hash, out entry)) { entry = new MaterialEntry() { - material = new Material(material) + material = new Material(baseMaterial) { - // hideFlags = HideFlags.HideAndDontSave, + hideFlags = HideFlags.HideAndDontSave, }, }; onModifyMaterial(entry.material); materialMap.Add(hash, entry); - Debug.LogFormat($"Register {hash} {entry.material}"); } entry.referenceCount++; @@ -65,110 +65,11 @@ public static Material Register(Material material, Hash128 hash, System.Action 0) return; - - - - public class MaterialCache - { - public ulong hash { get; private set; } - - public int referenceCount { get; private set; } - - public Texture texture { get; private set; } - - public Material material { get; private set; } - -#if UNITY_EDITOR - [UnityEditor.InitializeOnLoadMethod] - static void ClearCache() - { - foreach (var cache in materialCaches) - { - cache.material = null; - } - materialCaches.Clear(); - } -#endif - - public static List materialCaches = new List(); - - public static MaterialCache Register(ulong hash, Texture texture, System.Func onCreateMaterial) - { - var cache = materialCaches.FirstOrDefault(x => x.hash == hash); - if (cache != null && cache.material) - { - if (cache.material) - { - cache.referenceCount++; - } - else - { - - materialCaches.Remove(cache); - cache = null; - } - } - if (cache == null) - { - cache = new MaterialCache() - { - hash = hash, - material = onCreateMaterial(), - referenceCount = 1, - }; - materialCaches.Add(cache); - } - return cache; - } - - public static MaterialCache Register(ulong hash, System.Func onCreateMaterial) - { - var cache = materialCaches.FirstOrDefault(x => x.hash == hash); - if (cache != null) - { - cache.referenceCount++; - } - if (cache == null) - { - cache = new MaterialCache() - { - hash = hash, - material = onCreateMaterial(), - referenceCount = 1, - }; - materialCaches.Add(cache); - } - return cache; - } - - public static void Unregister(MaterialCache cache) - { - if (cache == null) - { - return; - } - - cache.referenceCount--; - if (cache.referenceCount <= 0) - { - MaterialCache.materialCaches.Remove(cache); - cache.material = null; - } + entry.Release(); + materialMap.Remove(hash); } } -} \ No newline at end of file +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs deleted file mode 100644 index 8f6cadb8..00000000 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs +++ /dev/null @@ -1,126 +0,0 @@ -#if UNITY_EDITOR -using System.IO; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEditor; -using UnityEngine; - -namespace Coffee.UIExtensions -{ - public class MaterialResolver - { - static readonly StringBuilder s_StringBuilder = new StringBuilder(); - - static readonly Dictionary s_MaterialMap = new Dictionary(); - - public static Material GetOrGenerateMaterialVariant(Shader shader, params object[] append) - { - if (!shader) - { - return null; - } - - Material mat = null; - string variantName = GetVariantName(shader, append); - if (s_MaterialMap.TryGetValue(variantName, out mat) && mat) - { - return mat; - } - - string[] keywords = append.Where(x => 0 < (int)x) - .Select(x => x.ToString().ToUpper()) - .ToArray(); - mat = GetMaterial(shader, append); - if (mat) - { - if (!mat.shaderKeywords.OrderBy(x => x).SequenceEqual(keywords.OrderBy(x => x))) - { - mat.shaderKeywords = keywords; - EditorUtility.SetDirty(mat); - if (!Application.isPlaying) - { - EditorApplication.delayCall += AssetDatabase.SaveAssets; - } - } - return mat; - } - - if (s_MaterialMap.TryGetValue(variantName, out mat) && mat) - { - return mat; - } - - Debug.Log("Generate material : " + variantName); - mat = new Material(shader); - mat.shaderKeywords = keywords; - - mat.name = variantName; - mat.hideFlags |= HideFlags.NotEditable; - s_MaterialMap[variantName] = mat; - - bool isMainAsset = append.Cast().All(x => x == 0); - EditorApplication.delayCall += () => SaveMaterial(mat, shader, isMainAsset); - return mat; - } - - static void SaveMaterial(Material mat, Shader shader, bool isMainAsset) - { - string materialPath = GetDefaultMaterialPath(shader); - -#if UIEFFECT_SEPARATE - string dir = Path.GetDirectoryName(materialPath); - materialPath = Path.Combine(Path.Combine(dir, "Separated"), mat.name + ".mat"); - isMainAsset = true; -#endif - if (isMainAsset) - { - Directory.CreateDirectory(Path.GetDirectoryName(materialPath)); - AssetDatabase.CreateAsset(mat, materialPath); - } - else - { - GetOrGenerateMaterialVariant(shader); - mat.hideFlags |= HideFlags.HideInHierarchy; - AssetDatabase.AddObjectToAsset(mat, materialPath); - } - AssetDatabase.SaveAssets(); - } - - public static Material GetMaterial(Shader shader, params object[] append) - { - string variantName = GetVariantName(shader, append); - return AssetDatabase.FindAssets("t:Material " + Path.GetFileName(shader.name)) - .Select(x => AssetDatabase.GUIDToAssetPath(x)) - .SelectMany(x => AssetDatabase.LoadAllAssetsAtPath(x)) - .OfType() - .FirstOrDefault(x => x.name == variantName); - } - - public static string GetDefaultMaterialPath(Shader shader) - { - var name = Path.GetFileName(shader.name); - return AssetDatabase.FindAssets("t:Material " + name) - .Select(x => AssetDatabase.GUIDToAssetPath(x)) - .FirstOrDefault(x => Path.GetFileNameWithoutExtension(x) == name) - ?? ("Assets/" + name + ".mat"); - } - - public static string GetVariantName(Shader shader, params object[] append) - { - s_StringBuilder.Length = 0; - -#if UIEFFECT_SEPARATE - s_StringBuilder.Append("[Separated] "); -#endif - s_StringBuilder.Append(Path.GetFileName(shader.name)); - foreach (object mode in append.Where(x=>0<(int)x)) - { - s_StringBuilder.Append("-"); - s_StringBuilder.Append(mode.ToString()); - } - return s_StringBuilder.ToString(); - } - } -} -#endif diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Matrix2x3.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Matrix2x3.cs index a7121cf5..5716d508 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Matrix2x3.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Matrix2x3.cs @@ -2,32 +2,32 @@ namespace Coffee.UIExtensions { - /// - /// Matrix2x3. - /// - public struct Matrix2x3 - { - public float m00, m01, m02, m10, m11, m12; + /// + /// Matrix2x3. + /// + public struct Matrix2x3 + { + public float m00, m01, m02, m10, m11, m12; - public Matrix2x3(Rect rect, float cos, float sin) - { - const float center = 0.5f; - float dx = -rect.xMin / rect.width - center; - float dy = -rect.yMin / rect.height - center; - m00 = cos / rect.width; - m01 = -sin / rect.height; - m02 = dx * cos - dy * sin + center; - m10 = sin / rect.width; - m11 = cos / rect.height; - m12 = dx * sin + dy * cos + center; - } + public Matrix2x3(Rect rect, float cos, float sin) + { + const float center = 0.5f; + float dx = -rect.xMin / rect.width - center; + float dy = -rect.yMin / rect.height - center; + m00 = cos / rect.width; + m01 = -sin / rect.height; + m02 = dx * cos - dy * sin + center; + m10 = sin / rect.width; + m11 = cos / rect.height; + m12 = dx * sin + dy * cos + center; + } - public static Vector2 operator*(Matrix2x3 m, Vector2 v) - { - return new Vector2( - (m.m00 * v.x) + (m.m01 * v.y) + m.m02, - (m.m10 * v.x) + (m.m11 * v.y) + m.m12 - ); - } - } -} \ No newline at end of file + public static Vector2 operator *(Matrix2x3 m, Vector2 v) + { + return new Vector2( + (m.m00 * v.x) + (m.m01 * v.y) + m.m02, + (m.m10 * v.x) + (m.m11 * v.y) + m.m12 + ); + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Packer.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Packer.cs index e39f0a0c..ffdb64f3 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Packer.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Packer.cs @@ -4,57 +4,58 @@ public static class Packer { - /// - /// Pack 4 low-precision [0-1] floats values to a float. - /// Each value [0-1] has 64 steps(6 bits). - /// - public static float ToFloat(float x, float y, float z, float w) - { - x = x < 0 ? 0 : 1 < x ? 1 : x; - y = y < 0 ? 0 : 1 < y ? 1 : y; - z = z < 0 ? 0 : 1 < z ? 1 : z; - w = w < 0 ? 0 : 1 < w ? 1 : w; - const int PRECISION = (1 << 6) - 1; - return (Mathf.FloorToInt(w * PRECISION) << 18) - + (Mathf.FloorToInt(z * PRECISION) << 12) - + (Mathf.FloorToInt(y * PRECISION) << 6) - + Mathf.FloorToInt(x * PRECISION); - } + /// + /// Pack 4 low-precision [0-1] floats values to a float. + /// Each value [0-1] has 64 steps(6 bits). + /// + public static float ToFloat(float x, float y, float z, float w) + { + x = x < 0 ? 0 : 1 < x ? 1 : x; + y = y < 0 ? 0 : 1 < y ? 1 : y; + z = z < 0 ? 0 : 1 < z ? 1 : z; + w = w < 0 ? 0 : 1 < w ? 1 : w; + const int PRECISION = (1 << 6) - 1; + return (Mathf.FloorToInt(w * PRECISION) << 18) + + (Mathf.FloorToInt(z * PRECISION) << 12) + + (Mathf.FloorToInt(y * PRECISION) << 6) + + Mathf.FloorToInt(x * PRECISION); + } - /// - /// Pack 4 low-precision [0-1] floats values to a float. - /// Each value [0-1] has 64 steps(6 bits). - /// - public static float ToFloat(Vector4 factor) - { - return ToFloat(Mathf.Clamp01(factor.x), Mathf.Clamp01(factor.y), Mathf.Clamp01(factor.z), Mathf.Clamp01(factor.w)); - } + /// + /// Pack 4 low-precision [0-1] floats values to a float. + /// Each value [0-1] has 64 steps(6 bits). + /// + public static float ToFloat(Vector4 factor) + { + return ToFloat(Mathf.Clamp01(factor.x), Mathf.Clamp01(factor.y), Mathf.Clamp01(factor.z), + Mathf.Clamp01(factor.w)); + } - /// - /// Pack 1 middle-precision & 2 low-precision [0-1] floats values to a float. - /// z value [0-1] has 4096 steps(12 bits) and xy value [0-1] has 64 steps(6 bits). - /// - public static float ToFloat(float x, float y, float z) - { - x = x < 0 ? 0 : 1 < x ? 1 : x; - y = y < 0 ? 0 : 1 < y ? 1 : y; - z = z < 0 ? 0 : 1 < z ? 1 : z; - const int PRECISION = (1 << 8) - 1; - return (Mathf.FloorToInt(z * PRECISION) << 16) - + (Mathf.FloorToInt(y * PRECISION) << 8) - + Mathf.FloorToInt(x * PRECISION); - } + /// + /// Pack 1 middle-precision & 2 low-precision [0-1] floats values to a float. + /// z value [0-1] has 4096 steps(12 bits) and xy value [0-1] has 64 steps(6 bits). + /// + public static float ToFloat(float x, float y, float z) + { + x = x < 0 ? 0 : 1 < x ? 1 : x; + y = y < 0 ? 0 : 1 < y ? 1 : y; + z = z < 0 ? 0 : 1 < z ? 1 : z; + const int PRECISION = (1 << 8) - 1; + return (Mathf.FloorToInt(z * PRECISION) << 16) + + (Mathf.FloorToInt(y * PRECISION) << 8) + + Mathf.FloorToInt(x * PRECISION); + } - /// - /// Pack 2 low-precision [0-1] floats values to a float. - /// Each value [0-1] has 4096 steps(12 bits). - /// - public static float ToFloat(float x, float y) - { - x = x < 0 ? 0 : 1 < x ? 1 : x; - y = y < 0 ? 0 : 1 < y ? 1 : y; - const int PRECISION = (1 << 12) - 1; - return (Mathf.FloorToInt(y * PRECISION) << 12) - + Mathf.FloorToInt(x * PRECISION); - } + /// + /// Pack 2 low-precision [0-1] floats values to a float. + /// Each value [0-1] has 4096 steps(12 bits). + /// + public static float ToFloat(float x, float y) + { + x = x < 0 ? 0 : 1 < x ? 1 : x; + y = y < 0 ? 0 : 1 < y ? 1 : y; + const int PRECISION = (1 << 12) - 1; + return (Mathf.FloorToInt(y * PRECISION) << 12) + + Mathf.FloorToInt(x * PRECISION); + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ParameterTexture.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ParameterTexture.cs index 9b221006..cb890f4c 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ParameterTexture.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ParameterTexture.cs @@ -6,185 +6,185 @@ namespace Coffee.UIExtensions { - public interface IParameterTexture - { - int parameterIndex { get; set; } - - ParameterTexture ptex { get; } - } - - /// - /// Parameter texture. - /// - [System.Serializable] - public class ParameterTexture - { - - //################################ - // Public Members. - //################################ - - /// - /// Initializes a new instance of the class. - /// - /// Channels. - /// Instance limit. - /// Property name. - public ParameterTexture(int channels, int instanceLimit, string propertyName) - { - _propertyName = propertyName; - _channels = ((channels - 1) / 4 + 1) * 4; - _instanceLimit = ((instanceLimit - 1) / 2 + 1) * 2; - _data = new byte[_channels * _instanceLimit]; - - _stack = new Stack(_instanceLimit); - for (int i = 1; i < _instanceLimit + 1; i++) - { - _stack.Push(i); - } - } - - - /// - /// Register the specified target. - /// - /// Target. - public void Register(IParameterTexture target) - { - Initialize(); - if (target.parameterIndex <= 0 && 0 < _stack.Count) - { - target.parameterIndex = _stack.Pop(); + public interface IParameterTexture + { + int parameterIndex { get; set; } + + ParameterTexture paramTex { get; } + } + + /// + /// Parameter texture. + /// + [System.Serializable] + public class ParameterTexture + { + //################################ + // Public Members. + //################################ + + /// + /// Initializes a new instance of the class. + /// + /// Channels. + /// Instance limit. + /// Property name. + public ParameterTexture(int channels, int instanceLimit, string propertyName) + { + _propertyName = propertyName; + _channels = ((channels - 1) / 4 + 1) * 4; + _instanceLimit = ((instanceLimit - 1) / 2 + 1) * 2; + _data = new byte[_channels * _instanceLimit]; + + _stack = new Stack(_instanceLimit); + for (int i = 1; i < _instanceLimit + 1; i++) + { + _stack.Push(i); + } + } + + + /// + /// Register the specified target. + /// + /// Target. + public void Register(IParameterTexture target) + { + Initialize(); + if (target.parameterIndex <= 0 && 0 < _stack.Count) + { + target.parameterIndex = _stack.Pop(); // Debug.LogFormat("@@@ Register {0} : {1}", target, target.parameterIndex); - } - } - - /// - /// Unregister the specified target. - /// - /// Target. - public void Unregister(IParameterTexture target) - { - if (0 < target.parameterIndex) - { + } + } + + /// + /// Unregister the specified target. + /// + /// Target. + public void Unregister(IParameterTexture target) + { + if (0 < target.parameterIndex) + { // Debug.LogFormat("@@@ Unregister {0} : {1}", target, target.parameterIndex); - _stack.Push(target.parameterIndex); - target.parameterIndex = 0; - } - } - - /// - /// Sets the data. - /// - /// Target. - /// Channel identifier. - /// Value. - public void SetData(IParameterTexture target, int channelId, byte value) - { - int index = (target.parameterIndex - 1) * _channels + channelId; - if (0 < target.parameterIndex && _data[index] != value) - { - _data[index] = value; - _needUpload = true; - } - } - - /// - /// Sets the data. - /// - /// Target. - /// Channel identifier. - /// Value. - public void SetData(IParameterTexture target, int channelId, float value) - { - SetData(target, channelId, (byte)(Mathf.Clamp01(value) * 255)); - } - - /// - /// Registers the material. - /// - /// Mat. - public void RegisterMaterial(Material mat) - { - if (_propertyId == 0) - { - _propertyId = Shader.PropertyToID(_propertyName); - } - if (mat) - { - mat.SetTexture(_propertyId, _texture); - } - } - - /// - /// Gets the index of the normalized. - /// - /// The normalized index. - /// Target. - public float GetNormalizedIndex(IParameterTexture target) - { - return ((float)target.parameterIndex - 0.5f) / _instanceLimit; - } - - - //################################ - // Private Members. - //################################ - - Texture2D _texture; - bool _needUpload; - int _propertyId; - readonly string _propertyName; - readonly int _channels; - readonly int _instanceLimit; - readonly byte[] _data; - readonly Stack _stack; - static List updates; - - /// - /// Initialize this instance. - /// - void Initialize() - { + _stack.Push(target.parameterIndex); + target.parameterIndex = 0; + } + } + + /// + /// Sets the data. + /// + /// Target. + /// Channel identifier. + /// Value. + public void SetData(IParameterTexture target, int channelId, byte value) + { + int index = (target.parameterIndex - 1) * _channels + channelId; + if (0 < target.parameterIndex && _data[index] != value) + { + _data[index] = value; + _needUpload = true; + } + } + + /// + /// Sets the data. + /// + /// Target. + /// Channel identifier. + /// Value. + public void SetData(IParameterTexture target, int channelId, float value) + { + SetData(target, channelId, (byte) (Mathf.Clamp01(value) * 255)); + } + + /// + /// Registers the material. + /// + /// Mat. + public void RegisterMaterial(Material mat) + { + if (_propertyId == 0) + { + _propertyId = Shader.PropertyToID(_propertyName); + } + + if (mat) + { + mat.SetTexture(_propertyId, _texture); + } + } + + /// + /// Gets the index of the normalized. + /// + /// The normalized index. + /// Target. + public float GetNormalizedIndex(IParameterTexture target) + { + return ((float) target.parameterIndex - 0.5f) / _instanceLimit; + } + + + //################################ + // Private Members. + //################################ + + Texture2D _texture; + bool _needUpload; + int _propertyId; + readonly string _propertyName; + readonly int _channels; + readonly int _instanceLimit; + readonly byte[] _data; + readonly Stack _stack; + static List updates; + + /// + /// Initialize this instance. + /// + void Initialize() + { #if UNITY_EDITOR - if (!UnityEditor.EditorApplication.isPlaying && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) - { - return; - } + if (!UnityEditor.EditorApplication.isPlaying && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) + { + return; + } #endif - if (updates == null) - { - updates = new List(); - Canvas.willRenderCanvases += () => - { - var count = updates.Count; - for (int i = 0; i < count; i++) - { - updates[i].Invoke(); - } - }; - } - - if (!_texture) - { - bool isLinear = QualitySettings.activeColorSpace == ColorSpace.Linear; - _texture = new Texture2D(_channels / 4, _instanceLimit, TextureFormat.RGBA32, false, isLinear); - _texture.filterMode = FilterMode.Point; - _texture.wrapMode = TextureWrapMode.Clamp; - - updates.Add(UpdateParameterTexture); - _needUpload = true; - } - } - - void UpdateParameterTexture() - { - if (_needUpload && _texture) - { - _needUpload = false; - _texture.LoadRawTextureData(_data); - _texture.Apply(false, false); - } - } - } -} \ No newline at end of file + if (updates == null) + { + updates = new List(); + Canvas.willRenderCanvases += () => + { + var count = updates.Count; + for (int i = 0; i < count; i++) + { + updates[i].Invoke(); + } + }; + } + + if (!_texture) + { + bool isLinear = QualitySettings.activeColorSpace == ColorSpace.Linear; + _texture = new Texture2D(_channels / 4, _instanceLimit, TextureFormat.RGBA32, false, isLinear); + _texture.filterMode = FilterMode.Point; + _texture.wrapMode = TextureWrapMode.Clamp; + + updates.Add(UpdateParameterTexture); + _needUpload = true; + } + } + + void UpdateParameterTexture() + { + if (_needUpload && _texture) + { + _needUpload = false; + _texture.LoadRawTextureData(_data); + _texture.Apply(false, false); + } + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs deleted file mode 100644 index 2c81fecd..00000000 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs +++ /dev/null @@ -1,268 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using UnityEngine; -using UnityEngine.UI; - -namespace Coffee.UIExtensions -{ - /// - /// Abstract effect base for UI. - /// - [DisallowMultipleComponent] - public abstract class UIEffectBase : BaseMeshEffect, IParameterTexture, IMaterialModifier -#if UNITY_EDITOR - , ISerializationCallbackReceiver -#endif - { - protected static readonly Vector2[] splitedCharacterPosition = { Vector2.up, Vector2.one, Vector2.right, Vector2.zero }; - protected static readonly List tempVerts = new List(); - protected static readonly StringBuilder stringBuilder = new StringBuilder(); - - [HideInInspector] - [SerializeField] int m_Version; - [SerializeField] protected Material m_EffectMaterial; - - /// - /// Gets or sets the parameter index. - /// - public int parameterIndex { get; set; } - - /// - /// Gets the parameter texture. - /// - public virtual ParameterTexture ptex { get { return null; } } - - /// - /// Gets target graphic for effect. - /// - public Graphic targetGraphic { get { return graphic; } } - - /// - /// Gets material for effect. - /// - public Material effectMaterial { get { return m_EffectMaterial; } } - - public virtual Hash128 GetMaterialHash(Material material) - { - return new Hash128(); - } - - public virtual Material GetModifiedMaterial(Material baseMaterial) - { - if(!isActiveAndEnabled) - { - return baseMaterial; - } - - var oldHash = _effectMaterialHash; - _effectMaterialHash = GetMaterialHash(baseMaterial); - var modifiedMaterial = baseMaterial; - if(_effectMaterialHash.isValid) - { - modifiedMaterial = MaterialRepository.Register(baseMaterial, _effectMaterialHash, ModifyMaterial); - } - MaterialRepository.Unregister(oldHash); - - return modifiedMaterial; - } - - protected bool isTMProMobile (Material material) - { - return material && material.shader && material.shader.name.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal); - } - - public virtual void ModifyMaterial(Material material) - { - Debug.Log("ModifyMaterial PTEX!!! " + ptex); - if(isActiveAndEnabled && ptex != null) - ptex.RegisterMaterial (material); - } - - protected void SetShaderVariants(Material material, params object[] variants) - { - // Set shader keywords as variants - var keywords = variants.Where(x => 0 < (int)x) - .Select(x => x.ToString().ToUpper()) - .Concat(material.shaderKeywords) - .ToArray(); - material.shaderKeywords = keywords; - - // Add variant name - stringBuilder.Length = 0; - stringBuilder.Append(Path.GetFileName(material.shader.name) ); - foreach(var keyword in keywords) - { - stringBuilder.Append("-"); - stringBuilder.Append(keyword); - } - material.name += stringBuilder.ToString(); - } - - Hash128 _effectMaterialHash; - -#if UNITY_EDITOR - protected override void Reset() - { - m_Version = 300; - OnValidate(); - } - - /// - /// Raises the validate event. - /// - protected override void OnValidate() - { - base.OnValidate (); - - var mat = GetMaterial(); - if (m_EffectMaterial != mat) - { - m_EffectMaterial = mat; - UnityEditor.EditorUtility.SetDirty(this); - } - - ModifyMaterial(); - SetVerticesDirty (); - SetEffectDirty (); - } - - public void OnBeforeSerialize() - { - } - - public void OnAfterDeserialize() - { - UnityEditor.EditorApplication.delayCall += UpgradeIfNeeded; - } - - protected bool IsShouldUpgrade(int expectedVersion) - { - if (m_Version < expectedVersion) - { - Debug.LogFormat(gameObject, "{0}({1}) has been upgraded: version {2} -> {3}", name, GetType().Name, m_Version, expectedVersion); - m_Version = expectedVersion; - - //UnityEditor.EditorApplication.delayCall += () => - { - UnityEditor.EditorUtility.SetDirty(this); - if (!Application.isPlaying && gameObject && gameObject.scene.IsValid()) - { - UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene); - } - } - ; - return true; - } - return false; - } - - protected virtual void UpgradeIfNeeded() - { - } - - /// - /// Gets the material. - /// - /// The material. - protected virtual Material GetMaterial() - { - return null; - } -#endif - - /// - /// Modifies the material. - /// - public virtual void ModifyMaterial() - { - targetGraphic.material = isActiveAndEnabled ? m_EffectMaterial : null; - } - - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable (); - - if (ptex != null) - { - ptex.Register(this); - } - ModifyMaterial(); - - graphic.SetMaterialDirty(); - graphic.SetVerticesDirty(); - SetEffectDirty(); - - - foreach (var mr in GetComponentsInChildren ()) - { - mr.GetComponent ().SetMaterialDirty (); - mr.GetComponent ().SetVerticesDirty (); - } - } - - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - protected override void OnDisable() - { - base.OnDisable (); - - graphic.SetMaterialDirty(); - graphic.SetVerticesDirty(); - if (ptex != null) - { - ptex.Unregister(this); - } - - MaterialRepository.Unregister(_effectMaterialHash); - _effectMaterialHash = new Hash128(); - - foreach (var mr in GetComponentsInChildren ()) - { - mr.GetComponent ().SetMaterialDirty (); - mr.GetComponent ().SetVerticesDirty (); - } - } - - /// - /// Mark the UIEffect as dirty. - /// - protected virtual void SetEffectDirty() - { - SetVerticesDirty(); - } - - protected override void OnDidApplyAnimationProperties() - { - SetEffectDirty(); - } - - protected override void OnTextChanged (UnityEngine.Object obj) - { - base.OnTextChanged (obj); - - - foreach (var sm in GetComponentsInChildren ()) - { - if(!sm.GetComponent()) - { - var mr = sm.gameObject.AddComponent (); - - targetGraphic.SetAllDirty (); - //targetGraphic.SetVerticesDirty (); - - //mr.GetComponent ().SetMaterialDirty (); - //mr.GetComponent ().SetVerticesDirty (); - - - } - } - } - } -} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors.meta new file mode 100644 index 00000000..5ec68a0e --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 73be53ea7d09c45bc83e7b957a5b99b5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs new file mode 100644 index 00000000..9fbb2d7b --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEngine.UI; + +namespace Coffee.UIExtensions +{ + public class BaseConnector + { + private static readonly List s_Connectors = new List(); + private static readonly Dictionary s_ConnectorMap = new Dictionary(); + private static readonly BaseConnector s_EmptyConnector = new BaseConnector(); + +#if UNITY_EDITOR + [UnityEditor.InitializeOnLoadMethod] +#endif + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + private static void Init() + { + AddConnector(s_EmptyConnector); + } + + protected static void AddConnector(BaseConnector connector) + { + s_Connectors.Add(connector); + s_Connectors.Sort((x, y) => y.priority - x.priority); + } + + public static BaseConnector FindConnector(Graphic graphic) + { + if (!graphic) return s_EmptyConnector; + + var type = graphic.GetType(); + BaseConnector connector = null; + if (s_ConnectorMap.TryGetValue(type, out connector)) return connector; + + foreach (var c in s_Connectors) + { + if (!c.IsValid(graphic)) continue; + + s_ConnectorMap.Add(type, c); + return c; + } + + return s_EmptyConnector; + } + + /// + /// The connector is valid for the component. + /// + protected virtual bool IsValid(Graphic graphic) + { + return true; + } + + /// + /// Find effect shader. + /// + public virtual Shader FindShader(string shaderName) + { + return null; + } + + /// + /// Priority. + /// + protected virtual int priority + { + get { return -1; } + } + + /// + /// Extra channel. + /// + public virtual AdditionalCanvasShaderChannels extraChannel + { + get { return AdditionalCanvasShaderChannels.None; } + } + + /// + /// Set material. + /// + public virtual void SetMaterial(Graphic graphic, Material material) + { + } + + /// + /// Get material. + /// + public virtual Material GetMaterial(Graphic graphic) + { + return null; + } + + /// + /// Mark the vertices as dirty. + /// + public virtual void SetVerticesDirty(Graphic graphic) + { + } + + /// + /// Mark the material as dirty. + /// + public virtual void SetMaterialDirty(Graphic graphic) + { + } + + /// + /// This function is called when the object becomes enabled and active. + /// + public virtual void OnEnable(Graphic graphic) + { + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + public virtual void OnDisable(Graphic graphic) + { + } + + /// + /// Event that is called just before Canvas rendering happens. + /// This allows you to delay processing / updating of canvas based elements until just before they are rendered. + /// + protected virtual void OnWillRenderCanvases() + { + } + + /// + /// Gets position factor for area. + /// + public void GetPositionFactor(EffectArea area, int index, Rect rect, Vector2 position, out float x, out float y) + { + if (area == EffectArea.Fit) + { + x = Mathf.Clamp01((position.x - rect.xMin) / rect.width); + y = Mathf.Clamp01((position.y - rect.yMin) / rect.height); + } + else + { + x = Mathf.Clamp01(position.x / rect.width + 0.5f); + y = Mathf.Clamp01(position.y / rect.height + 0.5f); + } + } + + /// + /// Normalize vertex position by local matrix. + /// + public void GetNormalizedFactor(EffectArea area, int index, Matrix2x3 matrix, Vector2 position, + out Vector2 normalizedPos) + { + normalizedPos = matrix * position; + } + + public virtual bool IsText(Graphic graphic) + { + return false; + } + + public virtual void SetExtraChannel(ref UIVertex vertex, Vector2 value) + { + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs.meta similarity index 69% rename from Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs.meta rename to Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs.meta index 6e5717fb..892259f2 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs.meta +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/BaseConnector.cs.meta @@ -1,8 +1,7 @@ fileFormatVersion: 2 -guid: 70261fa2760d4c040ac28aebb1f155fb -timeCreated: 1528297353 -licenseType: Free +guid: 1fa123391c81f40ffbbe761447b7913c MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: [] executionOrder: 0 diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs new file mode 100644 index 00000000..ba47501c --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs @@ -0,0 +1,70 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Coffee.UIExtensions +{ + public class GraphicConnector : BaseConnector + { +#if UNITY_EDITOR + [UnityEditor.InitializeOnLoadMethod] +#endif + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + private static void Init() + { + AddConnector(new GraphicConnector()); + } + + protected override int priority + { + get { return 0; } + } + + public override Material GetMaterial(Graphic graphic) + { + return graphic ? graphic.material : null; + } + + public override void SetMaterial(Graphic graphic, Material material) + { + if (graphic) + graphic.material = material; + } + + public override AdditionalCanvasShaderChannels extraChannel + { + get { return AdditionalCanvasShaderChannels.TexCoord1; } + } + + public override Shader FindShader(string shaderName) + { + return Shader.Find("Hidden/" + shaderName); + } + + protected override bool IsValid(Graphic graphic) + { + return graphic; + } + + public override void SetVerticesDirty(Graphic graphic) + { + if (graphic) + graphic.SetVerticesDirty(); + } + + public override void SetMaterialDirty(Graphic graphic) + { + if (graphic) + graphic.SetMaterialDirty(); + } + + public override bool IsText(Graphic graphic) + { + return graphic && graphic is Text; + } + + public override void SetExtraChannel(ref UIVertex vertex, Vector2 value) + { + vertex.uv1 = value; + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs.meta new file mode 100644 index 00000000..5652017f --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Connectors/GraphicConnector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 66636f82e05e6453781a33c8b7da8b93 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor.meta index 92b80be2..1e9742da 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor.meta +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor.meta @@ -1,9 +1,8 @@ fileFormatVersion: 2 guid: ee1bfc8c299e6482cb7175ba2f94495a folderAsset: yes -timeCreated: 1487152270 -licenseType: Pro DefaultImporter: + externalObjects: {} userData: assetBundleName: assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs new file mode 100644 index 00000000..1ab55b26 --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs @@ -0,0 +1,74 @@ +#if !UNITY_2019_1_OR_NEWER +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using UnityEditor; + +namespace Coffee.UIExtensions +{ + public static class ImportSampleMenu_UIEffect + { + [MenuItem("Assets/Samples/Import UIEffect Sample")] + private static void ImportSample() + { + const string jsonGuid = "546af75b6221c4768be79d67c9cea1fb"; + ImportSample(jsonGuid, "Demo"); + } + + private static void ImportSample(string jsonGuid, string sampleName) + { + var jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid); + var json = File.ReadAllText(jsonPath); + var version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value; + var displayName = Regex.Match(json, "\"displayName\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value; + var src = string.Format("{0}/Samples~/{1}", Path.GetDirectoryName(jsonPath), sampleName); + var srcAlt = string.Format("{0}/Samples/{1}", Path.GetDirectoryName(jsonPath), sampleName); + var dst = string.Format("Assets/Samples/{0}/{1}/{2}", displayName, version, sampleName); + var previousPath = GetPreviousSamplePath(displayName, sampleName); + + // Remove the previous sample directory. + if (!string.IsNullOrEmpty(previousPath)) + { + var msg = "A different version of the sample is already imported at\n\n" + + previousPath + + "\n\nIt will be deleted when you update. Are you sure you want to continue?"; + if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel")) + return; + + FileUtil.DeleteFileOrDirectory(previousPath); + + var metaFile = previousPath + ".meta"; + if (File.Exists(metaFile)) + FileUtil.DeleteFileOrDirectory(metaFile); + } + + if (!Directory.Exists(dst)) + FileUtil.DeleteFileOrDirectory(dst); + + var dstDir = Path.GetDirectoryName(dst); + if (!Directory.Exists(dstDir)) + Directory.CreateDirectory(dstDir); + + if (Directory.Exists(src)) + FileUtil.CopyFileOrDirectory(src, dst); + else if (Directory.Exists(srcAlt)) + FileUtil.CopyFileOrDirectory(srcAlt, dst); + else + throw new DirectoryNotFoundException(src); + + AssetDatabase.ImportAsset(dst, ImportAssetOptions.ImportRecursive); + } + + private static string GetPreviousSamplePath(string displayName, string sampleName) + { + var sampleRoot = string.Format("Assets/Samples/{0}", displayName); + var sampleRootInfo = new DirectoryInfo(sampleRoot); + if (!sampleRootInfo.Exists) return null; + + return sampleRootInfo.GetDirectories() + .Select(versionDir => Path.Combine(versionDir.ToString(), sampleName)) + .FirstOrDefault(Directory.Exists); + } + } +} +#endif diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs.meta new file mode 100644 index 00000000..50579154 --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/ImportSampleMenu.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f1d002a431e7c4b1ca5db78469cb0d1c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/MaterialDirtyScope.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/MaterialDirtyScope.cs index 8e59a7f9..2b9d38a8 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/MaterialDirtyScope.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/MaterialDirtyScope.cs @@ -4,33 +4,34 @@ namespace Coffee.UIExtensions.Editors { - /// - /// Changes in this scope cause the graphic's material to be dirty. - /// - internal class MaterialDirtyScope : EditorGUI.ChangeCheckScope - { - readonly Object [] targets; + /// + /// Changes in this scope cause the graphic's material to be dirty. + /// When you change a property, it marks the material as dirty. + /// + internal class MaterialDirtyScope : EditorGUI.ChangeCheckScope + { + readonly Object[] targets; - public MaterialDirtyScope (Object [] targets) - { - this.targets = targets; - } + public MaterialDirtyScope(Object[] targets) + { + this.targets = targets; + } - protected override void CloseScope () - { - if (changed) - { - var graphics = targets.OfType () - .Select (x => x.targetGraphic) - .Where (x => x); + protected override void CloseScope() + { + if (changed) + { + var graphics = targets.OfType() + .Select(x => x.graphic) + .Where(x => x); - foreach (var g in graphics) - { - g.SetMaterialDirty (); - } - } + foreach (var g in graphics) + { + BaseConnector.FindConnector(g).SetMaterialDirty(g); + } + } - base.CloseScope (); - } - } -} \ No newline at end of file + base.CloseScope(); + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIDissolveEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIDissolveEditor.cs index 8d63aa10..f2ac3367 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIDissolveEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIDissolveEditor.cs @@ -7,135 +7,115 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UIDissolve))] - [CanEditMultipleObjects] - public class UIDissolveEditor : BaseMeshEffectEditor - { - static int s_NoiseTexId; - - //################################ - // Public/Protected Members. - //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable (); - - _spMaterial = serializedObject.FindProperty("m_EffectMaterial"); - _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); - _spEffectArea = serializedObject.FindProperty("m_EffectArea"); - _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); - _spWidth = serializedObject.FindProperty("m_Width"); - _spColor = serializedObject.FindProperty("m_Color"); - _spSoftness = serializedObject.FindProperty("m_Softness"); - _spColorMode = serializedObject.FindProperty("m_ColorMode"); - _spNoiseTexture = serializedObject.FindProperty("m_NoiseTexture"); - _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); - _spReverse = serializedObject.FindProperty("m_Reverse"); - var player = serializedObject.FindProperty("m_Player"); - _spPlay = player.FindPropertyRelative("play"); - _spDuration = player.FindPropertyRelative("duration"); - _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); - _spLoop = player.FindPropertyRelative("loop"); - _spLoopDelay = player.FindPropertyRelative("loopDelay"); - _spUpdateMode = player.FindPropertyRelative("updateMode"); - - s_NoiseTexId = Shader.PropertyToID ("_NoiseTex"); - - _shader = Shader.Find ("TextMeshPro/Distance Field (UIDissolve)"); - _mobileShader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UIDissolve)"); - _spriteShader = Shader.Find ("TextMeshPro/Sprite (UIDissolve)"); - } - - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - - serializedObject.Update(); - - //================ - // Effect setting. - //================ - EditorGUILayout.PropertyField(_spEffectFactor); - EditorGUILayout.PropertyField(_spWidth); - EditorGUILayout.PropertyField(_spSoftness); - EditorGUILayout.PropertyField(_spColor); - - using (new MaterialDirtyScope (targets)) - { - EditorGUILayout.PropertyField (_spColorMode); - EditorGUILayout.PropertyField (_spNoiseTexture); - } - - //================ - // Advanced option. - //================ - EditorGUILayout.PropertyField(_spEffectArea); - EditorGUILayout.PropertyField(_spKeepAspectRatio); - - //================ - // Effect player. - //================ - EditorGUILayout.PropertyField(_spPlay); - EditorGUILayout.PropertyField(_spDuration); - EditorGUILayout.PropertyField(_spInitialPlayDelay); - EditorGUILayout.PropertyField(_spLoop); - EditorGUILayout.PropertyField(_spLoopDelay); - EditorGUILayout.PropertyField(_spUpdateMode); - EditorGUILayout.PropertyField(_spReverse); - - // Debug. - using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) - using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) - { - GUILayout.Label("Debug"); - - if (GUILayout.Button("Play", "ButtonLeft")) - { - (target as UIDissolve).Play(); - } - - if (GUILayout.Button("Stop", "ButtonRight")) - { - (target as UIDissolve).Stop(); - } - } - - ShowCanvasChannelsWarning (); - - serializedObject.ApplyModifiedProperties(); - - } - - //################################ - // Private Members. - //################################ - SerializedProperty _spMaterial; - SerializedProperty _spEffectFactor; - SerializedProperty _spWidth; - SerializedProperty _spColor; - SerializedProperty _spSoftness; - SerializedProperty _spColorMode; - SerializedProperty _spNoiseTexture; - SerializedProperty _spEffectArea; - SerializedProperty _spKeepAspectRatio; - SerializedProperty _spReverse; - SerializedProperty _spPlay; - SerializedProperty _spLoop; - SerializedProperty _spLoopDelay; - SerializedProperty _spDuration; - SerializedProperty _spInitialPlayDelay; - SerializedProperty _spUpdateMode; - - Shader _shader; - Shader _mobileShader; - Shader _spriteShader; - } + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UIDissolve))] + [CanEditMultipleObjects] + public class UIDissolveEditor : Editor + { + SerializedProperty _spEffectFactor; + SerializedProperty _spWidth; + SerializedProperty _spColor; + SerializedProperty _spSoftness; + SerializedProperty _spColorMode; + SerializedProperty _spTransitionTexture; + SerializedProperty _spEffectArea; + SerializedProperty _spKeepAspectRatio; + SerializedProperty _spReverse; + SerializedProperty _spPlay; + SerializedProperty _spLoop; + SerializedProperty _spLoopDelay; + SerializedProperty _spDuration; + SerializedProperty _spInitialPlayDelay; + SerializedProperty _spUpdateMode; + + //################################ + // Public/Protected Members. + //################################ + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); + _spEffectArea = serializedObject.FindProperty("m_EffectArea"); + _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); + _spWidth = serializedObject.FindProperty("m_Width"); + _spColor = serializedObject.FindProperty("m_Color"); + _spSoftness = serializedObject.FindProperty("m_Softness"); + _spColorMode = serializedObject.FindProperty("m_ColorMode"); + _spTransitionTexture = serializedObject.FindProperty("m_TransitionTexture"); + _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); + _spReverse = serializedObject.FindProperty("m_Reverse"); + var player = serializedObject.FindProperty("m_Player"); + _spPlay = player.FindPropertyRelative("play"); + _spDuration = player.FindPropertyRelative("duration"); + _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); + _spLoop = player.FindPropertyRelative("loop"); + _spLoopDelay = player.FindPropertyRelative("loopDelay"); + _spUpdateMode = player.FindPropertyRelative("updateMode"); + } + + /// + /// Implement this function to make a custom inspector. + /// + public override void OnInspectorGUI() + { + serializedObject.Update(); + + //================ + // Effect setting. + //================ + EditorGUILayout.PropertyField(_spEffectFactor); + EditorGUILayout.PropertyField(_spWidth); + EditorGUILayout.PropertyField(_spSoftness); + EditorGUILayout.PropertyField(_spColor); + + using (new MaterialDirtyScope(targets)) + { + EditorGUILayout.PropertyField(_spColorMode); + EditorGUILayout.PropertyField(_spTransitionTexture); + } + + + //================ + // Advanced option. + //================ + EditorGUILayout.PropertyField(_spEffectArea); + EditorGUILayout.PropertyField(_spKeepAspectRatio); + + + //================ + // Effect player. + //================ + EditorGUILayout.PropertyField(_spPlay); + EditorGUILayout.PropertyField(_spDuration); + EditorGUILayout.PropertyField(_spInitialPlayDelay); + EditorGUILayout.PropertyField(_spLoop); + EditorGUILayout.PropertyField(_spLoopDelay); + EditorGUILayout.PropertyField(_spUpdateMode); + EditorGUILayout.PropertyField(_spReverse); + + + // Debug. + using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) + using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) + { + GUILayout.Label("Debug"); + + if (GUILayout.Button("Play", "ButtonLeft")) + { + (target as UIDissolve).Play(); + } + + if (GUILayout.Button("Stop", "ButtonRight")) + { + (target as UIDissolve).Stop(); + } + } + + serializedObject.ApplyModifiedProperties(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIEffectEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIEffectEditor.cs index ba36f4e5..f5f2b420 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIEffectEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIEffectEditor.cs @@ -6,147 +6,112 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UIEffect))] - [CanEditMultipleObjects] - public class UIEffectEditor : BaseMeshEffectEditor - { - static readonly GUIContent contentEffectColor = new GUIContent ("Effect Color"); - - //################################ - // Public/Protected Members. - //################################ - - /// - /// Draw effect properties. - /// - public static void DrawEffectProperties(SerializedObject serializedObject, string colorProperty = "m_Color") - { - //================ - // Effect setting. - //================ - var spToneMode = serializedObject.FindProperty("m_EffectMode"); - EditorGUILayout.PropertyField(spToneMode); - - // When tone is enable, show parameters. - if (spToneMode.intValue != (int)EffectMode.None) - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectFactor")); - EditorGUI.indentLevel--; - } - - //================ - // Color setting. - //================ - var spColorMode = serializedObject.FindProperty("m_ColorMode"); - EditorGUILayout.PropertyField(spColorMode); - - // When color is enable, show parameters. - //if (spColorMode.intValue != (int)ColorMode.Multiply) - { - EditorGUI.indentLevel++; - - SerializedProperty spColor = serializedObject.FindProperty(colorProperty); - if (spColor == null && serializedObject.targetObject is UIEffect) { - spColor = new SerializedObject (serializedObject.targetObjects.Select(x=>(x as UIEffect).targetGraphic).ToArray()).FindProperty(colorProperty); - } - - EditorGUI.BeginChangeCheck (); - EditorGUI.showMixedValue = spColor.hasMultipleDifferentValues; -#if UNITY_2018_1_OR_NEWER - spColor.colorValue = EditorGUILayout.ColorField (contentEffectColor, spColor.colorValue, true, false, false); -#else - spColor.colorValue = EditorGUILayout.ColorField (contentEffectColor, spColor.colorValue, true, false, false, null); -#endif - if (EditorGUI.EndChangeCheck ()) { - spColor.serializedObject.ApplyModifiedProperties (); - } - - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ColorFactor")); - EditorGUI.indentLevel--; - } - - //================ - // Blur setting. - //================ - var spBlurMode = serializedObject.FindProperty("m_BlurMode"); - EditorGUILayout.PropertyField(spBlurMode); - - // When blur is enable, show parameters. - if (spBlurMode.intValue != (int)BlurMode.None) - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_BlurFactor")); - - var spAdvancedBlur = serializedObject.FindProperty("m_AdvancedBlur"); - if (spAdvancedBlur != null) - { - EditorGUILayout.PropertyField(spAdvancedBlur); - } - EditorGUI.indentLevel--; - } - } - - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - //================ - // Effect setting. - //================ - var spToneMode = serializedObject.FindProperty("m_EffectMode"); - using (new MaterialDirtyScope (targets)) - EditorGUILayout.PropertyField(spToneMode); - - // When tone is enable, show parameters. - if (spToneMode.intValue != (int)EffectMode.None) - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectFactor")); - EditorGUI.indentLevel--; - } - - //================ - // Color setting. - //================ - var spColorMode = serializedObject.FindProperty("m_ColorMode"); - using (new MaterialDirtyScope (targets)) - EditorGUILayout.PropertyField(spColorMode); - - // When color is enable, show parameters. - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField (serializedObject.FindProperty ("m_EffectColor")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ColorFactor")); - EditorGUI.indentLevel--; - } - - //================ - // Blur setting. - //================ - var spBlurMode = serializedObject.FindProperty("m_BlurMode"); - using (new MaterialDirtyScope (targets)) - EditorGUILayout.PropertyField(spBlurMode); - - // When blur is enable, show parameters. - if (spBlurMode.intValue != (int)BlurMode.None) - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_BlurFactor")); - - var spAdvancedBlur = serializedObject.FindProperty("m_AdvancedBlur"); - using (new MaterialDirtyScope (targets)) - EditorGUILayout.PropertyField(spAdvancedBlur); - EditorGUI.indentLevel--; - } - - ShowCanvasChannelsWarning (); - - serializedObject.ApplyModifiedProperties(); - } - } + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UIEffect))] + [CanEditMultipleObjects] + public class UIEffectEditor : Editor + { + SerializedProperty _spEffectMode; + SerializedProperty _spEffectFactor; + SerializedProperty _spColorMode; + SerializedProperty _spEffectColor; + SerializedProperty _spColorFactor; + SerializedProperty _spBlurMode; + SerializedProperty _spBlurFactor; + SerializedProperty _spAdvancedBlur; + + protected void OnEnable() + { + _spEffectMode = serializedObject.FindProperty("m_EffectMode"); + _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); + _spColorMode = serializedObject.FindProperty("m_ColorMode"); + _spEffectColor = serializedObject.FindProperty("m_EffectColor"); + _spColorFactor = serializedObject.FindProperty("m_ColorFactor"); + _spBlurMode = serializedObject.FindProperty("m_BlurMode"); + _spBlurFactor = serializedObject.FindProperty("m_BlurFactor"); + _spAdvancedBlur = serializedObject.FindProperty("m_AdvancedBlur"); + } + + public override void OnInspectorGUI() + { + //================ + // Effect setting. + //================ + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spEffectMode); + + // When effect is enable, show parameters. + if (_spEffectMode.intValue != (int) EffectMode.None) + { + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(_spEffectFactor); + EditorGUI.indentLevel--; + } + + + //================ + // Color setting. + //================ + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spColorMode); + + // When color is enable, show parameters. + { + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(_spEffectColor); + EditorGUILayout.PropertyField(_spColorFactor); + EditorGUI.indentLevel--; + } + + + //================ + // Blur setting. + //================ + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spBlurMode); + + // When blur is enable, show parameters. + if (_spBlurMode.intValue != (int) BlurMode.None) + { + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(_spBlurFactor); + + // When you change a property, it marks the material as dirty. + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spAdvancedBlur); + EditorGUI.indentLevel--; + + // Advanced blur requires uv2 channel. + if (_spAdvancedBlur.boolValue) + { + ShowCanvasChannelsWarning(); + } + } + + serializedObject.ApplyModifiedProperties(); + } + + void ShowCanvasChannelsWarning() + { + var effect = target as UIEffect; + if (effect == null || !effect.graphic) return; + + var channel = effect.uvMaskChannel; + var canvas = effect.graphic.canvas; + if (canvas == null || (canvas.additionalShaderChannels & channel) == channel) return; + + EditorGUILayout.BeginHorizontal(); + { + var msg = string.Format("Enable '{0}' of Canvas.additionalShaderChannels to use 'UIEffect'.", channel); + EditorGUILayout.HelpBox(msg, MessageType.Warning); + if (GUILayout.Button("Fix")) + { + canvas.additionalShaderChannels |= channel; + } + } + EditorGUILayout.EndHorizontal(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIGradientEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIGradientEditor.cs index e03ca117..c0729f6e 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIGradientEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIGradientEditor.cs @@ -4,99 +4,137 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UIGradient))] - [CanEditMultipleObjects] - public class UIGradientEditor : Editor - { - public override void OnInspectorGUI() - { - serializedObject.Update(); - - //================ - // Direction. - //================ - var spDirection = serializedObject.FindProperty("m_Direction"); - EditorGUILayout.PropertyField(spDirection); - - - //================ - // Color. - //================ - var spColor1 = serializedObject.FindProperty("m_Color1"); - var spColor2 = serializedObject.FindProperty("m_Color2"); - var spColor3 = serializedObject.FindProperty("m_Color3"); - var spColor4 = serializedObject.FindProperty("m_Color4"); - switch ((UIGradient.Direction)spDirection.intValue) - { - case UIGradient.Direction.Horizontal: - EditorGUILayout.PropertyField(spColor1, new GUIContent("Left")); - EditorGUILayout.PropertyField(spColor2, new GUIContent("Right")); - break; - case UIGradient.Direction.Vertical: - EditorGUILayout.PropertyField(spColor1, new GUIContent("Top")); - EditorGUILayout.PropertyField(spColor2, new GUIContent("Bottom")); - break; - case UIGradient.Direction.Angle: - EditorGUILayout.PropertyField(spColor1, new GUIContent("Color 1")); - EditorGUILayout.PropertyField(spColor2, new GUIContent("Color 2")); - break; - case UIGradient.Direction.Diagonal: - Rect r = EditorGUILayout.GetControlRect(false, 34); - - r = EditorGUI.PrefixLabel(r, new GUIContent("Diagonal Color")); - float w = r.width / 2; - - EditorGUI.PropertyField(new Rect(r.x, r.y, w, 16), spColor3, GUIContent.none); - EditorGUI.PropertyField(new Rect(r.x + w, r.y, w, 16), spColor4, GUIContent.none); - EditorGUI.PropertyField(new Rect(r.x, r.y + 18, w, 16), spColor1, GUIContent.none); - EditorGUI.PropertyField(new Rect(r.x + w, r.y + 18, w, 16), spColor2, GUIContent.none); - break; - } - - - //================ - // Angle. - //================ - if ((int)UIGradient.Direction.Angle <= spDirection.intValue) - { - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Rotation")); - } - - - //================ - // Offset. - //================ - if ((int)UIGradient.Direction.Diagonal == spDirection.intValue) - { - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Offset1"), new GUIContent("Vertical Offset")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Offset2"), new GUIContent("Horizontal Offset")); - } - else - { - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Offset1"), new GUIContent("Offset")); - } - - - //================ - // Advanced options. - //================ - EditorGUILayout.Space(); - EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel); - EditorGUI.indentLevel++; - { - //if ((target as UIGradient).targetGraphic is Text) - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_GradientStyle")); - - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ColorSpace")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_IgnoreAspectRatio")); - } - EditorGUI.indentLevel--; - - serializedObject.ApplyModifiedProperties(); - - } - } + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UIGradient))] + [CanEditMultipleObjects] + public class UIGradientEditor : Editor + { + private static readonly GUIContent k_TextVerticalOffset = new GUIContent("Vertical Offset"); + private static readonly GUIContent k_TextHorizontalOffset = new GUIContent("Horizontal Offset"); + private static readonly GUIContent k_TextOffset = new GUIContent("Offset"); + private static readonly GUIContent k_TextLeft = new GUIContent("Left"); + private static readonly GUIContent k_TextRight = new GUIContent("Right"); + private static readonly GUIContent k_TextTop = new GUIContent("Top"); + private static readonly GUIContent k_TextBottom = new GUIContent("Bottom"); + private static readonly GUIContent k_TextColor1 = new GUIContent("Color 1"); + private static readonly GUIContent k_TextColor2 = new GUIContent("Color 2"); + private static readonly GUIContent k_TextDiagonalColor = new GUIContent("Diagonal Color"); + + SerializedProperty _spDirection; + SerializedProperty _spColor1; + SerializedProperty _spColor2; + SerializedProperty _spColor3; + SerializedProperty _spColor4; + SerializedProperty _spRotation; + SerializedProperty _spOffset1; + SerializedProperty _spOffset2; + SerializedProperty _spIgnoreAspectRatio; + SerializedProperty _spGradientStyle; + SerializedProperty _spColorSpace; + + //################################ + // Public/Protected Members. + //################################ + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + _spIgnoreAspectRatio = serializedObject.FindProperty("m_IgnoreAspectRatio"); + _spDirection = serializedObject.FindProperty("m_Direction"); + _spColor1 = serializedObject.FindProperty("m_Color1"); + _spColor2 = serializedObject.FindProperty("m_Color2"); + _spColor3 = serializedObject.FindProperty("m_Color3"); + _spColor4 = serializedObject.FindProperty("m_Color4"); + _spRotation = serializedObject.FindProperty("m_Rotation"); + _spOffset1 = serializedObject.FindProperty("m_Offset1"); + _spOffset2 = serializedObject.FindProperty("m_Offset2"); + _spGradientStyle = serializedObject.FindProperty("m_GradientStyle"); + _spColorSpace = serializedObject.FindProperty("m_ColorSpace"); + } + + public override void OnInspectorGUI() + { + serializedObject.Update(); + + //================ + // Direction. + //================ + EditorGUILayout.PropertyField(_spDirection); + + + //================ + // Color. + //================ + switch ((UIGradient.Direction) _spDirection.intValue) + { + case UIGradient.Direction.Horizontal: + EditorGUILayout.PropertyField(_spColor1, k_TextLeft); + EditorGUILayout.PropertyField(_spColor2, k_TextRight); + break; + case UIGradient.Direction.Vertical: + EditorGUILayout.PropertyField(_spColor1, k_TextTop); + EditorGUILayout.PropertyField(_spColor2, k_TextBottom); + break; + case UIGradient.Direction.Angle: + EditorGUILayout.PropertyField(_spColor1, k_TextColor1); + EditorGUILayout.PropertyField(_spColor2, k_TextColor2); + break; + case UIGradient.Direction.Diagonal: + Rect r = EditorGUILayout.GetControlRect(false, 34); + + r = EditorGUI.PrefixLabel(r, k_TextDiagonalColor); + float w = r.width / 2; + + EditorGUI.PropertyField(new Rect(r.x, r.y, w, 16), _spColor3, GUIContent.none); + EditorGUI.PropertyField(new Rect(r.x + w, r.y, w, 16), _spColor4, GUIContent.none); + EditorGUI.PropertyField(new Rect(r.x, r.y + 18, w, 16), _spColor1, GUIContent.none); + EditorGUI.PropertyField(new Rect(r.x + w, r.y + 18, w, 16), _spColor2, GUIContent.none); + break; + } + + + //================ + // Angle. + //================ + if ((int) UIGradient.Direction.Angle <= _spDirection.intValue) + { + EditorGUILayout.PropertyField(_spRotation); + } + + + //================ + // Offset. + //================ + if ((int) UIGradient.Direction.Diagonal == _spDirection.intValue) + { + EditorGUILayout.PropertyField(_spOffset1, k_TextVerticalOffset); + EditorGUILayout.PropertyField(_spOffset2, k_TextHorizontalOffset); + } + else + { + EditorGUILayout.PropertyField(_spOffset1, k_TextOffset); + } + + + //================ + // Advanced options. + //================ + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel); + EditorGUI.indentLevel++; + { + //if ((target as UIGradient).targetGraphic is Text) + EditorGUILayout.PropertyField(_spGradientStyle); + + EditorGUILayout.PropertyField(_spColorSpace); + EditorGUILayout.PropertyField(_spIgnoreAspectRatio); + } + EditorGUI.indentLevel--; + + serializedObject.ApplyModifiedProperties(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIHsvModifierEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIHsvModifierEditor.cs index f7369241..0f087caf 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIHsvModifierEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIHsvModifierEditor.cs @@ -5,59 +5,49 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UIHsvModifier))] - [CanEditMultipleObjects] - public class UIHsvModifierEditor : BaseMeshEffectEditor - { - //################################ - // Public/Protected Members. - //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable (); - - _spTargetColor = serializedObject.FindProperty("m_TargetColor"); - _spRange = serializedObject.FindProperty("m_Range"); - _spHue = serializedObject.FindProperty("m_Hue"); - _spSaturation = serializedObject.FindProperty("m_Saturation"); - _spValue = serializedObject.FindProperty("m_Value"); - } - - - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - serializedObject.Update(); - - //================ - // Effect setting. - //================ - EditorGUILayout.PropertyField(_spTargetColor); - EditorGUILayout.PropertyField(_spRange); - EditorGUILayout.PropertyField(_spHue); - EditorGUILayout.PropertyField(_spSaturation); - EditorGUILayout.PropertyField(_spValue); - - ShowCanvasChannelsWarning (); - - serializedObject.ApplyModifiedProperties(); - } - - //################################ - // Private Members. - //################################ - SerializedProperty _spTargetColor; - SerializedProperty _spRange; - SerializedProperty _spHue; - SerializedProperty _spSaturation; - SerializedProperty _spValue; - } + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UIHsvModifier))] + [CanEditMultipleObjects] + public class UIHsvModifierEditor : Editor + { + SerializedProperty _spTargetColor; + SerializedProperty _spRange; + SerializedProperty _spHue; + SerializedProperty _spSaturation; + SerializedProperty _spValue; + + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + _spTargetColor = serializedObject.FindProperty("m_TargetColor"); + _spRange = serializedObject.FindProperty("m_Range"); + _spHue = serializedObject.FindProperty("m_Hue"); + _spSaturation = serializedObject.FindProperty("m_Saturation"); + _spValue = serializedObject.FindProperty("m_Value"); + } + + + /// + /// Implement this function to make a custom inspector. + /// + public override void OnInspectorGUI() + { + serializedObject.Update(); + + //================ + // Effect setting. + //================ + EditorGUILayout.PropertyField(_spTargetColor); + EditorGUILayout.PropertyField(_spRange); + EditorGUILayout.PropertyField(_spHue); + EditorGUILayout.PropertyField(_spSaturation); + EditorGUILayout.PropertyField(_spValue); + + serializedObject.ApplyModifiedProperties(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShadowEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShadowEditor.cs index 1e9e729d..a46fb836 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShadowEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShadowEditor.cs @@ -4,51 +4,59 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIShadow editor. - /// - [CustomEditor(typeof(UIShadow))] - [CanEditMultipleObjects] - public class UIShadowEditor : Editor - { - UIEffect uiEffect; - - void OnEnable() - { - uiEffect = (target as UIShadow).GetComponent(); - - } - - - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - serializedObject.Update(); - - //================ - // Shadow setting. - //================ - var spShadowMode = serializedObject.FindProperty("m_Style"); - EditorGUILayout.PropertyField(spShadowMode); - - // When shadow is enable, show parameters. - if (spShadowMode.intValue != (int)ShadowStyle.None) - { - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectDistance")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EffectColor")); - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_UseGraphicAlpha")); - - if (uiEffect && uiEffect.blurMode != BlurMode.None) - { - EditorGUILayout.PropertyField(serializedObject.FindProperty("m_BlurFactor")); - } - EditorGUI.indentLevel--; - } - - serializedObject.ApplyModifiedProperties(); - } - } -} \ No newline at end of file + /// + /// UIShadow editor. + /// + [CustomEditor(typeof(UIShadow))] + [CanEditMultipleObjects] + public class UIShadowEditor : Editor + { + UIEffect uiEffect; + SerializedProperty _spStyle; + SerializedProperty _spEffectDistance; + SerializedProperty _spEffectColor; + SerializedProperty _spUseGraphicAlpha; + SerializedProperty _spBlurFactor; + + void OnEnable() + { + uiEffect = (target as UIShadow).GetComponent(); + _spStyle = serializedObject.FindProperty("m_Style"); + _spEffectDistance = serializedObject.FindProperty("m_EffectDistance"); + _spEffectColor = serializedObject.FindProperty("m_EffectColor"); + _spUseGraphicAlpha = serializedObject.FindProperty("m_UseGraphicAlpha"); + _spBlurFactor = serializedObject.FindProperty("m_BlurFactor"); + } + + /// + /// Implement this function to make a custom inspector. + /// + public override void OnInspectorGUI() + { + serializedObject.Update(); + + //================ + // Shadow setting. + //================ + EditorGUILayout.PropertyField(_spStyle); + + // When shadow is enable, show parameters. + if (_spStyle.intValue != (int) ShadowStyle.None) + { + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(_spEffectDistance); + EditorGUILayout.PropertyField(_spEffectColor); + EditorGUILayout.PropertyField(_spUseGraphicAlpha); + + if (uiEffect && uiEffect.blurMode != BlurMode.None) + { + EditorGUILayout.PropertyField(_spBlurFactor); + } + + EditorGUI.indentLevel--; + } + + serializedObject.ApplyModifiedProperties(); + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShinyEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShinyEditor.cs index d627c586..2937357d 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShinyEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UIShinyEditor.cs @@ -5,109 +5,98 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UIShiny))] - [CanEditMultipleObjects] - public class UIShinyEditor : BaseMeshEffectEditor - { - //################################ - // Public/Protected Members. - //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable (); + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UIShiny))] + [CanEditMultipleObjects] + public class UIShinyEditor : Editor + { + SerializedProperty _spEffectFactor; + SerializedProperty _spWidth; + SerializedProperty _spRotation; + SerializedProperty _spSoftness; + SerializedProperty _spBrightness; + SerializedProperty _spGloss; + SerializedProperty _spEffectArea; + SerializedProperty _spPlay; + SerializedProperty _spLoop; + SerializedProperty _spLoopDelay; + SerializedProperty _spDuration; + SerializedProperty _spInitialPlayDelay; + SerializedProperty _spUpdateMode; - _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); - _spEffectArea = serializedObject.FindProperty("m_EffectArea"); - _spWidth = serializedObject.FindProperty("m_Width"); - _spRotation = serializedObject.FindProperty("m_Rotation"); - _spSoftness = serializedObject.FindProperty("m_Softness"); - _spBrightness = serializedObject.FindProperty("m_Brightness"); - _spGloss = serializedObject.FindProperty("m_Gloss"); - var player = serializedObject.FindProperty("m_Player"); - _spPlay = player.FindPropertyRelative("play"); - _spDuration = player.FindPropertyRelative("duration"); - _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); - _spLoop = player.FindPropertyRelative("loop"); - _spLoopDelay = player.FindPropertyRelative("loopDelay"); - _spUpdateMode = player.FindPropertyRelative("updateMode"); - } + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); + _spEffectArea = serializedObject.FindProperty("m_EffectArea"); + _spWidth = serializedObject.FindProperty("m_Width"); + _spRotation = serializedObject.FindProperty("m_Rotation"); + _spSoftness = serializedObject.FindProperty("m_Softness"); + _spBrightness = serializedObject.FindProperty("m_Brightness"); + _spGloss = serializedObject.FindProperty("m_Gloss"); + var player = serializedObject.FindProperty("m_Player"); + _spPlay = player.FindPropertyRelative("play"); + _spDuration = player.FindPropertyRelative("duration"); + _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); + _spLoop = player.FindPropertyRelative("loop"); + _spLoopDelay = player.FindPropertyRelative("loopDelay"); + _spUpdateMode = player.FindPropertyRelative("updateMode"); + } + /// + /// Implement this function to make a custom inspector. + /// + public override void OnInspectorGUI() + { + serializedObject.Update(); - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - serializedObject.Update(); + //================ + // Effect setting. + //================ + EditorGUILayout.PropertyField(_spEffectFactor); + EditorGUILayout.PropertyField(_spWidth); + EditorGUILayout.PropertyField(_spRotation); + EditorGUILayout.PropertyField(_spSoftness); + EditorGUILayout.PropertyField(_spBrightness); + EditorGUILayout.PropertyField(_spGloss); - //================ - // Effect setting. - //================ - EditorGUILayout.PropertyField(_spEffectFactor); - EditorGUILayout.PropertyField(_spWidth); - EditorGUILayout.PropertyField(_spRotation); - EditorGUILayout.PropertyField(_spSoftness); - EditorGUILayout.PropertyField(_spBrightness); - EditorGUILayout.PropertyField(_spGloss); + //================ + // Advanced option. + //================ + EditorGUILayout.PropertyField(_spEffectArea); - //================ - // Advanced option. - //================ - EditorGUILayout.PropertyField(_spEffectArea); + //================ + // Effect player. + //================ + EditorGUILayout.PropertyField(_spPlay); + EditorGUILayout.PropertyField(_spDuration); + EditorGUILayout.PropertyField(_spInitialPlayDelay); + EditorGUILayout.PropertyField(_spLoop); + EditorGUILayout.PropertyField(_spLoopDelay); + EditorGUILayout.PropertyField(_spUpdateMode); - //================ - // Effect player. - //================ - EditorGUILayout.PropertyField(_spPlay); - EditorGUILayout.PropertyField(_spDuration); - EditorGUILayout.PropertyField(_spInitialPlayDelay); - EditorGUILayout.PropertyField(_spLoop); - EditorGUILayout.PropertyField(_spLoopDelay); - EditorGUILayout.PropertyField(_spUpdateMode); + // Debug. + using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) + using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) + { + GUILayout.Label("Debug"); - // Debug. - using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) - using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) - { - GUILayout.Label("Debug"); + if (GUILayout.Button("Play", "ButtonLeft")) + { + (target as UIShiny).Play(); + } - if (GUILayout.Button("Play", "ButtonLeft")) - { - (target as UIShiny).Play(); - } + if (GUILayout.Button("Stop", "ButtonRight")) + { + (target as UIShiny).Stop(); + } + } - if (GUILayout.Button("Stop", "ButtonRight")) - { - (target as UIShiny).Stop(); - } - } - - ShowCanvasChannelsWarning (); - - serializedObject.ApplyModifiedProperties(); - } - - //################################ - // Private Members. - //################################ - SerializedProperty _spEffectFactor; - SerializedProperty _spWidth; - SerializedProperty _spRotation; - SerializedProperty _spSoftness; - SerializedProperty _spBrightness; - SerializedProperty _spGloss; - SerializedProperty _spEffectArea; - SerializedProperty _spPlay; - SerializedProperty _spLoop; - SerializedProperty _spLoopDelay; - SerializedProperty _spDuration; - SerializedProperty _spInitialPlayDelay; - SerializedProperty _spUpdateMode; - } + serializedObject.ApplyModifiedProperties(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UITransitionEffectEditor.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UITransitionEffectEditor.cs index 12397172..22aa0881 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UITransitionEffectEditor.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Editor/UITransitionEffectEditor.cs @@ -5,119 +5,111 @@ namespace Coffee.UIExtensions.Editors { - /// - /// UIEffect editor. - /// - [CustomEditor(typeof(UITransitionEffect))] - [CanEditMultipleObjects] - public class UITransitionEffectEditor : BaseMeshEffectEditor - { - //################################ - // Public/Protected Members. - //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - _spEffectMode = serializedObject.FindProperty("m_EffectMode"); - _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); - _spEffectArea = serializedObject.FindProperty("m_EffectArea"); - _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); - _spDissolveWidth = serializedObject.FindProperty("m_DissolveWidth"); - _spDissolveSoftness = serializedObject.FindProperty("m_DissolveSoftness"); - _spDissolveColor = serializedObject.FindProperty("m_DissolveColor"); - _spTransitionTexture = serializedObject.FindProperty("m_TransitionTexture"); - var player = serializedObject.FindProperty("m_Player"); - _spPlay = player.FindPropertyRelative("play"); - _spDuration = player.FindPropertyRelative("duration"); - _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); - _spLoop = player.FindPropertyRelative("loop"); - _spLoopDelay = player.FindPropertyRelative("loopDelay"); - _spUpdateMode = player.FindPropertyRelative("updateMode"); - _spPassRayOnHidden = serializedObject.FindProperty("m_PassRayOnHidden"); - } + /// + /// UIEffect editor. + /// + [CustomEditor(typeof(UITransitionEffect))] + [CanEditMultipleObjects] + public class UITransitionEffectEditor : Editor + { + SerializedProperty _spEffectMode; + SerializedProperty _spEffectFactor; + SerializedProperty _spEffectArea; + SerializedProperty _spKeepAspectRatio; + SerializedProperty _spDissolveWidth; + SerializedProperty _spDissolveSoftness; + SerializedProperty _spDissolveColor; + SerializedProperty _spTransitionTexture; + SerializedProperty _spPlay; + SerializedProperty _spLoop; + SerializedProperty _spLoopDelay; + SerializedProperty _spDuration; + SerializedProperty _spInitialPlayDelay; + SerializedProperty _spUpdateMode; + SerializedProperty _spPassRayOnHidden; + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + _spEffectMode = serializedObject.FindProperty("m_EffectMode"); + _spEffectFactor = serializedObject.FindProperty("m_EffectFactor"); + _spEffectArea = serializedObject.FindProperty("m_EffectArea"); + _spKeepAspectRatio = serializedObject.FindProperty("m_KeepAspectRatio"); + _spDissolveWidth = serializedObject.FindProperty("m_DissolveWidth"); + _spDissolveSoftness = serializedObject.FindProperty("m_DissolveSoftness"); + _spDissolveColor = serializedObject.FindProperty("m_DissolveColor"); + _spTransitionTexture = serializedObject.FindProperty("m_TransitionTexture"); + var player = serializedObject.FindProperty("m_Player"); + _spPlay = player.FindPropertyRelative("play"); + _spDuration = player.FindPropertyRelative("duration"); + _spInitialPlayDelay = player.FindPropertyRelative("initialPlayDelay"); + _spLoop = player.FindPropertyRelative("loop"); + _spLoopDelay = player.FindPropertyRelative("loopDelay"); + _spUpdateMode = player.FindPropertyRelative("updateMode"); + _spPassRayOnHidden = serializedObject.FindProperty("m_PassRayOnHidden"); + } - /// - /// Implement this function to make a custom inspector. - /// - public override void OnInspectorGUI() - { - //================ - // Effect setting. - //================ - using (new MaterialDirtyScope(targets)) - EditorGUILayout.PropertyField(_spEffectMode); + /// + /// Implement this function to make a custom inspector. + /// + public override void OnInspectorGUI() + { + //================ + // Effect setting. + //================ + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spEffectMode); - EditorGUI.indentLevel++; - EditorGUILayout.PropertyField(_spEffectFactor); - if (_spEffectMode.intValue == (int)UITransitionEffect.EffectMode.Dissolve) - { - EditorGUILayout.PropertyField(_spDissolveWidth); - EditorGUILayout.PropertyField(_spDissolveSoftness); - EditorGUILayout.PropertyField(_spDissolveColor); - } - EditorGUI.indentLevel--; + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(_spEffectFactor); + if (_spEffectMode.intValue == (int) UITransitionEffect.EffectMode.Dissolve) + { + EditorGUILayout.PropertyField(_spDissolveWidth); + EditorGUILayout.PropertyField(_spDissolveSoftness); + EditorGUILayout.PropertyField(_spDissolveColor); + } - //================ - // Advanced option. - //================ - EditorGUILayout.PropertyField(_spEffectArea); - using (new MaterialDirtyScope(targets)) - EditorGUILayout.PropertyField(_spTransitionTexture); - EditorGUILayout.PropertyField(_spKeepAspectRatio); - EditorGUILayout.PropertyField(_spPassRayOnHidden); + EditorGUI.indentLevel--; - //================ - // Effect player. - //================ - EditorGUILayout.PropertyField(_spPlay); - EditorGUILayout.PropertyField(_spDuration); - EditorGUILayout.PropertyField(_spInitialPlayDelay); - EditorGUILayout.PropertyField(_spLoop); - EditorGUILayout.PropertyField(_spLoopDelay); - EditorGUILayout.PropertyField(_spUpdateMode); + //================ + // Advanced option. + //================ + EditorGUILayout.PropertyField(_spEffectArea); + using (new MaterialDirtyScope(targets)) + EditorGUILayout.PropertyField(_spTransitionTexture); + EditorGUILayout.PropertyField(_spKeepAspectRatio); + EditorGUILayout.PropertyField(_spPassRayOnHidden); - // Debug. - using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) - using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) - { - GUILayout.Label("Debug"); + //================ + // Effect player. + //================ + EditorGUILayout.PropertyField(_spPlay); + EditorGUILayout.PropertyField(_spDuration); + EditorGUILayout.PropertyField(_spInitialPlayDelay); + EditorGUILayout.PropertyField(_spLoop); + EditorGUILayout.PropertyField(_spLoopDelay); + EditorGUILayout.PropertyField(_spUpdateMode); - if (GUILayout.Button("Show", "ButtonLeft")) - { - (target as UITransitionEffect).Show(); - } + // Debug. + using (new EditorGUI.DisabledGroupScope(!Application.isPlaying)) + using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox)) + { + GUILayout.Label("Debug"); - if (GUILayout.Button("Hide", "ButtonRight")) - { - (target as UITransitionEffect).Hide(); - } - } + if (GUILayout.Button("Show", "ButtonLeft")) + { + (target as UITransitionEffect).Show(); + } - ShowCanvasChannelsWarning(); + if (GUILayout.Button("Hide", "ButtonRight")) + { + (target as UITransitionEffect).Hide(); + } + } - serializedObject.ApplyModifiedProperties(); - } - - //################################ - // Private Members. - //################################ - SerializedProperty _spEffectMode; - SerializedProperty _spEffectFactor; - SerializedProperty _spEffectArea; - SerializedProperty _spKeepAspectRatio; - SerializedProperty _spDissolveWidth; - SerializedProperty _spDissolveSoftness; - SerializedProperty _spDissolveColor; - SerializedProperty _spTransitionTexture; - SerializedProperty _spPlay; - SerializedProperty _spLoop; - SerializedProperty _spLoopDelay; - SerializedProperty _spDuration; - SerializedProperty _spInitialPlayDelay; - SerializedProperty _spUpdateMode; - SerializedProperty _spPassRayOnHidden; - } + serializedObject.ApplyModifiedProperties(); + } + } } \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums.meta new file mode 100644 index 00000000..d1a3a2a1 --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 78cc1478fd16a484ba11857d5c4f4912 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/BlurMode.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/BlurMode.cs index fcc3cab6..68c56042 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/BlurMode.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/BlurMode.cs @@ -1,13 +1,13 @@ namespace Coffee.UIExtensions { - /// - /// Blur effect mode. - /// - public enum BlurMode - { - None = 0, - FastBlur = 1, - MediumBlur = 2, - DetailBlur = 3, - } -} \ No newline at end of file + /// + /// Blur effect mode. + /// + public enum BlurMode + { + None = 0, + FastBlur = 1, + MediumBlur = 2, + DetailBlur = 3, + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ColorMode.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ColorMode.cs index 8a7aae4c..a94ef1ec 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ColorMode.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ColorMode.cs @@ -1,13 +1,13 @@ namespace Coffee.UIExtensions { - /// - /// Color effect mode. - /// - public enum ColorMode - { - Multiply = 0, - Fill = 1, - Add = 2, - Subtract = 3, - } -} \ No newline at end of file + /// + /// Color effect mode. + /// + public enum ColorMode + { + Multiply = 0, + Fill = 1, + Add = 2, + Subtract = 3, + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectArea.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectArea.cs index d59c9deb..334e8b48 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectArea.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectArea.cs @@ -3,110 +3,114 @@ namespace Coffee.UIExtensions { - /// - /// Area for effect. - /// - public enum EffectArea - { - RectTransform, - Fit, - Character, - } + /// + /// Area for effect. + /// + public enum EffectArea + { + RectTransform, + Fit, + Character, + } - public static class EffectAreaExtensions - { - static readonly Rect rectForCharacter = new Rect(0, 0, 1, 1); - static readonly Vector2 [] splitedCharacterPosition = { Vector2.up, Vector2.one, Vector2.right, Vector2.zero }; + public static class EffectAreaExtensions + { + static readonly Rect rectForCharacter = new Rect(0, 0, 1, 1); + static readonly Vector2[] splitedCharacterPosition = {Vector2.up, Vector2.one, Vector2.right, Vector2.zero}; - /// - /// Gets effect for area. - /// - public static Rect GetEffectArea (this EffectArea area, VertexHelper vh, Rect rectangle, float aspectRatio = -1) - { - Rect rect = default(Rect); - switch (area) - { - case EffectArea.RectTransform: - rect = rectangle; - break; - case EffectArea.Character: - rect = rectForCharacter; - break; - case EffectArea.Fit: - // Fit to contents. - UIVertex vertex = default (UIVertex); - float xMin = float.MaxValue; - float yMin = float.MaxValue; - float xMax = float.MinValue; - float yMax = float.MinValue; - for (int i = 0; i < vh.currentVertCount; i++) - { - vh.PopulateUIVertex(ref vertex, i); - float x = vertex.position.x; - float y = vertex.position.y; - xMin = Mathf.Min(xMin, x); - yMin = Mathf.Min(yMin, y); - xMax = Mathf.Max(xMax, x); - yMax = Mathf.Max(yMax, y); - } - rect.Set (xMin, yMin, xMax - xMin, yMax - yMin); - break; - default: - rect = rectangle; - break; - } + /// + /// Gets effect for area. + /// + public static Rect GetEffectArea(this EffectArea area, VertexHelper vh, Rect rectangle, float aspectRatio = -1) + { + Rect rect = default(Rect); + switch (area) + { + case EffectArea.RectTransform: + rect = rectangle; + break; + case EffectArea.Character: + rect = rectForCharacter; + break; + case EffectArea.Fit: + // Fit to contents. + UIVertex vertex = default(UIVertex); + float xMin = float.MaxValue; + float yMin = float.MaxValue; + float xMax = float.MinValue; + float yMax = float.MinValue; + for (int i = 0; i < vh.currentVertCount; i++) + { + vh.PopulateUIVertex(ref vertex, i); + float x = vertex.position.x; + float y = vertex.position.y; + xMin = Mathf.Min(xMin, x); + yMin = Mathf.Min(yMin, y); + xMax = Mathf.Max(xMax, x); + yMax = Mathf.Max(yMax, y); + } + rect.Set(xMin, yMin, xMax - xMin, yMax - yMin); + break; + default: + rect = rectangle; + break; + } - if(0 < aspectRatio) - { - if (rect.width < rect.height) - { - rect.width = rect.height * aspectRatio; - } - else - { - rect.height = rect.width / aspectRatio; - } - } - return rect; - } - /// - /// Gets position factor for area. - /// - public static void GetPositionFactor (this EffectArea area, int index, Rect rect, Vector2 position, bool isText, bool isTMPro, out float x, out float y) - { - if (isText && area == EffectArea.Character) - { - index = isTMPro ? (index + 3) % 4 : index % 4; - x = splitedCharacterPosition [index].x; - y = splitedCharacterPosition [index].y; - } - else if (area == EffectArea.Fit) - { - x = Mathf.Clamp01 ((position.x - rect.xMin) / rect.width); - y = Mathf.Clamp01 ((position.y - rect.yMin) / rect.height); - } - else - { - x = Mathf.Clamp01 (position.x / rect.width + 0.5f); - y = Mathf.Clamp01 (position.y / rect.height + 0.5f); - } - } + if (0 < aspectRatio) + { + if (rect.width < rect.height) + { + rect.width = rect.height * aspectRatio; + } + else + { + rect.height = rect.width / aspectRatio; + } + } - /// - /// Normalize vertex position by local matrix. - /// - public static void GetNormalizedFactor (this EffectArea area, int index, Matrix2x3 matrix, Vector2 position, bool isText, out Vector2 nomalizedPos) - { - if (isText && area == EffectArea.Character) - { - nomalizedPos = matrix * splitedCharacterPosition [(index + 3) % 4]; - } - else - { - nomalizedPos = matrix * position; - } - } - } + return rect; + } + + /// + /// Gets position factor for area. + /// + public static void GetPositionFactor(this EffectArea area, int index, Rect rect, Vector2 position, bool isText, + bool isTMPro, out float x, out float y) + { + if (isText && area == EffectArea.Character) + { + index = isTMPro ? (index + 3) % 4 : index % 4; + x = splitedCharacterPosition[index].x; + y = splitedCharacterPosition[index].y; + } + else if (area == EffectArea.Fit) + { + x = Mathf.Clamp01((position.x - rect.xMin) / rect.width); + y = Mathf.Clamp01((position.y - rect.yMin) / rect.height); + } + else + { + x = Mathf.Clamp01(position.x / rect.width + 0.5f); + y = Mathf.Clamp01(position.y / rect.height + 0.5f); + } + } + + /// + /// Normalize vertex position by local matrix. + /// + public static void GetNormalizedFactor(this EffectArea area, int index, Matrix2x3 matrix, Vector2 position, + bool isText, out Vector2 nomalizedPos) + { + if (isText && area == EffectArea.Character) + { + nomalizedPos = matrix * splitedCharacterPosition[(index + 3) % 4]; + } + else + { + nomalizedPos = matrix * position; + } + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectMode.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectMode.cs index ea04f2eb..8c085784 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectMode.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/EffectMode.cs @@ -1,14 +1,14 @@ namespace Coffee.UIExtensions { - /// - /// Effect mode. - /// - public enum EffectMode - { - None = 0, - Grayscale = 1, - Sepia = 2, - Nega = 3, - Pixel = 4, - } -} \ No newline at end of file + /// + /// Effect mode. + /// + public enum EffectMode + { + None = 0, + Grayscale = 1, + Sepia = 2, + Nega = 3, + Pixel = 4, + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ShadowStyle.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ShadowStyle.cs index 4544ced4..d78b7187 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ShadowStyle.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/Enums/ShadowStyle.cs @@ -1,14 +1,14 @@ namespace Coffee.UIExtensions { - /// - /// Shadow effect style. - /// - public enum ShadowStyle - { - None = 0, - Shadow, - Outline, - Outline8, - Shadow3, - } -} \ No newline at end of file + /// + /// Shadow effect style. + /// + public enum ShadowStyle + { + None = 0, + Shadow, + Outline, + Outline8, + Shadow3, + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs index 41fe1ee8..1956fc28 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs @@ -9,443 +9,311 @@ namespace Coffee.UIExtensions { - /// - /// Dissolve effect for uGUI. - /// - [AddComponentMenu("UI/UIEffect/UIDissolve", 3)] - public class UIDissolve : UIEffectBase, IMaterialModifier - { - //################################ - // Constant or Static Members. - //################################ - static readonly ParameterTexture _ptex = new ParameterTexture(8, 128, "_ParamTex"); - - - //################################ - // Serialize Members. - //################################ - [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] Texture m_NoiseTexture; - - [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; - - #pragma warning disable 0414 - [Obsolete][HideInInspector] - [SerializeField][Range(0.1f, 10)] float m_Duration = 1; - [Obsolete][HideInInspector] - [SerializeField] AnimatorUpdateMode m_UpdateMode = AnimatorUpdateMode.Normal; - #pragma warning restore 0414 - - - //################################ - // Public Members. - //################################ - - /// - /// Effect factor between 0(start) and 1(end). - /// - [System.Obsolete("Use effectFactor instead (UnityUpgradable) -> effectFactor")] - public float location - { - get { return m_EffectFactor; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_EffectFactor, value)) - { - m_EffectFactor = value; - SetEffectDirty(); - } - } - } - - /// - /// Effect factor between 0(start) and 1(end). - /// - public float effectFactor - { - get { return m_EffectFactor; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_EffectFactor, value)) - { - m_EffectFactor = value; - SetEffectDirty(); - } - } - } - - /// - /// Edge width. - /// - public float width - { - get { return m_Width; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Width, value)) - { - m_Width = value; - SetEffectDirty(); - } - } - } - - /// - /// Edge softness. - /// - public float softness - { - get { return m_Softness; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Softness, value)) - { - m_Softness = value; - SetEffectDirty(); - } - } - } - - /// - /// Edge color. - /// - public Color color - { - get { return m_Color; } - set - { - if (m_Color != value) - { - m_Color = value; - SetEffectDirty(); - } - } - } - - /// - /// Noise texture. - /// - public Texture noiseTexture - { - get { return m_NoiseTexture ?? material.GetTexture("_NoiseTex"); } - set - { - if (m_NoiseTexture != value) - { - m_NoiseTexture = value; - if (graphic) - { - graphic.SetMaterialDirty(); - } - } - } - } - - /// - /// The area for effect. - /// - public EffectArea effectArea - { - get { return m_EffectArea; } - set - { - if (m_EffectArea != value) - { - m_EffectArea = value; - SetVerticesDirty(); - } - } - } - - /// - /// Keep aspect ratio. - /// - public bool keepAspectRatio - { - get { return m_KeepAspectRatio; } - set - { - if (m_KeepAspectRatio != value) - { - m_KeepAspectRatio = value; - SetVerticesDirty (); - } - } - } - - /// - /// Color effect mode. - /// - public ColorMode colorMode - { - get { return m_ColorMode; } - set - { - if (m_ColorMode != value) - { - m_ColorMode = value; - if (graphic) - { - graphic.SetMaterialDirty(); - } - } - } - } - - /// - /// Play effect on enable. - /// - [System.Obsolete("Use Play/Stop method instead")] - public bool play { get { return _player.play; } set { _player.play = value; } } - - /// - /// Play effect loop. - /// - [System.Obsolete] - public bool loop { get { return _player.loop; } set { _player.loop = value; } } - - /// - /// The duration for playing effect. - /// - public float duration { get { return _player.duration; } set { _player.duration = Mathf.Max(value, 0.1f); } } - - /// - /// Delay on loop effect. - /// - [System.Obsolete] - public float loopDelay { get { return _player.loopDelay; } set { _player.loopDelay = Mathf.Max(value, 0); } } - - /// - /// Update mode for playing effect. - /// - public AnimatorUpdateMode updateMode { get { return _player.updateMode; } set { _player.updateMode = value; } } - - /// - /// Reverse the dissolve effect. - /// - public bool reverse { get { return m_Reverse; } set { m_Reverse = value; } } - - /// - /// Gets the parameter texture. - /// - public override ParameterTexture ptex { get { return _ptex; } } - - public override Hash128 GetMaterialHash(Material material) - { - if(!isActiveAndEnabled || !material || !material.shader) - return new Hash128(); - - uint materialId = (uint)material.GetInstanceID(); - uint shaderId = 0 << 3; - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - shaderId += 2; - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - shaderId += 0; - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - shaderId += 1; - } - else - { - shaderId += 0; - } - - - uint shaderVariantId = (uint)((int)m_ColorMode << 6); - uint resourceId = m_NoiseTexture ? (uint)m_NoiseTexture.GetInstanceID() : 0; - return new Hash128( - materialId, - shaderId + shaderVariantId, - resourceId, - 0 - ); - } - - public override void ModifyMaterial(Material material) - { - Debug.LogFormat(this, $"ModifyMaterial {material}"); - - string materialShaderName = material.shader.name; - if(materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UIDissolve)"); - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Dissolve"); - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Distance Field (UIDissolve)"); - } - else - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Dissolve"); - } - - SetShaderVariants(material, m_ColorMode); - - if(m_NoiseTexture) - { - material.SetTexture("_NoiseTex", m_NoiseTexture); - } - ptex.RegisterMaterial (material); - } - - /// - /// Modifies the material. - /// - [ContextMenu("ModifyMaterial")] - public override void ModifyMaterial() - { - } - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled) - return; - - bool isText = isTMPro || graphic is Text; - float normalizedIndex = ptex.GetNormalizedIndex(this); - - // rect. - var tex = noiseTexture; - var aspectRatio = m_KeepAspectRatio && tex ? ((float)tex.width) / tex.height : -1; - Rect rect = m_EffectArea.GetEffectArea(vh, rectTransform.rect, aspectRatio); - - // Calculate vertex position. - UIVertex vertex = default(UIVertex); - float x, y; - int count = vh.currentVertCount; - for (int i = 0; i < count; i++) - { - vh.PopulateUIVertex(ref vertex, i); - m_EffectArea.GetPositionFactor (i, rect, vertex.position, isText, isTMPro, 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 SetEffectDirty() - { - ptex.SetData(this, 0, m_EffectFactor); // param1.x : location - ptex.SetData(this, 1, m_Width); // param1.y : width - ptex.SetData(this, 2, m_Softness); // param1.z : softness - ptex.SetData(this, 4, m_Color.r); // param2.x : red - ptex.SetData(this, 5, m_Color.g); // param2.y : green - ptex.SetData(this, 6, m_Color.b); // param2.z : blue - } - - /// - /// Play effect. - /// - public void Play(bool reset = true) - { - _player.Play(reset); - } - - /// - /// Stop effect. - /// - public void Stop(bool reset = true) - { - _player.Stop(reset); - } - - //################################ - // Protected Members. - //################################ - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable(); - _player.OnEnable((f) =>effectFactor = m_Reverse ? 1f - f : f); - } - - protected override void OnDisable() - { - base.OnDisable (); - _player.OnDisable(); - } - -#if UNITY_EDITOR - /// - /// Gets the material. - /// - /// The material. - protected override Material GetMaterial() - { - return null; - } - - #pragma warning disable 0612 - protected override void UpgradeIfNeeded() - { - // Upgrade for v3.0.0 - if (IsShouldUpgrade(300)) - { - _player.play = false; - _player.duration = m_Duration; - _player.loop = false; - _player.loopDelay = 1; - _player.updateMode = m_UpdateMode; - } - } - -#pragma warning restore 0612 -#endif - - //################################ - // Private Members. - //################################ - EffectPlayer _player{ get { return m_Player ?? (m_Player = new EffectPlayer()); } } - } + /// + /// Dissolve effect for uGUI. + /// + [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; + + /// + /// Effect factor between 0(start) and 1(end). + /// + 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(); + } + } + + /// + /// Edge width. + /// + 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(); + } + } + + /// + /// Edge softness. + /// + 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(); + } + } + + /// + /// Edge color. + /// + public Color color + { + get { return m_Color; } + set + { + if (m_Color == value) return; + m_Color = value; + SetEffectParamsDirty(); + } + } + + /// + /// Noise texture. + /// + public Texture transitionTexture + { + get + { + return m_TransitionTexture + ? m_TransitionTexture + : defaultTransitionTexture; + } + set + { + if (m_TransitionTexture == value) return; + m_TransitionTexture = value; + SetMaterialDirty(); + } + } + + /// + /// The area for effect. + /// + public EffectArea effectArea + { + get { return m_EffectArea; } + set + { + if (m_EffectArea == value) return; + m_EffectArea = value; + SetVerticesDirty(); + } + } + + /// + /// Keep aspect ratio. + /// + public bool keepAspectRatio + { + get { return m_KeepAspectRatio; } + set + { + if (m_KeepAspectRatio == value) return; + m_KeepAspectRatio = value; + SetVerticesDirty(); + } + } + + /// + /// Color effect mode. + /// + public ColorMode colorMode + { + get { return m_ColorMode; } + set + { + if (m_ColorMode == value) return; + m_ColorMode = value; + SetMaterialDirty(); + } + } + + /// + /// Gets the parameter texture. + /// + 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 + ); + } + + + private static Texture defaultTransitionTexture + { + get + { + return _defaultTransitionTexture + ? _defaultTransitionTexture + : (_defaultTransitionTexture = Resources.Load("Default-Transition")); + } + } + + public override void ModifyMaterial(Material newMaterial) + { + newMaterial.shader = connector.FindShader("UIDissolve"); + SetShaderVariants(newMaterial, m_ColorMode); + + newMaterial.SetTexture(k_TransitionTexId, transitionTexture); + paramTex.RegisterMaterial(newMaterial); + } + + /// + /// Modifies the mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + 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(); + } + + /// + /// Play effect. + /// + public void Play(bool reset = true) + { + _player.Play(reset); + } + + /// + /// Stop effect. + /// + 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()); } + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs.meta index f8c4e73c..80d3101c 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs.meta +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs.meta @@ -1,13 +1,11 @@ fileFormatVersion: 2 guid: ce76b7a490bd74f34a2f2752641aea24 -timeCreated: 1538866738 -licenseType: Pro MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: - - m_EffectMaterial: {instanceID: 0} - - m_PtexMaterial: {instanceID: 0} - - m_NoiseTexture: {fileID: 2800000, guid: 3e04c247fb2604af186173fce0bc62de, type: 3} + - m_TransitionTexture: {fileID: 2800000, guid: 3e04c247fb2604af186173fce0bc62de, + type: 3} executionOrder: 0 icon: {instanceID: 0} userData: diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs index 7e7951b4..9ffbe43a 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs @@ -3,604 +3,381 @@ using UnityEngine; using UnityEngine.UI; using UnityEngine.Serialization; - #if UNITY_EDITOR using System.IO; using System.Linq; using UnityEditor; + #endif namespace Coffee.UIExtensions { - /// - /// UIEffect. - /// - [ExecuteInEditMode] - [RequireComponent(typeof(Graphic))] - [DisallowMultipleComponent] - [AddComponentMenu("UI/UIEffect/UIEffect", 1)] - public class UIEffect : UIEffectBase, IMaterialModifier - { - //################################ - // Constant or Static Members. - //################################ - public const string shaderName = "UI/Hidden/UI-Effect"; - static readonly ParameterTexture _ptex = new ParameterTexture(8, 1024, "_ParamTex"); - - - //################################ - // Serialize Members. - //################################ - [FormerlySerializedAs("m_ToneLevel")] - [Tooltip("Effect factor between 0(no effect) and 1(complete effect).")] - [SerializeField][Range(0, 1)] float m_EffectFactor = 1; - - [Tooltip("Color effect factor between 0(no effect) and 1(complete effect).")] - [SerializeField][Range(0, 1)] float m_ColorFactor = 1; - - [FormerlySerializedAs("m_Blur")] - [Tooltip("How far is the blurring from the graphic.")] - [SerializeField][Range(0, 1)] float m_BlurFactor = 1; - - [FormerlySerializedAs("m_ToneMode")] - [Tooltip("Effect mode")] - [SerializeField] EffectMode m_EffectMode = EffectMode.None; - - [Tooltip("Color effect mode")] - [SerializeField] ColorMode m_ColorMode = ColorMode.Multiply; - - [Tooltip ("Color for effect")] - [ColorUsage(false)] - [SerializeField] Color m_EffectColor = Color.white; - - [Tooltip ("Blur effect mode")] - [SerializeField] BlurMode m_BlurMode = BlurMode.None; - - [Tooltip("Advanced blurring remove common artifacts in the blur effect for uGUI.")] - [SerializeField] bool m_AdvancedBlur = false; - - #pragma warning disable 0414 - [Obsolete][HideInInspector] - [SerializeField][Range(0, 1)] float m_ShadowBlur = 1; - [Obsolete][HideInInspector] - [SerializeField] ShadowStyle m_ShadowStyle; - [Obsolete][HideInInspector] - [SerializeField] Color m_ShadowColor = Color.black; - [Obsolete][HideInInspector] - [SerializeField] Vector2 m_EffectDistance = new Vector2(1f, -1f); - [Obsolete][HideInInspector] - [SerializeField] bool m_UseGraphicAlpha = true; - [Obsolete][HideInInspector] - [SerializeField] List m_AdditionalShadows = new List(); - #pragma warning restore 0414 - - public enum BlurEx - { - None = 0, - Ex = 1, - } - - //################################ - // Public Members. - //################################ -#if UNITY_2017_1_OR_NEWER - public override AdditionalCanvasShaderChannels requiredChannels - { - get - { - if (advancedBlur) - { - return isTMPro - ? AdditionalCanvasShaderChannels.TexCoord1 | AdditionalCanvasShaderChannels.TexCoord2 - : AdditionalCanvasShaderChannels.TexCoord1; - } - return AdditionalCanvasShaderChannels.None; - } - } -#endif - - /// - /// Effect factor between 0(no effect) and 1(complete effect). - /// - [System.Obsolete("Use effectFactor instead (UnityUpgradable) -> effectFactor")] - public float toneLevel - { - get { return m_EffectFactor; } - set - { - m_EffectFactor = Mathf.Clamp(value, 0, 1); - SetEffectDirty (); - } - } - - /// - /// Effect factor between 0(no effect) and 1(complete effect). - /// - public float effectFactor - { - get { return m_EffectFactor; } - set - { - m_EffectFactor = Mathf.Clamp(value, 0, 1); - SetEffectDirty (); - } - } - - /// - /// Color effect factor between 0(no effect) and 1(complete effect). - /// - public float colorFactor - { - get { return m_ColorFactor; } - set - { - m_ColorFactor = Mathf.Clamp(value, 0, 1); - SetEffectDirty (); - } - } - - /// - /// How far is the blurring from the graphic. - /// - [System.Obsolete("Use blurFactor instead (UnityUpgradable) -> blurFactor")] - public float blur - { - get { return m_BlurFactor; } - set - { - m_BlurFactor = Mathf.Clamp(value, 0, 1); - SetEffectDirty (); - } - } - - /// - /// How far is the blurring from the graphic. - /// - [System.Obsolete("Use effectFactor instead (UnityUpgradable) -> effectFactor")] - public float blurFactor - { - get { return m_BlurFactor; } - set - { - m_BlurFactor = Mathf.Clamp(value, 0, 1); - SetEffectDirty (); - } - } - - /// - /// Effect mode(readonly). - /// - [System.Obsolete("Use effectMode instead (UnityUpgradable) -> effectMode")] - public EffectMode toneMode { get { return m_EffectMode; } } - - /// - /// Effect mode(readonly). - /// - public EffectMode effectMode - { - get { return m_EffectMode; } - set - { - if (m_EffectMode != value) - { - m_EffectMode = value; - if (graphic) - { - graphic.SetMaterialDirty (); - } - } - } - } - - /// - /// Color effect mode. - /// - public ColorMode colorMode - { - get { return m_ColorMode; } - set - { - if (m_ColorMode != value) - { - m_ColorMode = value; - if (graphic) - { - graphic.SetMaterialDirty (); - } - } - } - } - - /// - /// Blur effect mode(readonly). - /// - public BlurMode blurMode - { - get { return m_BlurMode; } - set - { - if (m_BlurMode != value) - { - m_BlurMode = value; - if (graphic) - { - graphic.SetMaterialDirty (); - } - } - } - } - - /// - /// Color for the color effect. - /// - public Color effectColor - { - get { return m_EffectColor; } - set - { - if (m_EffectColor != value) - { - m_EffectColor = value; - SetEffectDirty (); - } - } - } - - /// - /// Gets the parameter texture. - /// - public override ParameterTexture ptex { get { return _ptex; } } - - /// - /// Advanced blurring remove common artifacts in the blur effect for uGUI. - /// - public bool advancedBlur - { - get { return m_AdvancedBlur; } - set - { - if (m_AdvancedBlur != value) - { - m_AdvancedBlur = value; - if (graphic) - { - graphic.SetMaterialDirty (); - } - } - } - } - - public override Hash128 GetMaterialHash (Material material) - { - if (!isActiveAndEnabled || !material || !material.shader) - return new Hash128 (); - - uint materialId = (uint)material.GetInstanceID (); - uint shaderId = 2 << 3; - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - shaderId += 2; - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - shaderId += 0; - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - shaderId += 1; - } - else - { - shaderId += 0; - } - - - uint shaderVariantId = (uint)(((int)m_EffectMode << 6) + ((int)m_ColorMode << 9) + ((int)m_BlurMode << 11) + ((m_AdvancedBlur ? 1 : 0) << 13)); - return new Hash128 ( - materialId, - shaderId + shaderVariantId, - 0, - 0 - ); - } - - public override void ModifyMaterial (Material material) - { - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UIEffect)"); - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect"); - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Distance Field (UIEffect)"); - } - else - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect"); - } - - SetShaderVariants (material, m_EffectMode, m_ColorMode, m_BlurMode, m_AdvancedBlur ? BlurEx.Ex : BlurEx.None); - - ptex.RegisterMaterial (material); - } - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled) - { - return; - } - - float normalizedIndex = ptex.GetNormalizedIndex(this); - - if (m_BlurMode != BlurMode.None && advancedBlur) - { - vh.GetUIVertexStream(tempVerts); - vh.Clear(); - var count = tempVerts.Count; - - // Bundle - int bundleSize = (targetGraphic is Text || isTMPro) ? 6 : count; - Rect posBounds = default(Rect); - Rect uvBounds = default(Rect); - Vector3 size = default(Vector3); - Vector3 tPos = default(Vector3); - Vector3 tUV = default(Vector3); - float expand = (float)blurMode * 6 * 2; - - for (int i = 0; i < count; i += bundleSize) - { - // min/max for bundled-quad - GetBounds(tempVerts, i, bundleSize, ref posBounds, ref uvBounds, true); - - // Pack uv mask. - Vector2 uvMask = new Vector2(Packer.ToFloat(uvBounds.xMin, uvBounds.yMin), Packer.ToFloat(uvBounds.xMax, uvBounds.yMax)); - - // Quad - for (int j = 0; j < bundleSize; j += 6) - { - Vector3 cornerPos1 = tempVerts[i + j + 1].position; - Vector3 cornerPos2 = tempVerts[i + j + 4].position; - - // Is outer quad? - bool hasOuterEdge = (bundleSize == 6) - || !posBounds.Contains(cornerPos1) - || !posBounds.Contains(cornerPos2); - if (hasOuterEdge) - { - Vector3 cornerUv1 = tempVerts[i + j + 1].uv0; - Vector3 cornerUv2 = tempVerts[i + j + 4].uv0; - - Vector3 centerPos = (cornerPos1 + cornerPos2) / 2; - Vector3 centerUV = (cornerUv1 + cornerUv2) / 2; - size = (cornerPos1 - cornerPos2); - - size.x = 1 + expand / Mathf.Abs(size.x); - size.y = 1 + expand / Mathf.Abs(size.y); - size.z = 1 + expand / Mathf.Abs(size.z); - - tPos = centerPos - Vector3.Scale(size, centerPos); - tUV = centerUV - Vector3.Scale(size, centerUV); - } - - // Vertex - for (int k = 0; k < 6; k++) - { - UIVertex vt = tempVerts[i + j + k]; - - Vector3 pos = vt.position; - Vector2 uv0 = vt.uv0; - - if (hasOuterEdge && (pos.x < posBounds.xMin || posBounds.xMax < pos.x)) - { - pos.x = pos.x * size.x + tPos.x; - uv0.x = uv0.x * size.x + tUV.x; - } - if (hasOuterEdge && (pos.y < posBounds.yMin || posBounds.yMax < pos.y)) - { - pos.y = pos.y * size.y + tPos.y; - uv0.y = uv0.y * size.y + tUV.y; - } - - vt.uv0 = new Vector2(Packer.ToFloat((uv0.x + 0.5f) / 2f, (uv0.y + 0.5f) / 2f), normalizedIndex); - vt.position = pos; - - if (isTMPro) - { - #if UNITY_2017_1_OR_NEWER - vt.uv2 = uvMask; - #endif - } - else - { - vt.uv1 = uvMask; - } - - tempVerts[i + j + k] = vt; - } - } - } - - vh.AddUIVertexTriangleStream(tempVerts); - tempVerts.Clear(); - } - else - { - int count = vh.currentVertCount; - UIVertex vt = default(UIVertex); - for (int i = 0; i < count; i++) - { - vh.PopulateUIVertex(ref vt, i); - Vector2 uv0 = vt.uv0; - vt.uv0 = new Vector2( - Packer.ToFloat((uv0.x + 0.5f) / 2f, (uv0.y + 0.5f) / 2f), - normalizedIndex - ); - vh.SetUIVertex(vt, i); - } - } - } - - protected override void SetEffectDirty () - { - ptex.SetData(this, 0, m_EffectFactor); // param.x : effect factor - ptex.SetData(this, 1, m_ColorFactor); // param.y : color factor - ptex.SetData(this, 2, m_BlurFactor); // param.z : blur factor - ptex.SetData (this, 4, m_EffectColor.r); // param2.x : red - ptex.SetData (this, 5, m_EffectColor.g); // param2.y : green - ptex.SetData (this, 6, m_EffectColor.b); // param2.z : blue - } - -#if UNITY_EDITOR - /// - /// Gets the material. - /// - /// The material. - protected override Material GetMaterial() - { - return null; - } - - #pragma warning disable 0612 - protected override void UpgradeIfNeeded() - { - // Upgrade for v3.0.0 - if (IsShouldUpgrade(300)) - { - if (m_ColorMode != ColorMode.Multiply) - { - Color col = targetGraphic.color; - col.r = m_EffectColor.r; - col.g = m_EffectColor.g; - col.b = m_EffectColor.b; - targetGraphic.color = col; - m_ColorFactor = m_EffectColor.a; - } - - if (m_ShadowStyle != ShadowStyle.None || m_AdditionalShadows.Any(x => x.style != ShadowStyle.None)) - { - if (m_ShadowStyle != ShadowStyle.None) - { - var shadow = gameObject.GetComponent() ?? gameObject.AddComponent(); - shadow.style = m_ShadowStyle; - shadow.effectDistance = m_EffectDistance; - shadow.effectColor = m_ShadowColor; - shadow.useGraphicAlpha = m_UseGraphicAlpha; - shadow.blurFactor = m_ShadowBlur; - } - - foreach (var s in m_AdditionalShadows) - { - if (s.style == ShadowStyle.None) - { - continue; - } - - var shadow = gameObject.AddComponent(); - shadow.style = s.style; - shadow.effectDistance = s.effectDistance; - shadow.effectColor = s.effectColor; - shadow.useGraphicAlpha = s.useGraphicAlpha; - shadow.blurFactor = s.blur; - } - - m_ShadowStyle = ShadowStyle.None; - m_AdditionalShadows = null; - - if (m_EffectMode == EffectMode.None && m_ColorMode == ColorMode.Multiply && m_BlurMode == BlurMode.None) - { - DestroyImmediate(this, true); - } - } - - int tone = (int)m_EffectMode; - const int Mono = 5; - const int Cutoff = 6; - const int Hue = 7; - if (tone == Hue) - { - var go = gameObject; - var hue = m_EffectFactor; - DestroyImmediate(this, true); - var hsv = go.GetComponent() ?? go.AddComponent(); - hsv.hue = hue; - hsv.range = 1; - } - - // Cutoff/Mono - if (tone == Cutoff || tone == Mono) - { - var go = gameObject; - var factor = m_EffectFactor; - var transitionMode = tone == Cutoff - ? UITransitionEffect.EffectMode.Cutoff - : UITransitionEffect.EffectMode.Fade; - DestroyImmediate(this, true); - var trans = go.GetComponent() ?? go.AddComponent(); - trans.effectFactor = factor; - - var sp = new SerializedObject(trans).FindProperty("m_EffectMode"); - sp.intValue = (int)transitionMode; - sp.serializedObject.ApplyModifiedProperties(); - } - } - } - #pragma warning restore 0612 -#endif - - //################################ - // Private Members. - //################################ - static void GetBounds(List verts, int start, int count, ref Rect posBounds, ref Rect uvBounds, bool global) - { - Vector2 minPos = new Vector2(float.MaxValue, float.MaxValue); - Vector2 maxPos = new Vector2(float.MinValue, float.MinValue); - Vector2 minUV = new Vector2(float.MaxValue, float.MaxValue); - Vector2 maxUV = new Vector2(float.MinValue, float.MinValue); - for (int i = start; i < start + count; i++) - { - UIVertex vt = verts[i]; - - Vector2 uv = vt.uv0; - Vector3 pos = vt.position; - - // Left-Bottom - if (minPos.x >= pos.x && minPos.y >= pos.y) - { - minPos = pos; - } - // Right-Top - else if (maxPos.x <= pos.x && maxPos.y <= pos.y) - { - maxPos = pos; - } - - // Left-Bottom - if (minUV.x >= uv.x && minUV.y >= uv.y) - { - minUV = uv; - } - // Right-Top - else if (maxUV.x <= uv.x && maxUV.y <= uv.y) - { - maxUV = uv; - } - } - - // Shrink coordinate for detect edge - posBounds.Set(minPos.x + 0.001f, minPos.y + 0.001f, maxPos.x - minPos.x - 0.002f, maxPos.y - minPos.y - 0.002f); - uvBounds.Set(minUV.x, minUV.y, maxUV.x - minUV.x, maxUV.y - minUV.y); - } - } + /// + /// UIEffect. + /// + [ExecuteInEditMode] + [RequireComponent(typeof(Graphic))] + [DisallowMultipleComponent] + [AddComponentMenu("UI/UIEffects/UIEffect", 1)] + public class UIEffect : BaseMaterialEffect, IMaterialModifier + { + private const uint k_ShaderId = 2 << 3; + private static readonly ParameterTexture s_ParamTex = new ParameterTexture(4, 1024, "_ParamTex"); + + [FormerlySerializedAs("m_ToneLevel")] + [Tooltip("Effect factor between 0(no effect) and 1(complete effect).")] + [SerializeField] + [Range(0, 1)] + float m_EffectFactor = 1; + + [Tooltip("Color effect factor between 0(no effect) and 1(complete effect).")] [SerializeField] [Range(0, 1)] + float m_ColorFactor = 1; + + [FormerlySerializedAs("m_Blur")] + [Tooltip("How far is the blurring from the graphic.")] + [SerializeField] + [Range(0, 1)] + float m_BlurFactor = 1; + + [FormerlySerializedAs("m_ToneMode")] [Tooltip("Effect mode")] [SerializeField] + EffectMode m_EffectMode = EffectMode.None; + + [Tooltip("Color effect mode")] [SerializeField] + ColorMode m_ColorMode = ColorMode.Multiply; + + [Tooltip("Color for effect")] [ColorUsage(false)] [SerializeField] + Color m_EffectColor = Color.white; + + [Tooltip("Blur effect mode")] [SerializeField] + BlurMode m_BlurMode = BlurMode.None; + + [Tooltip("Advanced blurring remove common artifacts in the blur effect for uGUI.")] [SerializeField] + bool m_AdvancedBlur = false; + + private enum BlurEx + { + None = 0, + Ex = 1, + } + + /// + /// Additional canvas shader channels to use this component. + /// + public AdditionalCanvasShaderChannels uvMaskChannel + { + get { return connector.extraChannel; } + } + + /// + /// Effect factor between 0(no effect) and 1(complete effect). + /// + 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(); + } + } + + /// + /// Color effect factor between 0(no effect) and 1(complete effect). + /// + public float colorFactor + { + get { return m_ColorFactor; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_ColorFactor, value)) return; + m_ColorFactor = value; + SetEffectParamsDirty(); + } + } + + /// + /// How far is the blurring from the graphic. + /// + public float blurFactor + { + get { return m_BlurFactor; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_BlurFactor, value)) return; + m_BlurFactor = value; + SetEffectParamsDirty(); + } + } + + /// + /// Effect mode. + /// + public EffectMode effectMode + { + get { return m_EffectMode; } + set + { + if (m_EffectMode == value) return; + m_EffectMode = value; + SetMaterialDirty(); + } + } + + /// + /// Color effect mode. + /// + public ColorMode colorMode + { + get { return m_ColorMode; } + set + { + if (m_ColorMode == value) return; + m_ColorMode = value; + SetMaterialDirty(); + } + } + + /// + /// Blur effect mode(readonly). + /// + public BlurMode blurMode + { + get { return m_BlurMode; } + set + { + if (m_BlurMode == value) return; + m_BlurMode = value; + SetMaterialDirty(); + } + } + + /// + /// Color for the color effect. + /// + public Color effectColor + { + get { return m_EffectColor; } + set + { + if (m_EffectColor == value) return; + m_EffectColor = value; + SetEffectParamsDirty(); + } + } + + /// + /// Gets the parameter texture. + /// + public override ParameterTexture paramTex + { + get { return s_ParamTex; } + } + + /// + /// Advanced blurring remove common artifacts in the blur effect for uGUI. + /// + public bool advancedBlur + { + get { return m_AdvancedBlur; } + set + { + if (m_AdvancedBlur == value) return; + m_AdvancedBlur = value; + SetVerticesDirty(); + SetMaterialDirty(); + } + } + + public override Hash128 GetMaterialHash(Material material) + { + if (!isActiveAndEnabled || !material || !material.shader) + return k_InvalidHash; + + var shaderVariantId = (uint) (((int) m_EffectMode << 6) + ((int) m_ColorMode << 9) + + ((int) m_BlurMode << 11) + ((m_AdvancedBlur ? 1 : 0) << 13)); + return new Hash128( + (uint) material.GetInstanceID(), + k_ShaderId + shaderVariantId, + 0, + 0 + ); + } + + public override void ModifyMaterial(Material newMaterial) + { + newMaterial.shader = connector.FindShader("UIEffect"); + SetShaderVariants(newMaterial, m_EffectMode, m_ColorMode, m_BlurMode, + m_AdvancedBlur ? BlurEx.Ex : BlurEx.None); + + paramTex.RegisterMaterial(newMaterial); + } + + /// + /// Modifies the mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + if (!isActiveAndEnabled) + { + return; + } + + var normalizedIndex = paramTex.GetNormalizedIndex(this); + + if (m_BlurMode != BlurMode.None && advancedBlur) + { + vh.GetUIVertexStream(s_TempVerts); + vh.Clear(); + var count = s_TempVerts.Count; + + // Bundle + int bundleSize = connector.IsText(graphic) ? 6 : count; + Rect posBounds = default(Rect); + Rect uvBounds = default(Rect); + Vector3 size = default(Vector3); + Vector3 tPos = default(Vector3); + Vector3 tUV = default(Vector3); + float expand = (float) blurMode * 6 * 2; + + for (int i = 0; i < count; i += bundleSize) + { + // min/max for bundled-quad + GetBounds(s_TempVerts, i, bundleSize, ref posBounds, ref uvBounds, true); + + // Pack uv mask. + Vector2 uvMask = new Vector2(Packer.ToFloat(uvBounds.xMin, uvBounds.yMin), + Packer.ToFloat(uvBounds.xMax, uvBounds.yMax)); + + // Quad + for (int j = 0; j < bundleSize; j += 6) + { + Vector3 cornerPos1 = s_TempVerts[i + j + 1].position; + Vector3 cornerPos2 = s_TempVerts[i + j + 4].position; + + // Is outer quad? + bool hasOuterEdge = (bundleSize == 6) + || !posBounds.Contains(cornerPos1) + || !posBounds.Contains(cornerPos2); + if (hasOuterEdge) + { + Vector3 cornerUv1 = s_TempVerts[i + j + 1].uv0; + Vector3 cornerUv2 = s_TempVerts[i + j + 4].uv0; + + Vector3 centerPos = (cornerPos1 + cornerPos2) / 2; + Vector3 centerUV = (cornerUv1 + cornerUv2) / 2; + size = (cornerPos1 - cornerPos2); + + size.x = 1 + expand / Mathf.Abs(size.x); + size.y = 1 + expand / Mathf.Abs(size.y); + size.z = 1 + expand / Mathf.Abs(size.z); + + tPos = centerPos - Vector3.Scale(size, centerPos); + tUV = centerUV - Vector3.Scale(size, centerUV); + } + + // Vertex + for (int k = 0; k < 6; k++) + { + UIVertex vt = s_TempVerts[i + j + k]; + + Vector3 pos = vt.position; + Vector2 uv0 = vt.uv0; + + if (hasOuterEdge && (pos.x < posBounds.xMin || posBounds.xMax < pos.x)) + { + pos.x = pos.x * size.x + tPos.x; + uv0.x = uv0.x * size.x + tUV.x; + } + + if (hasOuterEdge && (pos.y < posBounds.yMin || posBounds.yMax < pos.y)) + { + pos.y = pos.y * size.y + tPos.y; + uv0.y = uv0.y * size.y + tUV.y; + } + + vt.uv0 = new Vector2(Packer.ToFloat((uv0.x + 0.5f) / 2f, (uv0.y + 0.5f) / 2f), + normalizedIndex); + vt.position = pos; + + connector.SetExtraChannel(ref vt, uvMask); + + s_TempVerts[i + j + k] = vt; + } + } + } + + vh.AddUIVertexTriangleStream(s_TempVerts); + s_TempVerts.Clear(); + } + else + { + int count = vh.currentVertCount; + UIVertex vt = default(UIVertex); + for (int i = 0; i < count; i++) + { + vh.PopulateUIVertex(ref vt, i); + Vector2 uv0 = vt.uv0; + vt.uv0 = new Vector2( + Packer.ToFloat((uv0.x + 0.5f) / 2f, (uv0.y + 0.5f) / 2f), + normalizedIndex + ); + vh.SetUIVertex(vt, i); + } + } + } + + protected override void SetEffectParamsDirty() + { + paramTex.SetData(this, 0, m_EffectFactor); // param.x : effect factor + paramTex.SetData(this, 1, m_ColorFactor); // param.y : color factor + paramTex.SetData(this, 2, m_BlurFactor); // param.z : blur factor + } + + static void GetBounds(List verts, int start, int count, ref Rect posBounds, ref Rect uvBounds, + bool global) + { + Vector2 minPos = new Vector2(float.MaxValue, float.MaxValue); + Vector2 maxPos = new Vector2(float.MinValue, float.MinValue); + Vector2 minUV = new Vector2(float.MaxValue, float.MaxValue); + Vector2 maxUV = new Vector2(float.MinValue, float.MinValue); + for (int i = start; i < start + count; i++) + { + UIVertex vt = verts[i]; + + Vector2 uv = vt.uv0; + Vector3 pos = vt.position; + + // Left-Bottom + if (minPos.x >= pos.x && minPos.y >= pos.y) + { + minPos = pos; + } + // Right-Top + else if (maxPos.x <= pos.x && maxPos.y <= pos.y) + { + maxPos = pos; + } + + // Left-Bottom + if (minUV.x >= uv.x && minUV.y >= uv.y) + { + minUV = uv; + } + // Right-Top + else if (maxUV.x <= uv.x && maxUV.y <= uv.y) + { + maxUV = uv; + } + } + + // Shrink coordinate for detect edge + posBounds.Set(minPos.x + 0.001f, minPos.y + 0.001f, maxPos.x - minPos.x - 0.002f, + maxPos.y - minPos.y - 0.002f); + uvBounds.Set(minUV.x, minUV.y, maxUV.x - minUV.x, maxUV.y - minUV.y); + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs new file mode 100644 index 00000000..d36740e3 --- /dev/null +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs @@ -0,0 +1,110 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + + +namespace Coffee.UIExtensions +{ + /// + /// Dissolve effect for uGUI. + /// + [ExecuteInEditMode] + public class UIEffectMaterialResolver : MonoBehaviour, IMaterialModifier, IMeshModifier + { + [ContextMenu("test")] + void test() + { + GetComponent().SetMaterialDirty(); + GetComponent().SetVerticesDirty(); + } + + Hash128 _effectMaterialHash; + + + /// + /// This function is called when the object becomes enabled and active. + /// + protected void OnEnable() + { + //GetComponent ().SetMaterialDirty (); + //GetComponent ().SetVerticesDirty (); + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + protected void OnDisable() + { + MaterialCache.Unregister(_effectMaterialHash); + _effectMaterialHash = new Hash128(); + + //GetComponent ().SetMaterialDirty (); + //GetComponent ().SetVerticesDirty (); + } + + protected Hash128 GetMaterialHash(Material material) + { + return new Hash128(); + } + + public Material GetModifiedMaterial(Material baseMaterial) + { + if (!isActiveAndEnabled) + { + return baseMaterial; + } + + var modifier = transform.parent.GetComponent(); + + + var oldHash = _effectMaterialHash; + _effectMaterialHash = modifier.GetMaterialHash(baseMaterial); + var modifiedMaterial = baseMaterial; + if (_effectMaterialHash.isValid) + { + modifiedMaterial = MaterialCache.Register(baseMaterial, _effectMaterialHash, modifier.ModifyMaterial); + } + + MaterialCache.Unregister(oldHash); + + return modifiedMaterial; + } + + //public Material GetModifiedMaterial2 (Material baseMaterial) + //{ + // Debug.Log ("UIEffectMaterialResolver 0"); + // if (!isActiveAndEnabled) + // return baseMaterial; + + // var parent = transform.parent; + // if (!parent) + // return baseMaterial; + + // var modifier = parent.GetComponent (); + // if (modifier == null) + // { + // Debug.Log ("UIEffectMaterialResolver modifier is null"); + // return baseMaterial; + // } + + // Debug.Log ("UIEffectMaterialResolver !!! " + modifier); + // return modifier.GetModifiedMaterial (baseMaterial); + //} + + public void ModifyMesh(Mesh mesh) + { + } + + public void ModifyMesh(VertexHelper verts) + { + if (!isActiveAndEnabled) + { + return; + } + + var modifier = transform.parent.GetComponent(); + modifier.ModifyMesh(verts); + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectMaterialResolver.cs.meta b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs.meta similarity index 100% rename from Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectMaterialResolver.cs.meta rename to Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectChild.cs.meta diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectMaterialResolver.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectMaterialResolver.cs deleted file mode 100644 index 55743b12..00000000 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffectMaterialResolver.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.UI; - - -namespace Coffee.UIExtensions -{ - /// - /// Dissolve effect for uGUI. - /// - [ExecuteInEditMode] - public class UIEffectMaterialResolver : MonoBehaviour, IMaterialModifier, IMeshModifier - { - [ContextMenu("test")] - void test() - { - GetComponent ().SetMaterialDirty (); - GetComponent ().SetVerticesDirty (); - } - - Hash128 _effectMaterialHash; - - - /// - /// This function is called when the object becomes enabled and active. - /// - protected void OnEnable () - { - //GetComponent ().SetMaterialDirty (); - //GetComponent ().SetVerticesDirty (); - } - - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - protected void OnDisable () - { - MaterialRepository.Unregister (_effectMaterialHash); - _effectMaterialHash = new Hash128 (); - - //GetComponent ().SetMaterialDirty (); - //GetComponent ().SetVerticesDirty (); - } - - protected Hash128 GetMaterialHash (Material material) - { - return new Hash128 (); - } - - public Material GetModifiedMaterial (Material baseMaterial) - { - if (!isActiveAndEnabled) - { - return baseMaterial; - } - - var modifier = transform.parent.GetComponent (); - - - var oldHash = _effectMaterialHash; - _effectMaterialHash = modifier.GetMaterialHash (baseMaterial); - var modifiedMaterial = baseMaterial; - if (_effectMaterialHash.isValid) - { - modifiedMaterial = MaterialRepository.Register (baseMaterial, _effectMaterialHash, modifier.ModifyMaterial); - } - MaterialRepository.Unregister (oldHash); - - return modifiedMaterial; - } - - //public Material GetModifiedMaterial2 (Material baseMaterial) - //{ - // Debug.Log ("UIEffectMaterialResolver 0"); - // if (!isActiveAndEnabled) - // return baseMaterial; - - // var parent = transform.parent; - // if (!parent) - // return baseMaterial; - - // var modifier = parent.GetComponent (); - // if (modifier == null) - // { - // Debug.Log ("UIEffectMaterialResolver modifier is null"); - // return baseMaterial; - // } - - // Debug.Log ("UIEffectMaterialResolver !!! " + modifier); - // return modifier.GetModifiedMaterial (baseMaterial); - //} - - public void ModifyMesh (Mesh mesh) - { - } - - public void ModifyMesh (VertexHelper verts) - { - if (!isActiveAndEnabled) - { - return; - } - - var modifier = transform.parent.GetComponent (); - modifier.ModifyMesh (verts); - } - } -} \ No newline at end of file diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIFlip.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIFlip.cs index c97c0789..0e1f350a 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIFlip.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIFlip.cs @@ -5,57 +5,63 @@ namespace Coffee.UIExtensions { + [DisallowMultipleComponent] + [AddComponentMenu("UI/UIEffects/UIFlip", 102)] + public class UIFlip : BaseMeshEffect + { + [Tooltip("Flip horizontally.")] [SerializeField] + private bool m_Horizontal = false; - //[RequireComponent(typeof(Graphic))] - [DisallowMultipleComponent] - [AddComponentMenu("UI/MeshEffectForTextMeshPro/UIFlip", 102)] - public class UIFlip : BaseMeshEffect - { - //################################ - // Serialize Members. - //################################ + [Tooltip("Flip vertically.")] [SerializeField] + private bool m_Veritical = false; - [Tooltip("Flip horizontally.")] - [SerializeField] private bool m_Horizontal = false; + /// + /// Gets or sets a value indicating whether this should be flipped horizontally. + /// + /// true if be flipped horizontally; otherwise, false. + public bool horizontal + { + get { return m_Horizontal; } + set + { + if (m_Horizontal == value) return; + m_Horizontal = value; + SetEffectParamsDirty(); + } + } - [Tooltip("Flip vertically.")] - [SerializeField] private bool m_Veritical = false; + /// + /// Gets or sets a value indicating whether this should be flipped vertically. + /// + /// true if be flipped horizontally; otherwise, false. + public bool vertical + { + get { return m_Veritical; } + set + { + if (m_Veritical == value) return; + m_Veritical = value; + SetEffectParamsDirty(); + } + } - //################################ - // Public Members. - //################################ - /// - /// Gets or sets a value indicating whether this should be flipped horizontally. - /// - /// true if be flipped horizontally; otherwise, false. - public bool horizontal { get { return this.m_Horizontal; } set { this.m_Horizontal = value; SetVerticesDirty (); } } - - /// - /// Gets or sets a value indicating whether this should be flipped vertically. - /// - /// true if be flipped horizontally; otherwise, false. - public bool vertical { get { return this.m_Veritical; } set { this.m_Veritical = value; SetVerticesDirty (); } } - - /// - /// Call used to modify mesh. - /// - /// VertexHelper. - public override void ModifyMesh(VertexHelper vh) - { - RectTransform rt = graphic.rectTransform; - UIVertex vt = default(UIVertex); - Vector3 pos; - Vector2 center = rt.rect.center; - for (int i = 0; i < vh.currentVertCount; i++) - { - vh.PopulateUIVertex(ref vt, i); - pos = vt.position; - vt.position = new Vector3( - m_Horizontal ? -pos.x : pos.x, - m_Veritical ? -pos.y : pos.y - ); - vh.SetUIVertex(vt, i); - } - } - } -} \ No newline at end of file + /// + /// Call used to modify mesh. + /// + /// VertexHelper. + public override void ModifyMesh(VertexHelper vh) + { + var vt = default(UIVertex); + for (var i = 0; i < vh.currentVertCount; i++) + { + vh.PopulateUIVertex(ref vt, i); + var pos = vt.position; + vt.position = new Vector3( + m_Horizontal ? -pos.x : pos.x, + m_Veritical ? -pos.y : pos.y + ); + vh.SetUIVertex(vt, i); + } + } + } +} diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIGradient.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIGradient.cs index 23f820eb..0808c406 100755 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIGradient.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIGradient.cs @@ -3,384 +3,317 @@ namespace Coffee.UIExtensions { - /// - /// UIGradient. - /// - [DisallowMultipleComponent] - [AddComponentMenu("UI/MeshEffectForTextMeshPro/UIGradient", 101)] - public class UIGradient : BaseMeshEffect - { - //################################ - // Constant or Static Members. - //################################ - - /// - /// Gradient direction. - /// - public enum Direction - { - Horizontal, - Vertical, - Angle, - Diagonal, - } - - /// - /// Gradient space for Text. - /// - public enum GradientStyle - { - Rect, - Fit, - Split, - } - - - //################################ - // Serialize Members. - //################################ - - [Tooltip("Gradient Direction.")] - [SerializeField] Direction m_Direction; - - [Tooltip("Color1: Top or Left.")] - [SerializeField] Color m_Color1 = Color.white; - - [Tooltip("Color2: Bottom or Right.")] - [SerializeField] Color m_Color2 = Color.white; - - [Tooltip("Color3: For diagonal.")] - [SerializeField] Color m_Color3 = Color.white; - - [Tooltip("Color4: For diagonal.")] - [SerializeField] Color m_Color4 = Color.white; - - [Tooltip("Gradient rotation.")] - [SerializeField][Range(-180, 180)] float m_Rotation; - - [Tooltip("Gradient offset for Horizontal, Vertical or Angle.")] - [SerializeField][Range(-1, 1)] float m_Offset1; - - [Tooltip("Gradient offset for Diagonal.")] - [SerializeField][Range(-1, 1)] float m_Offset2; - - [Tooltip("Gradient style for Text.")] - [SerializeField] GradientStyle m_GradientStyle; - - [Tooltip("Color space to correct color.")] - [SerializeField] ColorSpace m_ColorSpace = ColorSpace.Uninitialized; - - [Tooltip("Ignore aspect ratio.")] - [SerializeField] bool m_IgnoreAspectRatio = true; - - - //################################ - // Public Members. - //################################ - public Graphic targetGraphic { get { return base.graphic; } } - - /// - /// Gradient Direction. - /// - public Direction direction - { - get { return m_Direction; } - set - { - if (m_Direction != value) - { - m_Direction = value; - SetVerticesDirty(); - } - } - } - - /// - /// Color1: Top or Left. - /// - public Color color1 - { - get { return m_Color1; } - set - { - if (m_Color1 != value) - { - m_Color1 = value; - SetVerticesDirty(); - } - } - } - - /// - /// Color2: Bottom or Right. - /// - public Color color2 - { - get { return m_Color2; } - set - { - if (m_Color2 != value) - { - m_Color2 = value; - SetVerticesDirty(); - } - } - } - - /// - /// Color3: For diagonal. - /// - public Color color3 - { - get { return m_Color3; } - set - { - if (m_Color3 != value) - { - m_Color3 = value; - SetVerticesDirty(); - } - } - } - - /// - /// Color4: For diagonal. - /// - public Color color4 - { - get { return m_Color4; } - set - { - if (m_Color4 != value) - { - m_Color4 = value; - SetVerticesDirty(); - } - } - } - - /// - /// Gradient rotation. - /// - public float rotation - { - get - { - return m_Direction == Direction.Horizontal ? -90 - : m_Direction == Direction.Vertical ? 0 - : m_Rotation; - } - set - { - if (!Mathf.Approximately(m_Rotation, value)) - { - m_Rotation = value; - SetVerticesDirty(); - } - } - } - - /// - /// Gradient offset for Horizontal, Vertical or Angle. - /// - public float offset - { - get { return m_Offset1; } - set - { - if (m_Offset1 != value) - { - m_Offset1 = value; - SetVerticesDirty(); - } - } - } - - /// - /// Gradient offset for Diagonal. - /// - public Vector2 offset2 - { - get { return new Vector2(m_Offset2, m_Offset1); } - set - { - if (m_Offset1 != value.y || m_Offset2 != value.x) - { - m_Offset1 = value.y; - m_Offset2 = value.x; - SetVerticesDirty(); - } - } - } - - /// - /// Gradient style for Text. - /// - public GradientStyle gradientStyle - { - get { return m_GradientStyle; } - set - { - if (m_GradientStyle != value) - { - m_GradientStyle = value; - SetVerticesDirty(); - } - } - } - - /// - /// Color space to correct color. - /// - public ColorSpace colorSpace - { - get { return m_ColorSpace; } - set - { - if (m_ColorSpace != value) - { - m_ColorSpace = value; - SetVerticesDirty(); - } - } - } - - /// - /// Ignore aspect ratio. - /// - public bool ignoreAspectRatio - { - get { return m_IgnoreAspectRatio; } - set - { - if (m_IgnoreAspectRatio != value) - { - m_IgnoreAspectRatio = value; - SetVerticesDirty(); - } - } - } - - - /// - /// Call used to modify mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!IsActive()) - return; - - // Gradient space. - Rect rect = default(Rect); - UIVertex vertex = default(UIVertex); - if (m_GradientStyle == GradientStyle.Rect) - { - // RectTransform. - rect = graphic.rectTransform.rect; - } - else if (m_GradientStyle == GradientStyle.Split) - { - // Each characters. - rect.Set(0, 0, 1, 1); - } - else if (m_GradientStyle == GradientStyle.Fit) - { - // Fit to contents. - rect.xMin = rect.yMin = float.MaxValue; - rect.xMax = rect.yMax = float.MinValue; - for (int i = 0; i < vh.currentVertCount; i++) - { - vh.PopulateUIVertex(ref vertex, i); - rect.xMin = Mathf.Min(rect.xMin, vertex.position.x); - rect.yMin = Mathf.Min(rect.yMin, vertex.position.y); - rect.xMax = Mathf.Max(rect.xMax, vertex.position.x); - rect.yMax = Mathf.Max(rect.yMax, vertex.position.y); - } - } - - // Gradient rotation. - float rad = rotation * Mathf.Deg2Rad; - Vector2 dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); - if (!m_IgnoreAspectRatio && Direction.Angle <= m_Direction) - { - dir.x *= rect.height / rect.width; - dir = dir.normalized; - } - - // Calculate vertex color. - Color color; - Vector2 nomalizedPos; - Matrix2x3 localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix. - for (int i = 0; i < vh.currentVertCount; i++) - { - vh.PopulateUIVertex(ref vertex, i); - - // Normalize vertex position by local matrix. - if (m_GradientStyle == GradientStyle.Split) - { - // Each characters. - nomalizedPos = localMatrix * s_SplitedCharacterPosition[i % 4] + offset2; - } - else - { - nomalizedPos = localMatrix * vertex.position + offset2; - } - - // Interpolate vertex color. - if (direction == Direction.Diagonal) - { - color = Color.LerpUnclamped( - Color.LerpUnclamped(m_Color1, m_Color2, nomalizedPos.x), - Color.LerpUnclamped(m_Color3, m_Color4, nomalizedPos.x), - nomalizedPos.y); - } - else - { - color = Color.LerpUnclamped(m_Color2, m_Color1, nomalizedPos.y); - } - - // Correct color. - vertex.color *= (m_ColorSpace == ColorSpace.Gamma) ? color.gamma - : (m_ColorSpace == ColorSpace.Linear) ? color.linear - : color; - - vh.SetUIVertex(vertex, i); - } - } - - - //################################ - // Private Members. - //################################ - static readonly Vector2[] s_SplitedCharacterPosition = { Vector2.up, Vector2.one, Vector2.right, Vector2.zero }; - - /// - /// Matrix2x3. - /// - struct Matrix2x3 - { - public float m00, m01, m02, m10, m11, m12; - - public Matrix2x3(Rect rect, float cos, float sin) - { - const float center = 0.5f; - float dx = -rect.xMin / rect.width - center; - float dy = -rect.yMin / rect.height - center; - m00 = cos / rect.width; - m01 = -sin / rect.height; - m02 = dx * cos - dy * sin + center; - m10 = sin / rect.width; - m11 = cos / rect.height; - m12 = dx * sin + dy * cos + center; - } - - public static Vector2 operator*(Matrix2x3 m, Vector2 v) - { - return new Vector2( - (m.m00 * v.x) + (m.m01 * v.y) + m.m02, - (m.m10 * v.x) + (m.m11 * v.y) + m.m12 - ); - } - } - } + /// + /// UIGradient. + /// + [DisallowMultipleComponent] + [AddComponentMenu("UI/UIEffects/UIGradient", 101)] + public class UIGradient : BaseMeshEffect + { + static readonly Vector2[] s_SplitedCharacterPosition = {Vector2.up, Vector2.one, Vector2.right, Vector2.zero}; + + /// + /// Gradient direction. + /// + public enum Direction + { + Horizontal, + Vertical, + Angle, + Diagonal, + } + + /// + /// Gradient space for Text. + /// + public enum GradientStyle + { + Rect, + Fit, + Split, + } + + + [Tooltip("Gradient Direction.")] [SerializeField] + Direction m_Direction; + + [Tooltip("Color1: Top or Left.")] [SerializeField] + Color m_Color1 = Color.white; + + [Tooltip("Color2: Bottom or Right.")] [SerializeField] + Color m_Color2 = Color.white; + + [Tooltip("Color3: For diagonal.")] [SerializeField] + Color m_Color3 = Color.white; + + [Tooltip("Color4: For diagonal.")] [SerializeField] + Color m_Color4 = Color.white; + + [Tooltip("Gradient rotation.")] [SerializeField] [Range(-180, 180)] + float m_Rotation; + + [Tooltip("Gradient offset for Horizontal, Vertical or Angle.")] [SerializeField] [Range(-1, 1)] + float m_Offset1; + + [Tooltip("Gradient offset for Diagonal.")] [SerializeField] [Range(-1, 1)] + float m_Offset2; + + [Tooltip("Gradient style for Text.")] [SerializeField] + GradientStyle m_GradientStyle; + + [Tooltip("Color space to correct color.")] [SerializeField] + ColorSpace m_ColorSpace = ColorSpace.Uninitialized; + + [Tooltip("Ignore aspect ratio.")] [SerializeField] + bool m_IgnoreAspectRatio = true; + + /// + /// Gradient Direction. + /// + public Direction direction + { + get { return m_Direction; } + set + { + if (m_Direction == value) return; + m_Direction = value; + SetVerticesDirty(); + } + } + + /// + /// Color1: Top or Left. + /// + public Color color1 + { + get { return m_Color1; } + set + { + if (m_Color1 == value) return; + m_Color1 = value; + SetVerticesDirty(); + } + } + + /// + /// Color2: Bottom or Right. + /// + public Color color2 + { + get { return m_Color2; } + set + { + if (m_Color2 == value) return; + m_Color2 = value; + SetVerticesDirty(); + } + } + + /// + /// Color3: For diagonal. + /// + public Color color3 + { + get { return m_Color3; } + set + { + if (m_Color3 == value) return; + m_Color3 = value; + SetVerticesDirty(); + } + } + + /// + /// Color4: For diagonal. + /// + public Color color4 + { + get { return m_Color4; } + set + { + if (m_Color4 == value) return; + m_Color4 = value; + SetVerticesDirty(); + } + } + + /// + /// Gradient rotation. + /// + public float rotation + { + get + { + return m_Direction == Direction.Horizontal ? -90 + : m_Direction == Direction.Vertical ? 0 + : m_Rotation; + } + set + { + if (Mathf.Approximately(m_Rotation, value)) return; + m_Rotation = value; + SetVerticesDirty(); + } + } + + /// + /// Gradient offset for Horizontal, Vertical or Angle. + /// + public float offset + { + get { return m_Offset1; } + set + { + if (Mathf.Approximately(m_Offset1, value)) return; + m_Offset1 = value; + SetVerticesDirty(); + } + } + + /// + /// Gradient offset for Diagonal. + /// + public Vector2 offset2 + { + get { return new Vector2(m_Offset2, m_Offset1); } + set + { + if (Mathf.Approximately(m_Offset1, value.y) && Mathf.Approximately(m_Offset2, value.x)) return; + m_Offset1 = value.y; + m_Offset2 = value.x; + SetVerticesDirty(); + } + } + + /// + /// Gradient style for Text. + /// + public GradientStyle gradientStyle + { + get { return m_GradientStyle; } + set + { + if (m_GradientStyle == value) return; + m_GradientStyle = value; + SetVerticesDirty(); + } + } + + /// + /// Color space to correct color. + /// + public ColorSpace colorSpace + { + get { return m_ColorSpace; } + set + { + if (m_ColorSpace == value) return; + m_ColorSpace = value; + SetVerticesDirty(); + } + } + + /// + /// Ignore aspect ratio. + /// + public bool ignoreAspectRatio + { + get { return m_IgnoreAspectRatio; } + set + { + if (m_IgnoreAspectRatio == value) return; + m_IgnoreAspectRatio = value; + SetVerticesDirty(); + } + } + + /// + /// Call used to modify mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + if (!IsActive()) + return; + + // Gradient space. + var rect = default(Rect); + var vertex = default(UIVertex); + switch (m_GradientStyle) + { + case GradientStyle.Rect: + // RectTransform. + rect = graphic.rectTransform.rect; + break; + case GradientStyle.Split: + // Each characters. + rect.Set(0, 0, 1, 1); + break; + case GradientStyle.Fit: + { + // Fit to contents. + rect.xMin = rect.yMin = float.MaxValue; + rect.xMax = rect.yMax = float.MinValue; + for (var i = 0; i < vh.currentVertCount; i++) + { + vh.PopulateUIVertex(ref vertex, i); + rect.xMin = Mathf.Min(rect.xMin, vertex.position.x); + rect.yMin = Mathf.Min(rect.yMin, vertex.position.y); + rect.xMax = Mathf.Max(rect.xMax, vertex.position.x); + rect.yMax = Mathf.Max(rect.yMax, vertex.position.y); + } + + break; + } + } + + // Gradient rotation. + var rad = rotation * Mathf.Deg2Rad; + var dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); + if (!m_IgnoreAspectRatio && Direction.Angle <= m_Direction) + { + dir.x *= rect.height / rect.width; + dir = dir.normalized; + } + + // Calculate vertex color. + var localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix. + for (var i = 0; i < vh.currentVertCount; i++) + { + vh.PopulateUIVertex(ref vertex, i); + + // Normalize vertex position by local matrix. + Vector2 normalizedPos; + if (m_GradientStyle == GradientStyle.Split) + { + // Each characters. + normalizedPos = localMatrix * s_SplitedCharacterPosition[i % 4] + offset2; + } + else + { + normalizedPos = localMatrix * vertex.position + offset2; + } + + // Interpolate vertex color. + Color color; + if (direction == Direction.Diagonal) + { + color = Color.LerpUnclamped( + Color.LerpUnclamped(m_Color1, m_Color2, normalizedPos.x), + Color.LerpUnclamped(m_Color3, m_Color4, normalizedPos.x), + normalizedPos.y); + } + else + { + color = Color.LerpUnclamped(m_Color2, m_Color1, normalizedPos.y); + } + + // Correct color. + vertex.color *= (m_ColorSpace == ColorSpace.Gamma) ? color.gamma + : (m_ColorSpace == ColorSpace.Linear) ? color.linear + : color; + + vh.SetUIVertex(vertex, i); + } + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIHsvModifier.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIHsvModifier.cs index 8672322a..d69e3e78 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIHsvModifier.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIHsvModifier.cs @@ -1,4 +1,4 @@ -using System; +using System; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; @@ -6,238 +6,163 @@ namespace Coffee.UIExtensions { - /// - /// HSV Modifier. - /// - [AddComponentMenu("UI/UIEffect/UIHsvModifier", 4)] - public class UIHsvModifier : UIEffectBase - { - //################################ - // Constant or Static Members. - //################################ - public const string shaderName = "UI/Hidden/UI-Effect-HSV"; - static readonly ParameterTexture _ptex = new ParameterTexture(7, 128, "_ParamTex"); - - - //################################ - // Serialize Members. - //################################ - [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; - - - //################################ - // Public Members. - //################################ - - /// - /// Target color to affect hsv shift. - /// - public Color targetColor - { - get { return m_TargetColor; } - set - { - if (m_TargetColor != value) - { - m_TargetColor = value; - SetEffectDirty (); - } - } - } - - /// - /// Color range to affect hsv shift [0 ~ 1]. - /// - public float range - { - get { return m_Range; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Range, value)) - { - m_Range = value; - SetEffectDirty (); - } - } - } - - /// - /// Saturation shift [-0.5 ~ 0.5]. - /// - public float saturation - { - get { return m_Saturation; } - set - { - value = Mathf.Clamp(value, -0.5f, 0.5f); - if (!Mathf.Approximately(m_Saturation, value)) - { - m_Saturation = value; - SetEffectDirty (); - } - } - } - - /// - /// Value shift [-0.5 ~ 0.5]. - /// - public float value - { - get { return m_Value; } - set - { - value = Mathf.Clamp(value, -0.5f, 0.5f); - if (!Mathf.Approximately(m_Value, value)) - { - m_Value = value; - SetEffectDirty (); - } - } - } - - /// - /// Hue shift [-0.5 ~ 0.5]. - /// - public float hue - { - get { return m_Hue; } - set - { - value = Mathf.Clamp(value, -0.5f, 0.5f); - if (!Mathf.Approximately(m_Hue, value)) - { - m_Hue = value; - SetEffectDirty (); - } - } - } - - /// - /// Gets the parameter texture. - /// - public override ParameterTexture ptex { get { return _ptex; } } - - public override Hash128 GetMaterialHash (Material material) - { - if (!isActiveAndEnabled || !material || !material.shader) - return new Hash128 (); - - uint materialId = (uint)material.GetInstanceID (); - uint shaderId = 6 << 3; - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - shaderId += 2; - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - shaderId += 0; - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - shaderId += 1; - } - else - { - shaderId += 0; - } - - return new Hash128 ( - materialId, - shaderId, - 0, - 0 - ); - } - - public override void ModifyMaterial (Material material) - { - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UIHsvModifier)"); - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-HSV"); - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Distance Field (UIHsvModifier)"); - } - else - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-HSV"); - } - - ptex.RegisterMaterial (material); - } - -#if UNITY_EDITOR - protected override Material GetMaterial() - { - return null; - } -#endif - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled) - return; - - float normalizedIndex = ptex.GetNormalizedIndex(this); - UIVertex vertex = default(UIVertex); - int count = vh.currentVertCount; - for (int 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 SetEffectDirty () - { - float h, s, v; - Color.RGBToHSV(m_TargetColor, out h, out s, out v); - - ptex.SetData(this, 0, h); // param1.x : target hue - ptex.SetData(this, 1, s); // param1.y : target saturation - ptex.SetData(this, 2, v); // param1.z : target value - ptex.SetData(this, 3, m_Range); // param1.w : target range - ptex.SetData(this, 4, m_Hue + 0.5f); // param2.x : hue shift - ptex.SetData(this, 5, m_Saturation + 0.5f); // param2.y : saturation shift - ptex.SetData(this, 6, m_Value + 0.5f); // param2.z : value shift - } - - //################################ - // Private Members. - //################################ - } + /// + /// HSV Modifier. + /// + [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; + + /// + /// Target color to affect hsv shift. + /// + public Color targetColor + { + get { return m_TargetColor; } + set + { + if (m_TargetColor == value) return; + m_TargetColor = value; + SetEffectParamsDirty(); + } + } + + /// + /// Color range to affect hsv shift [0 ~ 1]. + /// + 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(); + } + } + + /// + /// Saturation shift [-0.5 ~ 0.5]. + /// + 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(); + } + } + + /// + /// Value shift [-0.5 ~ 0.5]. + /// + 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(); + } + } + + /// + /// Hue shift [-0.5 ~ 0.5]. + /// + 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(); + } + } + + /// + /// Gets the parameter texture. + /// + 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) + { + newMaterial.shader = connector.FindShader("UIHsvModifier"); + paramTex.RegisterMaterial(newMaterial); + } + + public override void ModifyMesh(VertexHelper vh) + { + 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 + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs index 32b0f212..0241e9d5 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs @@ -3,459 +3,381 @@ using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; - #if UNITY_EDITOR using System.IO; using System.Linq; using UnityEditor; + #endif namespace Coffee.UIExtensions { - /// - /// UIEffect. - /// - [RequireComponent(typeof(Graphic))] - [AddComponentMenu("UI/UIEffect/UIShadow", 100)] - public class UIShadow : BaseMeshEffect, IParameterTexture -#if UNITY_EDITOR - , ISerializationCallbackReceiver -#endif - { - - /// - /// Additional shadow. - /// - [System.Obsolete] - [System.Serializable] - public class AdditionalShadow - { - /// - /// How far is the blurring shadow from the graphic. - /// - [FormerlySerializedAs("shadowBlur")] - [Range(0, 1)] public float blur = 0.25f; - - /// - /// Shadow effect mode. - /// - [FormerlySerializedAs("shadowMode")] - public ShadowStyle style = ShadowStyle.Shadow; - - /// - /// Color for the shadow effect. - /// - [FormerlySerializedAs("shadowColor")] - public Color effectColor = Color.black; - - /// - /// How far is the shadow from the graphic. - /// - public Vector2 effectDistance = new Vector2(1f, -1f); - - /// - /// Should the shadow inherit the alpha from the graphic? - /// - public bool useGraphicAlpha = true; - } - - //################################ - // Serialize Members. - //################################ - [Tooltip("How far is the blurring shadow from the graphic.")] - [FormerlySerializedAs("m_Blur")] - [SerializeField][Range(0, 1)] float m_BlurFactor = 1; - - [Tooltip("Shadow effect style.")] - [SerializeField] ShadowStyle m_Style = ShadowStyle.Shadow; - - #pragma warning disable 0414 - [HideInInspector][System.Obsolete] - [SerializeField] List m_AdditionalShadows = new List(); - #pragma warning restore 0414 - - - [SerializeField] - private Color m_EffectColor = new Color (0f, 0f, 0f, 0.5f); - - [SerializeField] - private Vector2 m_EffectDistance = new Vector2 (1f, -1f); - - [SerializeField] - private bool m_UseGraphicAlpha = true; - - private const float kMaxEffectDistance = 600f; - - public Color effectColor - { - get { return m_EffectColor; } - set - { - m_EffectColor = value; - if (graphic != null) - graphic.SetVerticesDirty (); - } - } - - public Vector2 effectDistance - { - get { return m_EffectDistance; } - set - { - if (value.x > kMaxEffectDistance) - value.x = kMaxEffectDistance; - if (value.x < -kMaxEffectDistance) - value.x = -kMaxEffectDistance; - - if (value.y > kMaxEffectDistance) - value.y = kMaxEffectDistance; - if (value.y < -kMaxEffectDistance) - value.y = -kMaxEffectDistance; - - if (m_EffectDistance == value) - return; - - m_EffectDistance = value; - - if (graphic != null) - graphic.SetVerticesDirty (); - } - } - - public bool useGraphicAlpha - { - get { return m_UseGraphicAlpha; } - set - { - m_UseGraphicAlpha = value; - if (graphic != null) - graphic.SetVerticesDirty (); - } - } - - //################################ - // Public Members. - //################################ - /// - /// How far is the blurring shadow from the graphic. - /// - [System.Obsolete("Use blurFactor instead (UnityUpgradable) -> blurFactor")] - public float blur - { - get { return m_BlurFactor; } - set - { - m_BlurFactor = Mathf.Clamp(value, 0, 2); - _SetDirty(); - } - } - - /// - /// How far is the blurring shadow from the graphic. - /// - public float blurFactor - { - get { return m_BlurFactor; } - set - { - m_BlurFactor = Mathf.Clamp(value, 0, 2); - _SetDirty(); - } - } - - /// - /// Shadow effect style. - /// - public ShadowStyle style - { - get { return m_Style; } - set - { - m_Style = value; - _SetDirty(); - } - } - - /// - /// Gets or sets the parameter index. - /// - public int parameterIndex { get; set; } - - /// - /// Gets the parameter texture. - /// - public ParameterTexture ptex{ get; private set; } - - int _graphicVertexCount; - static readonly List tmpShadows = new List(); - - protected override void OnEnable() - { - base.OnEnable(); - - _uiEffect = GetComponent(); - if (_uiEffect) - { - ptex = _uiEffect.ptex; - ptex.Register(this); - } - - #if TMP_PRESENT - if (isTMPro) - { - textMeshPro.onCullStateChanged.AddListener (OnCullStateChanged); - } - #endif - } - - protected override void OnDisable() - { - base.OnDisable(); - _uiEffect = null; - if (ptex != null) - { - ptex.Unregister(this); - ptex = null; - } - } - - - #if UNITY_EDITOR - protected override void OnValidate () - { - effectDistance = m_EffectDistance; - base.OnValidate (); - } - #endif - - #if TMP_PRESENT - protected void OnCullStateChanged (bool state) - { - SetVerticesDirty (); - } - - Vector2 res; - protected override void LateUpdate () - { - if (res.x != Screen.width || res.y != Screen.height) - { - res.x = Screen.width; - res.y = Screen.height; - SetVerticesDirty (); - } - if (textMeshPro && transform.hasChanged) - { - transform.hasChanged = false; - } - base.LateUpdate (); - } - #endif - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled || vh.currentVertCount <= 0 || m_Style == ShadowStyle.None) - { - return; - } - - vh.GetUIVertexStream(s_Verts); - - GetComponents(tmpShadows); - - foreach (var s in tmpShadows) - { - if (s.isActiveAndEnabled) - { - if (s == this) - { - foreach (var s2 in tmpShadows) - { - s2._graphicVertexCount = s_Verts.Count; - } - } - break; - } - } - - tmpShadows.Clear(); - - //================================ - // Append shadow vertices. - //================================ - { - _uiEffect = _uiEffect ?? GetComponent(); - var start = s_Verts.Count - _graphicVertexCount; - var end = s_Verts.Count; - - if (ptex != null && _uiEffect && _uiEffect.isActiveAndEnabled) - { - ptex.SetData(this, 0, _uiEffect.effectFactor); // param.x : effect factor - ptex.SetData(this, 1, 255); // param.y : color factor - ptex.SetData(this, 2, m_BlurFactor); // param.z : blur factor - } - - _ApplyShadow(s_Verts, effectColor, ref start, ref end, effectDistance, style, useGraphicAlpha); - } - - vh.Clear(); - vh.AddUIVertexTriangleStream(s_Verts); - - s_Verts.Clear(); - } - - UIEffect _uiEffect; - - //################################ - // Private Members. - //################################ - static readonly List s_Verts = new List(4096); - - /// - /// Append shadow vertices. - /// * It is similar to Shadow component implementation. - /// - void _ApplyShadow(List verts, Color color, ref int start, ref int end, Vector2 effectDistance, ShadowStyle style, bool useGraphicAlpha) - { - if (style == ShadowStyle.None || color.a <= 0) - return; - - // Append Shadow. - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, effectDistance.x, effectDistance.y, useGraphicAlpha); - - // Append Shadow3. - if (ShadowStyle.Shadow3 == style) - { - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, effectDistance.x, 0, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, 0, effectDistance.y, useGraphicAlpha); - } - - // Append Outline. - else if (ShadowStyle.Outline == style) - { - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, effectDistance.x, -effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, -effectDistance.x, effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, -effectDistance.x, -effectDistance.y, useGraphicAlpha); - } - - // Append Outline8. - else if (ShadowStyle.Outline8 == style) - { - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, effectDistance.x, -effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, -effectDistance.x, effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, -effectDistance.x, -effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, -effectDistance.x, 0, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, 0, -effectDistance.y, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, effectDistance.x, 0, useGraphicAlpha); - _ApplyShadowZeroAlloc(s_Verts, color, ref start, ref end, 0, effectDistance.y, useGraphicAlpha); - } - } - - /// - /// Append shadow vertices. - /// * It is similar to Shadow component implementation. - /// - void _ApplyShadowZeroAlloc(List verts, Color color, ref int start, ref int end, float x, float y, bool useGraphicAlpha) - { - // Check list capacity. - int count = end - start; - var neededCapacity = verts.Count + count; - if (verts.Capacity < neededCapacity) - verts.Capacity *= 2; - - float normalizedIndex = ptex != null && _uiEffect && _uiEffect.isActiveAndEnabled - ? ptex.GetNormalizedIndex(this) - : -1; - - // Add - UIVertex vt = default(UIVertex); - for (int i = 0; i < count; i++) - { - verts.Add(vt); - } - - // Move - for (int i = verts.Count - 1; count <= i; i--) - { - verts[i] = verts[i - count]; - } - - // Append shadow vertices to the front of list. - // * The original vertex is pushed backward. - for (int i = 0; i < count; ++i) - { - vt = verts[i + start + count]; - - Vector3 v = vt.position; - vt.position.Set(v.x + x, v.y + y, v.z); - - Color vertColor = effectColor; - vertColor.a = useGraphicAlpha ? color.a * vt.color.a / 255 : color.a; - vt.color = vertColor; - - - // Set UIEffect prameters - if (0 <= normalizedIndex) - { - vt.uv0 = new Vector2( - vt.uv0.x, - normalizedIndex - ); - } - - verts[i] = vt; - } - - // Update next shadow offset. - start = end; - end = verts.Count; - } - - /// - /// Mark the UIEffect as dirty. - /// - void _SetDirty() - { - if (graphic) - graphic.SetVerticesDirty(); - } - -#if UNITY_EDITOR - public void OnBeforeSerialize() - { - } - - public void OnAfterDeserialize() - { - EditorApplication.delayCall += UpgradeIfNeeded; - } - - - #pragma warning disable 0612 - void UpgradeIfNeeded() - { - if (0 < m_AdditionalShadows.Count) - { - foreach (var s in m_AdditionalShadows) - { - if (s.style == ShadowStyle.None) - { - continue; - } - - var shadow = gameObject.AddComponent(); - shadow.style = s.style; - shadow.effectDistance = s.effectDistance; - shadow.effectColor = s.effectColor; - shadow.useGraphicAlpha = s.useGraphicAlpha; - shadow.blurFactor = s.blur; - } - m_AdditionalShadows = null; - } - } - #pragma warning restore 0612 -#endif - } + /// + /// UIEffect. + /// + [RequireComponent(typeof(Graphic))] + [AddComponentMenu("UI/UIEffects/UIShadow", 100)] + public class UIShadow : BaseMeshEffect, IParameterTexture + { + static readonly List tmpShadows = new List(); + static readonly List s_Verts = new List(4096); + + int _graphicVertexCount; + UIEffect _uiEffect; + + [Tooltip("How far is the blurring shadow from the graphic.")] + [FormerlySerializedAs("m_Blur")] + [SerializeField] + [Range(0, 1)] + float m_BlurFactor = 1; + + [Tooltip("Shadow effect style.")] [SerializeField] + ShadowStyle m_Style = ShadowStyle.Shadow; + + [SerializeField] private Color m_EffectColor = new Color(0f, 0f, 0f, 0.5f); + + [SerializeField] private Vector2 m_EffectDistance = new Vector2(1f, -1f); + + [SerializeField] private bool m_UseGraphicAlpha = true; + + private const float kMaxEffectDistance = 600f; + + public Color effectColor + { + get { return m_EffectColor; } + set + { + if (m_EffectColor == value) return; + m_EffectColor = value; + SetVerticesDirty(); + } + } + + public Vector2 effectDistance + { + get { return m_EffectDistance; } + set + { + if (value.x > kMaxEffectDistance) + value.x = kMaxEffectDistance; + if (value.x < -kMaxEffectDistance) + value.x = -kMaxEffectDistance; + + if (value.y > kMaxEffectDistance) + value.y = kMaxEffectDistance; + if (value.y < -kMaxEffectDistance) + value.y = -kMaxEffectDistance; + + if (m_EffectDistance == value) return; + m_EffectDistance = value; + SetEffectParamsDirty(); + } + } + + public bool useGraphicAlpha + { + get { return m_UseGraphicAlpha; } + set + { + if (m_UseGraphicAlpha == value) return; + m_UseGraphicAlpha = value; + SetEffectParamsDirty(); + } + } + + /// + /// How far is the blurring shadow from the graphic. + /// + public float blurFactor + { + get { return m_BlurFactor; } + set + { + value = Mathf.Clamp(value, 0, 2); + if (Mathf.Approximately(m_BlurFactor, value)) return; + m_BlurFactor = value; + SetEffectParamsDirty(); + } + } + + /// + /// Shadow effect style. + /// + public ShadowStyle style + { + get { return m_Style; } + set + { + if (m_Style == value) return; + m_Style = value; + SetEffectParamsDirty(); + } + } + + /// + /// Gets or sets the parameter index. + /// + public int parameterIndex { get; set; } + + /// + /// Gets the parameter texture. + /// + public ParameterTexture paramTex { get; private set; } + + protected override void OnEnable() + { + base.OnEnable(); + + _uiEffect = GetComponent(); + if (!_uiEffect) return; + + paramTex = _uiEffect.paramTex; + paramTex.Register(this); + } + + protected override void OnDisable() + { + base.OnDisable(); + + _uiEffect = null; + if (paramTex == null) return; + + paramTex.Unregister(this); + paramTex = null; + } + + +// #if UNITY_EDITOR +// protected override void OnValidate() +// { +// effectDistance = m_EffectDistance; +// base.OnValidate(); +// } +// #endif + + // #if TMP_PRESENT + // protected void OnCullStateChanged (bool state) + // { + // SetVerticesDirty (); + // } + // + // Vector2 res; + // protected override void LateUpdate () + // { + // if (res.x != Screen.width || res.y != Screen.height) + // { + // res.x = Screen.width; + // res.y = Screen.height; + // SetVerticesDirty (); + // } + // if (textMeshPro && transform.hasChanged) + // { + // transform.hasChanged = false; + // } + // base.LateUpdate (); + // } + // #endif + + /// + /// Modifies the mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + if (!isActiveAndEnabled || vh.currentVertCount <= 0 || m_Style == ShadowStyle.None) + { + return; + } + + vh.GetUIVertexStream(s_Verts); + + GetComponents(tmpShadows); + + foreach (var s in tmpShadows) + { + if (!s.isActiveAndEnabled) continue; + if (s == this) + { + foreach (var s2 in tmpShadows) + { + s2._graphicVertexCount = s_Verts.Count; + } + } + + break; + } + + tmpShadows.Clear(); + + //================================ + // Append shadow vertices. + //================================ + { + _uiEffect = _uiEffect ? _uiEffect : GetComponent(); + var start = s_Verts.Count - _graphicVertexCount; + var end = s_Verts.Count; + + if (paramTex != null && _uiEffect && _uiEffect.isActiveAndEnabled) + { + paramTex.SetData(this, 0, _uiEffect.effectFactor); // param.x : effect factor + paramTex.SetData(this, 1, 255); // param.y : color factor + paramTex.SetData(this, 2, m_BlurFactor); // param.z : blur factor + } + + ApplyShadow(s_Verts, effectColor, ref start, ref end, effectDistance, style, useGraphicAlpha); + } + + vh.Clear(); + vh.AddUIVertexTriangleStream(s_Verts); + + s_Verts.Clear(); + } + + /// + /// Append shadow vertices. + /// * It is similar to Shadow component implementation. + /// + private void ApplyShadow(List verts, Color color, ref int start, ref int end, Vector2 distance, + ShadowStyle style, bool alpha) + { + if (style == ShadowStyle.None || color.a <= 0) + return; + + var x = distance.x; + var y = distance.y; + // Append Shadow. + ApplyShadowZeroAlloc(verts, color, ref start, ref end, x, y, alpha); + + switch (style) + { + // Append Shadow3. + case ShadowStyle.Shadow3: + ApplyShadowZeroAlloc(verts, color, ref start, ref end, x, 0, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, 0, y, alpha); + break; + // Append Outline. + case ShadowStyle.Outline: + ApplyShadowZeroAlloc(verts, color, ref start, ref end, x, -y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, -x, y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, -x, -y, alpha); + break; + // Append Outline8. + case ShadowStyle.Outline8: + ApplyShadowZeroAlloc(verts, color, ref start, ref end, x, -y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, -x, y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, -x, -y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, -x, 0, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, 0, -y, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, x, 0, alpha); + ApplyShadowZeroAlloc(verts, color, ref start, ref end, 0, y, alpha); + break; + } + } + + /// + /// Append shadow vertices. + /// * It is similar to Shadow component implementation. + /// + private void ApplyShadowZeroAlloc(List verts, Color color, ref int start, ref int end, float x, + float y, bool alpha) + { + // Check list capacity. + var count = end - start; + var neededCapacity = verts.Count + count; + if (verts.Capacity < neededCapacity) + verts.Capacity *= 2; + + var normalizedIndex = paramTex != null && _uiEffect && _uiEffect.isActiveAndEnabled + ? paramTex.GetNormalizedIndex(this) + : -1; + + // Add + var vt = default(UIVertex); + for (var i = 0; i < count; i++) + { + verts.Add(vt); + } + + // Move + for (var i = verts.Count - 1; count <= i; i--) + { + verts[i] = verts[i - count]; + } + + // Append shadow vertices to the front of list. + // * The original vertex is pushed backward. + for (var i = 0; i < count; ++i) + { + vt = verts[i + start + count]; + + var v = vt.position; + vt.position.Set(v.x + x, v.y + y, v.z); + + var vertColor = effectColor; + vertColor.a = alpha ? color.a * vt.color.a / 255 : color.a; + vt.color = vertColor; + + + // Set UIEffect parameters + if (0 <= normalizedIndex) + { + vt.uv0 = new Vector2( + vt.uv0.x, + normalizedIndex + ); + } + + verts[i] = vt; + } + + // Update next shadow offset. + start = end; + end = verts.Count; + } + + /// + /// Mark the UIEffect as dirty. + /// + // void _SetDirty() + // { + // if (graphic) + // graphic.SetVerticesDirty(); + // } + +// #if UNITY_EDITOR +// public void OnBeforeSerialize() +// { +// } +// +// public void OnAfterDeserialize() +// { +// EditorApplication.delayCall += UpgradeIfNeeded; +// } +// +// +// #pragma warning disable 0612 +// void UpgradeIfNeeded() +// { +// if (0 < m_AdditionalShadows.Count) +// { +// foreach (var s in m_AdditionalShadows) +// { +// if (s.style == ShadowStyle.None) +// { +// continue; +// } +// +// var shadow = gameObject.AddComponent(); +// shadow.style = s.style; +// shadow.effectDistance = s.effectDistance; +// shadow.effectColor = s.effectColor; +// shadow.useGraphicAlpha = s.useGraphicAlpha; +// shadow.blurFactor = s.blur; +// } +// +// m_AdditionalShadows = null; +// } +// } +// #pragma warning restore 0612 +// #endif + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs index 6c1ecd3d..473b3e7d 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShiny.cs @@ -5,452 +5,272 @@ using UnityEngine.UI; using System.Collections; -#if UNITY_EDITOR -using System.IO; -using System.Linq; -using UnityEditor; -#endif - namespace Coffee.UIExtensions { - /// - /// UIEffect. - /// - [AddComponentMenu("UI/UIEffect/UIShiny", 2)] - public class UIShiny : UIEffectBase - { - //################################ - // Constant or Static Members. - //################################ - //public const string shaderName = "UI/Hidden/UI-Effect-Shiny"; - static readonly ParameterTexture _ptex = new ParameterTexture(8, 128, "_ParamTex"); - - - //################################ - // Serialize Members. - //################################ - [Tooltip("Location for shiny effect.")] - [FormerlySerializedAs("m_Location")] - [SerializeField] [Range(0, 1)] float m_EffectFactor = 0; - - [Tooltip("Width for shiny effect.")] - [SerializeField] [Range(0, 1)] float m_Width = 0.25f; - - [Tooltip("Rotation for shiny effect.")] - [SerializeField] [Range(-180, 180)] float m_Rotation; - - [Tooltip("Softness for shiny effect.")] - [SerializeField][Range(0.01f, 1)] float m_Softness = 1f; - - [Tooltip("Brightness for shiny effect.")] - [FormerlySerializedAs("m_Alpha")] - [SerializeField][Range(0, 1)] float m_Brightness = 1f; - - [Tooltip("Gloss factor for shiny effect.")] - [FormerlySerializedAs("m_Highlight")] - [SerializeField][Range(0, 1)] float m_Gloss = 1; - - [Header("Advanced Option")] - [Tooltip("The area for effect.")] - [SerializeField] protected EffectArea m_EffectArea; - - [SerializeField] EffectPlayer m_Player; - - #pragma warning disable 0414 - [Obsolete][HideInInspector] - [SerializeField] bool m_Play = false; - [Obsolete][HideInInspector] - [SerializeField] bool m_Loop = false; - [Obsolete][HideInInspector] - [SerializeField][Range(0.1f, 10)] float m_Duration = 1; - [Obsolete][HideInInspector] - [SerializeField][Range(0, 10)] float m_LoopDelay = 1; - [Obsolete][HideInInspector] - [SerializeField] AnimatorUpdateMode m_UpdateMode = AnimatorUpdateMode.Normal; - #pragma warning restore 0414 - - - //################################ - // Public Members. - //################################ - - /// - /// Effect factor between 0(start) and 1(end). - /// - [System.Obsolete("Use effectFactor instead (UnityUpgradable) -> effectFactor")] - public float location - { - get { return m_EffectFactor; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_EffectFactor, value)) - { - m_EffectFactor = value; - SetEffectDirty(); - } - } - } - - /// - /// Effect factor between 0(start) and 1(end). - /// - public float effectFactor - { - get { return m_EffectFactor; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_EffectFactor, value)) - { - m_EffectFactor = value; - SetEffectDirty(); - } - } - } - - /// - /// Width for shiny effect. - /// - public float width - { - get { return m_Width; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Width, value)) - { - m_Width = value; - SetEffectDirty(); - } - } - } - - /// - /// Softness for shiny effect. - /// - public float softness - { - get { return m_Softness; } - set - { - value = Mathf.Clamp(value, 0.01f, 1); - if (!Mathf.Approximately(m_Softness, value)) - { - m_Softness = value; - SetEffectDirty(); - } - } - } - - /// - /// Brightness for shiny effect. - /// - [System.Obsolete("Use brightness instead (UnityUpgradable) -> brightness")] - public float alpha - { - get { return m_Brightness; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Brightness, value)) - { - m_Brightness = value; - SetEffectDirty(); - } - } - } - - /// - /// Brightness for shiny effect. - /// - public float brightness - { - get { return m_Brightness; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Brightness, value)) - { - m_Brightness = value; - SetEffectDirty(); - } - } - } - - /// - /// Gloss factor for shiny effect. - /// - [System.Obsolete("Use gloss instead (UnityUpgradable) -> gloss")] - public float highlight - { - get { return m_Gloss; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Gloss, value)) - { - m_Gloss = value; - SetEffectDirty(); - } - } - } - - /// - /// Gloss factor for shiny effect. - /// - public float gloss - { - get { return m_Gloss; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_Gloss, value)) - { - m_Gloss = value; - SetEffectDirty(); - } - } - } - - /// - /// Rotation for shiny effect. - /// - public float rotation - { - get { return m_Rotation; } - set - { - if (!Mathf.Approximately(m_Rotation, value)) - { - m_Rotation = _lastRotation = value; - SetVerticesDirty (); - } - } - } - - /// - /// The area for effect. - /// - public EffectArea effectArea - { - get { return m_EffectArea; } - set - { - if (m_EffectArea != value) - { - m_EffectArea = value; - SetVerticesDirty (); - } - } - } - - /// - /// Play shinning on enable. - /// - [System.Obsolete ("Use Play/Stop method instead")] - public bool play { get { return _player.play; } set { _player.play = value; } } - - /// - /// Play shinning loop. - /// - [System.Obsolete] - public bool loop { get { return _player.loop; } set { _player.loop = value; } } - - /// - /// Shinning duration. - /// - public float duration { get { return _player.duration; } set { _player.duration = Mathf.Max(value, 0.1f); } } - - /// - /// Delay on loop. - /// - [System.Obsolete] - public float loopDelay { get { return _player.loopDelay; } set { _player.loopDelay = Mathf.Max(value, 0); } } - - /// - /// Shinning update mode. - /// - public AnimatorUpdateMode updateMode { get { return _player.updateMode; } set { _player.updateMode = value; } } - - /// - /// Gets the parameter texture. - /// - public override ParameterTexture ptex { get { return _ptex; } } - - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable(); - _player.OnEnable(f => effectFactor = f); - } - - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - protected override void OnDisable() - { - base.OnDisable(); - _player.OnDisable(); - } - - - public override Hash128 GetMaterialHash (Material material) - { - if (!isActiveAndEnabled || !material || !material.shader) - return new Hash128 (); - - uint materialId = (uint)material.GetInstanceID (); - uint shaderId = 1 << 3; - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - shaderId += 2; - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - shaderId += 0; - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - shaderId += 1; - } - else - { - shaderId += 0; - } - - return new Hash128 ( - materialId, - shaderId, - 0, - 0 - ); - } - - public override void ModifyMaterial (Material material) - { - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UIShiny)"); - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Shiny"); - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Distance Field (UIShiny)"); - } - else - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Shiny"); - } - - ptex.RegisterMaterial (material); - } - -#if UNITY_EDITOR - protected override Material GetMaterial() - { - return null; - } - - #pragma warning disable 0612 - protected override void UpgradeIfNeeded() - { - // Upgrade for v3.0.0 - if (IsShouldUpgrade(300)) - { - _player.play = m_Play; - _player.duration = m_Duration; - _player.loop = m_Loop; - _player.loopDelay = m_LoopDelay; - _player.updateMode = m_UpdateMode; - } - } - #pragma warning restore 0612 - -#endif - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled) - return; - - bool isText = isTMPro || graphic is Text; - float normalizedIndex = ptex.GetNormalizedIndex(this); - - // rect. - Rect rect = m_EffectArea.GetEffectArea (vh, rectTransform.rect); - - // rotation. - float rad = m_Rotation * Mathf.Deg2Rad; - Vector2 dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); - dir.x *= rect.height / rect.width; - dir = dir.normalized; - - // Calculate vertex position. - UIVertex vertex = default(UIVertex); - Vector2 nomalizedPos; - Matrix2x3 localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix. - for (int i = 0; i < vh.currentVertCount; i++) - { - vh.PopulateUIVertex(ref vertex, i); - m_EffectArea.GetNormalizedFactor (i, localMatrix, vertex.position, isText, out nomalizedPos); - - vertex.uv0 = new Vector2 ( - Packer.ToFloat (vertex.uv0.x, vertex.uv0.y), - Packer.ToFloat (nomalizedPos.y, normalizedIndex) - ); - - vh.SetUIVertex(vertex, i); - } - } - - /// - /// Play effect. - /// - public void Play(bool reset = true) - { - _player.Play(reset); - } - - /// - /// Stop effect. - /// - public void Stop(bool reset = true) - { - _player.Stop(reset); - } - - protected override void SetEffectDirty() - { - ptex.SetData(this, 0, m_EffectFactor); // param1.x : location - ptex.SetData(this, 1, m_Width); // param1.y : width - ptex.SetData(this, 2, m_Softness); // param1.z : softness - ptex.SetData(this, 3, m_Brightness);// param1.w : blightness - ptex.SetData(this, 4, m_Gloss); // param2.x : gloss - - if (!Mathf.Approximately(_lastRotation, m_Rotation) && targetGraphic) - { - _lastRotation = m_Rotation; - SetVerticesDirty(); - } - } - - //################################ - // Private Members. - //################################ - float _lastRotation; - - EffectPlayer _player{ get { return m_Player ?? (m_Player = new EffectPlayer()); } } - } + /// + /// UIEffect. + /// + [AddComponentMenu("UI/UIEffects/UIShiny", 2)] + public class UIShiny : BaseMaterialEffect + { + private const uint k_ShaderId = 1 << 3; + private static readonly ParameterTexture s_ParamTex = new ParameterTexture(8, 128, "_ParamTex"); + + float _lastRotation; + EffectArea _lastEffectArea; + + [Tooltip("Location for shiny effect.")] [FormerlySerializedAs("m_Location")] [SerializeField] [Range(0, 1)] + float m_EffectFactor = 0.5f; + + [Tooltip("Width for shiny effect.")] [SerializeField] [Range(0, 1)] + float m_Width = 0.25f; + + [Tooltip("Rotation for shiny effect.")] [SerializeField] [Range(-180, 180)] + float m_Rotation = 135; + + [Tooltip("Softness for shiny effect.")] [SerializeField] [Range(0.01f, 1)] + float m_Softness = 1f; + + [Tooltip("Brightness for shiny effect.")] [FormerlySerializedAs("m_Alpha")] [SerializeField] [Range(0, 1)] + float m_Brightness = 1f; + + [Tooltip("Gloss factor for shiny effect.")] [FormerlySerializedAs("m_Highlight")] [SerializeField] [Range(0, 1)] + float m_Gloss = 1; + + [Header("Advanced Option")] [Tooltip("The area for effect.")] [SerializeField] + protected EffectArea m_EffectArea; + + [SerializeField] EffectPlayer m_Player; + + /// + /// Effect factor between 0(start) and 1(end). + /// + 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(); + } + } + + /// + /// Width for shiny effect. + /// + 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(); + } + } + + /// + /// Softness for shiny effect. + /// + public float softness + { + get { return m_Softness; } + set + { + value = Mathf.Clamp(value, 0.01f, 1); + if (Mathf.Approximately(m_Softness, value)) return; + m_Softness = value; + SetEffectParamsDirty(); + } + } + + /// + /// Brightness for shiny effect. + /// + public float brightness + { + get { return m_Brightness; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_Brightness, value)) return; + m_Brightness = value; + SetEffectParamsDirty(); + } + } + + /// + /// Gloss factor for shiny effect. + /// + public float gloss + { + get { return m_Gloss; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_Gloss, value)) return; + m_Gloss = value; + SetEffectParamsDirty(); + } + } + + /// + /// Rotation for shiny effect. + /// + public float rotation + { + get { return m_Rotation; } + set + { + if (Mathf.Approximately(m_Rotation, value)) return; + m_Rotation = value; + SetVerticesDirty(); + } + } + + /// + /// The area for effect. + /// + public EffectArea effectArea + { + get { return m_EffectArea; } + set + { + if (m_EffectArea == value) return; + m_EffectArea = value; + SetVerticesDirty(); + } + } + + /// + /// Gets the parameter texture. + /// + public override ParameterTexture paramTex + { + get { return s_ParamTex; } + } + + /// + /// This function is called when the object becomes enabled and active. + /// + protected override void OnEnable() + { + base.OnEnable(); + _player.OnEnable(f => effectFactor = f); + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + protected override void OnDisable() + { + base.OnDisable(); + _player.OnDisable(); + } + + + 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) + { + newMaterial.shader = connector.FindShader("UIShiny"); + paramTex.RegisterMaterial(newMaterial); + } + + /// + /// Modifies the mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + if (!isActiveAndEnabled) + return; + + var normalizedIndex = paramTex.GetNormalizedIndex(this); + var rect = m_EffectArea.GetEffectArea(vh, rectTransform.rect); + + // rotation. + var rad = m_Rotation * Mathf.Deg2Rad; + var dir = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); + dir.x *= rect.height / rect.width; + dir = dir.normalized; + + // Calculate vertex position. + var vertex = default(UIVertex); + var localMatrix = new Matrix2x3(rect, dir.x, dir.y); // Get local matrix. + for (int i = 0; i < vh.currentVertCount; i++) + { + vh.PopulateUIVertex(ref vertex, i); + Vector2 normalizedPos; + connector.GetNormalizedFactor(m_EffectArea, i, localMatrix, vertex.position, out normalizedPos); + + vertex.uv0 = new Vector2( + Packer.ToFloat(vertex.uv0.x, vertex.uv0.y), + Packer.ToFloat(normalizedPos.y, normalizedIndex) + ); + + vh.SetUIVertex(vertex, i); + } + } + + /// + /// Play effect. + /// + public void Play(bool reset = true) + { + _player.Play(reset); + } + + /// + /// Stop effect. + /// + public void Stop(bool reset = true) + { + _player.Stop(reset); + } + + 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, 3, m_Brightness); // param1.w : blightness + paramTex.SetData(this, 4, m_Gloss); // param2.x : gloss + } + + protected override void SetVerticesDirty() + { + base.SetVerticesDirty(); + + _lastRotation = m_Rotation; + _lastEffectArea = m_EffectArea; + } + + protected override void OnDidApplyAnimationProperties() + { + base.OnDidApplyAnimationProperties(); + + if (!Mathf.Approximately(_lastRotation, m_Rotation) + || _lastEffectArea != m_EffectArea) + SetVerticesDirty(); + } + + EffectPlayer _player + { + get { return m_Player ?? (m_Player = new EffectPlayer()); } + } + } } diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UITransitionEffect.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UITransitionEffect.cs index 7076a869..7275471a 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UITransitionEffect.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UITransitionEffect.cs @@ -5,383 +5,327 @@ namespace Coffee.UIExtensions { - /// - /// Transition effect. - /// - [AddComponentMenu("UI/UIEffect/UITransitionEffect", 5)] - public class UITransitionEffect : UIEffectBase - { - //################################ - // Constant or Static Members. - //################################ - public const string shaderName = "UI/Hidden/UI-Effect-Transition"; - static readonly ParameterTexture _ptex = new ParameterTexture(8, 128, "_ParamTex"); - - /// - /// Effect mode. - /// - public enum EffectMode - { - Fade = 1, - Cutoff = 2, - Dissolve = 3, - } - - - //################################ - // Serialize Members. - //################################ - [Tooltip("Effect mode.")] - [SerializeField] EffectMode m_EffectMode = EffectMode.Cutoff; - - [Tooltip("Effect factor between 0(hidden) and 1(shown).")] - [SerializeField][Range(0, 1)] float m_EffectFactor = 0.5f; - - [Tooltip("Transition texture (single channel texture).")] - [SerializeField] Texture m_TransitionTexture; - - [Header("Advanced Option")] - [Tooltip("The area for effect.")] - [SerializeField] EffectArea m_EffectArea = EffectArea.RectTransform; - - [Tooltip("Keep effect aspect ratio.")] - [SerializeField] bool m_KeepAspectRatio; - - [Tooltip("Dissolve edge width.")] - [SerializeField] [Range(0, 1)] float m_DissolveWidth = 0.5f; - - [Tooltip("Dissolve edge softness.")] - [SerializeField] [Range(0, 1)] float m_DissolveSoftness = 0.5f; - - [Tooltip("Dissolve edge color.")] - [SerializeField] [ColorUsage(false)] Color m_DissolveColor = new Color(0.0f, 0.25f, 1.0f); - - [Tooltip("Disable graphic's raycast target on hidden.")] - [SerializeField] bool m_PassRayOnHidden; - - [Header("Effect Player")] - [SerializeField] EffectPlayer m_Player; - - //################################ - // Public Members. - //################################ - - /// - /// Effect factor between 0(no effect) and 1(complete effect). - /// - public float effectFactor - { - get { return m_EffectFactor; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_EffectFactor, value)) - { - m_EffectFactor = value; - SetEffectDirty (); - } - } - } - - /// - /// Transition texture. - /// - public Texture transitionTexture - { - get { return m_TransitionTexture; } - set - { - if (m_TransitionTexture != value) - { - m_TransitionTexture = value; - if (graphic) - { - ModifyMaterial(); - } - } - } - } - - /// - /// Effect mode. - /// - public EffectMode effectMode { get { return m_EffectMode; } } - - /// - /// Keep aspect ratio. - /// - public bool keepAspectRatio - { - get { return m_KeepAspectRatio; } - set - { - if (m_KeepAspectRatio != value) - { - m_KeepAspectRatio = value; - targetGraphic.SetVerticesDirty(); - } - } - } - - /// - /// Gets the parameter texture. - /// - public override ParameterTexture ptex { get { return _ptex; } } - - /// - /// Dissolve edge width. - /// - public float dissolveWidth - { - get { return m_DissolveWidth; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_DissolveWidth, value)) - { - m_DissolveWidth = value; - SetEffectDirty (); - } - } - } - - /// - /// Dissolve edge softness. - /// - public float dissolveSoftness - { - get { return m_DissolveSoftness; } - set - { - value = Mathf.Clamp(value, 0, 1); - if (!Mathf.Approximately(m_DissolveSoftness, value)) - { - m_DissolveSoftness = value; - SetEffectDirty (); - } - } - } - - /// - /// Dissolve edge color. - /// - public Color dissolveColor - { - get { return m_DissolveColor; } - set - { - if (m_DissolveColor != value) - { - m_DissolveColor = value; - SetEffectDirty (); - } - } - } - - /// - /// Duration for showing/hiding. - /// - public float duration { get { return _player.duration; } set { _player.duration = Mathf.Max(value, 0.1f); } } - - /// - /// Disable graphic's raycast target on hidden. - /// - public bool passRayOnHidden { get { return m_PassRayOnHidden; } set { m_PassRayOnHidden = value; } } - - /// - /// Update mode for showing/hiding. - /// - public AnimatorUpdateMode updateMode { get { return _player.updateMode; } set { _player.updateMode = value; } } - - /// - /// Show transition. - /// - public void Show(bool reset = true) - { - _player.loop = false; - _player.Play(reset, f => effectFactor = f); - } - - /// - /// Hide transition. - /// - public void Hide(bool reset = true) - { - _player.loop = false; - _player.Play(reset, f => effectFactor = 1 - f); - } - - - public override Hash128 GetMaterialHash (Material material) - { - if (!isActiveAndEnabled || !material || !material.shader) - return new Hash128 (); - - uint materialId = (uint)material.GetInstanceID (); - uint shaderId = 5 << 3; - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - shaderId += 2; - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - shaderId += 0; - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - shaderId += 1; - } - else - { - shaderId += 0; - } - - - uint shaderVariantId = (uint)((int)m_EffectMode << 6); - uint resourceId = m_TransitionTexture ? (uint)m_TransitionTexture.GetInstanceID () : 0; - return new Hash128 ( - materialId, - shaderId + shaderVariantId, - resourceId, - 0 - ); - } - - public override void ModifyMaterial (Material material) - { - Debug.LogFormat (this, $"ModifyMaterial {material}"); - - string materialShaderName = material.shader.name; - if (materialShaderName.StartsWith ("TextMeshPro/Mobile/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Mobile/Distance Field (UITransition)"); - } - else if (materialShaderName.Equals ("TextMeshPro/Sprite", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Transition"); - } - else if (materialShaderName.StartsWith ("TextMeshPro/", StringComparison.Ordinal)) - { - material.shader = Shader.Find ("TextMeshPro/Distance Field (UITransition)"); - } - else - { - material.shader = Shader.Find ("UI/Hidden/UI-Effect-Transition"); - } - - SetShaderVariants (material, m_EffectMode); - - if (m_TransitionTexture) - { - material.SetTexture ("_NoiseTex", m_TransitionTexture); - } - ptex.RegisterMaterial (material); - } - - /// - /// Modifies the material. - /// - public override void ModifyMaterial() - { - } - - /// - /// Modifies the mesh. - /// - public override void ModifyMesh(VertexHelper vh) - { - if (!isActiveAndEnabled) - { - return; - } - - bool isText = isTMPro || graphic is Text; - float normalizedIndex = ptex.GetNormalizedIndex (this); - - // rect. - var tex = transitionTexture; - var aspectRatio = m_KeepAspectRatio && tex ? ((float)tex.width) / tex.height : -1; - Rect rect = m_EffectArea.GetEffectArea (vh, rectTransform.rect, aspectRatio); - - // Set prameters to vertex. - UIVertex vertex = default(UIVertex); - float x, y; - int count = vh.currentVertCount; - for (int i = 0; i < count; i++) - { - vh.PopulateUIVertex(ref vertex, i); - m_EffectArea.GetPositionFactor (i, rect, vertex.position, isText, isTMPro, 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 Members. - //################################ - - /// - /// This function is called when the object becomes enabled and active. - /// - protected override void OnEnable() - { - base.OnEnable(); - _player.OnEnable(null); - _player.loop = false; - } - - /// - /// This function is called when the behaviour becomes disabled () or inactive. - /// - protected override void OnDisable() - { - base.OnDisable (); - MaterialCache.Unregister(_materialCache); - _materialCache = null; - _player.OnDisable(); - } - - protected override void SetEffectDirty () - { - ptex.SetData(this, 0, m_EffectFactor); // param1.x : effect factor - if (m_EffectMode == EffectMode.Dissolve) - { - ptex.SetData(this, 1, m_DissolveWidth); // param1.y : width - ptex.SetData(this, 2, m_DissolveSoftness); // param1.z : softness - ptex.SetData(this, 4, m_DissolveColor.r); // param2.x : red - ptex.SetData(this, 5, m_DissolveColor.g); // param2.y : green - ptex.SetData(this, 6, m_DissolveColor.b); // param2.z : blue - } - - // Disable graphic's raycastTarget on hidden. - if (m_PassRayOnHidden) - { - targetGraphic.raycastTarget = 0 < m_EffectFactor; - } - } - -#if UNITY_EDITOR - /// - /// Gets the material. - /// - /// The material. - protected override Material GetMaterial() - { - return MaterialResolver.GetOrGenerateMaterialVariant(Shader.Find(shaderName), m_EffectMode); - } -#endif - - //################################ - // Private Members. - //################################ - MaterialCache _materialCache = null; - - EffectPlayer _player{ get { return m_Player ?? (m_Player = new EffectPlayer()); } } - } + /// + /// Transition effect. + /// + [AddComponentMenu("UI/UIEffects/UITransitionEffect", 5)] + public class UITransitionEffect : BaseMaterialEffect + { + /// + /// Effect mode. + /// + public enum EffectMode + { + Fade = 1, + Cutoff = 2, + Dissolve = 3, + } + + private const uint k_ShaderId = 5 << 3; + private static readonly int k_TransitionTexId = Shader.PropertyToID("_TransitionTex"); + private static readonly ParameterTexture s_ParamTex = new ParameterTexture(8, 128, "_ParamTex"); + + private bool _lastKeepAspectRatio; + private static Texture _defaultTransitionTexture; + + [Tooltip("Effect mode.")] [SerializeField] + EffectMode m_EffectMode = EffectMode.Cutoff; + + [Tooltip("Effect factor between 0(hidden) and 1(shown).")] [SerializeField] [Range(0, 1)] + float m_EffectFactor = 0.5f; + + [Tooltip("Transition texture (single channel texture).")] [SerializeField] + Texture m_TransitionTexture; + + [Header("Advanced Option")] [Tooltip("The area for effect.")] [SerializeField] + EffectArea m_EffectArea = EffectArea.RectTransform; + + [Tooltip("Keep effect aspect ratio.")] [SerializeField] + bool m_KeepAspectRatio; + + [Tooltip("Dissolve edge width.")] [SerializeField] [Range(0, 1)] + float m_DissolveWidth = 0.5f; + + [Tooltip("Dissolve edge softness.")] [SerializeField] [Range(0, 1)] + float m_DissolveSoftness = 0.5f; + + [Tooltip("Dissolve edge color.")] [SerializeField] [ColorUsage(false)] + Color m_DissolveColor = new Color(0.0f, 0.25f, 1.0f); + + [Tooltip("Disable the graphic's raycast target on hidden.")] [SerializeField] + bool m_PassRayOnHidden; + + [Header("Effect Player")] [SerializeField] + EffectPlayer m_Player; + + + /// + /// Effect factor between 0(no effect) and 1(complete effect). + /// + 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(); + } + } + + /// + /// Transition texture. + /// + 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("Default-Transition")); + } + } + + /// + /// Effect mode. + /// + public EffectMode effectMode + { + get { return m_EffectMode; } + set + { + if (m_EffectMode == value) return; + m_EffectMode = value; + SetMaterialDirty(); + } + } + + /// + /// Keep aspect ratio. + /// + public bool keepAspectRatio + { + get { return m_KeepAspectRatio; } + set + { + if (m_KeepAspectRatio == value) return; + m_KeepAspectRatio = value; + SetVerticesDirty(); + } + } + + /// + /// Gets the parameter texture. + /// + public override ParameterTexture paramTex + { + get { return s_ParamTex; } + } + + /// + /// Dissolve edge width. + /// + public float dissolveWidth + { + get { return m_DissolveWidth; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_DissolveWidth, value)) return; + m_DissolveWidth = value; + SetEffectParamsDirty(); + } + } + + /// + /// Dissolve edge softness. + /// + public float dissolveSoftness + { + get { return m_DissolveSoftness; } + set + { + value = Mathf.Clamp(value, 0, 1); + if (Mathf.Approximately(m_DissolveSoftness, value)) return; + m_DissolveSoftness = value; + SetEffectParamsDirty(); + } + } + + /// + /// Dissolve edge color. + /// + public Color dissolveColor + { + get { return m_DissolveColor; } + set + { + if (m_DissolveColor == value) return; + m_DissolveColor = value; + SetEffectParamsDirty(); + } + } + + /// + /// Disable graphic's raycast target on hidden. + /// + public bool passRayOnHidden + { + get { return m_PassRayOnHidden; } + set { m_PassRayOnHidden = value; } + } + + /// + /// Show transition. + /// + public void Show(bool reset = true) + { + _player.loop = false; + _player.Play(reset, f => effectFactor = f); + } + + /// + /// Hide transition. + /// + public void Hide(bool reset = true) + { + _player.loop = false; + _player.Play(reset, f => effectFactor = 1 - f); + } + + + public override Hash128 GetMaterialHash(Material material) + { + if (!isActiveAndEnabled || !material || !material.shader) + return k_InvalidHash; + + var shaderVariantId = (uint) ((int) m_EffectMode << 6); + var resourceId = (uint) transitionTexture.GetInstanceID(); + return new Hash128( + (uint) material.GetInstanceID(), + k_ShaderId + shaderVariantId, + resourceId, + 0 + ); + } + + public override void ModifyMaterial(Material newMaterial) + { + newMaterial.shader = connector.FindShader("UITransition"); + SetShaderVariants(newMaterial, m_EffectMode); + + newMaterial.SetTexture(k_TransitionTexId, transitionTexture); + paramTex.RegisterMaterial(newMaterial); + } + + /// + /// Modifies the mesh. + /// + public override void ModifyMesh(VertexHelper vh) + { + if (!isActiveAndEnabled) + { + return; + } + + 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); + + // Set parameters to vertex. + 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); + } + } + + /// + /// This function is called when the object becomes enabled and active. + /// + protected override void OnEnable() + { + base.OnEnable(); + _player.OnEnable(null); + _player.loop = false; + } + + /// + /// This function is called when the behaviour becomes disabled () or inactive. + /// + protected override void OnDisable() + { + base.OnDisable(); + _player.OnDisable(); + } + + protected override void SetEffectParamsDirty() + { + paramTex.SetData(this, 0, m_EffectFactor); // param1.x : effect factor + if (m_EffectMode == EffectMode.Dissolve) + { + paramTex.SetData(this, 1, m_DissolveWidth); // param1.y : width + paramTex.SetData(this, 2, m_DissolveSoftness); // param1.z : softness + paramTex.SetData(this, 4, m_DissolveColor.r); // param2.x : red + paramTex.SetData(this, 5, m_DissolveColor.g); // param2.y : green + paramTex.SetData(this, 6, m_DissolveColor.b); // param2.z : blue + } + + // Disable graphic's raycastTarget on hidden. + if (m_PassRayOnHidden) + { + graphic.raycastTarget = 0 < m_EffectFactor; + } + } + + protected override void SetVerticesDirty() + { + base.SetVerticesDirty(); + + _lastKeepAspectRatio = m_KeepAspectRatio; + } + + protected override void OnDidApplyAnimationProperties() + { + base.OnDidApplyAnimationProperties(); + + if (_lastKeepAspectRatio != m_KeepAspectRatio) + SetVerticesDirty(); + } + + EffectPlayer _player + { + get { return m_Player ?? (m_Player = new EffectPlayer()); } + } + } }