Editor.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.scene = new THREE.Scene();
  44. this.scene.name = 'Scene';
  45. this.sceneHelpers = new THREE.Scene();
  46. this.object = {};
  47. this.geometries = {};
  48. this.materials = {};
  49. this.textures = {};
  50. this.scripts = {};
  51. this.selected = null;
  52. this.helpers = {};
  53. };
  54. Editor.prototype = {
  55. setTheme: function ( value ) {
  56. document.getElementById( 'theme' ).href = value;
  57. this.signals.themeChanged.dispatch( value );
  58. },
  59. showDialog: function ( value ) {
  60. this.signals.showDialog.dispatch( value );
  61. },
  62. //
  63. setScene: function ( scene ) {
  64. this.scene.name = scene.name;
  65. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  66. // avoid render per object
  67. this.signals.sceneGraphChanged.active = false;
  68. while ( scene.children.length > 0 ) {
  69. this.addObject( scene.children[ 0 ] );
  70. }
  71. this.signals.sceneGraphChanged.active = true;
  72. this.signals.sceneGraphChanged.dispatch();
  73. },
  74. //
  75. addObject: function ( object ) {
  76. var scope = this;
  77. object.traverse( function ( child ) {
  78. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  79. if ( child.material !== undefined ) scope.addMaterial( child.material );
  80. scope.addHelper( child );
  81. } );
  82. this.scene.add( object );
  83. this.signals.objectAdded.dispatch( object );
  84. this.signals.sceneGraphChanged.dispatch();
  85. },
  86. setObjectName: function ( object, name ) {
  87. object.name = name;
  88. this.signals.sceneGraphChanged.dispatch();
  89. },
  90. removeObject: function ( object ) {
  91. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  92. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  93. var scope = this;
  94. object.traverse( function ( child ) {
  95. scope.removeHelper( child );
  96. } );
  97. object.parent.remove( object );
  98. this.signals.objectRemoved.dispatch( object );
  99. this.signals.sceneGraphChanged.dispatch();
  100. },
  101. addGeometry: function ( geometry ) {
  102. this.geometries[ geometry.uuid ] = geometry;
  103. },
  104. setGeometryName: function ( geometry, name ) {
  105. geometry.name = name;
  106. this.signals.sceneGraphChanged.dispatch();
  107. },
  108. addMaterial: function ( material ) {
  109. this.materials[ material.uuid ] = material;
  110. },
  111. setMaterialName: function ( material, name ) {
  112. material.name = name;
  113. this.signals.sceneGraphChanged.dispatch();
  114. },
  115. addTexture: function ( texture ) {
  116. this.textures[ texture.uuid ] = texture;
  117. },
  118. //
  119. addHelper: function () {
  120. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  121. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  122. return function ( object ) {
  123. var helper;
  124. if ( object instanceof THREE.Camera ) {
  125. helper = new THREE.CameraHelper( object, 10 );
  126. } else if ( object instanceof THREE.PointLight ) {
  127. helper = new THREE.PointLightHelper( object, 10 );
  128. } else if ( object instanceof THREE.DirectionalLight ) {
  129. helper = new THREE.DirectionalLightHelper( object, 20 );
  130. } else if ( object instanceof THREE.SpotLight ) {
  131. helper = new THREE.SpotLightHelper( object, 10 );
  132. } else if ( object instanceof THREE.HemisphereLight ) {
  133. helper = new THREE.HemisphereLightHelper( object, 10 );
  134. } else if ( object instanceof THREE.SkinnedMesh ) {
  135. helper = new THREE.SkeletonHelper( object );
  136. } else {
  137. // no helper for this object type
  138. return;
  139. }
  140. var picker = new THREE.Mesh( geometry, material );
  141. picker.name = 'picker';
  142. picker.userData.object = object;
  143. picker.visible = false;
  144. helper.add( picker );
  145. this.sceneHelpers.add( helper );
  146. this.helpers[ object.id ] = helper;
  147. this.signals.helperAdded.dispatch( helper );
  148. };
  149. }(),
  150. removeHelper: function ( object ) {
  151. if ( this.helpers[ object.id ] !== undefined ) {
  152. var helper = this.helpers[ object.id ];
  153. helper.parent.remove( helper );
  154. delete this.helpers[ object.id ];
  155. this.signals.helperRemoved.dispatch( helper );
  156. }
  157. },
  158. //
  159. parent: function ( object, parent ) {
  160. if ( parent === undefined ) {
  161. parent = this.scene;
  162. }
  163. parent.add( object );
  164. this.signals.sceneGraphChanged.dispatch();
  165. },
  166. //
  167. select: function ( object ) {
  168. if ( this.selected === object ) return;
  169. var uuid = null;
  170. if ( object !== null ) {
  171. uuid = object.uuid;
  172. }
  173. this.selected = object;
  174. this.config.setKey( 'selected', uuid );
  175. this.signals.objectSelected.dispatch( object );
  176. },
  177. selectById: function ( id ) {
  178. this.select( this.scene.getObjectById( id, true ) );
  179. },
  180. selectByUuid: function ( uuid ) {
  181. var scope = this;
  182. this.scene.traverse( function ( child ) {
  183. if ( child.uuid === uuid ) {
  184. scope.select( child );
  185. }
  186. } );
  187. },
  188. deselect: function () {
  189. this.select( null );
  190. },
  191. focus: function ( object ) {
  192. this.signals.objectFocused.dispatch( object );
  193. },
  194. focusById: function ( id ) {
  195. this.focus( this.scene.getObjectById( id, true ) );
  196. }
  197. }