Editor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. this.scene.add( object );
  44. this.addHelper( object );
  45. this.signals.objectAdded.dispatch( object );
  46. this.signals.sceneGraphChanged.dispatch();
  47. },
  48. removeObject: function ( object ) {
  49. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  50. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  51. object.parent.remove( object );
  52. this.removeHelper( object );
  53. this.signals.objectRemoved.dispatch( object );
  54. this.signals.sceneGraphChanged.dispatch();
  55. },
  56. addGeometry: function ( geometry ) {
  57. },
  58. removeGeometry: function ( geometry ) {
  59. },
  60. addMaterial: function ( material ) {
  61. },
  62. removeMaterial: function ( material ) {
  63. },
  64. addTexture: function ( texture ) {
  65. },
  66. removeTexture: function ( texture ) {
  67. },
  68. //
  69. addHelper: function ( object ) {
  70. if ( object instanceof THREE.PointLight ) {
  71. var helper = new THREE.PointLightHelper( object, 10 );
  72. helper.lightSphere.id = object.id;
  73. this.sceneHelpers.add( helper );
  74. this.helpers[ object.id ] = helper;
  75. this.signals.helperAdded.dispatch( helper );
  76. } else if ( object instanceof THREE.DirectionalLight ) {
  77. var helper = new THREE.DirectionalLightHelper( object, 10 );
  78. helper.lightSphere.id = object.id;
  79. this.sceneHelpers.add( helper );
  80. this.helpers[ object.id ] = helper;
  81. this.signals.helperAdded.dispatch( helper );
  82. } else if ( object instanceof THREE.SpotLight ) {
  83. var helper = new THREE.SpotLightHelper( object, 10 );
  84. helper.lightSphere.id = object.id;
  85. this.sceneHelpers.add( helper );
  86. this.helpers[ object.id ] = helper;
  87. this.signals.helperAdded.dispatch( helper );
  88. } else if ( object instanceof THREE.HemisphereLight ) {
  89. var helper = new THREE.HemisphereLightHelper( object, 10 );
  90. helper.lightSphere.id = object.id;
  91. this.sceneHelpers.add( helper );
  92. this.helpers[ object.id ] = helper;
  93. this.signals.helperAdded.dispatch( helper );
  94. }
  95. },
  96. removeHelper: function ( object ) {
  97. if ( this.helpers[ object.id ] !== undefined ) {
  98. this.helpers[ object.id ].parent.remove( this.helpers[ object.id ] );
  99. delete this.helpers[ object.id ];
  100. this.signals.helperRemoved.dispatch( helper );
  101. }
  102. },
  103. //
  104. parent: function ( object, parent ) {
  105. if ( parent === undefined ) {
  106. parent = this.scene;
  107. }
  108. parent.add( object );
  109. this.signals.sceneGraphChanged.dispatch();
  110. },
  111. //
  112. select: function ( object ) {
  113. this.selected = object;
  114. this.signals.objectSelected.dispatch( object );
  115. },
  116. selectById: function ( id ) {
  117. var scope = this;
  118. this.scene.traverse( function ( node ) {
  119. if ( node.id === id ) {
  120. scope.select( node );
  121. }
  122. } );
  123. },
  124. deselect: function () {
  125. this.select( null );
  126. }
  127. }
粤ICP备19079148号