Editor.js 4.1 KB

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