app.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var APP = {
  5. Player: function () {
  6. var scope = this;
  7. var loader = new THREE.ObjectLoader();
  8. var camera, scene, renderer;
  9. var vr, controls, effect;
  10. var events = {};
  11. this.dom = undefined;
  12. this.width = 500;
  13. this.height = 500;
  14. this.load = function ( json ) {
  15. vr = json.project.vr;
  16. renderer = new THREE.WebGLRenderer( { antialias: true } );
  17. renderer.setClearColor( 0x000000 );
  18. renderer.setPixelRatio( window.devicePixelRatio );
  19. if ( json.project.shadows ) renderer.shadowMap.enabled = true;
  20. this.dom = renderer.domElement;
  21. this.setScene( loader.parse( json.scene ) );
  22. this.setCamera( loader.parse( json.camera ) );
  23. events = {
  24. init: [],
  25. start: [],
  26. stop: [],
  27. keydown: [],
  28. keyup: [],
  29. mousedown: [],
  30. mouseup: [],
  31. mousemove: [],
  32. touchstart: [],
  33. touchend: [],
  34. touchmove: [],
  35. update: []
  36. };
  37. var scriptWrapParams = 'player,renderer,scene';
  38. var scriptWrapResultObj = {};
  39. for ( var eventKey in events ) {
  40. scriptWrapParams += ',' + eventKey;
  41. scriptWrapResultObj[ eventKey ] = eventKey;
  42. }
  43. var scriptWrapResult =
  44. JSON.stringify( scriptWrapResultObj ).replace( /\"/g, '' );
  45. for ( var uuid in json.scripts ) {
  46. var object = scene.getObjectByProperty( 'uuid', uuid, true );
  47. if ( object === undefined ) {
  48. console.warn( 'APP.Player: Script without object.', uuid );
  49. continue;
  50. }
  51. var scripts = json.scripts[ uuid ];
  52. for ( var i = 0; i < scripts.length; i ++ ) {
  53. var script = scripts[ i ];
  54. var functions = ( new Function( scriptWrapParams,
  55. script.source + '\nreturn ' + scriptWrapResult+ ';' ).bind( object ) )( this, renderer, scene );
  56. for ( var name in functions ) {
  57. if ( functions[ name ] === undefined ) continue;
  58. if ( events[ name ] === undefined ) {
  59. console.warn( 'APP.Player: Event type not supported (', name, ')' );
  60. continue;
  61. }
  62. events[ name ].push( functions[ name ].bind( object ) );
  63. }
  64. }
  65. }
  66. dispatch( events.init, arguments );
  67. };
  68. this.getCamera = function () {
  69. return camera;
  70. };
  71. this.setCamera = function ( value ) {
  72. camera = value;
  73. camera.aspect = this.width / this.height;
  74. camera.updateProjectionMatrix();
  75. if ( vr === true ) {
  76. if ( camera.parent === null ) {
  77. // camera needs to be in the scene so camera2 matrix updates
  78. scene.add( camera );
  79. }
  80. var camera2 = camera.clone();
  81. camera.add( camera2 );
  82. camera = camera2;
  83. controls = new THREE.VRControls( camera );
  84. effect = new THREE.VREffect( renderer );
  85. document.addEventListener( 'keyup', function ( event ) {
  86. switch ( event.keyCode ) {
  87. case 90:
  88. controls.zeroSensor();
  89. break;
  90. }
  91. } );
  92. this.dom.addEventListener( 'dblclick', function () {
  93. effect.setFullScreen( true );
  94. } );
  95. }
  96. };
  97. this.setScene = function ( value ) {
  98. scene = value;
  99. },
  100. this.setSize = function ( width, height ) {
  101. if ( renderer._fullScreen ) return;
  102. this.width = width;
  103. this.height = height;
  104. camera.aspect = this.width / this.height;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( width, height );
  107. };
  108. function dispatch( array, event ) {
  109. for ( var i = 0, l = array.length; i < l; i ++ ) {
  110. try {
  111. array[ i ]( event );
  112. } catch (e) {
  113. console.error( ( e.message || e ), ( e.stack || "" ) );
  114. }
  115. }
  116. }
  117. var prevTime, request;
  118. function animate( time ) {
  119. request = requestAnimationFrame( animate );
  120. dispatch( events.update, { time: time, delta: time - prevTime } );
  121. if ( vr === true ) {
  122. controls.update();
  123. effect.render( scene, camera );
  124. } else {
  125. renderer.render( scene, camera );
  126. }
  127. prevTime = time;
  128. }
  129. this.play = function () {
  130. document.addEventListener( 'keydown', onDocumentKeyDown );
  131. document.addEventListener( 'keyup', onDocumentKeyUp );
  132. document.addEventListener( 'mousedown', onDocumentMouseDown );
  133. document.addEventListener( 'mouseup', onDocumentMouseUp );
  134. document.addEventListener( 'mousemove', onDocumentMouseMove );
  135. document.addEventListener( 'touchstart', onDocumentTouchStart );
  136. document.addEventListener( 'touchend', onDocumentTouchEnd );
  137. document.addEventListener( 'touchmove', onDocumentTouchMove );
  138. dispatch( events.start, arguments );
  139. request = requestAnimationFrame( animate );
  140. prevTime = ( window.performance || Date ).now();
  141. };
  142. this.stop = function () {
  143. document.removeEventListener( 'keydown', onDocumentKeyDown );
  144. document.removeEventListener( 'keyup', onDocumentKeyUp );
  145. document.removeEventListener( 'mousedown', onDocumentMouseDown );
  146. document.removeEventListener( 'mouseup', onDocumentMouseUp );
  147. document.removeEventListener( 'mousemove', onDocumentMouseMove );
  148. document.removeEventListener( 'touchstart', onDocumentTouchStart );
  149. document.removeEventListener( 'touchend', onDocumentTouchEnd );
  150. document.removeEventListener( 'touchmove', onDocumentTouchMove );
  151. dispatch( events.stop, arguments );
  152. cancelAnimationFrame( request );
  153. };
  154. //
  155. function onDocumentKeyDown( event ) {
  156. dispatch( events.keydown, event );
  157. }
  158. function onDocumentKeyUp( event ) {
  159. dispatch( events.keyup, event );
  160. }
  161. function onDocumentMouseDown( event ) {
  162. dispatch( events.mousedown, event );
  163. }
  164. function onDocumentMouseUp( event ) {
  165. dispatch( events.mouseup, event );
  166. }
  167. function onDocumentMouseMove( event ) {
  168. dispatch( events.mousemove, event );
  169. }
  170. function onDocumentTouchStart( event ) {
  171. dispatch( events.touchstart, event );
  172. }
  173. function onDocumentTouchEnd( event ) {
  174. dispatch( events.touchend, event );
  175. }
  176. function onDocumentTouchMove( event ) {
  177. dispatch( events.touchmove, event );
  178. }
  179. }
  180. };