summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Projector/ProjectorsControl.cs
blob: 7949c37ed32c6f89b9fe954b4649f50e1db2b58a (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using UnityEngine;
using System.Collections.Generic;

[ExecuteInEditMode]
[RequireComponent(typeof(Projector))]
public class ProjectorsControl : MonoBehaviour
{
    //public delegate void TransitionCompleted();
    //public event TransitionCompleted OnPulseCompleted;

    #region variables

    private Projector projector;

    public bool enableAnim = false;
    public Color color = Color.white;
    public float falloff = 1.0f;
    public float amplify = 1.0f;
    public float argAccuracy = 0.01f;


    private Texture texture = null;    

    private Material projectorMat;
    private Color innerColor = Color.white;
    private Vector4 innerArg = new Vector4(1.0f, 1.0f, 0, 0);
    private Texture innerTexture = null;

    private static float colorT = 1 / 256.0f;
    
    #endregion

    void Awake()
    {
        if (projector == null)
            projector = GetComponent<Projector>();
        if (projector != null)
        {
            projectorMat = projector.material;
            if (projectorMat != null)
            {
                if (projectorMat.HasProperty("_Color"))
                    innerColor = projectorMat.GetColor("_Color");
                if (projectorMat.HasProperty("_Args"))
                    innerArg = projectorMat.GetVector("_Args");
                if (projectorMat.HasProperty("_ShadowTex"))
                    innerTexture = projectorMat.GetTexture("_ShadowTex");
            }
        }
    }

    void Update()
    {
        if (enableAnim && projectorMat != null)
        {
            //color
            float deltaR = Mathf.Abs(innerColor.r - color.r);
            float deltaG = Mathf.Abs(innerColor.g - color.g);
            float deltaB = Mathf.Abs(innerColor.b - color.b);
            float deltaA = Mathf.Abs(innerColor.a - color.a);

            if (deltaR >= colorT || deltaG >= colorT || deltaB >= colorT || deltaA >= colorT)
            {
                innerColor = color;
                projectorMat.SetColor("_Color", color);
            }

            float deltaFalloff = Mathf.Abs(innerArg.x - falloff);
            float deltaAmp = Mathf.Abs(innerArg.y - amplify);
            if (deltaFalloff >= argAccuracy || deltaAmp > argAccuracy)
            {
                innerArg.x = falloff;
                innerArg.y = amplify;
                projectorMat.SetVector("_Args", innerArg);
            }

            if (texture != null && innerTexture != texture)
            {
                innerTexture = texture;
                projectorMat.SetTexture("_ShadowTex", innerTexture);
            }
        }
    }
    //void FixedUpdate()
    //{
    //    if (rotate)
    //        transform.Rotate(Vector3.forward * rotationSpeed * rotationOffset * Time.deltaTime);

    //    if (pulse)
    //    {
    //        float pulseSize = projector.orthographicSize;
    //        float stepAmount = Time.deltaTime * pulseSpeed;

    //        if (!pulseFlip)
    //            pulseSize = Mathf.Lerp(pulseSize, pulseMax, stepAmount);
    //        else
    //            pulseSize = Mathf.Lerp(pulseSize, pulseMin, stepAmount);


    //        if (pulseTime < 1)
    //            pulseTime += stepAmount;
    //        else
    //        {
    //            pulseTime = 0;

    //            if (pulseLoop)
    //                pulseFlip = !pulseFlip;
    //            else
    //                pulse = false;

    //            if (OnPulseCompleted != null)
    //                OnPulseCompleted();
    //        }

    //        projector.orthographicSize = pulseSize;
    //    }

    //    if (colorBlend && colors.Count > 0)
    //    {
    //        float stepAmount = Time.deltaTime * colorSpeed;

    //        projector.material.color = Color.Lerp(projector.material.color, colors[colorIndex], stepAmount);

    //        if (colorTime < 1)
    //            colorTime += stepAmount;
    //        else
    //        {
    //            colorTime = 0;

    //            if (colorIndex < colors.Count - 1)
    //                colorIndex++;
    //            else
    //                colorIndex = 0;
    //        }
    //    }
    //}

    ///// <summary>
    ///// The default size of the projector.
    ///// </summary>
    //public float DefaultSize
    //{
    //    get
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null)
    //            return projector.orthographicSize;

    //        return 0;
    //    }

    //    set
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null)
    //            projector.orthographicSize = value * Scale;
    //    }
    //}

    ///// <summary>
    ///// The default texture used by the projector.
    ///// </summary>
    //public Texture DefaultTexture
    //{
    //    get
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null && projector.material != null)
    //            return projector.material.GetTexture("_ShadowTex");

    //        return null;
    //    }

    //    set
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null && projector.material != null)
    //            projector.material.SetTexture("_ShadowTex", value);
    //    }
    //}

    ///// <summary>
    ///// The default color used by the projector.
    ///// </summary>
    //public Color DefaultColor
    //{
    //    get
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null && projector.material != null)
    //            return projector.material.GetColor("_Color");

    //        return Color.black;
    //    }

    //    set
    //    {
    //        if (projector == null)
    //            projector = GetComponent<Projector>();

    //        if (projector != null && projector.material != null)
    //            projector.material.SetColor("_Color", value);
    //    }
    //}

    ///// <summary>
    ///// Adjusts the scale of the current projector based on a defined size value.
    ///// </summary>
    ///// <param name="size"></param>
    ///// <param name="scale"></param>
    //public void SetScale(float size, float scale)
    //{
    //    Scale = scale;
    //    DefaultSize = size;

    //    pulseMin *= scale;
    //    pulseMax *= scale;
    //}

    ///// <summary>
    ///// Sets the projectors material.
    ///// </summary>
    ///// <param name="material"></param>
    //public void SetMaterial(Material material)
    //{
    //    if (projector == null)
    //        projector = GetComponent<Projector>();

    //    if (projector != null)
    //        projector.material = material;
    //}

    ///// <summary>
    ///// Updates default values when creating the prefab.
    ///// </summary>
    //public void Initialize()
    //{
    //    if (projector == null)
    //        projector = GetComponent<Projector>();

    //    projector.orthographic = true;
    //}

    ///// <summary>
    ///// Forces an updated to reflect the current values.
    ///// </summary>
    //public void Refresh()
    //{
    //    Vector3 lastRotation = this.transform.localRotation.eulerAngles;
    //    this.transform.localRotation = Quaternion.Euler(lastRotation.x, rotation, lastRotation.z);
    //}
}