Editor.js 6.1 KB

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