Editor.js 9.8 KB

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