ui.three.js 15 KB

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