app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var APP = {
  5. Player: function () {
  6. var renderer = new THREE.WebGLRenderer( { antialias: true } );
  7. renderer.setPixelRatio( window.devicePixelRatio );
  8. renderer.outputEncoding = THREE.sRGBEncoding;
  9. var loader = new THREE.ObjectLoader();
  10. var camera, scene;
  11. var events = {};
  12. var dom = document.createElement( 'div' );
  13. dom.appendChild( renderer.domElement );
  14. this.dom = dom;
  15. this.width = 500;
  16. this.height = 500;
  17. this.load = function ( json ) {
  18. var project = json.project;
  19. if ( project.shadows ) renderer.shadowMap.enabled = true;
  20. if ( project.vr ) renderer.xr.enabled = true;
  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,camera';
  38. var scriptWrapResultObj = {};
  39. for ( var eventKey in events ) {
  40. scriptWrapParams += ',' + eventKey;
  41. scriptWrapResultObj[ eventKey ] = eventKey;
  42. }
  43. var scriptWrapResult = JSON.stringify( scriptWrapResultObj ).replace( /\"/g, '' );
  44. for ( var uuid in json.scripts ) {
  45. var object = scene.getObjectByProperty( 'uuid', uuid, true );
  46. if ( object === undefined ) {
  47. console.warn( 'APP.Player: Script without object.', uuid );
  48. continue;
  49. }
  50. var scripts = json.scripts[ uuid ];
  51. for ( var i = 0; i < scripts.length; i ++ ) {
  52. var script = scripts[ i ];
  53. var functions = ( new Function( scriptWrapParams, script.source + '\nreturn ' + scriptWrapResult + ';' ).bind( object ) )( this, renderer, scene, camera );
  54. for ( var name in functions ) {
  55. if ( functions[ name ] === undefined ) continue;
  56. if ( events[ name ] === undefined ) {
  57. console.warn( 'APP.Player: Event type not supported (', name, ')' );
  58. continue;
  59. }
  60. events[ name ].push( functions[ name ].bind( object ) );
  61. }
  62. }
  63. }
  64. dispatch( events.init, arguments );
  65. };
  66. this.setCamera = function ( value ) {
  67. camera = value;
  68. camera.aspect = this.width / this.height;
  69. camera.updateProjectionMatrix();
  70. };
  71. this.setScene = function ( value ) {
  72. scene = value;
  73. };
  74. this.setSize = function ( width, height ) {
  75. this.width = width;
  76. this.height = height;
  77. if ( camera ) {
  78. camera.aspect = this.width / this.height;
  79. camera.updateProjectionMatrix();
  80. }
  81. if ( renderer ) {
  82. renderer.setSize( width, height );
  83. }
  84. };
  85. function dispatch( array, event ) {
  86. for ( var i = 0, l = array.length; i < l; i ++ ) {
  87. array[ i ]( event );
  88. }
  89. }
  90. var time, prevTime;
  91. function animate() {
  92. time = performance.now();
  93. try {
  94. dispatch( events.update, { time: time, delta: time - prevTime } );
  95. } catch ( e ) {
  96. console.error( ( e.message || e ), ( e.stack || "" ) );
  97. }
  98. renderer.render( scene, camera );
  99. prevTime = time;
  100. }
  101. this.play = function () {
  102. prevTime = performance.now();
  103. document.addEventListener( 'keydown', onDocumentKeyDown );
  104. document.addEventListener( 'keyup', onDocumentKeyUp );
  105. document.addEventListener( 'mousedown', onDocumentMouseDown );
  106. document.addEventListener( 'mouseup', onDocumentMouseUp );
  107. document.addEventListener( 'mousemove', onDocumentMouseMove );
  108. document.addEventListener( 'touchstart', onDocumentTouchStart );
  109. document.addEventListener( 'touchend', onDocumentTouchEnd );
  110. document.addEventListener( 'touchmove', onDocumentTouchMove );
  111. dispatch( events.start, arguments );
  112. renderer.setAnimationLoop( animate );
  113. };
  114. this.stop = function () {
  115. document.removeEventListener( 'keydown', onDocumentKeyDown );
  116. document.removeEventListener( 'keyup', onDocumentKeyUp );
  117. document.removeEventListener( 'mousedown', onDocumentMouseDown );
  118. document.removeEventListener( 'mouseup', onDocumentMouseUp );
  119. document.removeEventListener( 'mousemove', onDocumentMouseMove );
  120. document.removeEventListener( 'touchstart', onDocumentTouchStart );
  121. document.removeEventListener( 'touchend', onDocumentTouchEnd );
  122. document.removeEventListener( 'touchmove', onDocumentTouchMove );
  123. dispatch( events.stop, arguments );
  124. renderer.setAnimationLoop( null );
  125. };
  126. this.dispose = function () {
  127. renderer.dispose();
  128. camera = undefined;
  129. scene = undefined;
  130. };
  131. //
  132. function onDocumentKeyDown( event ) {
  133. dispatch( events.keydown, event );
  134. }
  135. function onDocumentKeyUp( event ) {
  136. dispatch( events.keyup, event );
  137. }
  138. function onDocumentMouseDown( event ) {
  139. dispatch( events.mousedown, event );
  140. }
  141. function onDocumentMouseUp( event ) {
  142. dispatch( events.mouseup, event );
  143. }
  144. function onDocumentMouseMove( event ) {
  145. dispatch( events.mousemove, event );
  146. }
  147. function onDocumentTouchStart( event ) {
  148. dispatch( events.touchstart, event );
  149. }
  150. function onDocumentTouchEnd( event ) {
  151. dispatch( events.touchend, event );
  152. }
  153. function onDocumentTouchMove( event ) {
  154. dispatch( events.touchmove, event );
  155. }
  156. }
  157. };
  158. export { APP };