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.

script.ts 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. _testFinish, _testStart,
  3. ITexture,
  4. KTXLoadPlugin,
  5. LoadingScreenPlugin,
  6. Mesh,
  7. PlaneGeometry,
  8. SRGBColorSpace,
  9. ThreeViewer,
  10. UnlitMaterial,
  11. } from 'threepipe'
  12. async function init() {
  13. const viewer = new ThreeViewer({
  14. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  15. msaa: true,
  16. dropzone: {
  17. allowedExtensions: ['ktx'],
  18. },
  19. plugins: [LoadingScreenPlugin],
  20. })
  21. viewer.addPluginSync(KTXLoadPlugin)
  22. viewer.scene.setBackgroundColor('#555555')
  23. const urls = []
  24. // Checking which ktx formats are supported by the browser
  25. const formats = {
  26. astc: viewer.renderManager.renderer.extensions.has('WEBGL_compressed_texture_astc'),
  27. etc1: viewer.renderManager.renderer.extensions.has('WEBGL_compressed_texture_etc1'),
  28. s3tc: viewer.renderManager.renderer.extensions.has('WEBGL_compressed_texture_s3tc'),
  29. pvrtc: viewer.renderManager.renderer.extensions.has('WEBGL_compressed_texture_pvrtc'),
  30. }
  31. if (formats.pvrtc) urls.push(
  32. 'https://threejs.org/examples/textures/compressed/disturb_PVR2bpp.ktx',
  33. 'https://threejs.org/examples/textures/compressed/lensflare_PVR4bpp.ktx'
  34. )
  35. if (formats.s3tc) urls.push(
  36. 'https://threejs.org/examples/textures/compressed/disturb_BC1.ktx',
  37. 'https://threejs.org/examples/textures/compressed/lensflare_BC3.ktx'
  38. )
  39. if (formats.etc1) urls.push(
  40. 'https://threejs.org/examples/textures/compressed/disturb_ETC1.ktx'
  41. )
  42. if (formats.astc) urls.push(
  43. 'https://threejs.org/examples/textures/compressed/disturb_ASTC4x4.ktx',
  44. 'https://threejs.org/examples/textures/compressed/lensflare_ASTC8x8.ktx'
  45. )
  46. const geometry = new PlaneGeometry(1, 1)
  47. let i = 0
  48. for (const url of urls) {
  49. // Load the url as a Texture
  50. const texture = await viewer.load<ITexture>(url)
  51. if (!texture) continue
  52. texture.colorSpace = SRGBColorSpace
  53. const material = new UnlitMaterial({
  54. map: texture,
  55. transparent: true,
  56. })
  57. const plane = new Mesh(geometry, material)
  58. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  59. viewer.scene.addObject(plane)
  60. i++
  61. }
  62. // Listen to when a file is dropped
  63. viewer.assetManager.addEventListener('loadAsset', (e)=>{
  64. if (!e.data?.isTexture) return
  65. const texture = e.data as ITexture
  66. texture.colorSpace = SRGBColorSpace
  67. const material = new UnlitMaterial({
  68. map: texture,
  69. transparent: true,
  70. })
  71. const plane = new Mesh(geometry, material)
  72. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  73. viewer.scene.addObject(plane)
  74. i++
  75. })
  76. }
  77. _testStart()
  78. init().finally(_testFinish)