Editor.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. var Editor = function () {
  2. var SIGNALS = signals;
  3. this.signals = {
  4. // player
  5. startPlayer: new SIGNALS.Signal(),
  6. stopPlayer: new SIGNALS.Signal(),
  7. // actions
  8. playAnimation: new SIGNALS.Signal(),
  9. stopAnimation: new SIGNALS.Signal(),
  10. showDialog: new SIGNALS.Signal(),
  11. // notifications
  12. themeChanged: new SIGNALS.Signal(),
  13. transformModeChanged: new SIGNALS.Signal(),
  14. snapChanged: new SIGNALS.Signal(),
  15. spaceChanged: new SIGNALS.Signal(),
  16. rendererChanged: new SIGNALS.Signal(),
  17. sceneGraphChanged: new SIGNALS.Signal(),
  18. cameraChanged: new SIGNALS.Signal(),
  19. geometryChanged: new SIGNALS.Signal(),
  20. objectSelected: new SIGNALS.Signal(),
  21. objectAdded: new SIGNALS.Signal(),
  22. objectChanged: new SIGNALS.Signal(),
  23. objectRemoved: new SIGNALS.Signal(),
  24. helperAdded: new SIGNALS.Signal(),
  25. helperRemoved: new SIGNALS.Signal(),
  26. materialChanged: new SIGNALS.Signal(),
  27. fogTypeChanged: new SIGNALS.Signal(),
  28. fogColorChanged: new SIGNALS.Signal(),
  29. fogParametersChanged: new SIGNALS.Signal(),
  30. windowResize: new SIGNALS.Signal(),
  31. showGridChanged: new SIGNALS.Signal()
  32. };
  33. this.config = new Config();
  34. this.storage = new Storage();
  35. this.loader = new Loader( this );
  36. this.camera = new THREE.PerspectiveCamera( 50, 1, 1, 5000 );
  37. this.scene = new THREE.Scene();
  38. this.scene.name = 'Scene';
  39. this.sceneHelpers = new THREE.Scene();
  40. this.object = {};
  41. this.geometries = {};
  42. this.materials = {};
  43. this.textures = {};
  44. this.scripts = {};
  45. this.selected = null;
  46. this.helpers = {};
  47. };
  48. Editor.prototype = {
  49. setTheme: function ( value ) {
  50. document.getElementById( 'theme' ).href = value;
  51. this.signals.themeChanged.dispatch( value );
  52. },
  53. showDialog: function ( value ) {
  54. this.signals.showDialog.dispatch( value );
  55. },
  56. //
  57. setScene: function ( scene ) {
  58. this.scene.name = scene.name;
  59. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  60. // avoid render per object
  61. this.signals.sceneGraphChanged.active = false;
  62. while ( scene.children.length > 0 ) {
  63. this.addObject( scene.children[ 0 ] );
  64. }
  65. this.signals.sceneGraphChanged.active = true;
  66. this.signals.sceneGraphChanged.dispatch();
  67. },
  68. //
  69. addObject: function ( object ) {
  70. var scope = this;
  71. object.traverse( function ( child ) {
  72. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  73. if ( child.material !== undefined ) scope.addMaterial( child.material );
  74. scope.addHelper( child );
  75. } );
  76. this.scene.add( object );
  77. this.signals.objectAdded.dispatch( object );
  78. this.signals.sceneGraphChanged.dispatch();
  79. },
  80. setObjectName: function ( object, name ) {
  81. object.name = name;
  82. this.signals.sceneGraphChanged.dispatch();
  83. },
  84. removeObject: function ( object ) {
  85. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  86. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  87. var scope = this;
  88. object.traverse( function ( child ) {
  89. scope.removeHelper( child );
  90. } );
  91. object.parent.remove( object );
  92. this.signals.objectRemoved.dispatch( object );
  93. this.signals.sceneGraphChanged.dispatch();
  94. },
  95. addGeometry: function ( geometry ) {
  96. this.geometries[ geometry.uuid ] = geometry;
  97. },
  98. setGeometryName: function ( geometry, name ) {
  99. geometry.name = name;
  100. this.signals.sceneGraphChanged.dispatch();
  101. },
  102. addMaterial: function ( material ) {
  103. this.materials[ material.uuid ] = material;
  104. },
  105. setMaterialName: function ( material, name ) {
  106. material.name = name;
  107. this.signals.sceneGraphChanged.dispatch();
  108. },
  109. addTexture: function ( texture ) {
  110. this.textures[ texture.uuid ] = texture;
  111. },
  112. //
  113. addHelper: function () {
  114. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  115. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  116. return function ( object ) {
  117. var helper;
  118. if ( object instanceof THREE.Camera ) {
  119. helper = new THREE.CameraHelper( object, 10 );
  120. } else if ( object instanceof THREE.PointLight ) {
  121. helper = new THREE.PointLightHelper( object, 10 );
  122. } else if ( object instanceof THREE.DirectionalLight ) {
  123. helper = new THREE.DirectionalLightHelper( object, 20 );
  124. } else if ( object instanceof THREE.SpotLight ) {
  125. helper = new THREE.SpotLightHelper( object, 10 );
  126. } else if ( object instanceof THREE.HemisphereLight ) {
  127. helper = new THREE.HemisphereLightHelper( object, 10 );
  128. } else if ( object instanceof THREE.SkinnedMesh ) {
  129. helper = new THREE.SkeletonHelper( object );
  130. } else {
  131. // no helper for this object type
  132. return;
  133. }
  134. var picker = new THREE.Mesh( geometry, material );
  135. picker.name = 'picker';
  136. picker.userData.object = object;
  137. picker.visible = false;
  138. helper.add( picker );
  139. this.sceneHelpers.add( helper );
  140. this.helpers[ object.id ] = helper;
  141. this.signals.helperAdded.dispatch( helper );
  142. };
  143. }(),
  144. removeHelper: function ( object ) {
  145. if ( this.helpers[ object.id ] !== undefined ) {
  146. var helper = this.helpers[ object.id ];
  147. helper.parent.remove( helper );
  148. delete this.helpers[ object.id ];
  149. this.signals.helperRemoved.dispatch( helper );
  150. }
  151. },
  152. //
  153. parent: function ( object, parent ) {
  154. if ( parent === undefined ) {
  155. parent = this.scene;
  156. }
  157. parent.add( object );
  158. this.signals.sceneGraphChanged.dispatch();
  159. },
  160. //
  161. select: function ( object ) {
  162. this.selected = object;
  163. if ( object !== null ) {
  164. this.config.setKey( 'selected', object.uuid );
  165. } else {
  166. this.config.setKey( 'selected', null );
  167. }
  168. this.signals.objectSelected.dispatch( object );
  169. },
  170. selectById: function ( id ) {
  171. var scope = this;
  172. this.scene.traverse( function ( child ) {
  173. if ( child.id === id ) {
  174. scope.select( child );
  175. }
  176. } );
  177. },
  178. selectByUuid: function ( uuid ) {
  179. var scope = this;
  180. this.scene.traverse( function ( child ) {
  181. if ( child.uuid === uuid ) {
  182. scope.select( child );
  183. }
  184. } );
  185. },
  186. deselect: function () {
  187. this.select( null );
  188. }
  189. }