threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

KTX2LoadPlugin.ts 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {IViewerPluginSync, ThreeViewer} from '../../viewer'
  2. import {GLTFWriter2, ILoader, Importer, ImportResultExtras} from '../../assetmanager'
  3. import {KTX2Loader} from 'three/examples/jsm/loaders/KTX2Loader.js'
  4. import {CompressedTexture} from 'three'
  5. import {serializeTextureInExtras} from '../../utils'
  6. import {ITexture} from '../../core'
  7. /**
  8. * Adds support for loading Compressed Textures of format `.ktx2`, `image/ktx2` files and data uris.
  9. * @category Plugins
  10. */
  11. export class KTX2LoadPlugin implements IViewerPluginSync {
  12. declare ['constructor']: typeof KTX2LoadPlugin
  13. public static readonly PluginType = 'KTX2LoadPlugin'
  14. private _importer = new Importer(KTX2Loader2, ['ktx2'], ['image/ktx2'], false)
  15. public static TRANSCODER_LIBRARY_PATH = 'https://cdn.jsdelivr.net/gh/BinomialLLC/basis_universal@1.16.4/webgl/transcoder/build/'
  16. onAdded(viewer: ThreeViewer) {
  17. this._importer.onCtor = (l: KTX2Loader2) => l
  18. .setTranscoderPath(KTX2LoadPlugin.TRANSCODER_LIBRARY_PATH)
  19. .detectSupport(viewer.renderManager.renderer)
  20. viewer.assetManager.importer.addImporter(this._importer)
  21. viewer.assetManager.exporter.getExporter('gltf', 'glb')?.extensions?.push(glTFTextureBasisUExtensionExport)
  22. }
  23. onRemove(viewer: ThreeViewer) {
  24. viewer.assetManager.importer.removeImporter(this._importer)
  25. const exporter = viewer.assetManager.exporter.getExporter('gltf', 'glb')
  26. const index = exporter?.extensions?.indexOf(glTFTextureBasisUExtensionExport)
  27. if (index !== undefined && index !== -1) exporter?.extensions?.splice(index, 1)
  28. }
  29. dispose() {
  30. return
  31. }
  32. }
  33. export class KTX2Loader2 extends KTX2Loader implements ILoader {
  34. async createTexture(buffer: ArrayBuffer, config: any): Promise<CompressedTexture> {
  35. const buffer2 = new Uint8Array(buffer.slice(0)) // clones the buffer
  36. const texture = (await super.createTexture(buffer, config)) as CompressedTexture & ITexture
  37. texture.source._sourceImgBuffer = buffer2 // keep the same buffer when cloned and all, used in serializeTextureInExtras
  38. texture.userData.mimeType = 'image/ktx2'
  39. texture.toJSON = (meta?: any)=>{
  40. return serializeTextureInExtras(texture, meta, texture.name, 'image/ktx2')
  41. }
  42. texture.clone = ()=>{
  43. throw new Error('ktx2 texture cloning not supported')
  44. }
  45. return texture
  46. }
  47. }
  48. export const KHR_TEXTURE_BASISU = 'KHR_texture_basisu'
  49. const glTFTextureBasisUExtensionExport = (w: GLTFWriter2)=> ({
  50. writeTexture: (texture: ITexture&ImportResultExtras, textureDef: any) => {
  51. // if (!w.options.embedImages) return // option is removed.
  52. if (texture.userData.mimeType !== 'image/ktx2') return
  53. if (textureDef.source !== undefined && textureDef.source !== null) {
  54. console.warn('ktx2 export: source already set')
  55. return
  56. }
  57. const sourceBuffer = texture.source._sourceImgBuffer || texture.__sourceBuffer // todo do this for all images that have a __sourceBuffer (in GLTFExporter.processImage or GLTFWriter2.processTexture)
  58. if (!sourceBuffer) {
  59. console.warn('ktx2 export: no source buffer for ktx2')
  60. return
  61. }
  62. textureDef.extensions = textureDef.extensions || {}
  63. const extensionDef: any = {}
  64. const blob = new Blob([sourceBuffer], {type: 'image/ktx2'})
  65. extensionDef.source = w.processImageBlob(blob, texture)
  66. textureDef.extensions[ KHR_TEXTURE_BASISU ] = extensionDef
  67. w.extensionsUsed[ KHR_TEXTURE_BASISU ] = true
  68. },
  69. })