Viewport.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Viewport = function ( editor ) {
  5. var signals = editor.signals;
  6. var config = editor.config;
  7. var container = new UI.Panel();
  8. container.setId( 'viewport' );
  9. container.setPosition( 'absolute' );
  10. container.add( new Viewport.Info( editor ) );
  11. //
  12. var renderer = null;
  13. var camera = editor.camera;
  14. var scene = editor.scene;
  15. var sceneHelpers = editor.sceneHelpers;
  16. var objects = [];
  17. // helpers
  18. var grid = new THREE.GridHelper( 30, 30, 0x444444, 0x888888 );
  19. sceneHelpers.add( grid );
  20. var array = grid.geometry.attributes.color.array;
  21. for ( var i = 0; i < array.length; i += 60 ) {
  22. for ( var j = 0; j < 12; j ++ ) {
  23. array[ i + j ] = 0.26;
  24. }
  25. }
  26. //
  27. var box = new THREE.Box3();
  28. var selectionBox = new THREE.BoxHelper();
  29. selectionBox.material.depthTest = false;
  30. selectionBox.material.transparent = true;
  31. selectionBox.visible = false;
  32. sceneHelpers.add( selectionBox );
  33. var objectPositionOnDown = null;
  34. var objectRotationOnDown = null;
  35. var objectScaleOnDown = null;
  36. var transformControls = new THREE.TransformControls( camera, container.dom );
  37. transformControls.addEventListener( 'change', function () {
  38. var object = transformControls.object;
  39. if ( object !== undefined ) {
  40. selectionBox.setFromObject( object );
  41. if ( editor.helpers[ object.id ] !== undefined ) {
  42. editor.helpers[ object.id ].update();
  43. }
  44. signals.refreshSidebarObject3D.dispatch( object );
  45. }
  46. render();
  47. } );
  48. transformControls.addEventListener( 'mouseDown', function () {
  49. var object = transformControls.object;
  50. objectPositionOnDown = object.position.clone();
  51. objectRotationOnDown = object.rotation.clone();
  52. objectScaleOnDown = object.scale.clone();
  53. controls.enabled = false;
  54. } );
  55. transformControls.addEventListener( 'mouseUp', function () {
  56. var object = transformControls.object;
  57. if ( object !== undefined ) {
  58. switch ( transformControls.getMode() ) {
  59. case 'translate':
  60. if ( ! objectPositionOnDown.equals( object.position ) ) {
  61. editor.execute( new SetPositionCommand( object, object.position, objectPositionOnDown ) );
  62. }
  63. break;
  64. case 'rotate':
  65. if ( ! objectRotationOnDown.equals( object.rotation ) ) {
  66. editor.execute( new SetRotationCommand( object, object.rotation, objectRotationOnDown ) );
  67. }
  68. break;
  69. case 'scale':
  70. if ( ! objectScaleOnDown.equals( object.scale ) ) {
  71. editor.execute( new SetScaleCommand( object, object.scale, objectScaleOnDown ) );
  72. }
  73. break;
  74. }
  75. }
  76. controls.enabled = true;
  77. } );
  78. sceneHelpers.add( transformControls );
  79. // Displaying scene cameras
  80. var cameras = {
  81. [ camera.uuid ]: 'Main Camera'
  82. };
  83. var sceneCameraDisplay = new UI.Row();
  84. sceneCameraDisplay.setId( 'cameraSelect' ).setDisplay( 'none' );
  85. document.body.appendChild( sceneCameraDisplay.dom );
  86. var cameraSelect = new UI.Select().onChange( render );
  87. sceneCameraDisplay.add( cameraSelect );
  88. signals.objectAdded.add( function ( object ) {
  89. if ( object !== null ) {
  90. object.traverse( addCamera );
  91. cameraSelect.setOptions( cameras ).setValue( camera.uuid );
  92. sceneCameraDisplay.setDisplay( config.getKey( 'project/renderer/showSceneCameras' ) === true && Object.keys( cameras ).length > 0 ? 'block' : 'none' );
  93. }
  94. } );
  95. signals.objectRemoved.add( function ( object ) {
  96. if ( object !== null ) {
  97. object.traverse( removeCamera );
  98. cameraSelect.setOptions( cameras ).setValue( camera.uuid );
  99. sceneCameraDisplay.setDisplay( config.getKey( 'project/renderer/showSceneCameras' ) === true && Object.keys( cameras ).length > 0 ? 'block' : 'none' );
  100. }
  101. } );
  102. signals.sceneCamerasChanged.add( function () {
  103. var optionSelected = config.getKey( 'project/renderer/showSceneCameras' ) === true;
  104. sceneCameraDisplay.setDisplay( optionSelected && Object.keys( cameras ).length > 0 ? 'block' : 'none' );
  105. if ( optionSelected === false ) {
  106. cameraSelect.setValue( camera.uuid );
  107. render();
  108. }
  109. } );
  110. function addCamera( object ) {
  111. if ( object.isCamera === true ) {
  112. cameras[ object.uuid ] = object.name;
  113. }
  114. }
  115. function removeCamera( object ) {
  116. if ( object.isCamera === true ) {
  117. delete cameras[ object.uuid ];
  118. }
  119. }
  120. // object picking
  121. var raycaster = new THREE.Raycaster();
  122. var mouse = new THREE.Vector2();
  123. // events
  124. function getIntersects( point, objects ) {
  125. mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
  126. raycaster.setFromCamera( mouse, camera );
  127. return raycaster.intersectObjects( objects );
  128. }
  129. var onDownPosition = new THREE.Vector2();
  130. var onUpPosition = new THREE.Vector2();
  131. var onDoubleClickPosition = new THREE.Vector2();
  132. function getMousePosition( dom, x, y ) {
  133. var rect = dom.getBoundingClientRect();
  134. return [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ];
  135. }
  136. function handleClick() {
  137. if ( onDownPosition.distanceTo( onUpPosition ) === 0 ) {
  138. var intersects = getIntersects( onUpPosition, objects );
  139. if ( intersects.length > 0 ) {
  140. var object = intersects[ 0 ].object;
  141. if ( object.userData.object !== undefined ) {
  142. // helper
  143. editor.select( object.userData.object );
  144. } else {
  145. editor.select( object );
  146. }
  147. } else {
  148. editor.select( null );
  149. }
  150. render();
  151. }
  152. }
  153. function onMouseDown( event ) {
  154. event.preventDefault();
  155. var array = getMousePosition( container.dom, event.clientX, event.clientY );
  156. onDownPosition.fromArray( array );
  157. document.addEventListener( 'mouseup', onMouseUp, false );
  158. }
  159. function onMouseUp( event ) {
  160. var array = getMousePosition( container.dom, event.clientX, event.clientY );
  161. onUpPosition.fromArray( array );
  162. handleClick();
  163. document.removeEventListener( 'mouseup', onMouseUp, false );
  164. }
  165. function onTouchStart( event ) {
  166. var touch = event.changedTouches[ 0 ];
  167. var array = getMousePosition( container.dom, touch.clientX, touch.clientY );
  168. onDownPosition.fromArray( array );
  169. document.addEventListener( 'touchend', onTouchEnd, false );
  170. }
  171. function onTouchEnd( event ) {
  172. var touch = event.changedTouches[ 0 ];
  173. var array = getMousePosition( container.dom, touch.clientX, touch.clientY );
  174. onUpPosition.fromArray( array );
  175. handleClick();
  176. document.removeEventListener( 'touchend', onTouchEnd, false );
  177. }
  178. function onDoubleClick( event ) {
  179. var array = getMousePosition( container.dom, event.clientX, event.clientY );
  180. onDoubleClickPosition.fromArray( array );
  181. var intersects = getIntersects( onDoubleClickPosition, objects );
  182. if ( intersects.length > 0 ) {
  183. var intersect = intersects[ 0 ];
  184. signals.objectFocused.dispatch( intersect.object );
  185. }
  186. }
  187. container.dom.addEventListener( 'mousedown', onMouseDown, false );
  188. container.dom.addEventListener( 'touchstart', onTouchStart, false );
  189. container.dom.addEventListener( 'dblclick', onDoubleClick, false );
  190. // controls need to be added *after* main logic,
  191. // otherwise controls.enabled doesn't work.
  192. var controls = new THREE.EditorControls( camera, container.dom );
  193. controls.addEventListener( 'change', function () {
  194. signals.cameraChanged.dispatch( camera );
  195. } );
  196. // signals
  197. signals.editorCleared.add( function () {
  198. controls.center.set( 0, 0, 0 );
  199. render();
  200. } );
  201. signals.transformModeChanged.add( function ( mode ) {
  202. transformControls.setMode( mode );
  203. } );
  204. signals.snapChanged.add( function ( dist ) {
  205. transformControls.setTranslationSnap( dist );
  206. } );
  207. signals.spaceChanged.add( function ( space ) {
  208. transformControls.setSpace( space );
  209. } );
  210. signals.rendererChanged.add( function ( newRenderer ) {
  211. if ( renderer !== null ) {
  212. container.dom.removeChild( renderer.domElement );
  213. }
  214. renderer = newRenderer;
  215. renderer.autoClear = false;
  216. renderer.autoUpdateScene = false;
  217. renderer.gammaOutput = true;
  218. renderer.setPixelRatio( window.devicePixelRatio );
  219. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  220. container.dom.appendChild( renderer.domElement );
  221. render();
  222. } );
  223. signals.sceneGraphChanged.add( function () {
  224. render();
  225. } );
  226. signals.cameraChanged.add( function () {
  227. render();
  228. } );
  229. signals.objectSelected.add( function ( object ) {
  230. selectionBox.visible = false;
  231. transformControls.detach();
  232. if ( object !== null && object !== scene && object !== camera ) {
  233. box.setFromObject( object );
  234. if ( box.isEmpty() === false ) {
  235. selectionBox.setFromObject( object );
  236. selectionBox.visible = true;
  237. }
  238. transformControls.attach( object );
  239. }
  240. render();
  241. } );
  242. signals.objectFocused.add( function ( object ) {
  243. controls.focus( object );
  244. } );
  245. signals.geometryChanged.add( function ( object ) {
  246. if ( object !== undefined ) {
  247. selectionBox.setFromObject( object );
  248. }
  249. render();
  250. } );
  251. signals.objectAdded.add( function ( object ) {
  252. object.traverse( function ( child ) {
  253. objects.push( child );
  254. } );
  255. } );
  256. signals.objectChanged.add( function ( object ) {
  257. if ( editor.selected === object ) {
  258. selectionBox.setFromObject( object );
  259. }
  260. if ( object.isPerspectiveCamera ) {
  261. object.updateProjectionMatrix();
  262. }
  263. if ( editor.helpers[ object.id ] !== undefined ) {
  264. editor.helpers[ object.id ].update();
  265. }
  266. render();
  267. } );
  268. signals.objectRemoved.add( function ( object ) {
  269. if ( object === transformControls.object ) {
  270. transformControls.detach();
  271. }
  272. object.traverse( function ( child ) {
  273. objects.splice( objects.indexOf( child ), 1 );
  274. } );
  275. } );
  276. signals.helperAdded.add( function ( object ) {
  277. objects.push( object.getObjectByName( 'picker' ) );
  278. } );
  279. signals.helperRemoved.add( function ( object ) {
  280. objects.splice( objects.indexOf( object.getObjectByName( 'picker' ) ), 1 );
  281. } );
  282. signals.materialChanged.add( function ( material ) {
  283. render();
  284. } );
  285. // fog
  286. signals.sceneBackgroundChanged.add( function ( backgroundColor ) {
  287. scene.background.setHex( backgroundColor );
  288. render();
  289. } );
  290. var currentFogType = null;
  291. signals.sceneFogChanged.add( function ( fogType, fogColor, fogNear, fogFar, fogDensity ) {
  292. if ( currentFogType !== fogType ) {
  293. switch ( fogType ) {
  294. case 'None':
  295. scene.fog = null;
  296. break;
  297. case 'Fog':
  298. scene.fog = new THREE.Fog();
  299. break;
  300. case 'FogExp2':
  301. scene.fog = new THREE.FogExp2();
  302. break;
  303. }
  304. currentFogType = fogType;
  305. }
  306. if ( scene.fog ) {
  307. if ( scene.fog.isFog ) {
  308. scene.fog.color.setHex( fogColor );
  309. scene.fog.near = fogNear;
  310. scene.fog.far = fogFar;
  311. } else if ( scene.fog.isFogExp2 ) {
  312. scene.fog.color.setHex( fogColor );
  313. scene.fog.density = fogDensity;
  314. }
  315. }
  316. render();
  317. } );
  318. //
  319. signals.windowResize.add( function () {
  320. // TODO: Move this out?
  321. editor.DEFAULT_CAMERA.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  322. editor.DEFAULT_CAMERA.updateProjectionMatrix();
  323. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  324. camera.updateProjectionMatrix();
  325. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  326. render();
  327. } );
  328. signals.showGridChanged.add( function ( showGrid ) {
  329. grid.visible = showGrid;
  330. render();
  331. } );
  332. // animations
  333. var prevTime = performance.now();
  334. function animate( time ) {
  335. requestAnimationFrame( animate );
  336. var mixer = editor.mixer;
  337. if ( mixer.stats.actions.inUse > 0 ) {
  338. mixer.update( ( time - prevTime ) / 1000 );
  339. render();
  340. }
  341. prevTime = time;
  342. }
  343. requestAnimationFrame( animate );
  344. //
  345. function render() {
  346. sceneHelpers.updateMatrixWorld();
  347. scene.updateMatrixWorld();
  348. var cam = camera;
  349. var uuid = cameraSelect.getValue();
  350. if ( uuid !== camera.uuid && sceneCameraDisplay.dom.style.display != 'none' ) {
  351. var sceneCamera = scene.getObjectByProperty( 'uuid', uuid );
  352. if ( sceneCamera !== undefined && sceneCamera.isCamera === true ) {
  353. cam = sceneCamera;
  354. }
  355. }
  356. renderer.render( scene, cam );
  357. if ( config.getKey( 'project/renderer/showHelpers' ) === true && renderer instanceof THREE.RaytracingRenderer === false ) {
  358. renderer.render( sceneHelpers, cam );
  359. }
  360. }
  361. return container;
  362. };