Editor.js 3.5 KB

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