🔍

remotely touching – Realtime OSC communication with C# and Unity and LeapMotion

C# Codes

Sehr empfehlende OSC Basic Bibliothek von Thomas Fredericks für den Umgang mit OSC Daten in Unity. Funzt. Fertig. Braucht es für diese Lösung. https://github.com/thomasfredericks/UnityOSC

Das u.g. Skript baut auf diese OSC-Bibliothek auf und ist explizit für eine Hand der Leap Motion angepasst. Alle Details checkbar in der LiveCoding Session 🙂

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

public class osc_hand_manager : MonoBehaviour
{

    const int bonecount = 16;

    public OSC osc;

     public string osc_label = "lh"; 

     public bool do_x_mirror = false;
      public bool do_z_mirror = false;

    public Transform[] me_hand_dict = new Transform[bonecount];
    public Transform[] other_hand_dict = new Transform[bonecount];

    public float smooth_motion_speed = .05f;
    public float smooth_rotation_speed = .05f;
 

     Vector3[] t_other_positions = new Vector3[bonecount];
     Quaternion[] t_other_rotations = new Quaternion[bonecount];

     public Vector3 other_offset = Vector3.zero;

     
    //  0 wrist
    //  1 palm

    //  2 a_index
    //  3 a_middle
    //  4 a_pinky
    //  5 a_ring
    //  6 a_thumb

    //  12 b_index
    //  13 b_middle
    //  14 b_pinky
    //  15 b_ring
    //  16 b_thumb

    //  12 c_index
    //  13 c_middle
    //  14 c_pinky
    //  15 c_ring
    //  16 c_thumb


    // Start is called before the first frame update
    void Start()
    {
         
        Invoke("sendOSC",1f);
         osc.SetAddressHandler( "/" + osc_label + "_trans" , recievedOSC );

    }

    // Update is called once per frame
    void Update()
    {
        
       smooth_input_values();

    }

    void smooth_input_values(){


         for(int i=0;i<bonecount;i++){

            other_hand_dict[i].localPosition = Vector3.Lerp(other_hand_dict[i].localPosition,t_other_positions[i],smooth_motion_speed);
            other_hand_dict[i].localRotation = Quaternion.Lerp(other_hand_dict[i].localRotation,t_other_rotations[i],smooth_rotation_speed);
 
         }
 

    }

    void sendOSC(){

         OscMessage msg = new OscMessage(); //create a new OSC Message

        msg.address = "/" + osc_label + "_trans";

        for(int i=0;i<me_hand_dict.Length;i++){

            msg.values.Add(me_hand_dict[i].transform.localPosition.x);
            msg.values.Add(me_hand_dict[i].transform.localPosition.y);
            msg.values.Add(me_hand_dict[i].transform.localPosition.z);

            msg.values.Add(me_hand_dict[i].transform.localRotation.x);
            msg.values.Add(me_hand_dict[i].transform.localRotation.y);
            msg.values.Add(me_hand_dict[i].transform.localRotation.z);
            msg.values.Add(me_hand_dict[i].transform.localRotation.w);

        }

        osc.Send(msg);
        Invoke("sendOSC",.1f);
    }

    void recievedOSC(OscMessage msg){
		 
         int msg_tick = 0;
       for(int i=0;i<other_hand_dict.Length;i++){
 
          float x = msg.GetFloat(0 + msg_tick);
          float y = msg.GetFloat(1 + msg_tick);
          float z = msg.GetFloat(2 + msg_tick);

            // mirror movement
            if(msg_tick==0 && do_z_mirror){
                z *=-1f;
               
            }

             if(msg_tick==0 && do_x_mirror){
                x *=-1f;
             }

            

          Vector3 tpos = new Vector3(x,y,z);

           float rx = msg.GetFloat(3 + msg_tick);
          float ry = msg.GetFloat(4 + msg_tick);
          float rz = msg.GetFloat(5 + msg_tick);
          float rw = msg.GetFloat(6 + msg_tick);

           Quaternion trot = new Quaternion(rx,ry,rz,rw);

             if(msg_tick==0){

                  trot = Quaternion.Inverse(trot);
             }
          
            
            // set the target transforms
            t_other_positions[i] = tpos;
            
            
            t_other_rotations[i] = trot;

            if(msg_tick==0){
                t_other_positions[i] += other_offset;
                t_other_rotations[i] *=  Quaternion.Euler(new Vector3(0,180f,0));
            }

            msg_tick +=7;
       }
    }

}

Tiptop Werkzeuge


PROTOKOL ist ein tiptop Werkzeug von Hexler.net um OSC Datenströme zu checken. Free to use. Alles richtig gemacht!

https://hexler.net/protokol