Skip to content

Commit

Permalink
close #63; New architecture: Shared texture for effect parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai authored and mob-sakai committed Sep 28, 2018
1 parent 8f2b13a commit e3a4cf5
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 200 deletions.
119 changes: 119 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/ParameterTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Coffee.UIExtensions
{
public interface IParameterTexture
{
int parameterIndex { get; set; }
ParameterTexture ptex { get; }
}


public class ParameterTexture
{
Texture2D _texture;
bool _needUpload;
int _propertyId;
readonly string _propertyName;
readonly int _channels;
readonly int _instanceLimit;
readonly byte[] _data;
readonly Stack<int> _stack;

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<int>(_instanceLimit);
for (int i = 1; i < _instanceLimit + 1; i++)
{
_stack.Push(i);
}
}

void Initialize()
{
#if UNITY_EDITOR
if (!UnityEditor.EditorApplication.isPlaying && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
{
return;
}
#endif

if (!_texture)
{
_texture = new Texture2D(_channels / 4, _instanceLimit, TextureFormat.RGBA32, false, false);
_texture.filterMode = FilterMode.Point;
_texture.wrapMode = TextureWrapMode.Clamp;

// Update dispatcher
Canvas.willRenderCanvases += () =>
{
if (_needUpload && _texture)
{
_needUpload = false;
_texture.LoadRawTextureData(_data);
_texture.Apply(false, false);
}
};
}
}


public void Register(IParameterTexture target)
{
Initialize();
if (target.parameterIndex <= 0 && 0 < _stack.Count)
{
target.parameterIndex = _stack.Pop();
Debug.LogFormat("<color=green>@@@ Register {0} : {1}</color>", target, target.parameterIndex);
}
}

public void Unregister(IParameterTexture target)
{
if (0 < target.parameterIndex)
{
Debug.LogFormat("<color=red>@@@ Unregister {0} : {1}</color>", target, target.parameterIndex);
_stack.Push(target.parameterIndex);
target.parameterIndex = 0;
}
}

public void SetData(IParameterTexture target, int channelId, byte value)
{
if (0 < target.parameterIndex)
{
_data[(target.parameterIndex -1) * _channels + channelId] = value;
_needUpload = true;
}
}

public void SetData(IParameterTexture target, int channelId, float value)
{
SetData(target, channelId, (byte)(Mathf.Clamp01(value) * 255));
}

public void RegisterMaterial(Material mat)
{
if (_propertyId == 0)
{
_propertyId = Shader.PropertyToID(_propertyName);
}
if (mat)
{
mat.SetTexture(_propertyId, _texture);
}
}

public float GetNormalizedIndex(IParameterTexture target)
{
return ((float)target.parameterIndex) / _instanceLimit;
}
}
}
12 changes: 12 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/ParameterTexture.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class EffectRunner

public void OnEnable(Action<float> callback)
{
Canvas.willRenderCanvases -= OnWillRenderCanvases;
Canvas.willRenderCanvases += OnWillRenderCanvases;

_time = 0;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/Packer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static float ToFloat(Vector4 factor)
}

/// <summary>
/// Pack 3 low-precision [0-1] floats values to a float.
/// Each value [0-1] has 256 steps(8 bits).
/// 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).
/// </summary>
public static float ToFloat(float x, float y, float z)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ public enum ToneMode
Sepia = 2,
Nega = 3,
Pixel = 4,
[System.Obsolete]
Mono = 5,
[System.Obsolete]
Cutoff = 6,
[System.Obsolete]
Hue = 7,
}
}
41 changes: 32 additions & 9 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/UIEffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Coffee.UIExtensions
[ExecuteInEditMode]
[RequireComponent(typeof(Graphic))]
[DisallowMultipleComponent]
public abstract class UIEffectBase : BaseMeshEffect
public abstract class UIEffectBase : BaseMeshEffect, IParameterTexture
#if UNITY_EDITOR
, ISerializationCallbackReceiver
#endif
Expand All @@ -22,6 +22,17 @@ public abstract class UIEffectBase : BaseMeshEffect
[SerializeField] int m_Version;
[SerializeField] protected Material m_EffectMaterial;

/// <summary>
/// Gets or sets the parameter index.
/// </summary>
public int parameterIndex { get; set; }

/// <summary>
/// Gets the parameter texture.
/// </summary>
public virtual ParameterTexture ptex { get{return null;} }


/// <summary>
/// Gets target graphic for effect.
/// </summary>
Expand Down Expand Up @@ -51,6 +62,7 @@ protected override void OnValidate ()
UnityEditor.EditorUtility.SetDirty(this);
}
ModifyMaterial();
targetGraphic.SetVerticesDirty();
SetDirty();
}

Expand All @@ -73,7 +85,7 @@ protected bool IsShouldUpgrade(int expectedVersion)
//UnityEditor.EditorApplication.delayCall += () =>
{
UnityEditor.EditorUtility.SetDirty(this);
if (gameObject && gameObject.scene.IsValid())
if (!Application.isPlaying && gameObject && gameObject.scene.IsValid())
{
UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(gameObject.scene);
}
Expand Down Expand Up @@ -110,8 +122,13 @@ public virtual void ModifyMaterial()
/// </summary>
protected override void OnEnable()
{
if (ptex != null)
{
ptex.Register(this);
}
ModifyMaterial();
base.OnEnable();
targetGraphic.SetVerticesDirty();
SetDirty();
}

/// <summary>
Expand All @@ -120,18 +137,24 @@ protected override void OnEnable()
protected override void OnDisable()
{
ModifyMaterial();
base.OnDisable();
targetGraphic.SetVerticesDirty();
if (ptex != null)
{
ptex.Unregister(this);
}
}

/// <summary>
/// Mark the UIEffect as dirty.
/// </summary>
protected void SetDirty()
protected virtual void SetDirty()
{
if (targetGraphic)
{
targetGraphic.SetVerticesDirty();
}
targetGraphic.SetVerticesDirty();
}

protected override void OnDidApplyAnimationProperties()
{
SetDirty();
}
}
}
35 changes: 30 additions & 5 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/UIDissolve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UIDissolve : UIEffectBase
// Constant or Static Members.
//################################
public const string shaderName = "UI/Hidden/UI-Effect-Dissolve";
static readonly ParameterTexture _ptex = new ParameterTexture(8, 128, "_ParamTex");


//################################
Expand Down Expand Up @@ -192,6 +193,11 @@ public bool keepAspectRatio
/// </summary>
public AnimatorUpdateMode updateMode { get { return m_Runner.updateMode; } set { m_Runner.updateMode = value; } }

/// <summary>
/// Gets the parameter texture.
/// </summary>
public override ParameterTexture ptex { get { return _ptex; } }

/// <summary>
/// Modifies the material.
/// </summary>
Expand Down Expand Up @@ -236,6 +242,8 @@ public override void ModifyMesh(VertexHelper vh)
if (!isActiveAndEnabled)
return;

float normalizedIndex = ptex.GetNormalizedIndex(this);

// rect.
var tex = noiseTexture;
var aspectRatio = m_KeepAspectRatio && tex ? ((float)tex.width) / tex.height : -1;
Expand All @@ -245,7 +253,8 @@ public override void ModifyMesh(VertexHelper vh)
UIVertex vertex = default(UIVertex);
bool effectEachCharacter = graphic is Text && m_EffectArea == EffectArea.Character;
float x, y;
for (int i = 0; i < vh.currentVertCount; i++)
int count = vh.currentVertCount;
for (int i = 0; i < count; i++)
{
vh.PopulateUIVertex(ref vertex, i);

Expand All @@ -259,15 +268,27 @@ public override void ModifyMesh(VertexHelper vh)
x = Mathf.Clamp01(vertex.position.x / rect.width + 0.5f);
y = Mathf.Clamp01(vertex.position.y / rect.height + 0.5f);
}
vertex.uv1 = new Vector2(
Packer.ToFloat(x, y, location, m_Width),
Packer.ToFloat(m_Color.r, m_Color.g, m_Color.b, m_Softness)

vertex.uv0 = new Vector2(
Packer.ToFloat(vertex.uv0.x, vertex.uv0.y),
Packer.ToFloat(x, y, normalizedIndex)
);

vh.SetUIVertex(vertex, i);
}
}

protected override void SetDirty()
{
ptex.RegisterMaterial(targetGraphic.material);
ptex.SetData(this, 0, m_Location); // 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
}

/// <summary>
/// Play effect.
/// </summary>
Expand All @@ -286,15 +307,19 @@ public void Play()
protected override void OnEnable()
{
base.OnEnable();
if (m_Runner == null)
{
m_Runner = new EffectRunner();
}
m_Runner.OnEnable(f => location = f);
}

protected override void OnDisable()
{
MaterialCache.Unregister(_materialCache);
_materialCache = null;
base.OnDisable();
m_Runner.OnDisable();
base.OnDisable();
}

#if UNITY_EDITOR
Expand Down
Loading

0 comments on commit e3a4cf5

Please sign in to comment.