Editor.js 6.1 KB

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