Editor.js 5.9 KB

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