Editor.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. var Editor = function () {
  2. var SIGNALS = signals;
  3. this.signals = {
  4. // actions
  5. flattenSelectedObject: new SIGNALS.Signal(),
  6. cloneSelectedObject: new SIGNALS.Signal(),
  7. removeSelectedObject: new SIGNALS.Signal(),
  8. playAnimations: new SIGNALS.Signal(),
  9. // notifications
  10. transformModeChanged: new SIGNALS.Signal(),
  11. snapChanged: new SIGNALS.Signal(),
  12. rendererChanged: new SIGNALS.Signal(),
  13. sceneChanged: new SIGNALS.Signal(),
  14. objectAdded: new SIGNALS.Signal(),
  15. objectSelected: new SIGNALS.Signal(),
  16. objectChanged: 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. this.signals.sceneChanged.dispatch( this.scene );
  41. },
  42. //
  43. addObject: function ( object ) {
  44. this.scene.add( object );
  45. this.addHelper( object );
  46. this.signals.objectAdded.dispatch( object );
  47. this.signals.sceneChanged.dispatch();
  48. },
  49. removeObject: function ( object ) {
  50. if ( object === this.scene ) return;
  51. var name = object.name ? '"' + object.name + '"': "selected object";
  52. if ( confirm( 'Delete ' + name + '?' ) === false ) return;
  53. this.scene.remove( object );
  54. this.removeHelper( object );
  55. this.signals.sceneChanged.dispatch();
  56. },
  57. addGeometry: function ( geometry ) {
  58. },
  59. removeGeometry: function ( geometry ) {
  60. },
  61. addMaterial: function ( material ) {
  62. },
  63. removeMaterial: function ( material ) {
  64. },
  65. addTexture: function ( texture ) {
  66. },
  67. removeTexture: function ( texture ) {
  68. },
  69. //
  70. addHelper: function ( object ) {
  71. if ( object instanceof THREE.PointLight ) {
  72. this.helpers[ object.id ] = new THREE.PointLightHelper( object, 10 );
  73. this.sceneHelpers.add( this.helpers[ object.id ] );
  74. this.helpers[ object.id ].lightSphere.id = object.id;
  75. } else if ( object instanceof THREE.DirectionalLight ) {
  76. this.helpers[ object.id ] = new THREE.DirectionalLightHelper( object, 10 );
  77. this.sceneHelpers.add( this.helpers[ object.id ] );
  78. this.helpers[ object.id ].lightSphere.id = object.id;
  79. } else if ( object instanceof THREE.SpotLight ) {
  80. this.helpers[ object.id ] = new THREE.SpotLightHelper( object, 10 );
  81. this.sceneHelpers.add( this.helpers[ object.id ] );
  82. // this.helpers[ object.id ].lightSphere.id = object.id;
  83. } else if ( object instanceof THREE.HemisphereLight ) {
  84. this.helpers[ object.id ] = new THREE.HemisphereLightHelper( object, 10 );
  85. this.sceneHelpers.add( this.helpers[ object.id ] );
  86. this.helpers[ object.id ].lightSphere.id = object.id;
  87. }
  88. this.signals.sceneChanged.dispatch();
  89. },
  90. removeHelper: function ( object ) {
  91. if ( this.helpers[ object.id ] !== undefined ) {
  92. this.helpers[ object.id ].parent.remove( this.helpers[ object.id ] );
  93. delete this.helpers[ object.id ];
  94. }
  95. },
  96. //
  97. select: function ( object ) {
  98. this.selected = object;
  99. this.signals.objectSelected.dispatch( object );
  100. },
  101. selectById: function ( id ) {
  102. var scope = this;
  103. this.scene.traverse( function ( node ) {
  104. if ( node.id === id ) {
  105. scope.select( node );
  106. }
  107. } );
  108. },
  109. deselect: function () {
  110. this.selected = null;
  111. },
  112. //
  113. cloneObject: function ( object ) {
  114. this.addObject( object.clone() );
  115. },
  116. flattenObject: function ( object ) {
  117. var name = object.name ? '"' + object.name + '"': "selected object";
  118. if ( confirm( 'Flatten ' + name + '?' ) === false ) return;
  119. delete object.__webglInit; // TODO: Remove hack (WebGLRenderer refactoring)
  120. var geometry = object.geometry.clone();
  121. geometry.applyMatrix( object.matrix );
  122. object.setGeometry( geometry );
  123. object.position.set( 0, 0, 0 );
  124. object.rotation.set( 0, 0, 0 );
  125. object.scale.set( 1, 1, 1 );
  126. signals.objectChanged.dispatch( object );
  127. }
  128. }