123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- import * as THREE from '../../../build/three.module.js';
- import { RGBELoader } from '../../../examples/jsm/loaders/RGBELoader.js';
- import { TGALoader } from '../../../examples/jsm/loaders/TGALoader.js';
- import { UIElement, UISpan, UIDiv, UIRow, UIButton, UICheckbox, UIText, UINumber } from './ui.js';
- import { MoveObjectCommand } from '../commands/MoveObjectCommand.js';
- class UITexture extends UISpan {
- constructor( mapping ) {
- super();
- const scope = this;
- const form = document.createElement( 'form' );
- const input = document.createElement( 'input' );
- input.type = 'file';
- input.addEventListener( 'change', function ( event ) {
- loadFile( event.target.files[ 0 ] );
- } );
- form.appendChild( input );
- const canvas = document.createElement( 'canvas' );
- canvas.width = 32;
- canvas.height = 16;
- canvas.style.cursor = 'pointer';
- canvas.style.marginRight = '5px';
- canvas.style.border = '1px solid #888';
- canvas.addEventListener( 'click', function () {
- input.click();
- }, false );
- canvas.addEventListener( 'drop', function () {
- event.preventDefault();
- event.stopPropagation();
- loadFile( event.dataTransfer.files[ 0 ] );
- }, false );
- this.dom.appendChild( canvas );
- function loadFile( file ) {
- const extension = file.name.split( '.' ).pop().toLowerCase();
- const reader = new FileReader();
- if ( extension === 'hdr' ) {
- reader.addEventListener( 'load', function ( event ) {
- // assuming RGBE/Radiance HDR iamge format
- const loader = new RGBELoader().setDataType( THREE.UnsignedByteType );
- loader.load( event.target.result, function ( hdrTexture ) {
- hdrTexture.sourceFile = file.name;
- hdrTexture.isHDRTexture = true;
- scope.setValue( hdrTexture );
- if ( scope.onChangeCallback ) scope.onChangeCallback( hdrTexture );
- } );
- } );
- reader.readAsDataURL( file );
- } else if ( extension === 'tga' ) {
- reader.addEventListener( 'load', function ( event ) {
- const canvas = new TGALoader().parse( event.target.result );
- const texture = new THREE.CanvasTexture( canvas, mapping );
- texture.sourceFile = file.name;
- scope.setValue( texture );
- if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
- }, false );
- reader.readAsArrayBuffer( file );
- } else if ( file.type.match( 'image.*' ) ) {
- reader.addEventListener( 'load', function ( event ) {
- const image = document.createElement( 'img' );
- image.addEventListener( 'load', function () {
- const texture = new THREE.Texture( this, mapping );
- texture.sourceFile = file.name;
- texture.format = file.type === 'image/jpeg' ? THREE.RGBFormat : THREE.RGBAFormat;
- texture.needsUpdate = true;
- scope.setValue( texture );
- if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
- }, false );
- image.src = event.target.result;
- }, false );
- reader.readAsDataURL( file );
- }
- form.reset();
- }
- this.texture = null;
- this.onChangeCallback = null;
- }
- getValue() {
- return this.texture;
- }
- setValue( texture ) {
- const canvas = this.dom.children[ 0 ];
- const context = canvas.getContext( '2d' );
- // Seems like context can be null if the canvas is not visible
- if ( context ) {
- // Always clear the context before set new texture, because new texture may has transparency
- context.clearRect( 0, 0, canvas.width, canvas.height );
- }
- if ( texture !== null ) {
- const image = texture.image;
- if ( image !== undefined && image.width > 0 ) {
- canvas.title = texture.sourceFile;
- const scale = canvas.width / image.width;
- if ( image.data === undefined ) {
- context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
- } else {
- const canvas2 = renderToCanvas( texture );
- context.drawImage( canvas2, 0, 0, image.width * scale, image.height * scale );
- }
- } else {
- canvas.title = texture.sourceFile + ' (error)';
- }
- } else {
- canvas.title = 'empty';
- }
- this.texture = texture;
- }
- setEncoding( encoding ) {
- const texture = this.getValue();
- if ( texture !== null ) {
- texture.encoding = encoding;
- }
- return this;
- }
- onChange( callback ) {
- this.onChangeCallback = callback;
- return this;
- }
- }
- class UICubeTexture extends UIElement {
- constructor() {
- const container = new UIDiv();
- super( container.dom );
- this.cubeTexture = null;
- this.onChangeCallback = null;
- this.textures = [];
- const scope = this;
- const pRow = new UIRow();
- const nRow = new UIRow();
- pRow.add( new UIText( 'P:' ).setWidth( '35px' ) );
- nRow.add( new UIText( 'N:' ).setWidth( '35px' ) );
- const posXTexture = new UITexture().onChange( onTextureChanged );
- const negXTexture = new UITexture().onChange( onTextureChanged );
- const posYTexture = new UITexture().onChange( onTextureChanged );
- const negYTexture = new UITexture().onChange( onTextureChanged );
- const posZTexture = new UITexture().onChange( onTextureChanged );
- const negZTexture = new UITexture().onChange( onTextureChanged );
- this.textures.push( posXTexture, negXTexture, posYTexture, negYTexture, posZTexture, negZTexture );
- pRow.add( posXTexture );
- pRow.add( posYTexture );
- pRow.add( posZTexture );
- nRow.add( negXTexture );
- nRow.add( negYTexture );
- nRow.add( negZTexture );
- container.add( pRow, nRow );
- function onTextureChanged() {
- const images = [];
- for ( let i = 0; i < scope.textures.length; i ++ ) {
- const texture = scope.textures[ i ].getValue();
- if ( texture !== null ) {
- images.push( texture.isHDRTexture ? texture : texture.image );
- }
- }
- if ( images.length === 6 ) {
- const cubeTexture = new THREE.CubeTexture( images );
- cubeTexture.needsUpdate = true;
- if ( images[ 0 ].isHDRTexture ) cubeTexture.isHDRTexture = true;
- scope.cubeTexture = cubeTexture;
- if ( scope.onChangeCallback ) scope.onChangeCallback( cubeTexture );
- }
- }
- }
- setEncoding( encoding ) {
- const cubeTexture = this.getValue();
- if ( cubeTexture !== null ) {
- cubeTexture.encoding = encoding;
- }
- return this;
- }
- getValue() {
- return this.cubeTexture;
- }
- setValue( cubeTexture ) {
- this.cubeTexture = cubeTexture;
- if ( cubeTexture !== null ) {
- const images = cubeTexture.image;
- if ( Array.isArray( images ) === true && images.length === 6 ) {
- for ( let i = 0; i < images.length; i ++ ) {
- const image = images[ i ];
- const texture = new THREE.Texture( image );
- this.textures[ i ].setValue( texture );
- }
- }
- } else {
- const textures = this.textures;
- for ( let i = 0; i < textures.length; i ++ ) {
- textures[ i ].setValue( null );
- }
- }
- return this;
- }
- onChange( callback ) {
- this.onChangeCallback = callback;
- return this;
- }
- }
- class UIOutliner extends UIDiv {
- constructor( editor ) {
- super();
- this.dom.className = 'Outliner';
- this.dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
- const scope = this;
- // hack
- this.scene = editor.scene;
- // Prevent native scroll behavior
- this.dom.addEventListener( 'keydown', function ( event ) {
- switch ( event.keyCode ) {
- case 38: // up
- case 40: // down
- event.preventDefault();
- event.stopPropagation();
- break;
- }
- }, false );
- // Keybindings to support arrow navigation
- this.dom.addEventListener( 'keyup', function ( event ) {
- switch ( event.keyCode ) {
- case 38: // up
- scope.selectIndex( scope.selectedIndex - 1 );
- break;
- case 40: // down
- scope.selectIndex( scope.selectedIndex + 1 );
- break;
- }
- }, false );
- this.editor = editor;
- this.options = [];
- this.selectedIndex = - 1;
- this.selectedValue = null;
- }
- selectIndex( index ) {
- if ( index >= 0 && index < this.options.length ) {
- this.setValue( this.options[ index ].value );
- const changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- this.dom.dispatchEvent( changeEvent );
- }
- }
- setOptions( options ) {
- const scope = this;
- while ( scope.dom.children.length > 0 ) {
- scope.dom.removeChild( scope.dom.firstChild );
- }
- function onClick() {
- scope.setValue( this.value );
- const changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- scope.dom.dispatchEvent( changeEvent );
- }
- // Drag
- let currentDrag;
- function onDrag() {
- currentDrag = this;
- }
- function onDragStart( event ) {
- event.dataTransfer.setData( 'text', 'foo' );
- }
- function onDragOver( event ) {
- if ( this === currentDrag ) return;
- const area = event.offsetY / this.clientHeight;
- if ( area < 0.25 ) {
- this.className = 'option dragTop';
- } else if ( area > 0.75 ) {
- this.className = 'option dragBottom';
- } else {
- this.className = 'option drag';
- }
- }
- function onDragLeave() {
- if ( this === currentDrag ) return;
- this.className = 'option';
- }
- function onDrop( event ) {
- if ( this === currentDrag || currentDrag === undefined ) return;
- this.className = 'option';
- const scene = scope.scene;
- const object = scene.getObjectById( currentDrag.value );
- const area = event.offsetY / this.clientHeight;
- if ( area < 0.25 ) {
- const nextObject = scene.getObjectById( this.value );
- moveObject( object, nextObject.parent, nextObject );
- } else if ( area > 0.75 ) {
- let nextObject, parent;
- if ( this.nextSibling !== null ) {
- nextObject = scene.getObjectById( this.nextSibling.value );
- parent = nextObject.parent;
- } else {
- // end of list (no next object)
- nextObject = null;
- parent = scene.getObjectById( this.value ).parent;
- }
- moveObject( object, parent, nextObject );
- } else {
- const parentObject = scene.getObjectById( this.value );
- moveObject( object, parentObject );
- }
- }
- function moveObject( object, newParent, nextObject ) {
- if ( nextObject === null ) nextObject = undefined;
- let newParentIsChild = false;
- object.traverse( function ( child ) {
- if ( child === newParent ) newParentIsChild = true;
- } );
- if ( newParentIsChild ) return;
- const editor = scope.editor;
- editor.execute( new MoveObjectCommand( editor, object, newParent, nextObject ) );
- const changeEvent = document.createEvent( 'HTMLEvents' );
- changeEvent.initEvent( 'change', true, true );
- scope.dom.dispatchEvent( changeEvent );
- }
- //
- scope.options = [];
- for ( let i = 0; i < options.length; i ++ ) {
- const div = options[ i ];
- div.className = 'option';
- scope.dom.appendChild( div );
- scope.options.push( div );
- div.addEventListener( 'click', onClick );
- if ( div.draggable === true ) {
- div.addEventListener( 'drag', onDrag );
- div.addEventListener( 'dragstart', onDragStart ); // Firefox needs this
- div.addEventListener( 'dragover', onDragOver );
- div.addEventListener( 'dragleave', onDragLeave );
- div.addEventListener( 'drop', onDrop );
- }
- }
- return scope;
- }
- getValue() {
- return this.selectedValue;
- }
- setValue( value ) {
- for ( let i = 0; i < this.options.length; i ++ ) {
- const element = this.options[ i ];
- if ( element.value === value ) {
- element.classList.add( 'active' );
- // scroll into view
- const y = element.offsetTop - this.dom.offsetTop;
- const bottomY = y + element.offsetHeight;
- const minScroll = bottomY - this.dom.offsetHeight;
- if ( this.dom.scrollTop > y ) {
- this.dom.scrollTop = y;
- } else if ( this.dom.scrollTop < minScroll ) {
- this.dom.scrollTop = minScroll;
- }
- this.selectedIndex = i;
- } else {
- element.classList.remove( 'active' );
- }
- }
- this.selectedValue = value;
- return this;
- }
- }
- class UIPoints extends UISpan {
- constructor() {
- super();
- this.dom.style.display = 'inline-block';
- this.pointsList = new UIDiv();
- this.add( this.pointsList );
- this.pointsUI = [];
- this.lastPointIdx = 0;
- this.onChangeCallback = null;
- // TODO Remove this bind() stuff
- this.update = function () {
- if ( this.onChangeCallback !== null ) {
- this.onChangeCallback();
- }
- }.bind( this );
- }
- onChange( callback ) {
- this.onChangeCallback = callback;
- return this;
- }
- clear() {
- for ( let i = 0; i < this.pointsUI.length; ++ i ) {
- if ( this.pointsUI[ i ] ) {
- this.deletePointRow( i, true );
- }
- }
- this.lastPointIdx = 0;
- }
- deletePointRow( idx, dontUpdate ) {
- if ( ! this.pointsUI[ idx ] ) return;
- this.pointsList.remove( this.pointsUI[ idx ].row );
- this.pointsUI.splice( idx, 1 );
- if ( dontUpdate !== true ) {
- this.update();
- }
- this.lastPointIdx --;
- }
- }
- class UIPoints2 extends UIPoints {
- constructor() {
- super();
- const row = new UIRow();
- this.add( row );
- const addPointButton = new UIButton( '+' );
- addPointButton.onClick( () => {
- if ( this.pointsUI.length === 0 ) {
- this.pointsList.add( this.createPointRow( 0, 0 ) );
- } else {
- const point = this.pointsUI[ this.pointsUI.length - 1 ];
- this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
- }
- this.update();
- } );
- row.add( addPointButton );
- }
- getValue() {
- const points = [];
- let count = 0;
- for ( let i = 0; i < this.pointsUI.length; i ++ ) {
- const pointUI = this.pointsUI[ i ];
- if ( ! pointUI ) continue;
- points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
- ++ count;
- pointUI.lbl.setValue( count );
- }
- return points;
- }
- setValue( points ) {
- this.clear();
- for ( let i = 0; i < points.length; i ++ ) {
- const point = points[ i ];
- this.pointsList.add( this.createPointRow( point.x, point.y ) );
- }
- this.update();
- return this;
- }
- createPointRow( x, y ) {
- const pointRow = new UIDiv();
- const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
- const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
- const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
- const scope = this;
- const btn = new UIButton( '-' ).onClick( function () {
- if ( scope.isEditing ) return;
- const idx = scope.pointsList.getIndexOfChild( pointRow );
- scope.deletePointRow( idx );
- } );
- this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
- ++ this.lastPointIdx;
- pointRow.add( lbl, txtX, txtY, btn );
- return pointRow;
- }
- }
- class UIPoints3 extends UIPoints {
- constructor() {
- super();
- const row = new UIRow();
- this.add( row );
- const addPointButton = new UIButton( '+' );
- addPointButton.onClick( () => {
- if ( this.pointsUI.length === 0 ) {
- this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
- } else {
- const point = this.pointsUI[ this.pointsUI.length - 1 ];
- this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
- }
- this.update();
- } );
- row.add( addPointButton );
- }
- getValue() {
- const points = [];
- let count = 0;
- for ( let i = 0; i < this.pointsUI.length; i ++ ) {
- const pointUI = this.pointsUI[ i ];
- if ( ! pointUI ) continue;
- points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
- ++ count;
- pointUI.lbl.setValue( count );
- }
- return points;
- }
- setValue( points ) {
- this.clear();
- for ( let i = 0; i < points.length; i ++ ) {
- const point = points[ i ];
- this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
- }
- this.update();
- return this;
- }
- createPointRow( x, y, z ) {
- const pointRow = new UIDiv();
- const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
- const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
- const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
- const txtZ = new UINumber( z ).setWidth( '30px' ).onChange( this.update );
- const scope = this;
- const btn = new UIButton( '-' ).onClick( function () {
- if ( scope.isEditing ) return;
- const idx = scope.pointsList.getIndexOfChild( pointRow );
- scope.deletePointRow( idx );
- } );
- this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
- ++ this.lastPointIdx;
- pointRow.add( lbl, txtX, txtY, txtZ, btn );
- return pointRow;
- }
- }
- class UIBoolean extends UISpan {
- constructor( boolean, text ) {
- super();
- this.setMarginRight( '4px' );
- this.checkbox = new UICheckbox( boolean );
- this.text = new UIText( text ).setMarginLeft( '3px' );
- this.add( this.checkbox );
- this.add( this.text );
- }
- getValue() {
- return this.checkbox.getValue();
- }
- setValue( value ) {
- return this.checkbox.setValue( value );
- }
- }
- let renderer;
- function renderToCanvas( texture ) {
- if ( renderer === undefined ) {
- renderer = new THREE.WebGLRenderer();
- renderer.outputEncoding = THREE.sRGBEncoding;
- }
- const image = texture.image;
- renderer.setSize( image.width, image.height, false );
- const scene = new THREE.Scene();
- const camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
- const material = new THREE.MeshBasicMaterial( { map: texture } );
- const quad = new THREE.PlaneGeometry( 2, 2 );
- const mesh = new THREE.Mesh( quad, material );
- scene.add( mesh );
- renderer.render( scene, camera );
- return renderer.domElement;
- }
- export { UITexture, UICubeTexture, UIOutliner, UIPoints, UIPoints2, UIPoints3, UIBoolean };
|