Viewport.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. var Viewport = function ( editor, signals ) {
  2. var container = new UI.Panel();
  3. container.setPosition( 'absolute' );
  4. container.setBackgroundColor( '#aaa' );
  5. var info = new UI.Text();
  6. info.setPosition( 'absolute' );
  7. info.setRight( '5px' );
  8. info.setBottom( '5px' );
  9. info.setFontSize( '12px' );
  10. info.setColor( '#ffffff' );
  11. container.add( info );
  12. var scene = editor.scene;
  13. var sceneHelpers = editor.sceneHelpers;
  14. var clearColor = 0xAAAAAA;
  15. var grid = new THREE.GridHelper( 500, 25 );
  16. sceneHelpers.add( grid );
  17. //
  18. var camera = new THREE.PerspectiveCamera( 50, container.dom.offsetWidth / container.dom.offsetHeight, 1, 5000 );
  19. camera.position.set( 500, 250, 500 );
  20. camera.lookAt( scene.position );
  21. editor.select( camera );
  22. //
  23. var selectionBox = new THREE.BoxHelper();
  24. selectionBox.material.depthTest = false;
  25. selectionBox.material.transparent = true;
  26. selectionBox.visible = false;
  27. sceneHelpers.add( selectionBox );
  28. //
  29. var transformControls = new THREE.TransformControls( camera, container.dom );
  30. transformControls.addEventListener( 'change', function () {
  31. signals.objectChanged.dispatch( this.object );
  32. } );
  33. sceneHelpers.add( transformControls.gizmo );
  34. transformControls.hide();
  35. //
  36. var selected;
  37. // object picking
  38. var ray = new THREE.Raycaster();
  39. var projector = new THREE.Projector();
  40. // events
  41. var getIntersects = function ( event, object ) {
  42. var vector = new THREE.Vector3(
  43. ( event.layerX / container.dom.offsetWidth ) * 2 - 1,
  44. - ( event.layerY / container.dom.offsetHeight ) * 2 + 1,
  45. 0.5
  46. );
  47. projector.unprojectVector( vector, camera );
  48. ray.set( camera.position, vector.sub( camera.position ).normalize() );
  49. if ( object instanceof Array ) {
  50. return ray.intersectObjects( object, true );
  51. }
  52. return ray.intersectObject( object, true );
  53. };
  54. var onMouseDownPosition = new THREE.Vector2();
  55. var onMouseUpPosition = new THREE.Vector2();
  56. var onMouseDown = function ( event ) {
  57. event.preventDefault();
  58. onMouseDownPosition.set( event.layerX, event.layerY );
  59. if ( transformControls.hovered === false ) {
  60. controls.enabled = true;
  61. document.addEventListener( 'mouseup', onMouseUp, false );
  62. }
  63. };
  64. var onMouseUp = function ( event ) {
  65. onMouseUpPosition.set( event.layerX, event.layerY );
  66. if ( onMouseDownPosition.distanceTo( onMouseUpPosition ) < 1 ) {
  67. var hit;
  68. var intersect = getIntersects( event, [ scene, sceneHelpers ] );
  69. for ( var i in intersect ) {
  70. if ( editor.objects[ intersect[i].object.id ] ) {
  71. editor.selectById( intersect[i].object.id );
  72. hit = true;
  73. break;
  74. }
  75. }
  76. if ( !hit ) editor.deselectAll();
  77. }
  78. controls.enabled = false; // ?
  79. document.removeEventListener( 'mouseup', onMouseUp );
  80. };
  81. var onDoubleClick = function ( event ) {
  82. var intersect = getIntersects( event, [ scene, sceneHelpers ] );
  83. for ( var i in intersect ) {
  84. if ( editor.objects[ intersect[i].object.id ] ) {
  85. editor.selectById( intersect[i].object.id );
  86. controls.focus( editor.objects[ intersect[i].object.id ] );
  87. break;
  88. }
  89. }
  90. };
  91. container.dom.addEventListener( 'mousedown', onMouseDown, false );
  92. container.dom.addEventListener( 'dblclick', onDoubleClick, false );
  93. // controls need to be added *after* main logic,
  94. // otherwise controls.enabled doesn't work.
  95. var controls = new THREE.EditorControls( camera, container.dom );
  96. controls.addEventListener( 'change', function () {
  97. transformControls.update();
  98. signals.objectChanged.dispatch( camera );
  99. } );
  100. controls.enabled = false;
  101. function updateHelpers( object ) {
  102. if ( object.geometry !== undefined ) {
  103. selectionBox.visible = true;
  104. selectionBox.update( object );
  105. transformControls.update();
  106. }
  107. if ( editor.helpers[ object.id ] ) editor.helpers[ object.id ].update();
  108. }
  109. // signals
  110. signals.setTransformMode.add( function ( mode ) {
  111. transformControls.setMode( mode );
  112. render();
  113. } );
  114. signals.snapChanged.add( function ( dist ) {
  115. transformControls.snapDist = dist;
  116. } );
  117. signals.setRenderer.add( function ( object ) {
  118. container.dom.removeChild( renderer.domElement );
  119. renderer = object;
  120. renderer.setClearColor( clearColor );
  121. renderer.autoClear = false;
  122. renderer.autoUpdateScene = false;
  123. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  124. container.dom.appendChild( renderer.domElement );
  125. render();
  126. } );
  127. signals.selected.add( function () {
  128. selectionBox.visible = false;
  129. transformControls.detach();
  130. selected = editor.listSelected( 'object' );
  131. object = ( selected.length ) ? selected[0] : null;
  132. if ( object && object !== scene ) {
  133. transformControls.attach( object );
  134. updateHelpers( object );
  135. }
  136. render();
  137. } );
  138. signals.sceneChanged.add( function () {
  139. render();
  140. } );
  141. signals.objectAdded.add( function ( object ) {
  142. updateHelpers( object );
  143. updateInfo();
  144. render();
  145. } );
  146. signals.objectChanged.add( function ( object ) {
  147. updateHelpers( object );
  148. transformControls.update();
  149. updateInfo();
  150. render();
  151. } );
  152. signals.objectDeleted.add( function () {
  153. updateInfo();
  154. render();
  155. } );
  156. signals.materialChanged.add( function ( material ) {
  157. render();
  158. } );
  159. signals.geometryChanged.add( function ( material ) {
  160. updateInfo();
  161. render();
  162. } );
  163. signals.clearColorChanged.add( function ( color ) {
  164. renderer.setClearColor( color );
  165. render();
  166. clearColor = color;
  167. } );
  168. signals.fogChanged.add( function () {
  169. render();
  170. } );
  171. signals.windowResize.add( function () {
  172. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  175. render();
  176. } );
  177. signals.playAnimations.add( function (animations) {
  178. function animate() {
  179. requestAnimationFrame( animate );
  180. for (var i = 0; i < animations.length ; i++ ){
  181. animations[i].update(0.016);
  182. }
  183. render();
  184. }
  185. animate();
  186. } );
  187. //
  188. var renderer;
  189. if ( System.support.webgl === true ) {
  190. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false } );
  191. } else {
  192. renderer = new THREE.CanvasRenderer();
  193. }
  194. renderer.setClearColor( clearColor );
  195. renderer.autoClear = false;
  196. renderer.autoUpdateScene = false;
  197. container.dom.appendChild( renderer.domElement );
  198. animate();
  199. //
  200. function updateInfo() {
  201. var objects = 0;
  202. var vertices = 0;
  203. var faces = 0;
  204. scene.traverse( function ( object ) {
  205. if ( object instanceof THREE.Mesh ) {
  206. objects ++;
  207. vertices += object.geometry.vertices.length;
  208. faces += object.geometry.faces.length;
  209. }
  210. } );
  211. info.setValue( 'objects: ' + objects + ', vertices: ' + vertices + ', faces: ' + faces );
  212. }
  213. function animate() {
  214. requestAnimationFrame( animate );
  215. }
  216. function render() {
  217. sceneHelpers.updateMatrixWorld();
  218. scene.updateMatrixWorld();
  219. renderer.clear();
  220. renderer.render( scene, camera );
  221. renderer.render( sceneHelpers, camera );
  222. }
  223. return container;
  224. }