OculusControls.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.headquat = new THREE.Quaternion();
  12. this.freeze = false;
  13. this.object.useQuaternion = true;
  14. this.loadAjaxJSON = function ( url, callback ) {
  15. var xhr = new XMLHttpRequest();
  16. xhr.onreadystatechange = function () {
  17. if ( xhr.readyState === xhr.DONE ) {
  18. if ( xhr.status === 200 || xhr.status === 0 ) {
  19. if ( xhr.responseText ) {
  20. var json = JSON.parse( xhr.responseText );
  21. callback( json );
  22. }
  23. }
  24. }
  25. };
  26. xhr.open( "GET", url, true );
  27. xhr.withCredentials = false;
  28. xhr.send( null );
  29. };
  30. this.gotCoordinates = function( r ) {
  31. this.headquat.set(r.quat.x, r.quat.y, r.quat.z, r.quat.w);
  32. this.queuePoll();
  33. }
  34. this.pollOnce = function() {
  35. this.loadAjaxJSON('http://localhost:50000', bind(this, this.gotCoordinates));
  36. }
  37. this.queuePoll = function() {
  38. setTimeout(bind(this, this.pollOnce), 10);
  39. }
  40. this.update = function( delta ) {
  41. if ( this.freeze ) {
  42. return;
  43. }
  44. this.object.quaternion.multiply(this.headquat);
  45. };
  46. function bind( scope, fn ) {
  47. return function () {
  48. fn.apply( scope, arguments );
  49. };
  50. };
  51. this.connect = function() {
  52. this.queuePoll();
  53. };
  54. };