Viewport.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. var Viewport = function ( signals ) {
  2. var container = new UI.Panel( 'absolute' );
  3. container.setBackgroundColor( '#aaa' );
  4. //
  5. var objects = [];
  6. var sceneHelpers = new THREE.Scene();
  7. var size = 500, step = 25;
  8. var geometry = new THREE.Geometry();
  9. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  10. var color1 = new THREE.Color( 0x444444 ), color2 = new THREE.Color( 0x888888 );
  11. for ( var i = - size; i <= size; i += step ) {
  12. geometry.vertices.push( new THREE.Vector3( -size, 0, i ) );
  13. geometry.vertices.push( new THREE.Vector3( size, 0, i ) );
  14. geometry.vertices.push( new THREE.Vector3( i, 0, -size ) );
  15. geometry.vertices.push( new THREE.Vector3( i, 0, size ) );
  16. var color = i === 0 ? color1 : color2;
  17. geometry.colors.push( color, color, color, color );
  18. }
  19. var grid = new THREE.Line( geometry, material, THREE.LinePieces );
  20. sceneHelpers.add( grid );
  21. var selectionBox = new THREE.Mesh( new THREE.CubeGeometry( 1, 1, 1 ), new THREE.MeshBasicMaterial( { color: 0xffff00, wireframe: true } ) );
  22. selectionBox.matrixAutoUpdate = false;
  23. selectionBox.visible = false;
  24. sceneHelpers.add( selectionBox );
  25. var selectionAxis = new THREE.AxisHelper( 100 );
  26. selectionAxis.material.depthTest = false;
  27. selectionAxis.material.transparent = true;
  28. selectionAxis.matrixAutoUpdate = false;
  29. selectionAxis.visible = false;
  30. sceneHelpers.add( selectionAxis );
  31. //
  32. var scene = new THREE.Scene();
  33. var camera = new THREE.PerspectiveCamera( 50, 1, 1, 5000 );
  34. camera.position.set( 500, 250, 500 );
  35. camera.lookAt( scene.position );
  36. scene.add( camera );
  37. var light1 = new THREE.DirectionalLight( 0xffffff );
  38. light1.position.set( 1, 0.5, 0 ).normalize();
  39. scene.add( light1 );
  40. var light2 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  41. light2.position.set( - 1, - 0.5, 0 ).normalize();
  42. scene.add( light2 );
  43. // fog
  44. var oldFogType = "None";
  45. var oldFogColor = 0xaaaaaa;
  46. var oldFogNear = 1;
  47. var oldFogFar = 5000;
  48. var oldFogDensity = 0.00025;
  49. // default objects names
  50. camera.name = "Camera";
  51. light1.name = "Light 1";
  52. light1.target.name = "Light 1 Target";
  53. light2.name = "Light 2";
  54. light2.target.name = "Light 2 Target";
  55. signals.sceneChanged.dispatch( scene );
  56. // object picking
  57. var intersectionPlane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ) );
  58. intersectionPlane.visible = false;
  59. sceneHelpers.add( intersectionPlane );
  60. var ray = new THREE.Ray();
  61. var projector = new THREE.Projector();
  62. var offset = new THREE.Vector3();
  63. var picked = null;
  64. var cameraChanged = false;
  65. // events
  66. var onMouseDown = function ( event ) {
  67. container.dom.focus();
  68. event.preventDefault();
  69. if ( event.button === 0 ) {
  70. var vector = new THREE.Vector3(
  71. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  72. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  73. 0.5
  74. );
  75. projector.unprojectVector( vector, camera );
  76. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  77. var intersects = ray.intersectObjects( objects, true );
  78. if ( intersects.length > 0 ) {
  79. controls.enabled = false;
  80. intersectionPlane.position.copy( intersects[ 0 ].object.position );
  81. intersectionPlane.lookAt( camera.position );
  82. picked = intersects[ 0 ].object;
  83. selected = picked;
  84. signals.objectSelected.dispatch( selected );
  85. var intersects = ray.intersectObject( intersectionPlane );
  86. offset.copy( intersects[ 0 ].point ).subSelf( intersectionPlane.position );
  87. document.addEventListener( 'mousemove', onMouseMove, false );
  88. document.addEventListener( 'mouseup', onMouseUp, false );
  89. } else {
  90. controls.enabled = true;
  91. }
  92. }
  93. cameraChanged = false;
  94. };
  95. var onMouseMove = function ( event ) {
  96. var vector = new THREE.Vector3(
  97. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  98. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  99. 0.5
  100. );
  101. projector.unprojectVector( vector, camera );
  102. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  103. var intersects = ray.intersectObject( intersectionPlane );
  104. if ( intersects.length > 0 ) {
  105. picked.position.copy( intersects[ 0 ].point.subSelf( offset ) );
  106. signals.objectChanged.dispatch( picked );
  107. render();
  108. }
  109. };
  110. var onMouseUp = function ( event ) {
  111. document.removeEventListener( 'mousemove', onMouseMove );
  112. document.removeEventListener( 'mouseup', onMouseUp );
  113. };
  114. var onClick = function ( event ) {
  115. if ( event.button == 0 && cameraChanged === false ) {
  116. var vector = new THREE.Vector3(
  117. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  118. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  119. 0.5
  120. );
  121. projector.unprojectVector( vector, camera );
  122. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  123. var intersects = ray.intersectObjects( objects, true );
  124. if ( intersects.length > 0 && ! controls.enabled ) {
  125. selected = intersects[ 0 ].object;
  126. } else {
  127. selected = camera;
  128. }
  129. signals.objectSelected.dispatch( selected );
  130. }
  131. controls.enabled = true;
  132. };
  133. var onKeyDown = function ( event ) {
  134. switch ( event.keyCode ) {
  135. case 46: // delete
  136. signals.removeSelectedObject.dispatch();
  137. break;
  138. }
  139. };
  140. container.dom.addEventListener( 'mousedown', onMouseDown, false );
  141. container.dom.addEventListener( 'click', onClick, false );
  142. // controls need to be added *after* main logic,
  143. // otherwise controls.enabled doesn't work.
  144. var controls = new THREE.TrackballControls( camera, container.dom );
  145. controls.rotateSpeed = 1.0;
  146. controls.zoomSpeed = 1.2;
  147. controls.panSpeed = 0.8;
  148. controls.noZoom = false;
  149. controls.noPan = false;
  150. controls.staticMoving = true;
  151. controls.dynamicDampingFactor = 0.3;
  152. controls.addEventListener( 'change', function () {
  153. cameraChanged = true;
  154. signals.cameraChanged.dispatch( camera );
  155. render();
  156. } );
  157. // signals
  158. signals.objectAdded.add( function ( object ) {
  159. object.traverse( function ( child ) {
  160. objects.push( child );
  161. } );
  162. scene.add( object );
  163. render();
  164. signals.sceneChanged.dispatch( scene );
  165. } );
  166. signals.objectChanged.add( function ( object ) {
  167. if ( object instanceof THREE.Camera ) {
  168. object.updateProjectionMatrix();
  169. }
  170. render();
  171. } );
  172. signals.removeSelectedObject.add( function () {
  173. if ( selected === camera ) return;
  174. var name = selected.name ? '"' + selected.name + '"': "selected object";
  175. if ( ! confirm( 'Delete ' + name + '?' ) ) {
  176. return;
  177. }
  178. var toRemove = {};
  179. selected.traverse( function ( child ) {
  180. toRemove[ child.id ] = true;
  181. } );
  182. var newObjects = [];
  183. for ( var i = 0; i < objects.length; i ++ ) {
  184. var object = objects[ i ];
  185. if ( ! ( object.id in toRemove ) ) {
  186. newObjects.push( object );
  187. }
  188. }
  189. objects = newObjects;
  190. selectionBox.visible = false;
  191. selectionAxis.visible = false;
  192. scene.traverse( function( node ) {
  193. node.remove( selected );
  194. } );
  195. render();
  196. signals.sceneChanged.dispatch( scene );
  197. signals.objectSelected.dispatch( null );
  198. } );
  199. var selected = camera;
  200. signals.objectSelected.add( function ( object ) {
  201. selectionBox.visible = false;
  202. selectionAxis.visible = false;
  203. if ( object !== null && object.geometry ) {
  204. var geometry = object.geometry;
  205. if ( geometry.boundingBox === null ) {
  206. geometry.computeBoundingBox();
  207. }
  208. selectionBox.geometry.vertices[ 0 ].x = geometry.boundingBox.max.x;
  209. selectionBox.geometry.vertices[ 0 ].y = geometry.boundingBox.max.y;
  210. selectionBox.geometry.vertices[ 0 ].z = geometry.boundingBox.max.z;
  211. selectionBox.geometry.vertices[ 1 ].x = geometry.boundingBox.max.x;
  212. selectionBox.geometry.vertices[ 1 ].y = geometry.boundingBox.max.y;
  213. selectionBox.geometry.vertices[ 1 ].z = geometry.boundingBox.min.z;
  214. selectionBox.geometry.vertices[ 2 ].x = geometry.boundingBox.max.x;
  215. selectionBox.geometry.vertices[ 2 ].y = geometry.boundingBox.min.y;
  216. selectionBox.geometry.vertices[ 2 ].z = geometry.boundingBox.max.z;
  217. selectionBox.geometry.vertices[ 3 ].x = geometry.boundingBox.max.x;
  218. selectionBox.geometry.vertices[ 3 ].y = geometry.boundingBox.min.y;
  219. selectionBox.geometry.vertices[ 3 ].z = geometry.boundingBox.min.z;
  220. selectionBox.geometry.vertices[ 4 ].x = geometry.boundingBox.min.x;
  221. selectionBox.geometry.vertices[ 4 ].y = geometry.boundingBox.max.y;
  222. selectionBox.geometry.vertices[ 4 ].z = geometry.boundingBox.min.z;
  223. selectionBox.geometry.vertices[ 5 ].x = geometry.boundingBox.min.x;
  224. selectionBox.geometry.vertices[ 5 ].y = geometry.boundingBox.max.y;
  225. selectionBox.geometry.vertices[ 5 ].z = geometry.boundingBox.max.z;
  226. selectionBox.geometry.vertices[ 6 ].x = geometry.boundingBox.min.x;
  227. selectionBox.geometry.vertices[ 6 ].y = geometry.boundingBox.min.y;
  228. selectionBox.geometry.vertices[ 6 ].z = geometry.boundingBox.min.z;
  229. selectionBox.geometry.vertices[ 7 ].x = geometry.boundingBox.min.x;
  230. selectionBox.geometry.vertices[ 7 ].y = geometry.boundingBox.min.y;
  231. selectionBox.geometry.vertices[ 7 ].z = geometry.boundingBox.max.z;
  232. selectionBox.geometry.computeBoundingSphere();
  233. selectionBox.geometry.verticesNeedUpdate = true;
  234. selectionBox.matrixWorld = object.matrixWorld;
  235. selectionAxis.matrixWorld = object.matrixWorld;
  236. selectionBox.visible = true;
  237. selectionAxis.visible = true;
  238. }
  239. if ( object !== null ) {
  240. selected = object;
  241. }
  242. render();
  243. } );
  244. signals.materialChanged.add( function ( material ) {
  245. render();
  246. } );
  247. signals.clearColorChanged.add( function ( color ) {
  248. renderer.setClearColorHex( color, 1 );
  249. render();
  250. } );
  251. signals.fogTypeChanged.add( function ( fogType ) {
  252. if ( fogType !== oldFogType ) {
  253. if ( fogType === "None" ) {
  254. scene.fog = null;
  255. } else if ( fogType === "Fog" ) {
  256. scene.fog = new THREE.Fog( oldFogColor, oldFogNear, oldFogFar );
  257. } else if ( fogType === "FogExp2" ) {
  258. scene.fog = new THREE.FogExp2( oldFogColor, oldFogDensity );
  259. }
  260. updateMaterials( scene );
  261. var enableHelpersFog = true;
  262. if ( enableHelpersFog ) {
  263. sceneHelpers.fog = scene.fog;
  264. updateMaterials( sceneHelpers );
  265. }
  266. oldFogType = fogType;
  267. }
  268. render();
  269. } );
  270. signals.fogColorChanged.add( function ( fogColor ) {
  271. oldFogColor = fogColor;
  272. updateFog( scene );
  273. render();
  274. } );
  275. signals.fogParametersChanged.add( function ( near, far, density ) {
  276. oldFogNear = near;
  277. oldFogFar = far;
  278. oldFogDensity = density;
  279. updateFog( scene );
  280. render();
  281. } );
  282. signals.windowResize.add( function () {
  283. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  284. camera.updateProjectionMatrix();
  285. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  286. render();
  287. } );
  288. //
  289. var renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false, clearColor: 0xaaaaaa, clearAlpha: 1 } );
  290. renderer.autoClear = false;
  291. renderer.autoUpdateScene = false;
  292. container.dom.appendChild( renderer.domElement );
  293. animate();
  294. // set up for hotkeys
  295. // must be done here, otherwise it doesn't work
  296. container.dom.tabIndex = 1;
  297. container.dom.addEventListener( 'keydown', onKeyDown, false );
  298. container.dom.className = "hideFocus";
  299. //
  300. function updateMaterials( root ) {
  301. root.traverse( function ( node ) {
  302. if ( node.material ) {
  303. node.material.needsUpdate = true;
  304. }
  305. if ( node.geometry && node.geometry.materials ) {
  306. for ( var i = 0; i < node.geometry.materials.length; i ++ ) {
  307. node.geometry.materials[ i ].needsUpdate = true;
  308. }
  309. }
  310. } );
  311. }
  312. function updateFog( root ) {
  313. if ( root.fog ) {
  314. root.fog.color.setHex( oldFogColor );
  315. if ( root.fog.near !== undefined ) root.fog.near = oldFogNear;
  316. if ( root.fog.far !== undefined ) root.fog.far = oldFogFar;
  317. if ( root.fog.density !== undefined ) root.fog.density = oldFogDensity;
  318. }
  319. }
  320. function animate() {
  321. requestAnimationFrame( animate );
  322. controls.update();
  323. }
  324. function render() {
  325. sceneHelpers.updateMatrixWorld();
  326. scene.updateMatrixWorld();
  327. renderer.clear();
  328. renderer.render( scene, camera );
  329. renderer.render( sceneHelpers, camera );
  330. }
  331. return container;
  332. }