devNotes 11-28-2016 Buffer Scaling – Sarabande

http://spectrum.ieee.org/biomedical/devices/neuroscientists-wirelessly-control-the-brain-of-a-scampering-lab-mouse

dfhgnhfnfhnnh

 

 

 

using UnityEngine;
using System.Collections;
using VRTK;
using UnityEngine.UI;
using ArgosTweenSpacePuppy;

public class CenterSphere_Interactions : MonoBehaviour
{
    private VRTK_InteractableObject interact;
    private Vector3 myPosition;
    private Text posText;
    private bool isTouched;
    private Transform center;
    private MD_Measure_Cube md_Measure_Cube;
    private MD_Scale_Cube md_Scale_Cube;
    private bool wasTouched = false;
    private bool bSnapModeOn = false;
    private Rigidbody rb;
    private GameObject tController;

    private int wait_for_touch = 0;

    private float tAccum_Scaler;

    private VRTK_ControllerActions controllerActions;
    private VRTK_ControllerEvents controllerEvents;

    private Vector3 vRounded;

    public GameObject Phantom_Scaler;
    public float ps_Base_Scale;

    float waitForUpdate = 0;
    Vector3 vLast_Snap;

    Vector3 vLast_Spawn;
    float dt_since_Spawn;

    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
    }

    void Start()
    {
        interact = GetComponent<VRTK_InteractableObject>();
        interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoObjectTouched);
        interact.InteractableObjectUntouched += new InteractableObjectEventHandler(DoObjectUntouched);
        interact.InteractableObjectGrabbed += new InteractableObjectEventHandler(DoObjectGrabbed);
        interact.InteractableObjectUngrabbed += new InteractableObjectEventHandler(DoObjectUnGrabbed);

        rb = GetComponent<Rigidbody>();

        Text[] t = GetComponentsInChildren<Text>();
        posText = t[0];

        center = GetComponentInChildren<Inner_Sphere>().transform;

        posText.text = transform.position.ToString("F2");
        isTouched = false;
        HidePosition();

        if (Phantom_Scaler != null)
        {
            ps_Base_Scale = Phantom_Scaler.transform.localScale.x;
        }

        md_Measure_Cube = FindObjectOfType< MD_Measure_Cube >();
        md_Scale_Cube   = FindObjectOfType< MD_Scale_Cube >();
    }

    private void Init()
    {
        //interact = GetComponent<VRTK_InteractableObject>();
        //interact.InteractableObjectTouched += new InteractableObjectEventHandler(DoObjectTouched);
        //Text[] t = GetComponentsInChildren<Text>();
        //posText = t[0];

        //posText.text = transform.position.ToString("F2");
        //isTouched = true;
    }

    //public void SetPosition(int NoteIx)
    //{
    //    Text[] t = GetComponentsInChildren<Text>();
    //    t[0].text = transform.position.ToString("F2");
    //}

    public void setPhantom_Scaler(GameObject go)
    {
        Phantom_Scaler = go;
        ps_Base_Scale = Phantom_Scaler.transform.localScale.x;
    }

    public void SetText(string s)
    {
        Text[] t = GetComponentsInChildren<Text>();
        t[0].text = s;
    }

    void OnCollisionEnter(Collision collisionInfo)
    {
        //argos_SoundGen.playNote(NoteIdx);
        //dbg_Text.text = " DoObjectTouched_PlayNote called";
    }

    private void DoObjectGrabbed(object sender, InteractableObjectEventArgs e)
    {

    }

    private void DoObjectUnGrabbed(object sender, InteractableObjectEventArgs e)
    {
        if (md_Measure_Cube.isTriggerPressed())
        {
            vRounded.y += HMD_Ctrl_Tracking.Instance.Get_Grid_Height();
            rb.position = vRounded;
        }
        rb.velocity = Vector3.zero;
        rb.angularVelocity = Vector3.zero;
    }
    //StartTouching(GameObject currentTouchingObject)
    //public override void StartTouching(GameObject touchingObject)
    //{
    //    base.StartTouching(touchingObject);
    //    controllerActions = touchingObject.GetComponent<VRTK_ControllerActions>();
    //    controllerEvents = touchingObject.GetComponent<VRTK_ControllerEvents>();
    //    controllerActions.TriggerHapticPulse(3333, 0.5f, 0.01f);
    //}


    private void DoObjectTouched(object sender, InteractableObjectEventArgs e)
    {
        isTouched = true;
        tController = e.interactingObject;
        rb.constraints = RigidbodyConstraints.FreezeAll;
        ShowPosition();
        vLast_Spawn = rb.position;
        dt_since_Spawn = 0f;
    }

    private void DoObjectUntouched(object sender, InteractableObjectEventArgs e)
    {
        isTouched = false;
        HidePosition();
        StartCoroutine(WaitToUnConstrain());
        rb.velocity = Vector3.zero;
        rb.angularVelocity = Vector3.zero;
    }

    private IEnumerator WaitToUnConstrain()
    {
        yield return new WaitForSeconds(0.2f);
        //rb.constraints = RigidbodyConstraints.None;
        rb.velocity = Vector3.zero;
        rb.angularVelocity = Vector3.zero;
    }

    public void ShowPosition()
    {
        tAccum_Scaler = 0;
        StopCoroutine("TweenPosTextScale");
        StartCoroutine("TweenPosTextScale", true);
    }

    public void HidePosition()
    {
        tAccum_Scaler = 0;
        StopCoroutine("TweenPosTextScale");
        StartCoroutine("TweenPosTextScale", false);
    }

    private IEnumerator TweenPosTextScale(bool show)
    {
        float targetScale = 0;
        Vector3 Dir = -1 * Vector3.one;
        if (show)
        {
            targetScale = 1;
            Dir = Vector3.one;
        }
        int i = 0; //Sanity check for infinite loops
        while (i < 250 && ((show && center.localScale.x < targetScale) || (!show && center.localScale.x > targetScale)))
        {
            center.localScale += Dir * Time.deltaTime * 2f; //Tweening function - currently 0.25 second linear
            if (Phantom_Scaler != null)
            {
                Phantom_Scaler.transform.localScale = ps_Base_Scale*(0.9f * Vector3.one + 0.1f * center.localScale.x * Vector3.one);
            }
            yield return true;
            i++;
        }
        center.localScale = Dir * targetScale;
        if (Phantom_Scaler != null)
        {
            Phantom_Scaler.transform.localScale = ps_Base_Scale * (0.9f * Vector3.one + 0.1f * center.localScale.x * Vector3.one);
        }

        //transform.localScale = Dir * (.333f + .666f * targetScale);
        StopCoroutine("TweenPosTextScale");
    }

    private float round_to_Point_25(float val)
    {
        float doub = val * (4f/5f);
        doub = Mathf.Round(doub);
        return doub * (5f/4f);
    }

    void Update()
    {
        wait_for_touch++; //debounce Touch Events

        waitForUpdate -= Time.deltaTime;
        
        if (waitForUpdate < 0)
        {
            if (isTouched)
            {
                Vector3 v = rb.position;
                v.y -= HMD_Ctrl_Tracking.Instance.Get_Grid_Height();

                if (md_Measure_Cube.isTriggerPressed()) //snap mode
                {
                    v.x = round_to_Point_25(v.x);
                    v.y = round_to_Point_25(v.y);
                    v.z = round_to_Point_25(v.z);
                    vRounded = v;

                    if(vLast_Snap != v)
                    {
                        StartCoroutine(Haptic_Click());
                    }
                    vLast_Snap = v;
                    bSnapModeOn = true;
                }
                else
                {
                    if (bSnapModeOn)
                    {
                        this.GetComponent<VRTK_InteractableObject>().ForceStopInteracting();
                        vRounded.y += HMD_Ctrl_Tracking.Instance.Get_Grid_Height();
                        rb.position = vRounded;
                    }
                    bSnapModeOn = false;
                }

                md_Measure_Cube.Update_To_Position(v);

                Vector3 vScaled = v / 5f;

                posText.text = vScaled.ToString("F2");

                if (Camera.main != null)
                {
                    center.rotation = Camera.main.transform.rotation;
                }
                dt_since_Spawn += Time.deltaTime;
                Vector3 vMoved = v - vLast_Spawn;
                
                if(vMoved.magnitude > 0.5f && dt_since_Spawn > 0.025f)
                {
                    md_Scale_Cube.Spawn_Update_this_frame();
                    dt_since_Spawn = 0;
                    vLast_Spawn = v;
                }
            }
        }
    }

    private IEnumerator Haptic_Click()
    {
        int i = 0;

        while (i < 1)
        {
            if(tController != null)
            {
                tController.GetComponent<VRTK_ControllerActions>().TriggerHapticPulse(600);
            }
            yield return true;
            i++;
        }
        StopCoroutine(Haptic_Click());
    }

}

 

Scaling – Particle Buffer – Performance – Handle Center Sphere – Extents

Brushes = Types ideas for Shader Dynamics

 

 

 

 

 

sarabande