Editor.js 7.0 KB

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