Editor.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /*
  62. showDialog: function ( value ) {
  63. this.signals.showDialog.dispatch( value );
  64. },
  65. */
  66. //
  67. setScene: function ( scene ) {
  68. this.scene.uuid = scene.uuid;
  69. this.scene.name = scene.name;
  70. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  71. // avoid render per object
  72. this.signals.sceneGraphChanged.active = false;
  73. while ( scene.children.length > 0 ) {
  74. this.addObject( scene.children[ 0 ] );
  75. }
  76. this.signals.sceneGraphChanged.active = true;
  77. this.signals.sceneGraphChanged.dispatch();
  78. },
  79. //
  80. addObject: function ( object ) {
  81. var scope = this;
  82. object.traverse( function ( child ) {
  83. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  84. if ( child.material !== undefined ) scope.addMaterial( child.material );
  85. scope.addHelper( child );
  86. } );
  87. this.scene.add( object );
  88. this.signals.objectAdded.dispatch( object );
  89. this.signals.sceneGraphChanged.dispatch();
  90. },
  91. setObjectName: function ( object, name ) {
  92. object.name = name;
  93. this.signals.sceneGraphChanged.dispatch();
  94. },
  95. removeObject: function ( object ) {
  96. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  97. var scope = this;
  98. object.traverse( function ( child ) {
  99. scope.removeHelper( child );
  100. } );
  101. object.parent.remove( object );
  102. this.signals.objectRemoved.dispatch( object );
  103. this.signals.sceneGraphChanged.dispatch();
  104. },
  105. addGeometry: function ( geometry ) {
  106. this.geometries[ geometry.uuid ] = geometry;
  107. },
  108. setGeometryName: function ( geometry, name ) {
  109. geometry.name = name;
  110. this.signals.sceneGraphChanged.dispatch();
  111. },
  112. addMaterial: function ( material ) {
  113. this.materials[ material.uuid ] = material;
  114. },
  115. setMaterialName: function ( material, name ) {
  116. material.name = name;
  117. this.signals.sceneGraphChanged.dispatch();
  118. },
  119. addTexture: function ( texture ) {
  120. this.textures[ texture.uuid ] = texture;
  121. },
  122. //
  123. addHelper: function () {
  124. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  125. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  126. return function ( object ) {
  127. var helper;
  128. if ( object instanceof THREE.Camera ) {
  129. helper = new THREE.CameraHelper( object, 10 );
  130. } else if ( object instanceof THREE.PointLight ) {
  131. helper = new THREE.PointLightHelper( object, 10 );
  132. } else if ( object instanceof THREE.DirectionalLight ) {
  133. helper = new THREE.DirectionalLightHelper( object, 20 );
  134. } else if ( object instanceof THREE.SpotLight ) {
  135. helper = new THREE.SpotLightHelper( object, 10 );
  136. } else if ( object instanceof THREE.HemisphereLight ) {
  137. helper = new THREE.HemisphereLightHelper( object, 10 );
  138. } else if ( object instanceof THREE.SkinnedMesh ) {
  139. helper = new THREE.SkeletonHelper( object );
  140. } else {
  141. // no helper for this object type
  142. return;
  143. }
  144. var picker = new THREE.Mesh( geometry, material );
  145. picker.name = 'picker';
  146. picker.userData.object = object;
  147. picker.visible = false;
  148. helper.add( picker );
  149. this.sceneHelpers.add( helper );
  150. this.helpers[ object.id ] = helper;
  151. this.signals.helperAdded.dispatch( helper );
  152. };
  153. }(),
  154. removeHelper: function ( object ) {
  155. if ( this.helpers[ object.id ] !== undefined ) {
  156. var helper = this.helpers[ object.id ];
  157. helper.parent.remove( helper );
  158. delete this.helpers[ object.id ];
  159. this.signals.helperRemoved.dispatch( helper );
  160. }
  161. },
  162. //
  163. parent: function ( object, parent ) {
  164. if ( parent === undefined ) {
  165. parent = this.scene;
  166. }
  167. parent.add( object );
  168. this.signals.sceneGraphChanged.dispatch();
  169. },
  170. //
  171. select: function ( object ) {
  172. if ( this.selected === object ) return;
  173. var uuid = null;
  174. if ( object !== null ) {
  175. uuid = object.uuid;
  176. }
  177. this.selected = object;
  178. this.config.setKey( 'selected', uuid );
  179. this.signals.objectSelected.dispatch( object );
  180. },
  181. selectById: function ( id ) {
  182. if ( id === this.camera.id ) {
  183. this.select( this.camera );
  184. return;
  185. }
  186. this.select( this.scene.getObjectById( id, true ) );
  187. },
  188. selectByUuid: function ( uuid ) {
  189. var scope = this;
  190. this.scene.traverse( function ( child ) {
  191. if ( child.uuid === uuid ) {
  192. scope.select( child );
  193. }
  194. } );
  195. },
  196. deselect: function () {
  197. this.select( null );
  198. },
  199. focus: function ( object ) {
  200. this.signals.objectFocused.dispatch( object );
  201. },
  202. focusById: function ( id ) {
  203. this.focus( this.scene.getObjectById( id, true ) );
  204. },
  205. clear: function () {
  206. this.camera.position.set( 500, 250, 500 );
  207. this.camera.lookAt( new THREE.Vector3() );
  208. var objects = this.scene.children;
  209. while ( objects.length > 0 ) {
  210. this.removeObject( objects[ 0 ] );
  211. }
  212. this.geometries = {};
  213. this.materials = {};
  214. this.textures = {};
  215. this.scripts = {};
  216. this.deselect();
  217. },
  218. //
  219. fromJSON: function ( json ) {
  220. var loader = new THREE.ObjectLoader();
  221. // backwards
  222. if ( json.scene === undefined ) {
  223. var scene = loader.parse( json );
  224. this.setScene( scene );
  225. return;
  226. }
  227. // TODO: Clean this up somehow
  228. var camera = loader.parse( json.camera );
  229. this.camera.position.copy( camera.position );
  230. this.camera.rotation.copy( camera.rotation );
  231. this.camera.aspect = camera.aspect;
  232. this.camera.near = camera.near;
  233. this.camera.far = camera.far;
  234. this.setScene( loader.parse( json.scene ) );
  235. this.scripts = json.scripts;
  236. },
  237. toJSON: function () {
  238. return {
  239. camera: this.camera.toJSON(),
  240. scene: this.scene.toJSON(),
  241. scripts: this.scripts
  242. };
  243. }
  244. }
粤ICP备19079148号