Viewport.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. var Viewport = function ( signals ) {
  2. var container = new UI.Panel( 'absolute' );
  3. container.setBackgroundColor( '#aaa' );
  4. // settings
  5. var enableHelpersFog = true;
  6. // helpers
  7. var objects = [];
  8. var sceneHelpers = new THREE.Scene();
  9. var size = 500, step = 25;
  10. var geometry = new THREE.Geometry();
  11. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  12. var color1 = new THREE.Color( 0x444444 ), color2 = new THREE.Color( 0x888888 );
  13. for ( var i = - size; i <= size; i += step ) {
  14. geometry.vertices.push( new THREE.Vector3( -size, 0, i ) );
  15. geometry.vertices.push( new THREE.Vector3( size, 0, i ) );
  16. geometry.vertices.push( new THREE.Vector3( i, 0, -size ) );
  17. geometry.vertices.push( new THREE.Vector3( i, 0, size ) );
  18. var color = i === 0 ? color1 : color2;
  19. geometry.colors.push( color, color, color, color );
  20. }
  21. var grid = new THREE.Line( geometry, material, THREE.LinePieces );
  22. sceneHelpers.add( grid );
  23. var selectionBox = new THREE.Mesh( new THREE.CubeGeometry( 1, 1, 1 ), new THREE.MeshBasicMaterial( { color: 0xffff00, wireframe: true, fog: false } ) );
  24. selectionBox.matrixAutoUpdate = false;
  25. selectionBox.visible = false;
  26. sceneHelpers.add( selectionBox );
  27. var selectionAxis = new THREE.AxisHelper( 100 );
  28. selectionAxis.material.depthTest = false;
  29. selectionAxis.material.transparent = true;
  30. selectionAxis.matrixAutoUpdate = false;
  31. selectionAxis.visible = false;
  32. sceneHelpers.add( selectionAxis );
  33. //
  34. var scene = new THREE.Scene();
  35. var camera = new THREE.PerspectiveCamera( 50, 1, 1, 5000 );
  36. camera.position.set( 500, 250, 500 );
  37. camera.lookAt( scene.position );
  38. scene.add( camera );
  39. var light1 = new THREE.DirectionalLight( 0xffffff );
  40. light1.position.set( 1, 0.5, 0 ).multiplyScalar( 400 );
  41. var light2 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  42. light2.position.set( - 1, - 0.5, 0 ).multiplyScalar( 400 );
  43. light1.target.properties.targetInverse = light1;
  44. light2.target.properties.targetInverse = light2;
  45. var light3 = new THREE.PointLight( 0xffaa00, 0.75 );
  46. light3.position.set( 0, -200, 0 );
  47. var light4 = new THREE.AmbientLight( 0x111111 );
  48. // fog
  49. var oldFogType = "None";
  50. var oldFogColor = 0xaaaaaa;
  51. var oldFogNear = 1;
  52. var oldFogFar = 5000;
  53. var oldFogDensity = 0.00025;
  54. // default objects names
  55. camera.name = "Camera";
  56. light1.name = "Light 1";
  57. light1.target.name = "Light 1 Target";
  58. light2.name = "Light 2";
  59. light2.target.name = "Light 2 Target";
  60. light3.name = "Light 3";
  61. light4.name = "Light 4";
  62. // active objects
  63. camera.properties.active = true;
  64. // object picking
  65. var intersectionPlane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ) );
  66. intersectionPlane.visible = false;
  67. sceneHelpers.add( intersectionPlane );
  68. var ray = new THREE.Ray();
  69. var projector = new THREE.Projector();
  70. var offset = new THREE.Vector3();
  71. var cameraChanged = false;
  72. var helpersVisible = true;
  73. //
  74. var picked = null;
  75. var selected = camera;
  76. // events
  77. var onMouseDown = function ( event ) {
  78. container.dom.focus();
  79. event.preventDefault();
  80. if ( event.button === 0 ) {
  81. var vector = new THREE.Vector3(
  82. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  83. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  84. 0.5
  85. );
  86. projector.unprojectVector( vector, camera );
  87. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  88. var intersects = ray.intersectObjects( objects, true );
  89. if ( intersects.length > 0 ) {
  90. controls.enabled = false;
  91. picked = intersects[ 0 ].object;
  92. var root;
  93. if ( picked.properties.isGizmo ) {
  94. root = picked.properties.gizmoRoot;
  95. selected = picked.properties.gizmoSubject;
  96. } else {
  97. root = picked;
  98. selected = picked;
  99. }
  100. intersectionPlane.position.copy( root.position );
  101. intersectionPlane.lookAt( camera.position );
  102. signals.objectSelected.dispatch( selected );
  103. var intersects = ray.intersectObject( intersectionPlane );
  104. offset.copy( intersects[ 0 ].point ).subSelf( intersectionPlane.position );
  105. document.addEventListener( 'mousemove', onMouseMove, false );
  106. document.addEventListener( 'mouseup', onMouseUp, false );
  107. } else {
  108. controls.enabled = true;
  109. }
  110. }
  111. cameraChanged = false;
  112. };
  113. var onMouseMove = function ( event ) {
  114. var vector = new THREE.Vector3(
  115. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  116. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  117. 0.5
  118. );
  119. projector.unprojectVector( vector, camera );
  120. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  121. var intersects = ray.intersectObject( intersectionPlane );
  122. if ( intersects.length > 0 ) {
  123. intersects[ 0 ].point.subSelf( offset );
  124. if ( picked.properties.isGizmo ) {
  125. picked.properties.gizmoRoot.position.copy( intersects[ 0 ].point );
  126. picked.properties.gizmoSubject.position.copy( intersects[ 0 ].point );
  127. signals.objectChanged.dispatch( picked.properties.gizmoSubject );
  128. } else {
  129. picked.position.copy( intersects[ 0 ].point );
  130. signals.objectChanged.dispatch( picked );
  131. }
  132. render();
  133. }
  134. };
  135. var onMouseUp = function ( event ) {
  136. document.removeEventListener( 'mousemove', onMouseMove );
  137. document.removeEventListener( 'mouseup', onMouseUp );
  138. };
  139. var onClick = function ( event ) {
  140. if ( event.button == 0 && cameraChanged === false ) {
  141. var vector = new THREE.Vector3(
  142. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  143. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  144. 0.5
  145. );
  146. projector.unprojectVector( vector, camera );
  147. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  148. var intersects = ray.intersectObjects( objects, true );
  149. if ( intersects.length > 0 && ! controls.enabled ) {
  150. selected = intersects[ 0 ].object;
  151. } else {
  152. selected = camera;
  153. }
  154. if ( selected.properties.isGizmo ) {
  155. signals.objectSelected.dispatch( selected.properties.gizmoSubject );
  156. } else {
  157. signals.objectSelected.dispatch( selected );
  158. }
  159. }
  160. controls.enabled = true;
  161. };
  162. var onKeyDown = function ( event ) {
  163. switch ( event.keyCode ) {
  164. case 46: // delete
  165. signals.removeSelectedObject.dispatch();
  166. break;
  167. case 72: // h
  168. signals.toggleHelpers.dispatch();
  169. break;
  170. }
  171. };
  172. container.dom.addEventListener( 'mousedown', onMouseDown, false );
  173. container.dom.addEventListener( 'click', onClick, false );
  174. // controls need to be added *after* main logic,
  175. // otherwise controls.enabled doesn't work.
  176. var controls = new THREE.TrackballControls( camera, container.dom );
  177. controls.rotateSpeed = 1.0;
  178. controls.zoomSpeed = 1.2;
  179. controls.panSpeed = 0.8;
  180. controls.noZoom = false;
  181. controls.noPan = false;
  182. controls.staticMoving = true;
  183. controls.dynamicDampingFactor = 0.3;
  184. controls.addEventListener( 'change', function () {
  185. cameraChanged = true;
  186. signals.cameraChanged.dispatch( camera );
  187. render();
  188. } );
  189. var handleAddition = function ( object ) {
  190. // add to picking list
  191. objects.push( object );
  192. // create helpers for invisible object types (lights, cameras, targets)
  193. if ( object instanceof THREE.DirectionalLight ) {
  194. var sphereSize = 5;
  195. var arrowLength = 30;
  196. var lightGizmo = new THREE.DirectionalLightHelper( object, sphereSize, arrowLength );
  197. sceneHelpers.add( lightGizmo );
  198. sceneHelpers.add( lightGizmo.targetSphere );
  199. sceneHelpers.add( lightGizmo.targetLine );
  200. object.properties.helper = lightGizmo;
  201. object.properties.pickingProxy = lightGizmo.lightSphere;
  202. object.target.properties.pickingProxy = lightGizmo.targetSphere;
  203. objects.push( lightGizmo.lightSphere );
  204. objects.push( lightGizmo.targetSphere );
  205. objects.push( lightGizmo.targetLine );
  206. } else if ( object instanceof THREE.PointLight ) {
  207. var sphereSize = 5;
  208. var lightGizmo = new THREE.PointLightHelper( object, sphereSize );
  209. sceneHelpers.add( lightGizmo );
  210. object.properties.helper = lightGizmo;
  211. object.properties.pickingProxy = lightGizmo.lightSphere;
  212. objects.push( lightGizmo.lightSphere );
  213. } else if ( object instanceof THREE.SpotLight ) {
  214. //var lightGizmo = new THREE.SpotLightHelper( object );
  215. //sceneHelpers.add( lightGizmo );
  216. } else if ( object instanceof THREE.HemisphereLight ) {
  217. //var lightGizmo = new THREE.HemisphereLightHelper( object );
  218. //sceneHelpers.add( lightGizmo );
  219. }
  220. };
  221. // signals
  222. signals.objectAdded.add( function ( object ) {
  223. object.traverse( handleAddition );
  224. scene.add( object );
  225. render();
  226. signals.sceneChanged.dispatch( scene );
  227. } );
  228. signals.objectChanged.add( function ( object ) {
  229. if ( object instanceof THREE.Camera ) {
  230. object.updateProjectionMatrix();
  231. } else if ( object instanceof THREE.DirectionalLight ) {
  232. object.properties.helper.update();
  233. } else if ( object instanceof THREE.PointLight ) {
  234. object.properties.helper.update();
  235. } else if ( object instanceof THREE.SpotLight ) {
  236. } else if ( object instanceof THREE.HemisphereLight ) {
  237. } else if ( object.properties.targetInverse ) {
  238. object.properties.targetInverse.properties.helper.update();
  239. }
  240. render();
  241. } );
  242. signals.removeSelectedObject.add( function () {
  243. if ( selected === camera ) return;
  244. var name = selected.name ? '"' + selected.name + '"': "selected object";
  245. if ( ! confirm( 'Delete ' + name + '?' ) ) {
  246. return;
  247. }
  248. // remove from picking list
  249. var toRemove = {};
  250. selected.traverse( function ( child ) {
  251. toRemove[ child.id ] = true;
  252. } );
  253. var newObjects = [];
  254. for ( var i = 0; i < objects.length; i ++ ) {
  255. var object = objects[ i ];
  256. if ( ! ( object.id in toRemove ) ) {
  257. newObjects.push( object );
  258. }
  259. }
  260. objects = newObjects;
  261. //
  262. selectionBox.visible = false;
  263. selectionAxis.visible = false;
  264. scene.traverse( function( node ) {
  265. node.remove( selected );
  266. } );
  267. render();
  268. signals.sceneChanged.dispatch( scene );
  269. signals.objectSelected.dispatch( null );
  270. } );
  271. signals.objectSelected.add( function ( object ) {
  272. selectionBox.visible = false;
  273. selectionAxis.visible = false;
  274. var geometry;
  275. var hasRotation;
  276. if ( object !== null ) {
  277. selected = object;
  278. if ( object.geometry ) {
  279. geometry = object.geometry;
  280. hasRotation = true;
  281. } else if ( object.properties.pickingProxy ) {
  282. geometry = object.properties.pickingProxy.geometry;
  283. hasRotation = false;
  284. }
  285. }
  286. if ( geometry ) {
  287. if ( geometry.boundingBox === null ) {
  288. geometry.computeBoundingBox();
  289. }
  290. var vertices = selectionBox.geometry.vertices;
  291. vertices[ 0 ].x = geometry.boundingBox.max.x;
  292. vertices[ 0 ].y = geometry.boundingBox.max.y;
  293. vertices[ 0 ].z = geometry.boundingBox.max.z;
  294. vertices[ 1 ].x = geometry.boundingBox.max.x;
  295. vertices[ 1 ].y = geometry.boundingBox.max.y;
  296. vertices[ 1 ].z = geometry.boundingBox.min.z;
  297. vertices[ 2 ].x = geometry.boundingBox.max.x;
  298. vertices[ 2 ].y = geometry.boundingBox.min.y;
  299. vertices[ 2 ].z = geometry.boundingBox.max.z;
  300. vertices[ 3 ].x = geometry.boundingBox.max.x;
  301. vertices[ 3 ].y = geometry.boundingBox.min.y;
  302. vertices[ 3 ].z = geometry.boundingBox.min.z;
  303. vertices[ 4 ].x = geometry.boundingBox.min.x;
  304. vertices[ 4 ].y = geometry.boundingBox.max.y;
  305. vertices[ 4 ].z = geometry.boundingBox.min.z;
  306. vertices[ 5 ].x = geometry.boundingBox.min.x;
  307. vertices[ 5 ].y = geometry.boundingBox.max.y;
  308. vertices[ 5 ].z = geometry.boundingBox.max.z;
  309. vertices[ 6 ].x = geometry.boundingBox.min.x;
  310. vertices[ 6 ].y = geometry.boundingBox.min.y;
  311. vertices[ 6 ].z = geometry.boundingBox.min.z;
  312. vertices[ 7 ].x = geometry.boundingBox.min.x;
  313. vertices[ 7 ].y = geometry.boundingBox.min.y;
  314. vertices[ 7 ].z = geometry.boundingBox.max.z;
  315. selectionBox.geometry.computeBoundingSphere();
  316. selectionBox.geometry.verticesNeedUpdate = true;
  317. selectionBox.matrixWorld = object.matrixWorld;
  318. selectionAxis.matrixWorld = object.matrixWorld;
  319. selectionBox.visible = true;
  320. }
  321. if ( hasRotation ) {
  322. selectionAxis.visible = true;
  323. }
  324. render();
  325. } );
  326. signals.materialChanged.add( function ( material ) {
  327. render();
  328. } );
  329. signals.clearColorChanged.add( function ( color ) {
  330. renderer.setClearColorHex( color, 1 );
  331. render();
  332. } );
  333. signals.fogTypeChanged.add( function ( fogType ) {
  334. if ( fogType !== oldFogType ) {
  335. if ( fogType === "None" ) {
  336. scene.fog = null;
  337. } else if ( fogType === "Fog" ) {
  338. scene.fog = new THREE.Fog( oldFogColor, oldFogNear, oldFogFar );
  339. } else if ( fogType === "FogExp2" ) {
  340. scene.fog = new THREE.FogExp2( oldFogColor, oldFogDensity );
  341. }
  342. updateMaterials( scene );
  343. if ( enableHelpersFog ) {
  344. sceneHelpers.fog = scene.fog;
  345. updateMaterials( sceneHelpers );
  346. }
  347. oldFogType = fogType;
  348. }
  349. render();
  350. } );
  351. signals.fogColorChanged.add( function ( fogColor ) {
  352. oldFogColor = fogColor;
  353. updateFog( scene );
  354. render();
  355. } );
  356. signals.fogParametersChanged.add( function ( near, far, density ) {
  357. oldFogNear = near;
  358. oldFogFar = far;
  359. oldFogDensity = density;
  360. updateFog( scene );
  361. render();
  362. } );
  363. signals.windowResize.add( function () {
  364. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  365. camera.updateProjectionMatrix();
  366. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  367. render();
  368. } );
  369. signals.exportScene.add( function ( root ) {
  370. var clearColor = renderer.getClearColor();
  371. var clearAlpha = renderer.getClearAlpha();
  372. var output = new THREE.SceneExporter().parse( root, clearColor, clearAlpha );
  373. var blob = new Blob( [ output ], { type: 'text/plain' } );
  374. var objectURL = URL.createObjectURL( blob );
  375. window.open( objectURL, '_blank' );
  376. window.focus();
  377. } );
  378. signals.toggleHelpers.add( function () {
  379. helpersVisible = !helpersVisible;
  380. render();
  381. } );
  382. signals.resetScene.add( function ( newScene, newCamera, newClearColor ) {
  383. scene = newScene;
  384. // remove old gizmos
  385. var toRemove = {};
  386. sceneHelpers.traverse( function ( child ) {
  387. if ( child.properties.isGizmo ) {
  388. toRemove[ child.id ] = child;
  389. }
  390. } );
  391. for ( var id in toRemove ) {
  392. sceneHelpers.remove( toRemove[ id ] );
  393. }
  394. // reset picking list
  395. objects = [];
  396. // add new gizmos and fill picking list
  397. scene.traverse( handleAddition );
  398. //
  399. if ( newCamera ) {
  400. camera = newCamera;
  401. camera.properties.active = true;
  402. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  403. camera.updateProjectionMatrix();
  404. controls.object = camera;
  405. controls.update();
  406. }
  407. if ( newClearColor ) {
  408. signals.clearColorChanged.dispatch( newClearColor.getHex() );
  409. }
  410. if ( newScene.fog ) {
  411. oldFogColor = newScene.fog.color.getHex();
  412. if ( newScene.fog instanceof THREE.Fog ) {
  413. oldFogType = "Fog";
  414. oldFogNear = newScene.fog.near;
  415. oldFogFar = newScene.fog.far;
  416. } else if ( newScene.fog instanceof THREE.FogExp2 ) {
  417. oldFogType = "FogExp2";
  418. oldFogDensity = newScene.fog.density;
  419. }
  420. } else {
  421. oldFogType = "None";
  422. }
  423. if ( enableHelpersFog ) {
  424. sceneHelpers.fog = scene.fog;
  425. updateMaterials( sceneHelpers );
  426. }
  427. signals.sceneChanged.dispatch( scene );
  428. signals.objectSelected.dispatch( null );
  429. } );
  430. //
  431. var renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false, clearColor: 0xaaaaaa, clearAlpha: 1 } );
  432. renderer.autoClear = false;
  433. renderer.autoUpdateScene = false;
  434. container.dom.appendChild( renderer.domElement );
  435. animate();
  436. // set up for hotkeys
  437. // must be done here, otherwise it doesn't work
  438. container.dom.tabIndex = 1;
  439. container.dom.style.outline = 'transparent';
  440. container.dom.addEventListener( 'keydown', onKeyDown, false );
  441. // must come after listeners are registered
  442. signals.sceneChanged.dispatch( scene );
  443. signals.objectAdded.dispatch( light1 );
  444. signals.objectAdded.dispatch( light2 );
  445. signals.objectAdded.dispatch( light3 );
  446. signals.objectAdded.dispatch( light4 );
  447. //
  448. function updateMaterials( root ) {
  449. root.traverse( function ( node ) {
  450. if ( node.material ) {
  451. node.material.needsUpdate = true;
  452. }
  453. if ( node.geometry && node.geometry.materials ) {
  454. for ( var i = 0; i < node.geometry.materials.length; i ++ ) {
  455. node.geometry.materials[ i ].needsUpdate = true;
  456. }
  457. }
  458. } );
  459. }
  460. function updateFog( root ) {
  461. if ( root.fog ) {
  462. root.fog.color.setHex( oldFogColor );
  463. if ( root.fog.near !== undefined ) root.fog.near = oldFogNear;
  464. if ( root.fog.far !== undefined ) root.fog.far = oldFogFar;
  465. if ( root.fog.density !== undefined ) root.fog.density = oldFogDensity;
  466. }
  467. }
  468. function animate() {
  469. requestAnimationFrame( animate );
  470. controls.update();
  471. }
  472. function render() {
  473. sceneHelpers.updateMatrixWorld();
  474. scene.updateMatrixWorld();
  475. renderer.clear();
  476. renderer.render( scene, camera );
  477. if ( helpersVisible ) {
  478. renderer.render( sceneHelpers, camera );
  479. }
  480. }
  481. return container;
  482. }