1
0

ui.three.js 16 KB

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