cursor work

 

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

public class Cursor_1 : MonoBehaviour
{
    StreamWriter sWrite;

    public float T1 = 0.5f;
    public float T2 = 5f;
    public float wiggleFreq = 0.5f;
    public float startScale = 1f;
    public float pctModulate = 0.5f;

    private int numHQuadsInCursor = 300; 

    public UserMovement userMovement;
    public ArgosSphere_Indexing argos_Sphere;

    public PaintElement hexElement;
    public PaintElement paintElement;

    PaintElement instHexElement;
    PaintElement instPaintElement;

    Quaternion myStartRotation;
    int iCurrHexIDX;

    MeshDraft mdScratch; //Watch out for stepping on template Hex Quads

    public class Hex_Track
    {
        public float timer;
        public int type;
        public int iHAddess;
        public float currScale;
        public float vCV_Mag; //magnitude of vector Center of quad to vertex;
    }

    public List<Hex_Track> htHexList = new List<Hex_Track>();

    public class Paint_Track
    {
        public float timer;
        public int type;
    }

    public List<Paint_Track> ptPaintList = new List<Paint_Track>();

    Vector3 v0 = new Vector3();
    Vector3 v1 = new Vector3();
    Vector3 v2 = new Vector3();
    Vector3 v3 = new Vector3();

    Quaternion LocalRotation;
    Vector3 vCurrLoc;
    Vector3 vRotated;

    int count = 0;
    Vector3 vC;
    Vector3 vC0;
    Hex_Track ht;
    Vector3 vCentLast;
    float vAvgDistVc1_Vc2;

    void Start ()
    {
        sWrite = new StreamWriter("Argos_Cursor.txt");
    }

    void OnApplicationQuit()
    {
        sWrite.Close();
    }

    void OnEnable()
    {
        mdScratch = new MeshDraft();
        Initialize();
    }

    void OnDisable()
    {

    }

    void Initialize()
    {
        if(instHexElement != null)
        {
            Destroy(instHexElement.gameObject);
            htHexList.Clear();
        }

        instHexElement = Instantiate(hexElement);

        instHexElement.transform.SetParent(argos_Sphere.transform);
        instHexElement.transform.localPosition = Vector3.zero;
        myStartRotation = argos_Sphere.transform.rotation;

        iCurrHexIDX = 0;
    }

    void Update ()
    {
        int idxCheck;
        vCurrLoc = userMovement.cursorTran.pos - argos_Sphere.transform.position;
        LocalRotation = Quaternion.Inverse(argos_Sphere.transform.rotation);
        vRotated = LocalRotation * vCurrLoc;
        idxCheck = argos_Sphere.getHexIdx(vRotated);
        
        if (idxCheck != iCurrHexIDX)
        {
            if(instHexElement.GetNumVertices() > numHQuadsInCursor*4)
            {
                instHexElement.Mdraft.Remove_Front_Quad();
                htHexList.RemoveAt(0);
                count--;
            }

            iCurrHexIDX = idxCheck;
            mdScratch.Copy_MeshDraft(argos_Sphere.HexQuads[idxCheck].mdH);
            mdScratch.Rotate(myStartRotation);

            instHexElement.AddMeshDraft_Only(mdScratch);

            ht = new Hex_Track();
            ht.iHAddess = iCurrHexIDX;
            ht.type = 0;
            ht.timer = 0f;
            ht.currScale = 1.0f;

            vC = (mdScratch.vertices[0] + mdScratch.vertices[1] + mdScratch.vertices[2] + mdScratch.vertices[3]) / 4f;
            vC0 = mdScratch.vertices[0] - vC;

            ht.vCV_Mag = vC0.magnitude;
            htHexList.Add(ht);

            count++;

            instHexElement.MeshDraft_ToMesh();
        }

        Vector3 vCent;
        for (int i = 0; i < htHexList.Count; i++)
        {
            htHexList[i].timer += Time.deltaTime;
            float scale = argos_wiggle_evnvelope(T1, T2, htHexList[i].timer);
            float deltaScale = 1f + scale - htHexList[i].currScale;
            htHexList[i].currScale = scale;

            v0 = instHexElement.GetVertex((i * 4) + 0);
            v1 = instHexElement.GetVertex((i * 4) + 1);
            v2 = instHexElement.GetVertex((i * 4) + 2);
            v3 = instHexElement.GetVertex((i * 4) + 3);

            vCent = (v0 + v1 + v2 + v3) / 4f;

            v0 = v0 - vCent;
            v1 = v1 - vCent;
            v2 = v2 - vCent;
            v3 = v3 - vCent;

            v0.Normalize();
            v1.Normalize();
            v2.Normalize();
            v3.Normalize();

            instHexElement.Mdraft.vertices[i * 4 + 0] = vCent + scale * htHexList[i].vCV_Mag * v0 * startScale;
            instHexElement.Mdraft.vertices[i * 4 + 1] = vCent + scale * htHexList[i].vCV_Mag * v1 * startScale;
            instHexElement.Mdraft.vertices[i * 4 + 2] = vCent + scale * htHexList[i].vCV_Mag * v2 * startScale;
            instHexElement.Mdraft.vertices[i * 4 + 3] = vCent + scale * htHexList[i].vCV_Mag * v3 * startScale;
        }
        instHexElement.MeshDraft_ToMesh();
    }

    public float argos_bloom_envelope(float T1, float T2, float t)
    {
        if(t<T1)
        {
            return Mathf.Sin((t / T1) * (Mathf.PI / 2f));
        }
        else if(t>T1 && t<T2+T1)
        {
            return Mathf.SmoothStep(1f, 0f, (t - T1) / T2);
        }
        return 0f;
    }

    public float argos_wiggle_evnvelope(float T1, float T2, float t)
    {
        float bas,amp;

        bas = 1f - (pctModulate / 2f);
        amp = pctModulate / 2f;

        float lev = argos_bloom_envelope(T1, T2, t);
        float wiggle = bas + amp * Mathf.Sin((t) * (wiggleFreq*2f*Mathf.PI));
        if(t>0f && t < T2 + T1)
        {
            return lev * wiggle;
        }
        return 0f;
    }
}