ui.three.js 18 KB

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