Editor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. removeObject: function ( object ) {
  56. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  57. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  58. var scope = this;
  59. object.traverse( function ( child ) {
  60. scope.removeHelper( child );
  61. } );
  62. object.parent.remove( object );
  63. this.signals.objectRemoved.dispatch( object );
  64. this.signals.sceneGraphChanged.dispatch();
  65. },
  66. addGeometry: function ( geometry ) {
  67. this.geometries[ geometry.uuid ] = geometry;
  68. },
  69. addMaterial: function ( material ) {
  70. this.materials[ material.uuid ] = material;
  71. },
  72. addTexture: function ( texture ) {
  73. this.textures[ texture.uuid ] = texture;
  74. },
  75. //
  76. addHelper: function () {
  77. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  78. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  79. return function ( object ) {
  80. if ( object instanceof THREE.Camera ) {
  81. var picker = new THREE.Mesh( geometry, material );
  82. picker.name = 'picker';
  83. picker.userData.object = object;
  84. picker.visible = false;
  85. var helper = new THREE.CameraHelper( object, 10 );
  86. helper.add( picker );
  87. this.sceneHelpers.add( helper );
  88. this.helpers[ object.id ] = helper;
  89. this.signals.helperAdded.dispatch( helper );
  90. } else if ( object instanceof THREE.PointLight ) {
  91. var picker = new THREE.Mesh( geometry, material );
  92. picker.name = 'picker';
  93. picker.userData.object = object;
  94. picker.visible = false;
  95. var helper = new THREE.PointLightHelper( object, 10 );
  96. helper.add( picker );
  97. this.sceneHelpers.add( helper );
  98. this.helpers[ object.id ] = helper;
  99. this.signals.helperAdded.dispatch( helper );
  100. } else if ( object instanceof THREE.DirectionalLight ) {
  101. var picker = new THREE.Mesh( geometry, material );
  102. picker.name = 'picker';
  103. picker.userData.object = object;
  104. picker.visible = false;
  105. var helper = new THREE.DirectionalLightHelper( object, 20 );
  106. helper.add( picker );
  107. this.sceneHelpers.add( helper );
  108. this.helpers[ object.id ] = helper;
  109. this.signals.helperAdded.dispatch( helper );
  110. } else if ( object instanceof THREE.SpotLight ) {
  111. var picker = new THREE.Mesh( geometry, material );
  112. picker.name = 'picker';
  113. picker.userData.object = object;
  114. picker.visible = false;
  115. var helper = new THREE.SpotLightHelper( object, 10 );
  116. helper.add( picker );
  117. this.sceneHelpers.add( helper );
  118. this.helpers[ object.id ] = helper;
  119. this.signals.helperAdded.dispatch( helper );
  120. } else if ( object instanceof THREE.HemisphereLight ) {
  121. var picker = new THREE.Mesh( geometry, material );
  122. picker.name = 'picker';
  123. picker.userData.object = object;
  124. picker.visible = false;
  125. var helper = new THREE.HemisphereLightHelper( object, 10 );
  126. helper.add( picker );
  127. this.sceneHelpers.add( helper );
  128. this.helpers[ object.id ] = helper;
  129. this.signals.helperAdded.dispatch( helper );
  130. }
  131. };
  132. }(),
  133. removeHelper: function ( object ) {
  134. if ( this.helpers[ object.id ] !== undefined ) {
  135. var helper = this.helpers[ object.id ];
  136. helper.parent.remove( helper );
  137. delete this.helpers[ object.id ];
  138. this.signals.helperRemoved.dispatch( helper );
  139. }
  140. },
  141. //
  142. parent: function ( object, parent ) {
  143. if ( parent === undefined ) {
  144. parent = this.scene;
  145. }
  146. parent.add( object );
  147. this.signals.sceneGraphChanged.dispatch();
  148. },
  149. //
  150. select: function ( object ) {
  151. this.selected = object;
  152. this.signals.objectSelected.dispatch( object );
  153. },
  154. selectById: function ( id ) {
  155. var object = null;
  156. this.scene.traverse( function ( child ) {
  157. if ( child.id === id ) {
  158. object = child;
  159. }
  160. } );
  161. this.select( object );
  162. },
  163. deselect: function () {
  164. this.select( null );
  165. },
  166. // utils
  167. getObjectType: function ( object ) {
  168. var types = {
  169. 'Scene': THREE.Scene,
  170. 'PerspectiveCamera': THREE.PerspectiveCamera,
  171. 'AmbientLight': THREE.AmbientLight,
  172. 'DirectionalLight': THREE.DirectionalLight,
  173. 'HemisphereLight': THREE.HemisphereLight,
  174. 'PointLight': THREE.PointLight,
  175. 'SpotLight': THREE.SpotLight,
  176. 'Mesh': THREE.Mesh,
  177. 'Object3D': THREE.Object3D
  178. };
  179. for ( var type in types ) {
  180. if ( object instanceof types[ type ] ) return type;
  181. }
  182. },
  183. getGeometryType: function ( geometry ) {
  184. var types = {
  185. 'CircleGeometry': THREE.CircleGeometry,
  186. 'CubeGeometry': THREE.CubeGeometry,
  187. 'CylinderGeometry': THREE.CylinderGeometry,
  188. 'ExtrudeGeometry': THREE.ExtrudeGeometry,
  189. 'IcosahedronGeometry': THREE.IcosahedronGeometry,
  190. 'LatheGeometry': THREE.LatheGeometry,
  191. 'OctahedronGeometry': THREE.OctahedronGeometry,
  192. 'ParametricGeometry': THREE.ParametricGeometry,
  193. 'PlaneGeometry': THREE.PlaneGeometry,
  194. 'PolyhedronGeometry': THREE.PolyhedronGeometry,
  195. 'ShapeGeometry': THREE.ShapeGeometry,
  196. 'SphereGeometry': THREE.SphereGeometry,
  197. 'TetrahedronGeometry': THREE.TetrahedronGeometry,
  198. 'TextGeometry': THREE.TextGeometry,
  199. 'TorusGeometry': THREE.TorusGeometry,
  200. 'TorusKnotGeometry': THREE.TorusKnotGeometry,
  201. 'TubeGeometry': THREE.TubeGeometry,
  202. 'Geometry': THREE.Geometry,
  203. 'BufferGeometry': THREE.BufferGeometry
  204. };
  205. for ( var type in types ) {
  206. if ( geometry instanceof types[ type ] ) return type;
  207. }
  208. },
  209. getMaterialType: function ( material ) {
  210. var types = {
  211. 'LineBasicMaterial': THREE.LineBasicMaterial,
  212. 'LineDashedMaterial': THREE.LineDashedMaterial,
  213. 'MeshBasicMaterial': THREE.MeshBasicMaterial,
  214. 'MeshDepthMaterial': THREE.MeshDepthMaterial,
  215. 'MeshFaceMaterial': THREE.MeshFaceMaterial,
  216. 'MeshLambertMaterial': THREE.MeshLambertMaterial,
  217. 'MeshNormalMaterial': THREE.MeshNormalMaterial,
  218. 'MeshPhongMaterial': THREE.MeshPhongMaterial,
  219. 'ParticleBasicMaterial': THREE.ParticleBasicMaterial,
  220. 'ParticleCanvasMaterial': THREE.ParticleCanvasMaterial,
  221. 'ShaderMaterial': THREE.ShaderMaterial,
  222. 'Material': THREE.Material
  223. };
  224. for ( var type in types ) {
  225. if ( material instanceof types[ type ] ) return type;
  226. }
  227. }
  228. }