Editor.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Editor = function () {
  5. this.DEFAULT_CAMERA = new THREE.PerspectiveCamera( 50, 1, 0.01, 1000 );
  6. this.DEFAULT_CAMERA.name = 'Camera';
  7. this.DEFAULT_CAMERA.position.set( 0, 5, 10 );
  8. this.DEFAULT_CAMERA.lookAt( new THREE.Vector3() );
  9. var Signal = signals.Signal;
  10. this.signals = {
  11. // script
  12. editScript: new Signal(),
  13. // player
  14. startPlayer: new Signal(),
  15. stopPlayer: new Signal(),
  16. // notifications
  17. editorCleared: new Signal(),
  18. savingStarted: new Signal(),
  19. savingFinished: new Signal(),
  20. themeChanged: new Signal(),
  21. transformModeChanged: new Signal(),
  22. snapChanged: new Signal(),
  23. spaceChanged: new Signal(),
  24. rendererChanged: new Signal(),
  25. sceneBackgroundChanged: new Signal(),
  26. sceneFogChanged: new Signal(),
  27. sceneGraphChanged: new Signal(),
  28. cameraChanged: new Signal(),
  29. geometryChanged: new Signal(),
  30. objectSelected: new Signal(),
  31. objectFocused: new Signal(),
  32. objectAdded: new Signal(),
  33. objectChanged: new Signal(),
  34. objectRemoved: new Signal(),
  35. helperAdded: new Signal(),
  36. helperRemoved: new Signal(),
  37. materialChanged: new Signal(),
  38. scriptAdded: new Signal(),
  39. scriptChanged: new Signal(),
  40. scriptRemoved: new Signal(),
  41. windowResize: new Signal(),
  42. showGridChanged: new Signal(),
  43. refreshSidebarObject3D: new Signal(),
  44. historyChanged: new Signal()
  45. };
  46. this.config = new Config();
  47. this.history = new History( this );
  48. this.storage = new Storage();
  49. this.strings = new Strings( this.config );
  50. this.loader = new Loader( this );
  51. this.camera = this.DEFAULT_CAMERA.clone();
  52. this.scene = new THREE.Scene();
  53. this.scene.name = 'Scene';
  54. this.scene.background = new THREE.Color( 0xaaaaaa );
  55. this.sceneHelpers = new THREE.Scene();
  56. this.object = {};
  57. this.geometries = {};
  58. this.materials = {};
  59. this.textures = {};
  60. this.scripts = {};
  61. this.animations = {};
  62. this.mixer = new THREE.AnimationMixer( this.scene );
  63. this.selected = null;
  64. this.helpers = {};
  65. };
  66. Editor.prototype = {
  67. setTheme: function ( value ) {
  68. document.getElementById( 'theme' ).href = value;
  69. this.signals.themeChanged.dispatch( value );
  70. },
  71. //
  72. setScene: function ( scene ) {
  73. this.scene.uuid = scene.uuid;
  74. this.scene.name = scene.name;
  75. if ( scene.background !== null ) this.scene.background = scene.background.clone();
  76. if ( scene.fog !== null ) this.scene.fog = scene.fog.clone();
  77. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  78. // avoid render per object
  79. this.signals.sceneGraphChanged.active = false;
  80. while ( scene.children.length > 0 ) {
  81. this.addObject( scene.children[ 0 ] );
  82. }
  83. this.signals.sceneGraphChanged.active = true;
  84. this.signals.sceneGraphChanged.dispatch();
  85. },
  86. //
  87. addObject: function ( object ) {
  88. var scope = this;
  89. object.traverse( function ( child ) {
  90. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  91. if ( child.material !== undefined ) scope.addMaterial( child.material );
  92. scope.addHelper( child );
  93. } );
  94. this.scene.add( object );
  95. this.signals.objectAdded.dispatch( object );
  96. this.signals.sceneGraphChanged.dispatch();
  97. },
  98. moveObject: function ( object, parent, before ) {
  99. if ( parent === undefined ) {
  100. parent = this.scene;
  101. }
  102. parent.add( object );
  103. // sort children array
  104. if ( before !== undefined ) {
  105. var index = parent.children.indexOf( before );
  106. parent.children.splice( index, 0, object );
  107. parent.children.pop();
  108. }
  109. this.signals.sceneGraphChanged.dispatch();
  110. },
  111. nameObject: function ( object, name ) {
  112. object.name = name;
  113. this.signals.sceneGraphChanged.dispatch();
  114. },
  115. removeObject: function ( object ) {
  116. if ( object.parent === null ) return; // avoid deleting the camera or scene
  117. var scope = this;
  118. object.traverse( function ( child ) {
  119. scope.removeHelper( child );
  120. } );
  121. object.parent.remove( object );
  122. this.signals.objectRemoved.dispatch( object );
  123. this.signals.sceneGraphChanged.dispatch();
  124. },
  125. addGeometry: function ( geometry ) {
  126. this.geometries[ geometry.uuid ] = geometry;
  127. },
  128. setGeometryName: function ( geometry, name ) {
  129. geometry.name = name;
  130. this.signals.sceneGraphChanged.dispatch();
  131. },
  132. addMaterial: function ( material ) {
  133. this.materials[ material.uuid ] = material;
  134. },
  135. setMaterialName: function ( material, name ) {
  136. material.name = name;
  137. this.signals.sceneGraphChanged.dispatch();
  138. },
  139. addTexture: function ( texture ) {
  140. this.textures[ texture.uuid ] = texture;
  141. },
  142. addAnimation: function ( object, animations ) {
  143. if ( animations.length > 0 ) {
  144. this.animations[ object.uuid ] = animations;
  145. }
  146. },
  147. //
  148. addHelper: function () {
  149. var geometry = new THREE.SphereBufferGeometry( 2, 4, 2 );
  150. var material = new THREE.MeshBasicMaterial( { color: 0xff0000, visible: false } );
  151. return function ( object ) {
  152. var helper;
  153. if ( object.isCamera ) {
  154. helper = new THREE.CameraHelper( object, 1 );
  155. } else if ( object.isPointLight ) {
  156. helper = new THREE.PointLightHelper( object, 1 );
  157. } else if ( object.isDirectionalLight ) {
  158. helper = new THREE.DirectionalLightHelper( object, 1 );
  159. } else if ( object.isSpotLight ) {
  160. helper = new THREE.SpotLightHelper( object, 1 );
  161. } else if ( object.isHemisphereLight ) {
  162. helper = new THREE.HemisphereLightHelper( object, 1 );
  163. } else if ( object.isSkinnedMesh ) {
  164. helper = new THREE.SkeletonHelper( object.skeleton.bones[ 0 ] );
  165. } else {
  166. // no helper for this object type
  167. return;
  168. }
  169. var picker = new THREE.Mesh( geometry, material );
  170. picker.name = 'picker';
  171. picker.userData.object = object;
  172. helper.add( picker );
  173. this.sceneHelpers.add( helper );
  174. this.helpers[ object.id ] = helper;
  175. this.signals.helperAdded.dispatch( helper );
  176. };
  177. }(),
  178. removeHelper: function ( object ) {
  179. if ( this.helpers[ object.id ] !== undefined ) {
  180. var helper = this.helpers[ object.id ];
  181. helper.parent.remove( helper );
  182. delete this.helpers[ object.id ];
  183. this.signals.helperRemoved.dispatch( helper );
  184. }
  185. },
  186. //
  187. addScript: function ( object, script ) {
  188. if ( this.scripts[ object.uuid ] === undefined ) {
  189. this.scripts[ object.uuid ] = [];
  190. }
  191. this.scripts[ object.uuid ].push( script );
  192. this.signals.scriptAdded.dispatch( script );
  193. },
  194. removeScript: function ( object, script ) {
  195. if ( this.scripts[ object.uuid ] === undefined ) return;
  196. var index = this.scripts[ object.uuid ].indexOf( script );
  197. if ( index !== - 1 ) {
  198. this.scripts[ object.uuid ].splice( index, 1 );
  199. }
  200. this.signals.scriptRemoved.dispatch( script );
  201. },
  202. getObjectMaterial: function ( object, slot ) {
  203. var material = object.material;
  204. if ( Array.isArray( material ) ) {
  205. material = material[ slot ];
  206. }
  207. return material;
  208. },
  209. setObjectMaterial: function ( object, slot, newMaterial ) {
  210. if ( Array.isArray( object.material ) ) {
  211. object.material[ slot ] = newMaterial;
  212. } else {
  213. object.material = newMaterial;
  214. }
  215. },
  216. //
  217. select: function ( object ) {
  218. if ( this.selected === object ) return;
  219. var uuid = null;
  220. if ( object !== null ) {
  221. uuid = object.uuid;
  222. }
  223. this.selected = object;
  224. this.config.setKey( 'selected', uuid );
  225. this.signals.objectSelected.dispatch( object );
  226. },
  227. selectById: function ( id ) {
  228. if ( id === this.camera.id ) {
  229. this.select( this.camera );
  230. return;
  231. }
  232. this.select( this.scene.getObjectById( id, true ) );
  233. },
  234. selectByUuid: function ( uuid ) {
  235. var scope = this;
  236. this.scene.traverse( function ( child ) {
  237. if ( child.uuid === uuid ) {
  238. scope.select( child );
  239. }
  240. } );
  241. },
  242. deselect: function () {
  243. this.select( null );
  244. },
  245. focus: function ( object ) {
  246. this.signals.objectFocused.dispatch( object );
  247. },
  248. focusById: function ( id ) {
  249. this.focus( this.scene.getObjectById( id, true ) );
  250. },
  251. clear: function () {
  252. this.history.clear();
  253. this.storage.clear();
  254. this.camera.copy( this.DEFAULT_CAMERA );
  255. this.scene.background.setHex( 0xaaaaaa );
  256. this.scene.fog = null;
  257. var objects = this.scene.children;
  258. while ( objects.length > 0 ) {
  259. this.removeObject( objects[ 0 ] );
  260. }
  261. this.geometries = {};
  262. this.materials = {};
  263. this.textures = {};
  264. this.scripts = {};
  265. this.animations = {};
  266. this.mixer.stopAllAction();
  267. this.deselect();
  268. this.signals.editorCleared.dispatch();
  269. },
  270. //
  271. fromJSON: function ( json ) {
  272. var loader = new THREE.ObjectLoader();
  273. // backwards
  274. if ( json.scene === undefined ) {
  275. this.setScene( loader.parse( json ) );
  276. return;
  277. }
  278. var camera = loader.parse( json.camera );
  279. this.camera.copy( camera );
  280. this.camera.aspect = this.DEFAULT_CAMERA.aspect;
  281. this.camera.updateProjectionMatrix();
  282. this.history.fromJSON( json.history );
  283. this.scripts = json.scripts;
  284. this.setScene( loader.parse( json.scene ) );
  285. },
  286. toJSON: function () {
  287. // scripts clean up
  288. var scene = this.scene;
  289. var scripts = this.scripts;
  290. for ( var key in scripts ) {
  291. var script = scripts[ key ];
  292. if ( script.length === 0 || scene.getObjectByProperty( 'uuid', key ) === undefined ) {
  293. delete scripts[ key ];
  294. }
  295. }
  296. //
  297. return {
  298. metadata: {},
  299. project: {
  300. shadows: this.config.getKey( 'project/renderer/shadows' ),
  301. vr: this.config.getKey( 'project/vr' )
  302. },
  303. camera: this.camera.toJSON(),
  304. scene: this.scene.toJSON(),
  305. scripts: this.scripts,
  306. history: this.history.toJSON()
  307. };
  308. },
  309. objectByUuid: function ( uuid ) {
  310. return this.scene.getObjectByProperty( 'uuid', uuid, true );
  311. },
  312. execute: function ( cmd, optionalName ) {
  313. this.history.execute( cmd, optionalName );
  314. },
  315. undo: function () {
  316. this.history.undo();
  317. },
  318. redo: function () {
  319. this.history.redo();
  320. }
  321. };