OculusControls.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @author possan / http://possan.se/
  3. *
  4. * Oculus headtracking control
  5. * - use together with the oculus-rest project to get headtracking
  6. * coordinates from the rift: http://github.com/possan/oculus-rest
  7. */
  8. THREE.OculusControls = function ( object ) {
  9. this.object = object;
  10. this.target = new THREE.Vector3( 0, 0, 0 );
  11. this.enabled = true;
  12. this.headquat = new THREE.Quaternion();
  13. this.loadAjaxJSON = function ( url, callback ) {
  14. var xhr = new XMLHttpRequest();
  15. xhr.onreadystatechange = function () {
  16. if ( xhr.readyState === xhr.DONE ) {
  17. if ( xhr.status === 200 || xhr.status === 0 ) {
  18. if ( xhr.responseText ) {
  19. var json = JSON.parse( xhr.responseText );
  20. callback( json );
  21. }
  22. }
  23. }
  24. };
  25. xhr.open( "GET", url, true );
  26. xhr.withCredentials = false;
  27. xhr.send( null );
  28. };
  29. this.gotCoordinates = function( r ) {
  30. this.headquat.set(r.quat.x, r.quat.y, r.quat.z, r.quat.w);
  31. this.queuePoll();
  32. }
  33. this.pollOnce = function() {
  34. this.loadAjaxJSON('http://localhost:50000', bind(this, this.gotCoordinates));
  35. }
  36. this.queuePoll = function() {
  37. setTimeout(bind(this, this.pollOnce), 10);
  38. }
  39. this.update = function( delta ) {
  40. if ( this.enabled === false ) return;
  41. this.object.quaternion.multiply(this.headquat);
  42. };
  43. function bind( scope, fn ) {
  44. return function () {
  45. fn.apply( scope, arguments );
  46. };
  47. };
  48. this.connect = function() {
  49. this.queuePoll();
  50. };
  51. };