Editor.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. var Editor = function () {
  2. var SIGNALS = signals;
  3. this.signals = {
  4. // actions
  5. playAnimations: new SIGNALS.Signal(),
  6. // notifications
  7. transformModeChanged: new SIGNALS.Signal(),
  8. snapChanged: new SIGNALS.Signal(),
  9. spaceChanged: new SIGNALS.Signal(),
  10. rendererChanged: new SIGNALS.Signal(),
  11. sceneGraphChanged: new SIGNALS.Signal(),
  12. objectSelected: new SIGNALS.Signal(),
  13. objectAdded: new SIGNALS.Signal(),
  14. objectChanged: new SIGNALS.Signal(),
  15. objectRemoved: new SIGNALS.Signal(),
  16. helperAdded: new SIGNALS.Signal(),
  17. helperRemoved: new SIGNALS.Signal(),
  18. materialChanged: new SIGNALS.Signal(),
  19. clearColorChanged: new SIGNALS.Signal(),
  20. fogTypeChanged: new SIGNALS.Signal(),
  21. fogColorChanged: new SIGNALS.Signal(),
  22. fogParametersChanged: new SIGNALS.Signal(),
  23. windowResize: new SIGNALS.Signal()
  24. };
  25. this.loader = new Loader( this );
  26. this.scene = new THREE.Scene();
  27. this.sceneHelpers = new THREE.Scene();
  28. this.object = {};
  29. this.geometries = {};
  30. this.materials = {};
  31. this.textures = {};
  32. this.selected = null;
  33. this.helpers = {};
  34. };
  35. Editor.prototype = {
  36. setScene: function ( scene ) {
  37. this.scene.name = scene.name;
  38. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  39. while ( scene.children.length > 0 ) {
  40. this.addObject( scene.children[ 0 ] );
  41. }
  42. },
  43. //
  44. addObject: function ( object ) {
  45. var scope = this;
  46. object.traverse( function ( child ) {
  47. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  48. if ( child.material !== undefined ) scope.addMaterial( child.material );
  49. scope.addHelper( child );
  50. } );
  51. this.scene.add( object );
  52. this.signals.objectAdded.dispatch( object );
  53. this.signals.sceneGraphChanged.dispatch();
  54. },
  55. setObjectName: function ( object, name ) {
  56. object.name = name;
  57. this.signals.sceneGraphChanged.dispatch();
  58. },
  59. removeObject: function ( object ) {
  60. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  61. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  62. var scope = this;
  63. object.traverse( function ( child ) {
  64. scope.removeHelper( child );
  65. } );
  66. object.parent.remove( object );
  67. this.signals.objectRemoved.dispatch( object );
  68. this.signals.sceneGraphChanged.dispatch();
  69. },
  70. addGeometry: function ( geometry ) {
  71. this.geometries[ geometry.uuid ] = geometry;
  72. },
  73. setGeometryName: function ( geometry, name ) {
  74. geometry.name = name;
  75. this.signals.sceneGraphChanged.dispatch();
  76. },
  77. addMaterial: function ( material ) {
  78. this.materials[ material.uuid ] = material;
  79. },
  80. setMaterialName: function ( material, name ) {
  81. material.name = name;
  82. this.signals.sceneGraphChanged.dispatch();
  83. },
  84. addTexture: function ( texture ) {
  85. this.textures[ texture.uuid ] = texture;
  86. },
  87. //
  88. addHelper: function () {
  89. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  90. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  91. return function ( object ) {
  92. if ( object instanceof THREE.Camera ) {
  93. var picker = new THREE.Mesh( geometry, material );
  94. picker.name = 'picker';
  95. picker.userData.object = object;
  96. picker.visible = false;
  97. var helper = new THREE.CameraHelper( object, 10 );
  98. helper.add( picker );
  99. this.sceneHelpers.add( helper );
  100. this.helpers[ object.id ] = helper;
  101. this.signals.helperAdded.dispatch( helper );
  102. } else if ( object instanceof THREE.PointLight ) {
  103. var picker = new THREE.Mesh( geometry, material );
  104. picker.name = 'picker';
  105. picker.userData.object = object;
  106. picker.visible = false;
  107. var helper = new THREE.PointLightHelper( object, 10 );
  108. helper.add( picker );
  109. this.sceneHelpers.add( helper );
  110. this.helpers[ object.id ] = helper;
  111. this.signals.helperAdded.dispatch( helper );
  112. } else if ( object instanceof THREE.DirectionalLight ) {
  113. var picker = new THREE.Mesh( geometry, material );
  114. picker.name = 'picker';
  115. picker.userData.object = object;
  116. picker.visible = false;
  117. var helper = new THREE.DirectionalLightHelper( object, 20 );
  118. helper.add( picker );
  119. this.sceneHelpers.add( helper );
  120. this.helpers[ object.id ] = helper;
  121. this.signals.helperAdded.dispatch( helper );
  122. } else if ( object instanceof THREE.SpotLight ) {
  123. var picker = new THREE.Mesh( geometry, material );
  124. picker.name = 'picker';
  125. picker.userData.object = object;
  126. picker.visible = false;
  127. var helper = new THREE.SpotLightHelper( object, 10 );
  128. helper.add( picker );
  129. this.sceneHelpers.add( helper );
  130. this.helpers[ object.id ] = helper;
  131. this.signals.helperAdded.dispatch( helper );
  132. } else if ( object instanceof THREE.HemisphereLight ) {
  133. var picker = new THREE.Mesh( geometry, material );
  134. picker.name = 'picker';
  135. picker.userData.object = object;
  136. picker.visible = false;
  137. var helper = new THREE.HemisphereLightHelper( object, 10 );
  138. helper.add( picker );
  139. this.sceneHelpers.add( helper );
  140. this.helpers[ object.id ] = helper;
  141. this.signals.helperAdded.dispatch( helper );
  142. }
  143. };
  144. }(),
  145. removeHelper: function ( object ) {
  146. if ( this.helpers[ object.id ] !== undefined ) {
  147. var helper = this.helpers[ object.id ];
  148. helper.parent.remove( helper );
  149. delete this.helpers[ object.id ];
  150. this.signals.helperRemoved.dispatch( helper );
  151. }
  152. },
  153. //
  154. parent: function ( object, parent ) {
  155. if ( parent === undefined ) {
  156. parent = this.scene;
  157. }
  158. parent.add( object );
  159. this.signals.sceneGraphChanged.dispatch();
  160. },
  161. //
  162. select: function ( object ) {
  163. this.selected = object;
  164. this.signals.objectSelected.dispatch( object );
  165. },
  166. selectById: function ( id ) {
  167. var object = null;
  168. this.scene.traverse( function ( child ) {
  169. if ( child.id === id ) {
  170. object = child;
  171. }
  172. } );
  173. this.select( object );
  174. },
  175. deselect: function () {
  176. this.select( null );
  177. },
  178. // utils
  179. getObjectType: function ( object ) {
  180. var types = {
  181. 'Scene': THREE.Scene,
  182. 'PerspectiveCamera': THREE.PerspectiveCamera,
  183. 'AmbientLight': THREE.AmbientLight,
  184. 'DirectionalLight': THREE.DirectionalLight,
  185. 'HemisphereLight': THREE.HemisphereLight,
  186. 'PointLight': THREE.PointLight,
  187. 'SpotLight': THREE.SpotLight,
  188. 'Mesh': THREE.Mesh,
  189. 'Object3D': THREE.Object3D
  190. };
  191. for ( var type in types ) {
  192. if ( object instanceof types[ type ] ) return type;
  193. }
  194. },
  195. getGeometryType: function ( geometry ) {
  196. var types = {
  197. 'CircleGeometry': THREE.CircleGeometry,
  198. 'CubeGeometry': THREE.CubeGeometry,
  199. 'CylinderGeometry': THREE.CylinderGeometry,
  200. 'ExtrudeGeometry': THREE.ExtrudeGeometry,
  201. 'IcosahedronGeometry': THREE.IcosahedronGeometry,
  202. 'LatheGeometry': THREE.LatheGeometry,
  203. 'OctahedronGeometry': THREE.OctahedronGeometry,
  204. 'ParametricGeometry': THREE.ParametricGeometry,
  205. 'PlaneGeometry': THREE.PlaneGeometry,
  206. 'PolyhedronGeometry': THREE.PolyhedronGeometry,
  207. 'ShapeGeometry': THREE.ShapeGeometry,
  208. 'SphereGeometry': THREE.SphereGeometry,
  209. 'TetrahedronGeometry': THREE.TetrahedronGeometry,
  210. 'TextGeometry': THREE.TextGeometry,
  211. 'TorusGeometry': THREE.TorusGeometry,
  212. 'TorusKnotGeometry': THREE.TorusKnotGeometry,
  213. 'TubeGeometry': THREE.TubeGeometry,
  214. 'Geometry': THREE.Geometry,
  215. 'BufferGeometry': THREE.BufferGeometry
  216. };
  217. for ( var type in types ) {
  218. if ( geometry instanceof types[ type ] ) return type;
  219. }
  220. },
  221. getMaterialType: function ( material ) {
  222. var types = {
  223. 'LineBasicMaterial': THREE.LineBasicMaterial,
  224. 'LineDashedMaterial': THREE.LineDashedMaterial,
  225. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  226. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  227. 'MeshFaceMaterial': THREE.MeshFaceMaterial,
  228. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  229. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  230. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  231. 'ParticleBasicMaterial': THREE.ParticleBasicMaterial,
  232. 'ParticleCanvasMaterial': THREE.ParticleCanvasMaterial,
  233. 'ShaderMaterial': THREE.ShaderMaterial,
  234. 'Material': THREE.Material
  235. };
  236. for ( var type in types ) {
  237. if ( material instanceof types[ type ] ) return type;
  238. }
  239. }
  240. }