ui.three.js 16 KB

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