Last active
August 1, 2017 07:38
-
-
Save cmdr2/0e8e367d07e9aae93fccd51a51e955e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using WebSocketSharp; | |
/** | |
* a reference client for the fakedaydreamcontroller | |
* by cmdr2 <secondary.cmdr2@gmail.com> | |
* | |
* Set the hostname to the IP address of your headset's phone. | |
* Uses: UnitySerializer.cs (https://git.io/v7B7Y, also found | |
* in fakedaydream.unitypackage) | |
*/ | |
public class FakeDaydreamControllerClient : MonoBehaviour { | |
/* dependencies */ | |
public string hostname = "localhost"; | |
/* scratchpad */ | |
private WebSocket socket; | |
private float prevGyroX = 0; | |
private float prevGyroY = 0; | |
private float prevGyroZ = 0; | |
private Vector3 gyro = Vector3.zero; | |
private Vector2 centerTouchPos = new Vector2(0.5f, 0.5f); | |
private bool ready = true; | |
private bool simulateGyroWithMouse = true; | |
private bool allowRunningOnAndroid = false; | |
void Start () { | |
if (!allowRunningOnAndroid && Application.platform == RuntimePlatform.Android) { | |
return; | |
} | |
Cursor.lockState = CursorLockMode.Locked; | |
socket = new WebSocket ("ws://" + hostname + ":1337/controller"); | |
socket.OnMessage += OnMessage; | |
socket.Connect (); | |
print ("socket: " + socket.Url); | |
//UnityEngine.VR.InputTracking.Recenter (); | |
} | |
void Update () { | |
if (!allowRunningOnAndroid && Application.platform == RuntimePlatform.Android) { | |
return; | |
} | |
//print (socket.ReadyState); | |
if (socket.ReadyState != WebSocketState.Open) { | |
return; | |
} | |
if (ready) { | |
SendPacket (); | |
} | |
} | |
private void SendPacket() { | |
var rotX = 0; //cam.localRotation.y; // y | |
var rotY = 0; //cam.localRotation.z; // z | |
var rotZ = 0; //cam.localRotation.x; // x | |
var orientation = Quaternion.identity; // cam.localRotation; | |
gyro.x = rotX - prevGyroX; | |
gyro.y = (simulateGyroWithMouse ? Input.GetAxis("Mouse X") / 5f : rotY - prevGyroY); | |
gyro.z = (simulateGyroWithMouse ? Input.GetAxis("Mouse Y") / 5f : rotZ - prevGyroZ); | |
var accel = Vector3.zero; | |
var isTouching = Input.GetButton("Fire1"); | |
var touchDown = Input.GetButtonDown("Fire1"); | |
var touchUp = Input.GetButtonUp("Fire1"); | |
var touchPos = centerTouchPos; | |
var recentering = Input.GetKey(KeyCode.Z); | |
var recentered = Input.GetKeyUp(KeyCode.Z); | |
var clickButton = Input.GetButton ("Fire3"); | |
var clickButtonDown = Input.GetButtonDown ("Fire3"); | |
var clickButtonUp = Input.GetButtonUp ("Fire3"); | |
var appButton = Input.GetButton ("Fire2"); | |
var appButtonDown = Input.GetButtonDown ("Fire2"); | |
var appButtonUp = Input.GetButtonUp ("Fire2"); | |
// var homeButton = Input.GetKey(KeyCode.Escape); | |
gyro *= 150; | |
var packet = new UnitySerializer(); | |
packet.Serialize (orientation); | |
packet.Serialize (gyro); | |
packet.Serialize (accel); | |
packet.Serialize (isTouching); | |
packet.Serialize (touchDown); | |
packet.Serialize (touchUp); | |
packet.Serialize (touchPos); | |
packet.Serialize (recentering); | |
packet.Serialize (recentered); | |
packet.Serialize (clickButton); | |
packet.Serialize (clickButtonDown); | |
packet.Serialize (clickButtonUp); | |
packet.Serialize (appButton); | |
packet.Serialize (appButtonDown); | |
packet.Serialize (appButtonUp); | |
// packet.Serialize (homeButton); | |
socket.Send (packet.ByteArray); | |
prevGyroX = rotX; | |
prevGyroY = rotY; | |
prevGyroZ = rotZ; | |
} | |
private void OnMessage(object sender, MessageEventArgs e) { | |
//print ("server: " + e.Data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment