summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Common/ParameterTexture.cs
blob: 324982884224bdaef20b72b9a567e29b3da8b947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using System;

namespace Coffee.UIEffects
{
    public interface IParameterTexture
    {
        int parameterIndex { get; set; }

        ParameterTexture paramTex { get; }
    }

    /// <summary>
    /// Parameter texture.
    /// </summary>
    [System.Serializable]
    public class ParameterTexture
    {
        //################################
        // Public Members.
        //################################

        /// <summary>
        /// Initializes a new instance of the <see cref="Coffee.UIEffects.ParameterTexture"/> class.
        /// </summary>
        /// <param name="channels">Channels.</param>
        /// <param name="instanceLimit">Instance limit.</param>
        /// <param name="propertyName">Property name.</param>
        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);
            }
        }


        /// <summary>
        /// Register the specified target.
        /// </summary>
        /// <param name="target">Target.</param>
        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);
            }
        }

        /// <summary>
        /// Unregister the specified target.
        /// </summary>
        /// <param name="target">Target.</param>
        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;
            }
        }

        /// <summary>
        /// Sets the data.
        /// </summary>
        /// <param name="target">Target.</param>
        /// <param name="channelId">Channel identifier.</param>
        /// <param name="value">Value.</param>
        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;
            }
        }

        /// <summary>
        /// Sets the data.
        /// </summary>
        /// <param name="target">Target.</param>
        /// <param name="channelId">Channel identifier.</param>
        /// <param name="value">Value.</param>
        public void SetData(IParameterTexture target, int channelId, float value)
        {
            SetData(target, channelId, (byte) (Mathf.Clamp01(value) * 255));
        }

        /// <summary>
        /// Registers the material.
        /// </summary>
        /// <param name="mat">Mat.</param>
        public void RegisterMaterial(Material mat)
        {
            if (_propertyId == 0)
            {
                _propertyId = Shader.PropertyToID(_propertyName);
            }

            if (mat)
            {
                mat.SetTexture(_propertyId, _texture);
            }
        }

        /// <summary>
        /// Gets the index of the normalized.
        /// </summary>
        /// <returns>The normalized index.</returns>
        /// <param name="target">Target.</param>
        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<int> _stack;
        static List<Action> updates;

        /// <summary>
        /// Initialize this instance.
        /// </summary>
        void Initialize()
        {
#if UNITY_EDITOR
            if (!UnityEditor.EditorApplication.isPlaying && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }
#endif
            if (updates == null)
            {
                updates = new List<Action>();
                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);
            }
        }
    }
}