threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. _testFinish,
  3. ITexture,
  4. KTX2LoadPlugin,
  5. LoadingScreenPlugin,
  6. Mesh,
  7. PlaneGeometry,
  8. SRGBColorSpace,
  9. ThreeViewer,
  10. UnlitMaterial,
  11. BufferGeometry,
  12. } from 'threepipe'
  13. async function init() {
  14. const viewer = new ThreeViewer({
  15. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  16. msaa: true,
  17. dropzone: {
  18. allowedExtensions: ['ktx2'],
  19. },
  20. plugins: [KTX2LoadPlugin, LoadingScreenPlugin],
  21. })
  22. viewer.scene.setBackgroundColor('#555555')
  23. const urls = [
  24. 'https://cdn.jsdelivr.net/gh/repalash/three.js-modded@v0.153.1001/examples/textures/compressed/sample_etc1s.ktx2',
  25. 'https://cdn.jsdelivr.net/gh/repalash/three.js-modded@v0.153.1001/examples/textures/compressed/sample_uastc.ktx2',
  26. 'https://cdn.jsdelivr.net/gh/repalash/three.js-modded@v0.153.1001/examples/textures/compressed/sample_uastc_zstd.ktx2',
  27. ]
  28. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  29. const geometry = flipY(new PlaneGeometry(1, 1))
  30. let i = 0
  31. for (const url of urls) {
  32. // Load the url as a Texture
  33. const texture = await viewer.load<ITexture>(url)
  34. if (!texture) continue
  35. texture.colorSpace = SRGBColorSpace
  36. const material = new UnlitMaterial({
  37. map: texture,
  38. transparent: true,
  39. })
  40. const plane = new Mesh(geometry, material)
  41. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  42. viewer.scene.addObject(plane)
  43. i++
  44. }
  45. // Listen to when a file is dropped
  46. viewer.assetManager.addEventListener('loadAsset', (e)=>{
  47. if (!e.data?.isTexture) return
  48. const texture = e.data as ITexture
  49. texture.colorSpace = SRGBColorSpace
  50. const material = new UnlitMaterial({
  51. map: texture,
  52. transparent: true,
  53. })
  54. const plane = new Mesh(geometry, material)
  55. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  56. viewer.scene.addObject(plane)
  57. i++
  58. })
  59. }
  60. init().finally(_testFinish)
  61. /** Correct UVs to be compatible with `flipY=false` textures. */
  62. function flipY(geometry: BufferGeometry) {
  63. const uv = geometry.attributes.uv
  64. for (let i = 0; i < uv.count; i++) {
  65. uv.setY(i, 1 - uv.getY(i))
  66. }
  67. return geometry
  68. }