Editor.js 5.3 KB

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