Editor.js 6.3 KB

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