summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Projector/ShapeProjectorsControl.cs
blob: a260fa1e49fe3e99ec8b0ccdc2ad9691e8aeda5a (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
using UnityEngine;
using System.Collections.Generic;

[ExecuteInEditMode]
[RequireComponent(typeof(Projector))]
public class ShapeProjectorsControl : MonoBehaviour
{
    #region variables

    private Projector projector;

    public bool enableAnim = false;
    public Color tintColor = Color.red;
    public Color outlineColor = Color.white;

    public float colorTransition = 1.1f;
    public float colorScale = 1.0f;
    public float outlineWidth = 0.1f;
    public float outlineScale = 1.0f;
    public float angle = 0.0f;
    public float argAccuracy = 0.01f;

    private Material projectorMat;

    private Color innerTintColor = Color.red;
    private Color innerOutlineColor = Color.white;

    private Vector4 innerArg = new Vector4(1.01f, 1.0f, 0.1f, 1.0f);
    private float innerAngle = 0.0f;

    private bool hasAngle = false;
    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)
            {
                innerTintColor = projectorMat.GetColor("_TintColor");
                innerOutlineColor = projectorMat.GetColor("_OutlineColor");
                innerArg = projectorMat.GetVector("_Arg");
                hasAngle = projectorMat.HasProperty("_Angle");
                if (hasAngle)
                    innerAngle = projectorMat.GetFloat("_Angle");
            }
        }
    }

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

            if (deltaR >= colorT || deltaG >= colorT || deltaB >= colorT || deltaA >= colorT)
            {
                innerTintColor = tintColor;
                projectorMat.SetColor("_TintColor", tintColor);
            }
            deltaR = Mathf.Abs(innerOutlineColor.r - outlineColor.r);
            deltaG = Mathf.Abs(innerOutlineColor.g - outlineColor.g);
            deltaB = Mathf.Abs(innerOutlineColor.b - outlineColor.b);
            deltaA = Mathf.Abs(innerOutlineColor.a - outlineColor.a);

            if (deltaR >= colorT || deltaG >= colorT || deltaB >= colorT || deltaA >= colorT)
            {
                innerOutlineColor = outlineColor;
                projectorMat.SetColor("_OutlineColor", outlineColor);
            }

            float deltaColorTransition = Mathf.Abs(innerArg.x - colorTransition);
            float deltaColorScale = Mathf.Abs(innerArg.y - colorScale);
            float deltaOutlineWidth = Mathf.Abs(innerArg.z - outlineWidth);
            float deltaOutlineScale = Mathf.Abs(innerArg.w - outlineScale);

            if (deltaColorTransition >= argAccuracy || 
                deltaColorScale > argAccuracy||
                deltaOutlineWidth > argAccuracy ||
                deltaOutlineScale > argAccuracy)
            {
                innerArg.x = colorTransition;
                innerArg.y = colorScale;
                innerArg.z = outlineWidth;
                innerArg.w = outlineScale;
                projectorMat.SetVector("_Arg", innerArg);
            }
            if (hasAngle)
            {
                float deltaAngle = Mathf.Abs(innerAngle - angle);
                if (deltaAngle >= argAccuracy)
                {
                    projectorMat.SetFloat("_Angle", angle);
                }
            }

        }
    }   
}