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 { 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.keyCode ) {
  158. case 38: // up
  159. case 40: // down
  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.keyCode ) {
  168. case 38: // up
  169. scope.selectIndex( scope.selectedIndex - 1 );
  170. break;
  171. case 40: // down
  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 = document.createEvent( 'HTMLEvents' );
  185. changeEvent.initEvent( 'change', true, true );
  186. this.dom.dispatchEvent( changeEvent );
  187. }
  188. }
  189. setOptions( options ) {
  190. const scope = this;
  191. while ( scope.dom.children.length > 0 ) {
  192. scope.dom.removeChild( scope.dom.firstChild );
  193. }
  194. function onClick() {
  195. scope.setValue( this.value );
  196. const changeEvent = document.createEvent( 'HTMLEvents' );
  197. changeEvent.initEvent( 'change', true, true );
  198. scope.dom.dispatchEvent( changeEvent );
  199. }
  200. // Drag
  201. let currentDrag;
  202. function onDrag() {
  203. currentDrag = this;
  204. }
  205. function onDragStart( event ) {
  206. event.dataTransfer.setData( 'text', 'foo' );
  207. }
  208. function onDragOver( event ) {
  209. if ( this === currentDrag ) return;
  210. const area = event.offsetY / this.clientHeight;
  211. if ( area < 0.25 ) {
  212. this.className = 'option dragTop';
  213. } else if ( area > 0.75 ) {
  214. this.className = 'option dragBottom';
  215. } else {
  216. this.className = 'option drag';
  217. }
  218. }
  219. function onDragLeave() {
  220. if ( this === currentDrag ) return;
  221. this.className = 'option';
  222. }
  223. function onDrop( event ) {
  224. if ( this === currentDrag || currentDrag === undefined ) return;
  225. this.className = 'option';
  226. const scene = scope.scene;
  227. const object = scene.getObjectById( currentDrag.value );
  228. const area = event.offsetY / this.clientHeight;
  229. if ( area < 0.25 ) {
  230. const nextObject = scene.getObjectById( this.value );
  231. moveObject( object, nextObject.parent, nextObject );
  232. } else if ( area > 0.75 ) {
  233. let nextObject, parent;
  234. if ( this.nextSibling !== null ) {
  235. nextObject = scene.getObjectById( this.nextSibling.value );
  236. parent = nextObject.parent;
  237. } else {
  238. // end of list (no next object)
  239. nextObject = null;
  240. parent = scene.getObjectById( this.value ).parent;
  241. }
  242. moveObject( object, parent, nextObject );
  243. } else {
  244. const parentObject = scene.getObjectById( this.value );
  245. moveObject( object, parentObject );
  246. }
  247. }
  248. function moveObject( object, newParent, nextObject ) {
  249. if ( nextObject === null ) nextObject = undefined;
  250. let newParentIsChild = false;
  251. object.traverse( function ( child ) {
  252. if ( child === newParent ) newParentIsChild = true;
  253. } );
  254. if ( newParentIsChild ) return;
  255. const editor = scope.editor;
  256. editor.execute( new MoveObjectCommand( editor, object, newParent, nextObject ) );
  257. const changeEvent = document.createEvent( 'HTMLEvents' );
  258. changeEvent.initEvent( 'change', true, true );
  259. scope.dom.dispatchEvent( changeEvent );
  260. }
  261. //
  262. scope.options = [];
  263. for ( let i = 0; i < options.length; i ++ ) {
  264. const div = options[ i ];
  265. div.className = 'option';
  266. scope.dom.appendChild( div );
  267. scope.options.push( div );
  268. div.addEventListener( 'click', onClick );
  269. if ( div.draggable === true ) {
  270. div.addEventListener( 'drag', onDrag );
  271. div.addEventListener( 'dragstart', onDragStart ); // Firefox needs this
  272. div.addEventListener( 'dragover', onDragOver );
  273. div.addEventListener( 'dragleave', onDragLeave );
  274. div.addEventListener( 'drop', onDrop );
  275. }
  276. }
  277. return scope;
  278. }
  279. getValue() {
  280. return this.selectedValue;
  281. }
  282. setValue( value ) {
  283. for ( let i = 0; i < this.options.length; i ++ ) {
  284. const element = this.options[ i ];
  285. if ( element.value === value ) {
  286. element.classList.add( 'active' );
  287. // scroll into view
  288. const y = element.offsetTop - this.dom.offsetTop;
  289. const bottomY = y + element.offsetHeight;
  290. const minScroll = bottomY - this.dom.offsetHeight;
  291. if ( this.dom.scrollTop > y ) {
  292. this.dom.scrollTop = y;
  293. } else if ( this.dom.scrollTop < minScroll ) {
  294. this.dom.scrollTop = minScroll;
  295. }
  296. this.selectedIndex = i;
  297. } else {
  298. element.classList.remove( 'active' );
  299. }
  300. }
  301. this.selectedValue = value;
  302. return this;
  303. }
  304. }
  305. class UIPoints extends UISpan {
  306. constructor() {
  307. super();
  308. this.dom.style.display = 'inline-block';
  309. this.pointsList = new UIDiv();
  310. this.add( this.pointsList );
  311. this.pointsUI = [];
  312. this.lastPointIdx = 0;
  313. this.onChangeCallback = null;
  314. // TODO Remove this bind() stuff
  315. this.update = function () {
  316. if ( this.onChangeCallback !== null ) {
  317. this.onChangeCallback();
  318. }
  319. }.bind( this );
  320. }
  321. onChange( callback ) {
  322. this.onChangeCallback = callback;
  323. return this;
  324. }
  325. clear() {
  326. for ( let i = 0; i < this.pointsUI.length; ++ i ) {
  327. if ( this.pointsUI[ i ] ) {
  328. this.deletePointRow( i, true );
  329. }
  330. }
  331. this.lastPointIdx = 0;
  332. }
  333. deletePointRow( idx, dontUpdate ) {
  334. if ( ! this.pointsUI[ idx ] ) return;
  335. this.pointsList.remove( this.pointsUI[ idx ].row );
  336. this.pointsUI.splice( idx, 1 );
  337. if ( dontUpdate !== true ) {
  338. this.update();
  339. }
  340. this.lastPointIdx --;
  341. }
  342. }
  343. class UIPoints2 extends UIPoints {
  344. constructor() {
  345. super();
  346. const row = new UIRow();
  347. this.add( row );
  348. const addPointButton = new UIButton( '+' );
  349. addPointButton.onClick( () => {
  350. if ( this.pointsUI.length === 0 ) {
  351. this.pointsList.add( this.createPointRow( 0, 0 ) );
  352. } else {
  353. const point = this.pointsUI[ this.pointsUI.length - 1 ];
  354. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
  355. }
  356. this.update();
  357. } );
  358. row.add( addPointButton );
  359. }
  360. getValue() {
  361. const points = [];
  362. let count = 0;
  363. for ( let i = 0; i < this.pointsUI.length; i ++ ) {
  364. const pointUI = this.pointsUI[ i ];
  365. if ( ! pointUI ) continue;
  366. points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
  367. ++ count;
  368. pointUI.lbl.setValue( count );
  369. }
  370. return points;
  371. }
  372. setValue( points ) {
  373. this.clear();
  374. for ( let i = 0; i < points.length; i ++ ) {
  375. const point = points[ i ];
  376. this.pointsList.add( this.createPointRow( point.x, point.y ) );
  377. }
  378. this.update();
  379. return this;
  380. }
  381. createPointRow( x, y ) {
  382. const pointRow = new UIDiv();
  383. const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  384. const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  385. const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  386. const scope = this;
  387. const btn = new UIButton( '-' ).onClick( function () {
  388. if ( scope.isEditing ) return;
  389. const idx = scope.pointsList.getIndexOfChild( pointRow );
  390. scope.deletePointRow( idx );
  391. } );
  392. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
  393. ++ this.lastPointIdx;
  394. pointRow.add( lbl, txtX, txtY, btn );
  395. return pointRow;
  396. }
  397. }
  398. class UIPoints3 extends UIPoints {
  399. constructor() {
  400. super();
  401. const row = new UIRow();
  402. this.add( row );
  403. const addPointButton = new UIButton( '+' );
  404. addPointButton.onClick( () => {
  405. if ( this.pointsUI.length === 0 ) {
  406. this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
  407. } else {
  408. const point = this.pointsUI[ this.pointsUI.length - 1 ];
  409. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
  410. }
  411. this.update();
  412. } );
  413. row.add( addPointButton );
  414. }
  415. getValue() {
  416. const points = [];
  417. let count = 0;
  418. for ( let i = 0; i < this.pointsUI.length; i ++ ) {
  419. const pointUI = this.pointsUI[ i ];
  420. if ( ! pointUI ) continue;
  421. points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
  422. ++ count;
  423. pointUI.lbl.setValue( count );
  424. }
  425. return points;
  426. }
  427. setValue( points ) {
  428. this.clear();
  429. for ( let i = 0; i < points.length; i ++ ) {
  430. const point = points[ i ];
  431. this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
  432. }
  433. this.update();
  434. return this;
  435. }
  436. createPointRow( x, y, z ) {
  437. const pointRow = new UIDiv();
  438. const lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  439. const txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  440. const txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  441. const txtZ = new UINumber( z ).setWidth( '30px' ).onChange( this.update );
  442. const scope = this;
  443. const btn = new UIButton( '-' ).onClick( function () {
  444. if ( scope.isEditing ) return;
  445. const idx = scope.pointsList.getIndexOfChild( pointRow );
  446. scope.deletePointRow( idx );
  447. } );
  448. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
  449. ++ this.lastPointIdx;
  450. pointRow.add( lbl, txtX, txtY, txtZ, btn );
  451. return pointRow;
  452. }
  453. }
  454. class UIBoolean extends UISpan {
  455. constructor( boolean, text ) {
  456. super();
  457. this.setMarginRight( '4px' );
  458. this.checkbox = new UICheckbox( boolean );
  459. this.text = new UIText( text ).setMarginLeft( '3px' );
  460. this.add( this.checkbox );
  461. this.add( this.text );
  462. }
  463. getValue() {
  464. return this.checkbox.getValue();
  465. }
  466. setValue( value ) {
  467. return this.checkbox.setValue( value );
  468. }
  469. }
  470. let renderer, fsQuad;
  471. function renderToCanvas( texture ) {
  472. if ( renderer === undefined ) {
  473. renderer = new THREE.WebGLRenderer();
  474. }
  475. if ( fsQuad === undefined ) {
  476. fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial() );
  477. }
  478. const image = texture.image;
  479. renderer.setSize( image.width, image.height, false );
  480. fsQuad.material.map = texture;
  481. fsQuad.render( renderer );
  482. return renderer.domElement;
  483. }
  484. export { UITexture, UIOutliner, UIPoints, UIPoints2, UIPoints3, UIBoolean };