Editor.js 8.2 KB

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