触れるVRを目指して ~senso glove DK2買ってみた話 その3 haptic編~

1.前提条件・機材解説
2.導入
3.unityでのhaptic実験 ← イマココ
4.unityでのハンドトラッキング実験
5.メイン検証

〇unity Senso pluginでは触覚フィードバックできない

oculus touch持ってるのにわざわざgloveを買ったのはもちろん,
ラッキングではなく触覚フィードバックが欲しいからです.
しかし,公式のUnity Pluginのドキュメントはなんと同梱のプレハブの解説のみ.
どうやら触覚フィードバックの実装は自分で書かないといけないっぽいです.

幸い公式のDriver Protocolを読むと,
BLEサーバーを通してgloveに指定書式のjsonTCP通信で送ればいいことがわかります.

〇サンプルコード

Unity Socket Example(外部リンク:qiita)を参考に親指に100ms間強さ10(max)の振動を返すサンプルソースがこちら
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;

public class sensoVibrato : MonoBehaviour {
    NetworkStream stream =null;

    // Use this for initialization
    void Start () {
        string message = "{\"dst\": \"cc:78:ab:6c:3e:04\", //自分の右手gloveのmacアドレス
            \"type\": \"vibration\",
            \"data\": {\"type\":\"thumb\", //親指
                \"dur\":100, //長さ(0 - 65535ms)
                \"str\":10 //強さ(0-10)、0を指定したら現在の振動をストップ
            }}";
        StartCoroutine (SendMessage (message));
    }

    private IEnumerator SendMessage(string message){
        Debug.Log ("START SendMessage:" + message);
        if (stream == null) {
            stream = GetNetworkStream ();
        }
        Encoding enc = Encoding.UTF8;
        byte[] sendBytes = enc.GetBytes (message + "\n");
        stream.Write (sendBytes, 0, sendBytes.Length);
        yield break;
    }

    private NetworkStream GetNetworkStream(){
        if (stream != null && stream.CanRead) {
            return stream;
        }
        string ipOrHost = "127.0.0.1"; //BLEサーバーのアドレス
        int port = 53450; //右手グローブのポート番号
        TcpClient tcp = new TcpClient (ipOrHost, port);
        Debug.Log ("success conn server");
        return tcp.GetStream ();
    }
}

いろいろベタ打ちで汎用性のかけらもありませんが,とりあえず動作確認はこれでOK


続く