app.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. var scripts = json.scripts[ uuid ];
  48. for ( var i = 0; i < scripts.length; i ++ ) {
  49. var script = scripts[ i ];
  50. var functions = ( new Function( scriptWrapParams,
  51. script.source + '\nreturn ' + scriptWrapResult+ ';' ).bind( object ) )( this, renderer, scene );
  52. for ( var name in functions ) {
  53. if ( functions[ name ] === undefined ) continue;
  54. if ( events[ name ] === undefined ) {
  55. console.warn( 'APP.Player: event type not supported (', name, ')' );
  56. continue;
  57. }
  58. events[ name ].push( functions[ name ].bind( object ) );
  59. }
  60. }
  61. }
  62. dispatch( events.init, arguments );
  63. };
  64. this.getCamera = function () {
  65. return camera;
  66. };
  67. this.setCamera = function ( value ) {
  68. camera = value;
  69. camera.aspect = this.width / this.height;
  70. camera.updateProjectionMatrix();
  71. if ( vr === true ) {
  72. if ( camera.parent === null ) {
  73. // camera needs to be in the scene so camera2 matrix updates
  74. scene.add( camera );
  75. }
  76. var camera2 = camera.clone();
  77. camera.add( camera2 );
  78. camera = camera2;
  79. controls = new THREE.VRControls( camera );
  80. effect = new THREE.VREffect( renderer );
  81. document.addEventListener( 'keyup', function ( event ) {
  82. switch ( event.keyCode ) {
  83. case 90:
  84. controls.zeroSensor();
  85. break;
  86. }
  87. } );
  88. this.dom.addEventListener( 'dblclick', function () {
  89. effect.setFullScreen( true );
  90. } );
  91. }
  92. };
  93. this.setScene = function ( value ) {
  94. scene = value;
  95. },
  96. this.setSize = function ( width, height ) {
  97. if ( renderer._fullScreen ) return;
  98. this.width = width;
  99. this.height = height;
  100. camera.aspect = this.width / this.height;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( width, height );
  103. };
  104. function dispatch( array, event ) {
  105. for ( var i = 0, l = array.length; i < l; i ++ ) {
  106. try {
  107. array[ i ]( event );
  108. } catch (e) {
  109. console.error( ( e.message || e ), ( e.stack || "" ) );
  110. }
  111. }
  112. }
  113. var prevTime, request;
  114. function animate( time ) {
  115. request = requestAnimationFrame( animate );
  116. dispatch( events.update, { time: time, delta: time - prevTime } );
  117. if ( vr === true ) {
  118. controls.update();
  119. effect.render( scene, camera );
  120. } else {
  121. renderer.render( scene, camera );
  122. }
  123. prevTime = time;
  124. }
  125. this.play = function () {
  126. document.addEventListener( 'keydown', onDocumentKeyDown );
  127. document.addEventListener( 'keyup', onDocumentKeyUp );
  128. document.addEventListener( 'mousedown', onDocumentMouseDown );
  129. document.addEventListener( 'mouseup', onDocumentMouseUp );
  130. document.addEventListener( 'mousemove', onDocumentMouseMove );
  131. document.addEventListener( 'touchstart', onDocumentTouchStart );
  132. document.addEventListener( 'touchend', onDocumentTouchEnd );
  133. document.addEventListener( 'touchmove', onDocumentTouchMove );
  134. dispatch( events.start, arguments );
  135. request = requestAnimationFrame( animate );
  136. prevTime = ( window.performance || Date ).now();
  137. };
  138. this.stop = function () {
  139. document.removeEventListener( 'keydown', onDocumentKeyDown );
  140. document.removeEventListener( 'keyup', onDocumentKeyUp );
  141. document.removeEventListener( 'mousedown', onDocumentMouseDown );
  142. document.removeEventListener( 'mouseup', onDocumentMouseUp );
  143. document.removeEventListener( 'mousemove', onDocumentMouseMove );
  144. document.removeEventListener( 'touchstart', onDocumentTouchStart );
  145. document.removeEventListener( 'touchend', onDocumentTouchEnd );
  146. document.removeEventListener( 'touchmove', onDocumentTouchMove );
  147. dispatch( events.stop, arguments );
  148. cancelAnimationFrame( request );
  149. };
  150. //
  151. function onDocumentKeyDown( event ) {
  152. dispatch( events.keydown, event );
  153. }
  154. function onDocumentKeyUp( event ) {
  155. dispatch( events.keyup, event );
  156. }
  157. function onDocumentMouseDown( event ) {
  158. dispatch( events.mousedown, event );
  159. }
  160. function onDocumentMouseUp( event ) {
  161. dispatch( events.mouseup, event );
  162. }
  163. function onDocumentMouseMove( event ) {
  164. dispatch( events.mousemove, event );
  165. }
  166. function onDocumentTouchStart( event ) {
  167. dispatch( events.touchstart, event );
  168. }
  169. function onDocumentTouchEnd( event ) {
  170. dispatch( events.touchend, event );
  171. }
  172. function onDocumentTouchMove( event ) {
  173. dispatch( events.touchmove, event );
  174. }
  175. }
  176. };