Editor.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.loader = new Loader( this );
  25. this.scene = new THREE.Scene();
  26. this.sceneHelpers = new THREE.Scene();
  27. this.object = {};
  28. this.geometries = {};
  29. this.materials = {};
  30. this.textures = {};
  31. this.selected = null;
  32. this.helpers = {};
  33. };
  34. Editor.prototype = {
  35. setScene: function ( scene ) {
  36. this.scene.name = scene.name;
  37. this.scene.userData = JSON.parse( JSON.stringify( scene.userData ) );
  38. while ( scene.children.length > 0 ) {
  39. this.addObject( scene.children[ 0 ] );
  40. }
  41. },
  42. //
  43. addObject: function ( object ) {
  44. var scope = this;
  45. object.traverse( function ( child ) {
  46. scope.addHelper( child );
  47. } );
  48. this.scene.add( object );
  49. this.signals.objectAdded.dispatch( object );
  50. this.signals.sceneGraphChanged.dispatch();
  51. },
  52. removeObject: function ( object ) {
  53. if ( object.parent === undefined ) return; // avoid deleting the camera or scene
  54. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  55. var scope = this;
  56. object.traverse( function ( child ) {
  57. scope.removeHelper( child );
  58. } );
  59. object.parent.remove( object );
  60. this.signals.objectRemoved.dispatch( object );
  61. this.signals.sceneGraphChanged.dispatch();
  62. },
  63. addGeometry: function ( geometry ) {
  64. },
  65. removeGeometry: function ( geometry ) {
  66. },
  67. addMaterial: function ( material ) {
  68. },
  69. removeMaterial: function ( material ) {
  70. },
  71. addTexture: function ( texture ) {
  72. },
  73. removeTexture: function ( texture ) {
  74. },
  75. //
  76. addHelper: function () {
  77. var geometry = new THREE.SphereGeometry( 20, 4, 2 );
  78. var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  79. return function ( object ) {
  80. if ( object instanceof THREE.Camera ) {
  81. var picker = new THREE.Mesh( geometry, material );
  82. picker.name = 'picker';
  83. picker.userData.object = object;
  84. picker.visible = false;
  85. var helper = new THREE.CameraHelper( object, 10 );
  86. helper.add( picker );
  87. this.sceneHelpers.add( helper );
  88. this.helpers[ object.id ] = helper;
  89. this.signals.helperAdded.dispatch( helper );
  90. } else if ( object instanceof THREE.PointLight ) {
  91. var picker = new THREE.Mesh( geometry, material );
  92. picker.name = 'picker';
  93. picker.userData.object = object;
  94. picker.visible = false;
  95. var helper = new THREE.PointLightHelper( object, 10 );
  96. helper.add( picker );
  97. this.sceneHelpers.add( helper );
  98. this.helpers[ object.id ] = helper;
  99. this.signals.helperAdded.dispatch( helper );
  100. } else if ( object instanceof THREE.DirectionalLight ) {
  101. var picker = new THREE.Mesh( geometry, material );
  102. picker.name = 'picker';
  103. picker.userData.object = object;
  104. picker.visible = false;
  105. var helper = new THREE.DirectionalLightHelper( object, 20 );
  106. helper.add( picker );
  107. this.sceneHelpers.add( helper );
  108. this.helpers[ object.id ] = helper;
  109. this.signals.helperAdded.dispatch( helper );
  110. } else if ( object instanceof THREE.SpotLight ) {
  111. var picker = new THREE.Mesh( geometry, material );
  112. picker.name = 'picker';
  113. picker.userData.object = object;
  114. picker.visible = false;
  115. var helper = new THREE.SpotLightHelper( object, 10 );
  116. helper.add( picker );
  117. this.sceneHelpers.add( helper );
  118. this.helpers[ object.id ] = helper;
  119. this.signals.helperAdded.dispatch( helper );
  120. } else if ( object instanceof THREE.HemisphereLight ) {
  121. var picker = new THREE.Mesh( geometry, material );
  122. picker.name = 'picker';
  123. picker.userData.object = object;
  124. picker.visible = false;
  125. var helper = new THREE.HemisphereLightHelper( object, 10 );
  126. helper.add( picker );
  127. this.sceneHelpers.add( helper );
  128. this.helpers[ object.id ] = helper;
  129. this.signals.helperAdded.dispatch( helper );
  130. }
  131. };
  132. }(),
  133. removeHelper: function ( object ) {
  134. if ( this.helpers[ object.id ] !== undefined ) {
  135. var helper = this.helpers[ object.id ];
  136. helper.parent.remove( helper );
  137. delete this.helpers[ object.id ];
  138. this.signals.helperRemoved.dispatch( helper );
  139. }
  140. },
  141. //
  142. parent: function ( object, parent ) {
  143. if ( parent === undefined ) {
  144. parent = this.scene;
  145. }
  146. parent.add( object );
  147. this.signals.sceneGraphChanged.dispatch();
  148. },
  149. //
  150. select: function ( object ) {
  151. this.selected = object;
  152. this.signals.objectSelected.dispatch( object );
  153. },
  154. selectById: function ( id ) {
  155. this.select( this.scene.getObjectById( id, true ) );
  156. },
  157. deselect: function () {
  158. this.select( null );
  159. }
  160. }