Editor.js 7.4 KB

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