123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- var Viewport = function ( editor, signals ) {
- var container = new UI.Panel();
- container.setPosition( 'absolute' );
- container.setBackgroundColor( '#aaa' );
- var info = new UI.Text();
- info.setPosition( 'absolute' );
- info.setRight( '5px' );
- info.setBottom( '5px' );
- info.setFontSize( '12px' );
- info.setColor( '#ffffff' );
- container.add( info );
- var scene = editor.scene;
- var sceneHelpers = editor.sceneHelpers;
- var clearColor = 0xAAAAAA;
- var grid = new THREE.GridHelper( 500, 25 );
- sceneHelpers.add( grid );
- //
- var camera = new THREE.PerspectiveCamera( 50, container.dom.offsetWidth / container.dom.offsetHeight, 1, 5000 );
- camera.position.set( 500, 250, 500 );
- camera.lookAt( scene.position );
- editor.select( camera );
- //
- var selectionBox = new THREE.BoxHelper();
- selectionBox.material.depthTest = false;
- selectionBox.material.transparent = true;
- selectionBox.visible = false;
- sceneHelpers.add( selectionBox );
- //
- var transformControls = new THREE.TransformControls( camera, container.dom );
- transformControls.addEventListener( 'change', function () {
- signals.objectChanged.dispatch( this.object );
- } );
- sceneHelpers.add( transformControls.gizmo );
- transformControls.hide();
- //
- var selected;
- // object picking
- var ray = new THREE.Raycaster();
- var projector = new THREE.Projector();
- // events
- var getIntersects = function ( event, object ) {
- var vector = new THREE.Vector3(
- ( event.layerX / container.dom.offsetWidth ) * 2 - 1,
- - ( event.layerY / container.dom.offsetHeight ) * 2 + 1,
- 0.5
- );
- projector.unprojectVector( vector, camera );
- ray.set( camera.position, vector.sub( camera.position ).normalize() );
- if ( object instanceof Array ) {
- return ray.intersectObjects( object, true );
- }
- return ray.intersectObject( object, true );
- };
- var onMouseDownPosition = new THREE.Vector2();
- var onMouseUpPosition = new THREE.Vector2();
- var onMouseDown = function ( event ) {
- event.preventDefault();
- onMouseDownPosition.set( event.layerX, event.layerY );
- if ( transformControls.hovered === false ) {
- controls.enabled = true;
- document.addEventListener( 'mouseup', onMouseUp, false );
- }
- };
- var onMouseUp = function ( event ) {
- onMouseUpPosition.set( event.layerX, event.layerY );
- if ( onMouseDownPosition.distanceTo( onMouseUpPosition ) < 1 ) {
- var hit;
- var intersect = getIntersects( event, [ scene, sceneHelpers ] );
- for ( var i in intersect ) {
- if ( editor.objects[ intersect[i].object.id ] ) {
- editor.selectById( intersect[i].object.id );
- hit = true;
- break;
- }
- }
- if ( !hit ) editor.deselectAll();
- }
- controls.enabled = false; // ?
- document.removeEventListener( 'mouseup', onMouseUp );
- };
- var onDoubleClick = function ( event ) {
- var intersect = getIntersects( event, [ scene, sceneHelpers ] );
- for ( var i in intersect ) {
- if ( editor.objects[ intersect[i].object.id ] ) {
- editor.selectById( intersect[i].object.id );
- controls.focus( editor.objects[ intersect[i].object.id ] );
- break;
- }
- }
- };
- container.dom.addEventListener( 'mousedown', onMouseDown, false );
- container.dom.addEventListener( 'dblclick', onDoubleClick, false );
- // controls need to be added *after* main logic,
- // otherwise controls.enabled doesn't work.
- var controls = new THREE.EditorControls( camera, container.dom );
- controls.addEventListener( 'change', function () {
- transformControls.update();
- signals.objectChanged.dispatch( camera );
- } );
- controls.enabled = false;
- function updateHelpers( object ) {
- if ( object.geometry !== undefined ) {
- selectionBox.visible = true;
- selectionBox.update( object );
- transformControls.update();
- }
- if ( editor.helpers[ object.id ] ) editor.helpers[ object.id ].update();
- }
- // signals
- signals.setTransformMode.add( function ( mode ) {
- transformControls.setMode( mode );
- render();
- } );
- signals.snapChanged.add( function ( dist ) {
- transformControls.snapDist = dist;
- } );
- signals.setRenderer.add( function ( object ) {
- container.dom.removeChild( renderer.domElement );
- renderer = object;
- renderer.setClearColor( clearColor );
- renderer.autoClear = false;
- renderer.autoUpdateScene = false;
- renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
- container.dom.appendChild( renderer.domElement );
- render();
- } );
- signals.selected.add( function () {
- selectionBox.visible = false;
- transformControls.detach();
- selected = editor.listSelected( 'object' );
- object = ( selected.length ) ? selected[0] : null;
- if ( object && object !== scene ) {
- transformControls.attach( object );
- updateHelpers( object );
- }
- render();
- } );
- signals.sceneChanged.add( function () {
- render();
- } );
- signals.objectAdded.add( function ( object ) {
- updateHelpers( object );
- updateInfo();
- render();
- } );
- signals.objectChanged.add( function ( object ) {
- updateHelpers( object );
- transformControls.update();
- updateInfo();
- render();
- } );
- signals.objectDeleted.add( function () {
- updateInfo();
- render();
- } );
- signals.materialChanged.add( function ( material ) {
- render();
- } );
- signals.geometryChanged.add( function ( material ) {
- updateInfo();
- render();
- } );
- signals.clearColorChanged.add( function ( color ) {
- renderer.setClearColor( color );
- render();
- clearColor = color;
- } );
- signals.fogChanged.add( function () {
- render();
- } );
- signals.windowResize.add( function () {
- camera.aspect = container.dom.offsetWidth / container.dom.offsetHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
- render();
- } );
- signals.playAnimations.add( function (animations) {
- function animate() {
- requestAnimationFrame( animate );
- for (var i = 0; i < animations.length ; i++ ){
- animations[i].update(0.016);
- }
- render();
- }
- animate();
- } );
-
- //
- var renderer;
- if ( System.support.webgl === true ) {
- renderer = new THREE.WebGLRenderer( { antialias: true, alpha: false } );
- } else {
- renderer = new THREE.CanvasRenderer();
- }
- renderer.setClearColor( clearColor );
- renderer.autoClear = false;
- renderer.autoUpdateScene = false;
- container.dom.appendChild( renderer.domElement );
- animate();
- //
- function updateInfo() {
- var objects = 0;
- var vertices = 0;
- var faces = 0;
- scene.traverse( function ( object ) {
- if ( object instanceof THREE.Mesh ) {
- objects ++;
- vertices += object.geometry.vertices.length;
- faces += object.geometry.faces.length;
- }
- } );
- info.setValue( 'objects: ' + objects + ', vertices: ' + vertices + ', faces: ' + faces );
- }
- function animate() {
- requestAnimationFrame( animate );
- }
- function render() {
- sceneHelpers.updateMatrixWorld();
- scene.updateMatrixWorld();
- renderer.clear();
- renderer.render( scene, camera );
- renderer.render( sceneHelpers, camera );
- }
- return container;
- }
|