Editor.js 8.2 KB

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