ui.three.js 17 KB

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