Viewport.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. // default objects names
  44. camera.name = "Camera";
  45. light1.name = "Light 1";
  46. light1.target.name = "Light 1 Target";
  47. light2.name = "Light 2";
  48. light2.target.name = "Light 2 Target";
  49. signals.sceneChanged.dispatch( scene );
  50. // object picking
  51. var intersectionPlane = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000, 8, 8 ) );
  52. intersectionPlane.visible = false;
  53. sceneHelpers.add( intersectionPlane );
  54. var ray = new THREE.Ray();
  55. var projector = new THREE.Projector();
  56. var offset = new THREE.Vector3();
  57. var picked = null;
  58. // events
  59. var onMouseDown = function ( event ) {
  60. event.preventDefault();
  61. if ( event.button === 0 ) {
  62. var vector = new THREE.Vector3(
  63. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  64. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  65. 0.5
  66. );
  67. projector.unprojectVector( vector, camera );
  68. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  69. var intersects = ray.intersectObjects( objects, true );
  70. if ( intersects.length > 0 ) {
  71. controls.enabled = false;
  72. intersectionPlane.position.copy( intersects[ 0 ].object.position );
  73. intersectionPlane.lookAt( camera.position );
  74. picked = intersects[ 0 ].object;
  75. selected = picked;
  76. signals.objectSelected.dispatch( selected );
  77. var intersects = ray.intersectObject( intersectionPlane );
  78. offset.copy( intersects[ 0 ].point ).subSelf( intersectionPlane.position );
  79. document.addEventListener( 'mousemove', onMouseMove, false );
  80. document.addEventListener( 'mouseup', onMouseUp, false );
  81. } else {
  82. controls.enabled = true;
  83. }
  84. }
  85. };
  86. var onMouseMove = function ( event ) {
  87. var vector = new THREE.Vector3(
  88. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  89. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  90. 0.5
  91. );
  92. projector.unprojectVector( vector, camera );
  93. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  94. var intersects = ray.intersectObject( intersectionPlane );
  95. if ( intersects.length > 0 ) {
  96. picked.position.copy( intersects[ 0 ].point.subSelf( offset ) );
  97. signals.objectChanged.dispatch( picked );
  98. render();
  99. }
  100. };
  101. var onMouseUp = function ( event ) {
  102. document.removeEventListener( 'mousemove', onMouseMove );
  103. document.removeEventListener( 'mouseup', onMouseUp );
  104. };
  105. var onClick = function ( event ) {
  106. if ( event.button === 0 ) {
  107. var vector = new THREE.Vector3(
  108. ( ( event.clientX - container.dom.offsetLeft ) / container.dom.offsetWidth ) * 2 - 1,
  109. - ( ( event.clientY - container.dom.offsetTop ) / container.dom.offsetHeight ) * 2 + 1,
  110. 0.5
  111. );
  112. projector.unprojectVector( vector, camera );
  113. ray.set( camera.position, vector.subSelf( camera.position ).normalize() );
  114. var intersects = ray.intersectObjects( objects, true );
  115. if ( intersects.length > 0 ) {
  116. selected = intersects[ 0 ].object;
  117. } else {
  118. selected = null;
  119. }
  120. signals.objectSelected.dispatch( selected );
  121. }
  122. controls.enabled = true;
  123. };
  124. container.dom.addEventListener( 'mousedown', onMouseDown, false );
  125. container.dom.addEventListener( 'click', onClick, false );
  126. // controls need to be added *after* main logic,
  127. // otherwise controls.enabled doesn't work.
  128. var controls = new THREE.TrackballControls( camera, container.dom );
  129. controls.rotateSpeed = 1.0;
  130. controls.zoomSpeed = 1.2;
  131. controls.panSpeed = 0.8;
  132. controls.noZoom = false;
  133. controls.noPan = false;
  134. controls.staticMoving = true;
  135. controls.dynamicDampingFactor = 0.3;
  136. controls.addEventListener( 'change', render );
  137. // signals
  138. signals.objectAdded.add( function ( object ) {
  139. object.traverse( function ( child ) {
  140. objects.push( child );
  141. } );
  142. scene.add( object );
  143. render();
  144. signals.sceneChanged.dispatch( scene );
  145. } );
  146. signals.objectChanged.add( function ( object ) {
  147. if ( object instanceof THREE.Camera ) {
  148. object.updateProjectionMatrix();
  149. }
  150. render();
  151. } );
  152. signals.objectRemoved.add( function ( ) {
  153. if ( !selected ) {
  154. console.warn( "No object selected for delete" );
  155. return;
  156. }
  157. var toRemove = {};
  158. selected.traverse( function ( child ) {
  159. toRemove[ child.id ] = true;
  160. } );
  161. var newObjects = [];
  162. for ( var i = 0; i < objects.length; i ++ ) {
  163. var object = objects[ i ];
  164. if ( ! ( object.id in toRemove ) ) {
  165. newObjects.push( object );
  166. }
  167. }
  168. objects = newObjects;
  169. selectionBox.visible = false;
  170. selectionAxis.visible = false;
  171. scene.traverse( function( node ) {
  172. node.remove( selected );
  173. } );
  174. render();
  175. signals.sceneChanged.dispatch( scene );
  176. signals.objectSelected.dispatch( null );
  177. } );
  178. var selected = null;
  179. signals.objectSelected.add( function ( object ) {
  180. selectionBox.visible = false;
  181. selectionAxis.visible = false;
  182. if ( object !== null && object.geometry ) {
  183. var geometry = object.geometry;
  184. if ( geometry.boundingBox === null ) {
  185. geometry.computeBoundingBox();
  186. }
  187. selectionBox.geometry.vertices[ 0 ].x = geometry.boundingBox.max.x;
  188. selectionBox.geometry.vertices[ 0 ].y = geometry.boundingBox.max.y;
  189. selectionBox.geometry.vertices[ 0 ].z = geometry.boundingBox.max.z;
  190. selectionBox.geometry.vertices[ 1 ].x = geometry.boundingBox.max.x;
  191. selectionBox.geometry.vertices[ 1 ].y = geometry.boundingBox.max.y;
  192. selectionBox.geometry.vertices[ 1 ].z = geometry.boundingBox.min.z;
  193. selectionBox.geometry.vertices[ 2 ].x = geometry.boundingBox.max.x;
  194. selectionBox.geometry.vertices[ 2 ].y = geometry.boundingBox.min.y;
  195. selectionBox.geometry.vertices[ 2 ].z = geometry.boundingBox.max.z;
  196. selectionBox.geometry.vertices[ 3 ].x = geometry.boundingBox.max.x;
  197. selectionBox.geometry.vertices[ 3 ].y = geometry.boundingBox.min.y;
  198. selectionBox.geometry.vertices[ 3 ].z = geometry.boundingBox.min.z;
  199. selectionBox.geometry.vertices[ 4 ].x = geometry.boundingBox.min.x;
  200. selectionBox.geometry.vertices[ 4 ].y = geometry.boundingBox.max.y;
  201. selectionBox.geometry.vertices[ 4 ].z = geometry.boundingBox.min.z;
  202. selectionBox.geometry.vertices[ 5 ].x = geometry.boundingBox.min.x;
  203. selectionBox.geometry.vertices[ 5 ].y = geometry.boundingBox.max.y;
  204. selectionBox.geometry.vertices[ 5 ].z = geometry.boundingBox.max.z;
  205. selectionBox.geometry.vertices[ 6 ].x = geometry.boundingBox.min.x;
  206. selectionBox.geometry.vertices[ 6 ].y = geometry.boundingBox.min.y;
  207. selectionBox.geometry.vertices[ 6 ].z = geometry.boundingBox.min.z;
  208. selectionBox.geometry.vertices[ 7 ].x = geometry.boundingBox.min.x;
  209. selectionBox.geometry.vertices[ 7 ].y = geometry.boundingBox.min.y;
  210. selectionBox.geometry.vertices[ 7 ].z = geometry.boundingBox.max.z;
  211. selectionBox.geometry.computeBoundingSphere();
  212. selectionBox.geometry.verticesNeedUpdate = true;
  213. selectionBox.matrixWorld = object.matrixWorld;
  214. selectionAxis.matrixWorld = object.matrixWorld;
  215. selectionBox.visible = true;
  216. selectionAxis.visible = true;
  217. }
  218. if ( object !== null ) {
  219. selected = object;
  220. }
  221. render();
  222. } );
  223. signals.materialChanged.add( function ( material ) {
  224. render();
  225. } );
  226. signals.windowResize.add( function () {
  227. camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
  228. camera.updateProjectionMatrix();
  229. renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
  230. render();
  231. } );
  232. //
  233. var renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false, clearColor: 0xaaaaaa, clearAlpha: 1 } );
  234. renderer.autoClear = false;
  235. renderer.autoUpdateScene = false;
  236. container.dom.appendChild( renderer.domElement );
  237. animate();
  238. //
  239. function animate() {
  240. requestAnimationFrame( animate );
  241. controls.update();
  242. }
  243. function render() {
  244. sceneHelpers.updateMatrixWorld();
  245. scene.updateMatrixWorld();
  246. renderer.clear();
  247. renderer.render( scene, camera );
  248. renderer.render( sceneHelpers, camera );
  249. }
  250. return container;
  251. }