Skip to content

Instantly share code, notes, and snippets.

@gtk2k
Last active August 29, 2015 14:01
Show Gist options
  • Save gtk2k/7958f720ed97237717ea to your computer and use it in GitHub Desktop.
Save gtk2k/7958f720ed97237717ea to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using WebSocketSharp;
using MiniJSON;
using System;
using System.Collections.Generic;
public class CreateLabel : MonoBehaviour {
WebSocket ws;
double fov;
double screenSizeH;
double screenSizeV;
double eyeToScreen;
double lensDistance;
double interpupillaryDistance;
double screenResolutionH;
double screenResolutionV;
double[] distortionK = new double[4];
double quatX;
double quatY;
double quatZ;
double quatW;
double acc0;
double acc1;
double acc2;
void Awake()
{
// こっちはスタンドアロンアプリ用WebSocketクライアント
// WebSocket-SharpをAssetsにいれて使用。
//ws = new WebSocket("ws://localhost:9005");
//ws.OnMessage += (sender, e) => {
// ParseJSON(e.Data);
//};
//ws.Connect();
}
void OnGUI()
{
string fmt =
"fov = {0:#.###}\n" +
"interpupillaryDistance = {1:#.###}" +
"eyeToScreen = {2:#.###}\n" +
"lensDistance = {3:#.###}\n" +
"distortionK = [0]={4:#.###}, [1]={5:#.###}, [2]={6:#.###}, [3]={7:#.###}\n" +
"screenSizeH = {8:#.###}\n" +
"screenSizeV = {9:#.###}\n" +
"screenResolutionH = {10:#.###}\n" +
"screenResolutionV = {11:#.###}\n" +
"\n" +
"Quaternion\n" +
" x = {12:#.###}\n" +
" y = {13:#.###}\n" +
" z = {14:#.###}\n" +
" w = {15:#.###}\n" +
"\n" +
"Acceleration\n" +
" 0 = {16:#.###}\n" +
" 1 = {17:#.###}\n" +
" 2 = {18:#.###}";
GUI.Label (new Rect (10, 10, 800, 800),
String.Format (fmt,
fov,
interpupillaryDistance,
eyeToScreen,
lensDistance,
distortionK [0], distortionK [1], distortionK [2], distortionK [3],
screenSizeH,
screenSizeV,
screenResolutionH,
screenResolutionV,
quatX,
quatY,
quatZ,
quatW,
acc0,
acc1,
acc2
)
);
}
public void FromBrowserJavascript(string data)
{
ParseJSON (data);
}
void ParseJSON(string data)
{
if (data.StartsWith ("{")) {
// Oculus Bridgeでのデータ取得
Dictionary<string,object> msg = (Dictionary<string,object>)Json.Deserialize (data);
switch ((string)msg ["m"]) {
case "config":
fov = (double)msg ["fov"];
interpupillaryDistance = (double)msg ["interpupillaryDistance"];
eyeToScreen = (double)msg ["eyeToScreen"];
lensDistance = (double)msg ["lensDistance"];
List<object> dist = msg ["distortion"] as List<object>;
distortionK [0] = (double)dist [0];
distortionK [1] = (double)dist [1];
distortionK [2] = (double)dist [2];
distortionK [3] = (double)dist [3];
List<object> sSize = msg ["screenSize"] as List<object>;
screenSizeH = (double)sSize [0];
screenSizeV = (double)sSize [1];
List<object> sRes = msg ["screenResolution"] as List<object>;
screenResolutionH = (double)sRes [0];
screenResolutionV = (double)sRes [1];
break;
case "update":
List<object> quaternion = msg ["o"] as List<object>;
quatW = (double)quaternion [0];
quatX = (double)quaternion [1];
quatY = (double)quaternion [2];
quatZ = (double)quaternion [3];
List<object> acceleration = msg ["a"] as List<object>;
acc0 = (double)acceleration [0];
acc1 = (double)acceleration [1];
acc2 = (double)acceleration [2];
break;
}
} else {
// WebRiftでのデータ取得
string[] quaternion = data.Split (',');
quatW = double.Parse (quaternion[0]);
quatX = double.Parse (quaternion[1]);
quatY = double.Parse (quaternion[2]);
quatZ = double.Parse (quaternion[3]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment