ui.three.js 16 KB

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