devNotes 4-07-16 slerpspergsburg unclamped

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class ArgosHexLighter : MonoBehaviour
{
    public Slider sld_Variance;
    public Slider sld_PauseTime;
    public Slider sld_MoverTime;
    public Slider sld_NeighborSeeks;

    public Text VarianceText;
    public Text PauseTimeText;
    public Text MoverTimeText;
    public Text NeighborSeeksText;

    public float moveTime = 4.0f;
    public float pauseTime = 2.0f;
    public float variance = 0.1f;
    public int neighborSeeks = 10;
    ArgSphereUtility asu;
    public GameObject hexPrefab;
    public GameObject quadPrefab;
    GameObject ArgosSphere;
    ArgosSphere_Indexing asi;

    public Color quadColor;

    void Start ()
    {
        asu = GetComponent<ArgSphereUtility>();

        ArgosSphere = GameObject.Find("Argos_Sphere");
        asi = ArgosSphere.GetComponent<ArgosSphere_Indexing>();


        asu.init();
        init_mover();
    }

    //Slider sld_Variance;
    //Slider sld_PauseTime;
    //Slider sld_MoverTime;

    //Text VarianceText;
    //Text PauseTimeText;
    //Text MoverTimeText;

    public void onSliderVariance()
    {
        variance = sld_Variance.value;
        VarianceText.text = "Variance = " + variance.ToString("F2");
    }

    public void onSliderPauseTime()
    {
        pauseTime = sld_PauseTime.value;
        PauseTimeText.text = "PauseTime = " + pauseTime.ToString("F1");
    }

    public void onSliderMoveTime()
    {
        moveTime = sld_MoverTime.value;
        MoverTimeText.text = "MoveTime = " + moveTime.ToString("F1");
    }

    public void onSliderNeighborSeeks()
    {
        if(sld_NeighborSeeks.value < 15)
        {
            neighborSeeks = (int)sld_NeighborSeeks.value;
        }
        else
        {
            neighborSeeks = 15 + (int)(sld_NeighborSeeks.value*40f);
        }

        
        NeighborSeeksText.text = "Neighbor Seeks = " + neighborSeeks.ToString();
    }


    public enum moverStates
    {
        Paused,
        Moving,
    };

    class ToFroms
    {
        public int hIdx;
        public Quaternion q0;
        public Quaternion q1;
        public int aIdx;
        public moverStates state;
        public float timer;
        public float pauseTime;
        public float movetime;
    }

    List<ToFroms> toFromList = new List<ToFroms>();

    void init_mover()
    {
        int idxStart;
        int idxEnd;
        Color col;
        Quaternion q0, q1;

        for (int i = 0; i < 300; i++)
        {
            idxStart = 4096 + Random.Range(-1000, 1000);

            int idx = idxStart;
            for (int j = 0; j < 5; j++)
            {
                idx = asi.HexQuads[idx].neighborLoc[Random.Range(0, 5)];
            }

            idxEnd = idx;
            col = getRandColor();
            asu.lightHex(idxStart, hexPrefab, col);

            ToFroms tf = new ToFroms();

            q0 = asi.HexQuads[idxStart].GetQuat();
            q1 = asi.HexQuads[idxEnd].GetQuat();
            tf.q1 = q1 * Quaternion.Inverse(q0);
            tf.q0 = Quaternion.identity;

            tf.aIdx = i;
            tf.hIdx = idxEnd;

            tf.state = moverStates.Paused;
            tf.timer = 0.0f;
            tf.pauseTime = pauseTime + Random.Range(-pauseTime * variance / 2, pauseTime * variance / 2);
            tf.movetime = moveTime + Random.Range(-moveTime * variance / 2, moveTime * variance / 2);

            toFromList.Add(tf);

        }
    }


    void Update()
    {
        Quaternion q0, q1;

        for (int i = 0; i < 300; i++)
        {
            toFromList[i].timer += Time.deltaTime;

            if (toFromList[i].state == moverStates.Paused)
            {
                toFromList[i].timer += Time.deltaTime;

                if (toFromList[i].timer > toFromList[i].pauseTime)
                {
                    toFromList[i].state = moverStates.Moving;
                    toFromList[i].timer = 0.0f;
                }
            }
            else //(toFromList[i].state == moverStates.Moveing)
            {
                float sTime = ElasticEaseInOut(toFromList[i].timer, 0.0f, 1.0f, toFromList[i].movetime);

                asu.aQuads[toFromList[i].aIdx].go.transform.rotation = Quaternion.LerpUnclamped(toFromList[i].q0, toFromList[i].q1, sTime);

                if (toFromList[i].timer > toFromList[i].movetime)
                {
                    asu.aQuads[toFromList[i].aIdx].go.transform.rotation = Quaternion.LerpUnclamped(toFromList[i].q0, toFromList[i].q1, 1.0f);
                    toFromList[i].state = moverStates.Paused;
                    toFromList[i].timer = 0.0f;

                    int idx = toFromList[i].hIdx;
                    int idxTest;
                    int idxStart, idxEnd;

                    int seeks = neighborSeeks + Random.Range((int)(-(float)(variance/2)*neighborSeeks), (int)((float)(variance / 2) * neighborSeeks)) ;

                    for (int j = 0; j < seeks; j++)//Let it randomly find the next point by searching through "seeks" neighbors
                    {
                        idxTest = asi.HexQuads[idx].neighborLoc[Random.Range(0, 6)];
                        if (idxTest > 1000 && idxTest < 7000) idx = idxTest;
                        else idx = toFromList[i].hIdx; 
                    }
                    idxStart = toFromList[i].hIdx;
                    idxEnd = idx;
                    toFromList[i].hIdx = idx;
                    q1 = asi.HexQuads[idxEnd].GetQuat();
                    q0 = asi.HexQuads[idxStart].GetQuat();
                    toFromList[i].q1 = q1 * Quaternion.Inverse(q0) * asu.aQuads[toFromList[i].aIdx].go.transform.rotation;
                    toFromList[i].q0 = asu.aQuads[toFromList[i].aIdx].go.transform.rotation;

                    toFromList[i].state = moverStates.Paused;
                    toFromList[i].timer = 0.0f;
                    toFromList[i].pauseTime = pauseTime + Random.Range(-pauseTime * variance / 2, pauseTime * variance / 2);
                    toFromList[i].movetime = moveTime + Random.Range(-moveTime * variance / 2, moveTime * variance / 2);
                }
            }
        }
    }

    //q0 = asi.HexQuads[3060].GetQuat();
    //q1 = asi.HexQuads[4064].GetQuat();
    //q2 =  q1* Quaternion.Inverse(q0);
    //q0 = Quaternion.identity;
    void init_OneOFF()
    {
        int idxStart;
        int idxEnd;
        Color col;
        Quaternion q0,q1;

        for (int i = 0; i < 30; i++)
        {
            idxStart = 4096 + Random.Range(-1000, 1000);

            int idx = idxStart;
            for (int j = 0; j<20; j++)
            {
                idx = asi.HexQuads[idx].neighborLoc[Random.Range(0, 5)];
            }
            idxEnd = idx;
            col = getRandColor();
            asu.lightHex(idxStart, hexPrefab, col);

            asu.lightHex(idxStart, hexPrefab, col);

            asu.lightHex(idxEnd, hexPrefab, col);

            ToFroms tf = new ToFroms();

            q0 = asi.HexQuads[idxStart].GetQuat();
            q1 = asi.HexQuads[idxEnd].GetQuat();
            tf.q0 = Quaternion.identity;
            tf.q1 = q1 * Quaternion.Inverse(q0);

            tf.aIdx = i * 3 + 1;
            tf.hIdx = idxStart;

            toFromList.Add(tf);
        }
    }

    float accumTime = 0.0f; 
    //void Update_OneOff()
    //{
    //    accumTime += Time.deltaTime;
    //    float linTime;
    //    float sTime;

    //    if(accumTime>2f)
    //    {
    //        linTime = (accumTime - 2f);

    //        for (int i = 0; i < 30; i++)
    //        {
    //            sTime = ElasticEaseInOut(linTime, 0.0f, 1.0f, durr);

    //            if (linTime > durr) sTime = 1.0f;

    //            asu.aQuads[toFromList[i].aIdx].go.transform.rotation = Quaternion.SlerpUnclamped(toFromList[i].q0, toFromList[i].q1, sTime);
    //        }
    //        if (accumTime > 2+durr) accumTime = 0;
    //    }    
    //}


    /// <summary>
    /// Easing equation function for an elastic (exponentially decaying sine wave) easing out: 
    /// decelerating from zero velocity.
    /// </summary>
    /// <param name="t">Current time in seconds.</param>
    /// <param name="b">Starting value.</param>
    /// <param name="c">Final value.</param>
    /// <param name="d">Duration of animation.</param>
    /// <returns>The correct value.</returns>
    public static float ElasticEaseOut(float t, float b, float c, float d)
    {
        if ((t /= d) == 1)
            return b + c;

        float p = d * 0.3f;
        float s = p / 4;

        return (c * Mathf.Pow(2, -10 * t) * Mathf.Sin((t * d - s) * (2 * Mathf.PI) / p) + c + b);
    }

    /// <summary>
    /// Easing equation function for an elastic (exponentially decaying sine wave) easing in: 
    /// accelerating from zero velocity.
    /// </summary>
    /// <param name="t">Current time in seconds.</param>
    /// <param name="b">Starting value.</param>
    /// <param name="c">Final value.</param>
    /// <param name="d">Duration of animation.</param>
    /// <returns>The correct value.</returns>
    public static float ElasticEaseIn(float t, float b, float c, float d)
    {
        if ((t /= d) == 1)
            return b + c;

        float p = d * 0.3f;
        float s = p / 4;

        return -(c * Mathf.Pow(2, 10 * (t -= 1)) * Mathf.Sin((t * d - s) * (2f * Mathf.PI) / p)) + b;
    }

    /// <summary>
    /// Easing equation function for an elastic (exponentially decaying sine wave) easing in/out: 
    /// acceleration until halfway, then deceleration.
    /// </summary>
    /// <param name="t">Current time in seconds.</param>
    /// <param name="b">Starting value.</param>
    /// <param name="c">Final value.</param>
    /// <param name="d">Duration of animation.</param>
    /// <returns>The correct value.</returns>
    public static float ElasticEaseInOut(float t, float b, float c, float d)
    {
        if ((t /= d / 2) == 2)
            return b + c;

        float p = d * (0.3f * 1.5f);
        float s = p / 4;

        if (t < 1)
            return -0.5f * (c * Mathf.Pow(2, 10 * (t -= 1)) * Mathf.Sin((t * d - s) * (2 * Mathf.PI) / p)) + b;
        return c * Mathf.Pow(2, -10 * (t -= 1)) * Mathf.Sin((t * d - s) * (2 * Mathf.PI) / p) * 0.5f + c + b;
    }

    /// <summary>
    /// Easing equation function for an elastic (exponentially decaying sine wave) easing out/in: 
    /// deceleration until halfway, then acceleration.
    /// </summary>
    /// <param name="t">Current time in seconds.</param>
    /// <param name="b">Starting value.</param>
    /// <param name="c">Final value.</param>
    /// <param name="d">Duration of animation.</param>
    /// <returns>The correct value.</returns>
    public static float ElasticEaseOutIn(float t, float b, float c, float d)
    {
        if (t < d / 2)
            return ElasticEaseOut(t * 2, b, c / 2, d);
        return ElasticEaseIn((t * 2) - d, b + c / 2, c / 2, d);
    }






    Color getRandColor()
    {
        Color col;

        col.r = 0.4f + Random.Range(0.0f, 0.6f);
        col.g = 0.5f + Random.Range(0.0f, 0.5f);
        col.b = 0.1f + Random.Range(0.0f, 0.9f);
        col.a = 0.8f + Random.Range(0.0f, 0.2f);
        return col;
    }

    bool drawn = false;
    float numSpawned = 0;
    int quadIdx = 1536;

	void Update_Org ()
    {
        accumTime += Time.deltaTime;
        Color col;
        if (accumTime>1.0f && !drawn)
        {
            //HEX Quads
            //Draw 3 sets until done - (50 times ~ 1 second)
            for (int i = 0; i < 3; i++)
            {        
                int centerIdx = 4096 + Random.Range(-2048, 2048);
                asu.lightHexDuration(centerIdx, hexPrefab, getRandColor(), 4.0f + Random.Range(0f,2.5f));

                col = getRandColor();
                for(int j = 0; j<6; j++)
                {   
                    asu.lightHexDuration(asi.HexQuads[centerIdx].neighborLoc[j], hexPrefab, col,  8.0f + Random.Range(0f, .25f));
                }  
            }
            if (++numSpawned > 150)
            {
                drawn = true;
            }
        }
        //Quad Quads

        for (int i = 0; i < 2; i++)
        {
            asu.lightQuadDuration(quadIdx, quadPrefab, quadColor, 1.0f);
            if (++quadIdx > 2560) quadIdx = 1536;
        }

        if (drawn && accumTime > 14.0f)
        {
            drawn = false;
            accumTime = 0.0f;
            numSpawned = 0;
        }
    }
}