ui.three.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. import * as THREE from '../../../build/three.module.js';
  2. import { RGBELoader } from '../../../examples/jsm/loaders/RGBELoader.js';
  3. import { TGALoader } from '../../../examples/jsm/loaders/TGALoader.js';
  4. import { UIElement, UISpan, UIDiv, UIRow, UIButton, UICheckbox, UIText, UINumber } from './ui.js';
  5. import { MoveObjectCommand } from '../commands/MoveObjectCommand.js';
  6. class UITexture extends UISpan {
  7. constructor( mapping ) {
  8. super();
  9. const scope = this;
  10. const form = document.createElement( 'form' );
  11. const input = document.createElement( 'input' );
  12. input.type = 'file';
  13. input.addEventListener( 'change', function ( event ) {
  14. loadFile( event.target.files[ 0 ] );
  15. } );
  16. form.appendChild( input );
  17. const canvas = document.createElement( 'canvas' );
  18. canvas.width = 32;
  19. canvas.height = 16;
  20. canvas.style.cursor = 'pointer';
  21. canvas.style.marginRight = '5px';
  22. canvas.style.border = '1px solid #888';
  23. canvas.addEventListener( 'click', function () {
  24. input.click();
  25. }, false );
  26. canvas.addEventListener( 'drop', function () {
  27. event.preventDefault();
  28. event.stopPropagation();
  29. loadFile( event.dataTransfer.files[ 0 ] );
  30. }, false );
  31. this.dom.appendChild( canvas );
  32. function loadFile( file ) {
  33. const extension = file.name.split( '.' ).pop().toLowerCase();
  34. const reader = new FileReader();
  35. if ( extension === 'hdr' ) {
  36. reader.addEventListener( 'load', function ( event ) {
  37. // assuming RGBE/Radiance HDR iamge format
  38. const loader = new RGBELoader().setDataType( THREE.UnsignedByteType );
  39. loader.load( event.target.result, function ( hdrTexture ) {
  40. hdrTexture.sourceFile = file.name;
  41. hdrTexture.isHDRTexture = true;
  42. scope.setValue( hdrTexture );
  43. if ( scope.onChangeCallback ) scope.onChangeCallback( hdrTexture );
  44. } );
  45. } );
  46. reader.readAsDataURL( file );
  47. } else if ( extension === 'tga' ) {
  48. reader.addEventListener( 'load', function ( event ) {
  49. const canvas = new TGALoader().parse( event.target.result );
  50. const texture = new THREE.CanvasTexture( canvas, mapping );
  51. texture.sourceFile = file.name;
  52. scope.setValue( texture );
  53. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  54. }, false );
  55. reader.readAsArrayBuffer( file );
  56. } else if ( file.type.match( 'image.*' ) ) {
  57. reader.addEventListener( 'load', function ( event ) {
  58. const image = document.createElement( 'img' );
  59. image.addEventListener( 'load', function () {
  60. const texture = new THREE.Texture( this, mapping );
  61. texture.sourceFile = file.name;
  62. texture.format = file.type === 'image/jpeg' ? THREE.RGBFormat : THREE.RGBAFormat;
  63. texture.needsUpdate = true;
  64. scope.setValue( texture );
  65. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  66. }, false );
  67. image.src = event.target.result;
  68. }, false );
  69. reader.readAsDataURL( file );
  70. }
  71. form.reset();
  72. }
  73. this.texture = null;
  74. this.onChangeCallback = null;
  75. }
  76. getValue() {
  77. return this.texture;
  78. }
  79. setValue( texture ) {
  80. const canvas = this.dom.children[ 0 ];
  81. const context = canvas.getContext( '2d' );
  82. // Seems like context can be null if the canvas is not visible
  83. if ( context ) {
  84. // Always clear the context before set new texture, because new texture may has transparency
  85. context.clearRect( 0, 0, canvas.width, canvas.height );
  86. }
  87. if ( texture !== null ) {
  88. const image = texture.image;
  89. if ( image !== undefined && image.width > 0 ) {
  90. canvas.title = texture.sourceFile;
  91. const scale = canvas.width / image.width;
  92. if ( image.data === undefined ) {
  93. context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
  94. } else {
  95. const canvas2 = renderToCanvas( texture );
  96. context.drawImage( canvas2, 0, 0, image.width * scale, image.height * scale );
  97. }
  98. } else {
  99. canvas.title = texture.sourceFile + ' (error)';
  100. }
  101. } else {
  102. canvas.title = 'empty';
  103. }
  104. this.texture = texture;
  105. }
  106. setEncoding( encoding ) {
  107. const texture = this.getValue();
  108. if ( texture !== null ) {
  109. texture.encoding = encoding;
  110. }
  111. return this;
  112. }
  113. onChange( callback ) {
  114. this.onChangeCallback = callback;
  115. return this;
  116. }
  117. }
  118. class UICubeTexture extends UIElement {
  119. constructor() {
  120. const container = new UIDiv();
  121. super( container.dom );
  122. this.cubeTexture = null;
  123. this.onChangeCallback = null;
  124. this.textures = [];
  125. const scope = this;
  126. const pRow = new UIRow();
  127. const nRow = new UIRow();
  128. pRow.add( new UIText( 'P:' ).setWidth( '35px' ) );
  129. nRow.add( new UIText( 'N:' ).setWidth( '35px' ) );
  130. const posXTexture = new UITexture().onChange( onTextureChanged );
  131. const negXTexture = new UITexture().onChange( onTextureChanged );
  132. const posYTexture = new UITexture().onChange( onTextureChanged );
  133. const negYTexture = new UITexture().onChange( onTextureChanged );
  134. const posZTexture = new UITexture().onChange( onTextureChanged );
  135. const negZTexture = new UITexture().onChange( onTextureChanged );
  136. this.textures.push( posXTexture, negXTexture, posYTexture, negYTexture, posZTexture, negZTexture );
  137. pRow.add( posXTexture );
  138. pRow.add( posYTexture );
  139. pRow.add( posZTexture );
  140. nRow.add( negXTexture );
  141. nRow.add( negYTexture );
  142. nRow.add( negZTexture );
  143. container.add( pRow, nRow );
  144. function onTextureChanged() {
  145. const images = [];
  146. for ( let i = 0; i < scope.textures.length; i ++ ) {
  147. const texture = scope.textures[ i ].getValue();
  148. if ( texture !== null ) {
  149. images.push( texture.isHDRTexture ? texture : texture.image );
  150. }
  151. }
  152. if ( images.length === 6 ) {
  153. const cubeTexture = new THREE.CubeTexture( images );
  154. cubeTexture.needsUpdate = true;
  155. if ( images[ 0 ].isHDRTexture ) cubeTexture.isHDRTexture = true;
  156. scope.cubeTexture = cubeTexture;
  157. if ( scope.onChangeCallback ) scope.onChangeCallback( cubeTexture );
  158. }
  159. }
  160. }
  161. setEncoding( encoding ) {
  162. const cubeTexture = this.getValue();
  163. if ( cubeTexture !== null ) {
  164. cubeTexture.encoding = encoding;
  165. }
  166. return this;
  167. }
  168. getValue() {
  169. return this.cubeTexture;
  170. }
  171. setValue( cubeTexture ) {
  172. this.cubeTexture = cubeTexture;
  173. if ( cubeTexture !== null ) {
  174. const images = cubeTexture.image;
  175. if ( Array.isArray( images ) === true && images.length === 6 ) {
  176. for ( let i = 0; i < images.length; i ++ ) {
  177. const image = images[ i ];
  178. const texture = new THREE.Texture( image );
  179. this.textures[ i ].setValue( texture );
  180. }
  181. }
  182. } else {
  183. const textures = this.textures;
  184. for ( let i = 0; i < textures.length; i ++ ) {
  185. textures[ i ].setValue( null );
  186. }
  187. }
  188. return this;
  189. }
  190. onChange( callback ) {
  191. this.onChangeCallback = callback;
  192. return this;
  193. }
  194. }
  195. class UIOutliner extends UIDiv {
  196. constructor( editor ) {
  197. super();
  198. this.dom.className = 'Outliner';
  199. this.dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  200. const scope = this;
  201. // hack
  202. this.scene = editor.scene;
  203. // Prevent native scroll behavior
  204. this.dom.addEventListener( 'keydown', function ( event ) {
  205. switch ( event.keyCode ) {
  206. case 38: // up
  207. case 40: // down
  208. event.preventDefault();
  209. event.stopPropagation();
  210. break;
  211. }
  212. }, false );
  213. // Keybindings to support arrow navigation
  214. this.dom.addEventListener( 'keyup', function ( event ) {
  215. switch ( event.keyCode ) {
  216. case 38: // up
  217. scope.selectIndex( scope.selectedIndex - 1 );
  218. break;
  219. case 40: // down
  220. scope.selectIndex( scope.selectedIndex + 1 );
  221. break;
  222. }
  223. }, false );
  224. this.editor = editor;
  225. this.options = [];
  226. this.selectedIndex = - 1;
  227. this.selectedValue = null;
  228. }
  229. selectIndex( index ) {
  230. if ( index >= 0 && index < this.options.length ) {
  231. this.setValue( this.options[ index ].value );
  232. const changeEvent = document.createEvent( 'HTMLEvents' );
  233. changeEvent.initEvent( 'change', true, true );
  234. this.dom.dispatchEvent( changeEvent );
  235. }
  236. }
  237. setOptions( options ) {
  238. const scope = this;
  239. while ( scope.dom.children.length > 0 ) {
  240. scope.dom.removeChild( scope.dom.firstChild );
  241. }
  242. function onClick() {
  243. scope.setValue( this.value );
  244. const changeEvent = document.createEvent( 'HTMLEvents' );
  245. changeEvent.initEvent( 'change', true, true );
  246. scope.dom.dispatchEvent( changeEvent );
  247. }
  248. // Drag
  249. let currentDrag;
  250. function onDrag() {
  251. currentDrag = this;
  252. }
  253. function onDragStart( event ) {
  254. event.dataTransfer.setData( 'text', 'foo' );
  255. }
  256. function onDragOver( event ) {
  257. if ( this === currentDrag ) return;
  258. const area = event.offsetY / this.clientHeight;
  259. if ( area < 0.25 ) {
  260. this.className = 'option dragTop';
  261. } else if ( area > 0.75 ) {
  262. this.className = 'option dragBottom';
  263. } else {
  264. this.className = 'option drag';
  265. }
  266. }
  267. function onDragLeave() {
  268. if ( this === currentDrag ) return;
  269. this.className = 'option';
  270. }
  271. function onDrop( event ) {
  272. if ( this === currentDrag || currentDrag === undefined ) return;
  273. this.className = 'option';
  274. const scene = scope.scene;
  275. const object = scene.getObjectById( currentDrag.value );
  276. const area = event.offsetY / this.clientHeight;
  277. if ( area < 0.25 ) {
  278. const nextObject = scene.getObjectById( this.value );
  279. moveObject( object, nextObject.parent, nextObject );
  280. } else if ( area > 0.75 ) {
  281. let nextObject, parent;
  282. if ( this.nextSibling !== null ) {
  283. nextObject = scene.getObjectById( this.nextSibling.value );
  284. parent = nextObject.parent;
  285. } else {
  286. // end of list (no next object)
  287. nextObject = null;
  288. parent = scene.getObjectById( this.value ).parent;
  289. }
  290. moveObject( object, parent, nextObject );
  291. } else {
  292. const parentObject = scene.getObjectById( this.value );
  293. moveObject( object, parentObject );
  294. }
  295. }
  296. function moveObject( object, newParent, nextObject ) {
  297. if ( nextObject === null ) nextObject = undefined;
  298. let newParentIsChild = false;
  299. object.traverse( function ( child ) {
  300. if ( child === newParent ) newParentIsChild = true;
  301. } );
  302. if ( newParentIsChild ) return;
  303. const editor = scope.editor;
  304. editor.execute( new MoveObjectCommand( editor, object, newParent, nextObject ) );
  305. const changeEvent = document.createEvent( 'HTMLEvents' );
  306. changeEvent.initEvent( 'change', true, true );
  307. scope.dom.dispatchEvent( changeEvent );
  308. }
  309. //
  310. scope.options = [];
  311. for ( let i = 0; i < options.length; i ++ ) {
  312. const div = options[ i ];
  313. div.className = 'option';
  314. scope.dom.appendChild( div );
  315. scope.options.push( div );
  316. div.addEventListener( 'click', onClick );
  317. if ( div.draggable === true ) {
  318. div.addEventListener( 'drag', onDrag );
  319. div.addEventListener( 'dragstart', onDragStart ); // Firefox needs this
  320. div.addEventListener( 'dragover', onDragOver );
  321. div.addEventListener( 'dragleave', onDragLeave );
  322. div.addEventListener( 'drop', onDrop );
  323. }
  324. }
  325. return scope;
  326. }
  327. getValue() {
  328. return this.selectedValue;
  329. }
  330. setValue( value ) {
  331. for ( let i = 0; i < this.options.length; i ++ ) {
  332. const element = this.options[ i ];
  333. if ( element.value === value ) {
  334. element.classList.add( 'active' );
  335. // scroll into view
  336. const y = element.offsetTop - this.dom.offsetTop;
  337. const bottomY = y + element.offsetHeight;
  338. const minScroll = bottomY - this.dom.offsetHeight;
  339. if ( this.dom.scrollTop > y ) {
  340. this.dom.scrollTop = y;
  341. } else if ( this.dom.scrollTop < minScroll ) {
  342. this.dom.scrollTop = minScroll;
  343. }
  344. this.selectedIndex = i;
  345. } else {
  346. element.classList.remove( 'active' );
  347. }
  348. }
  349. this.selectedValue = value;
  350. return this;
  351. }
  352. }
  353. class UIPoints extends UISpan {
  354. constructor() {
  355. super();
  356. this.dom.style.display = 'inline-block';
  357. this.pointsList = new UIDiv();
  358. this.add( this.pointsList );
  359. this.pointsUI = [];
  360. this.lastPointIdx = 0;
  361. this.onChangeCallback = null;
  362. // TODO Remove this bind() stuff
  363. this.update = function () {
  364. if ( this.onChangeCallback !== null ) {
  365. this.onChangeCallback();
  366. }
  367. }.bind( this );
  368. }
  369. onChange( callback ) {
  370. this.onChangeCallback = callback;
  371. return this;
  372. }
  373. clear() {
  374. for ( let i = 0; i < this.pointsUI.length; ++ i ) {
  375. if ( this.pointsUI[ i ] ) {
  376. this.deletePointRow( i, true );
  377. }
  378. }
  379. this.lastPointIdx = 0;
  380. }
  381. deletePointRow( idx, dontUpdate ) {
  382. if ( ! this.pointsUI[ idx ] ) return;
  383. this.pointsList.remove( this.pointsUI[ idx ].row );
  384. this.pointsUI.splice( idx, 1 );
  385. if ( dontUpdate !== true ) {
  386. this.update();
  387. }
  388. this.lastPointIdx --;
  389. }
  390. }
  391. class UIPoints2 extends UIPoints {
  392. constructor() {
  393. super();
  394. const row = new UIRow();
  395. this.add( row );
  396. const addPointButton = new UIButton( '+' );
  397. addPointButton.onClick( () => {
  398. if ( this.pointsUI.length === 0 ) {
  399. this.pointsList.add( this.createPointRow( 0, 0 ) );
  400. } else {
  401. const point = this.pointsUI[ this.pointsUI.length - 1 ];
  402. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
  403. }
  404. this.update();
  405. } );
  406. row.add( addPointButton );
  407. }
  408. getValue() {
  409. const points = [];
  410. let count = 0;
  411. for ( let i = 0; i < this.pointsUI.length; i ++ ) {
  412. const pointUI = this.pointsUI[ i ];
  413. if ( ! pointUI ) continue;
  414. points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
  415. ++ count;
  416. pointUI.lbl.setValue( count );
  417. }
  418. return points;
  419. }
  420. setValue( points ) {
  421. this.clear();
  422. for ( let i = 0; i < points.length; i ++ ) {
  423. const point = points[ i ];
  424. this.pointsList.add( this.createPointRow( point.x, point.y ) );
  425. }
  426. this.update();
  427. return this;
  428. }
  429. createPointRow( x, y ) {
  430. const pointRow = new UIDiv();
  431. const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  432. const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  433. const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  434. const scope = this;
  435. const btn = new UIButton( '-' ).onClick( function () {
  436. if ( scope.isEditing ) return;
  437. const idx = scope.pointsList.getIndexOfChild( pointRow );
  438. scope.deletePointRow( idx );
  439. } );
  440. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
  441. ++ this.lastPointIdx;
  442. pointRow.add( lbl, txtX, txtY, btn );
  443. return pointRow;
  444. }
  445. }
  446. class UIPoints3 extends UIPoints {
  447. constructor() {
  448. super();
  449. const row = new UIRow();
  450. this.add( row );
  451. const addPointButton = new UIButton( '+' );
  452. addPointButton.onClick( () => {
  453. if ( this.pointsUI.length === 0 ) {
  454. this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
  455. } else {
  456. const point = this.pointsUI[ this.pointsUI.length - 1 ];
  457. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
  458. }
  459. this.update();
  460. } );
  461. row.add( addPointButton );
  462. }
  463. getValue() {
  464. const points = [];
  465. let count = 0;
  466. for ( let i = 0; i < this.pointsUI.length; i ++ ) {
  467. const pointUI = this.pointsUI[ i ];
  468. if ( ! pointUI ) continue;
  469. points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
  470. ++ count;
  471. pointUI.lbl.setValue( count );
  472. }
  473. return points;
  474. }
  475. setValue( points ) {
  476. this.clear();
  477. for ( let i = 0; i < points.length; i ++ ) {
  478. const point = points[ i ];
  479. this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
  480. }
  481. this.update();
  482. return this;
  483. }
  484. createPointRow( x, y, z ) {
  485. const pointRow = new UIDiv();
  486. const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  487. const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  488. const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  489. const txtZ = new UINumber( z ).setWidth( '30px' ).onChange( this.update );
  490. const scope = this;
  491. const btn = new UIButton( '-' ).onClick( function () {
  492. if ( scope.isEditing ) return;
  493. const idx = scope.pointsList.getIndexOfChild( pointRow );
  494. scope.deletePointRow( idx );
  495. } );
  496. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
  497. ++ this.lastPointIdx;
  498. pointRow.add( lbl, txtX, txtY, txtZ, btn );
  499. return pointRow;
  500. }
  501. }
  502. class UIBoolean extends UISpan {
  503. constructor( boolean, text ) {
  504. super();
  505. this.setMarginRight( '4px' );
  506. this.checkbox = new UICheckbox( boolean );
  507. this.text = new UIText( text ).setMarginLeft( '3px' );
  508. this.add( this.checkbox );
  509. this.add( this.text );
  510. }
  511. getValue() {
  512. return this.checkbox.getValue();
  513. }
  514. setValue( value ) {
  515. return this.checkbox.setValue( value );
  516. }
  517. }
  518. let renderer;
  519. function renderToCanvas( texture ) {
  520. if ( renderer === undefined ) {
  521. renderer = new THREE.WebGLRenderer();
  522. renderer.outputEncoding = THREE.sRGBEncoding;
  523. }
  524. const image = texture.image;
  525. renderer.setSize( image.width, image.height, false );
  526. const scene = new THREE.Scene();
  527. const camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  528. const material = new THREE.MeshBasicMaterial( { map: texture } );
  529. const quad = new THREE.PlaneGeometry( 2, 2 );
  530. const mesh = new THREE.Mesh( quad, material );
  531. scene.add( mesh );
  532. renderer.render( scene, camera );
  533. return renderer.domElement;
  534. }
  535. export { UITexture, UICubeTexture, UIOutliner, UIPoints, UIPoints2, UIPoints3, UIBoolean };