Editor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. rendererChanged: new SIGNALS.Signal(),
  10. objectSelected: new SIGNALS.Signal(),
  11. objectAdded: new SIGNALS.Signal(),
  12. objectChanged: new SIGNALS.Signal(),
  13. objectRemoved: new SIGNALS.Signal(),
  14. helperAdded: new SIGNALS.Signal(),
  15. helperRemoved: new SIGNALS.Signal(),
  16. materialChanged: new SIGNALS.Signal(),
  17. clearColorChanged: new SIGNALS.Signal(),
  18. fogTypeChanged: new SIGNALS.Signal(),
  19. fogColorChanged: new SIGNALS.Signal(),
  20. fogParametersChanged: new SIGNALS.Signal(),
  21. windowResize: new SIGNALS.Signal()
  22. };
  23. this.scene = new THREE.Scene();
  24. this.sceneHelpers = new THREE.Scene();
  25. this.object = {};
  26. this.geometries = {};
  27. this.materials = {};
  28. this.textures = {};
  29. this.selected = null;
  30. this.helpers = {};
  31. };
  32. Editor.prototype = {
  33. setScene: function ( scene ) {
  34. this.scene.name = scene.name;
  35. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  36. while ( scene.children.length > 0 ) {
  37. this.addObject( scene.children[ 0 ] );
  38. }
  39. },
  40. //
  41. addObject: function ( object ) {
  42. this.scene.add( object );
  43. this.addHelper( object );
  44. this.signals.objectAdded.dispatch( object );
  45. },
  46. removeObject: function ( object ) {
  47. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  48. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  49. this.scene.remove( object );
  50. this.removeHelper( object );
  51. this.signals.objectRemoved.dispatch( object );
  52. },
  53. addGeometry: function ( geometry ) {
  54. },
  55. removeGeometry: function ( geometry ) {
  56. },
  57. addMaterial: function ( material ) {
  58. },
  59. removeMaterial: function ( material ) {
  60. },
  61. addTexture: function ( texture ) {
  62. },
  63. removeTexture: function ( texture ) {
  64. },
  65. //
  66. addHelper: function ( object ) {
  67. if ( object instanceof THREE.PointLight ) {
  68. var helper = new THREE.PointLightHelper( object, 10 );
  69. helper.lightSphere.id = object.id;
  70. this.sceneHelpers.add( helper );
  71. this.helpers[ object.id ] = helper;
  72. this.signals.helperAdded.dispatch( helper );
  73. } else if ( object instanceof THREE.DirectionalLight ) {
  74. var helper = new THREE.DirectionalLightHelper( object, 10 );
  75. helper.lightSphere.id = object.id;
  76. this.sceneHelpers.add( helper );
  77. this.helpers[ object.id ] = helper;
  78. this.signals.helperAdded.dispatch( helper );
  79. } else if ( object instanceof THREE.SpotLight ) {
  80. var helper = new THREE.SpotLightHelper( object, 10 );
  81. helper.lightSphere.id = object.id;
  82. this.sceneHelpers.add( helper );
  83. this.helpers[ object.id ] = helper;
  84. this.signals.helperAdded.dispatch( helper );
  85. } else if ( object instanceof THREE.HemisphereLight ) {
  86. var helper = new THREE.HemisphereLightHelper( object, 10 );
  87. helper.lightSphere.id = object.id;
  88. this.sceneHelpers.add( helper );
  89. this.helpers[ object.id ] = helper;
  90. this.signals.helperAdded.dispatch( helper );
  91. }
  92. },
  93. removeHelper: function ( object ) {
  94. if ( this.helpers[ object.id ] !== undefined ) {
  95. this.helpers[ object.id ].parent.remove( this.helpers[ object.id ] );
  96. delete this.helpers[ object.id ];
  97. this.signals.helperRemoved.dispatch( helper );
  98. }
  99. },
  100. //
  101. select: function ( object ) {
  102. this.selected = object;
  103. this.signals.objectSelected.dispatch( object );
  104. },
  105. selectById: function ( id ) {
  106. var scope = this;
  107. this.scene.traverse( function ( node ) {
  108. if ( node.id === id ) {
  109. scope.select( node );
  110. }
  111. } );
  112. },
  113. deselect: function () {
  114. this.select( null );
  115. }
  116. }