ui.three.js 17 KB

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