ui.three.js 16 KB

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