OculusControls.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.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.freeze ) {
  41. return;
  42. }
  43. this.object.quaternion.multiply(this.headquat);
  44. };
  45. function bind( scope, fn ) {
  46. return function () {
  47. fn.apply( scope, arguments );
  48. };
  49. };
  50. this.connect = function() {
  51. this.queuePoll();
  52. };
  53. };