1
0

KTX2Exporter.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import {
  2. ColorManagement,
  3. FloatType,
  4. HalfFloatType,
  5. UnsignedByteType,
  6. RGBAFormat,
  7. RGFormat,
  8. RGIntegerFormat,
  9. RedFormat,
  10. RedIntegerFormat,
  11. NoColorSpace,
  12. LinearSRGBColorSpace,
  13. SRGBColorSpace,
  14. SRGBTransfer,
  15. DataTexture,
  16. REVISION,
  17. } from 'three';
  18. import {
  19. write,
  20. KTX2Container,
  21. KHR_DF_CHANNEL_RGBSDA_ALPHA,
  22. KHR_DF_CHANNEL_RGBSDA_BLUE,
  23. KHR_DF_CHANNEL_RGBSDA_GREEN,
  24. KHR_DF_CHANNEL_RGBSDA_RED,
  25. KHR_DF_MODEL_RGBSDA,
  26. KHR_DF_PRIMARIES_BT709,
  27. KHR_DF_PRIMARIES_UNSPECIFIED,
  28. KHR_DF_SAMPLE_DATATYPE_FLOAT,
  29. KHR_DF_SAMPLE_DATATYPE_LINEAR,
  30. KHR_DF_SAMPLE_DATATYPE_SIGNED,
  31. KHR_DF_TRANSFER_LINEAR,
  32. KHR_DF_TRANSFER_SRGB,
  33. VK_FORMAT_R16_SFLOAT,
  34. VK_FORMAT_R16G16_SFLOAT,
  35. VK_FORMAT_R16G16B16A16_SFLOAT,
  36. VK_FORMAT_R32_SFLOAT,
  37. VK_FORMAT_R32G32_SFLOAT,
  38. VK_FORMAT_R32G32B32A32_SFLOAT,
  39. VK_FORMAT_R8_SRGB,
  40. VK_FORMAT_R8_UNORM,
  41. VK_FORMAT_R8G8_SRGB,
  42. VK_FORMAT_R8G8_UNORM,
  43. VK_FORMAT_R8G8B8A8_SRGB,
  44. VK_FORMAT_R8G8B8A8_UNORM,
  45. } from '../libs/ktx-parse.module.js';
  46. /**
  47. * References:
  48. * - https://github.khronos.org/KTX-Specification/ktxspec.v2.html
  49. * - https://registry.khronos.org/DataFormat/specs/1.3/dataformat.1.3.html
  50. * - https://github.com/donmccurdy/KTX-Parse
  51. */
  52. const VK_FORMAT_MAP = {
  53. [ RGBAFormat ]: {
  54. [ FloatType ]: {
  55. [ NoColorSpace ]: VK_FORMAT_R32G32B32A32_SFLOAT,
  56. [ LinearSRGBColorSpace ]: VK_FORMAT_R32G32B32A32_SFLOAT,
  57. },
  58. [ HalfFloatType ]: {
  59. [ NoColorSpace ]: VK_FORMAT_R16G16B16A16_SFLOAT,
  60. [ LinearSRGBColorSpace ]: VK_FORMAT_R16G16B16A16_SFLOAT,
  61. },
  62. [ UnsignedByteType ]: {
  63. [ NoColorSpace ]: VK_FORMAT_R8G8B8A8_UNORM,
  64. [ LinearSRGBColorSpace ]: VK_FORMAT_R8G8B8A8_UNORM,
  65. [ SRGBColorSpace ]: VK_FORMAT_R8G8B8A8_SRGB,
  66. },
  67. },
  68. [ RGFormat ]: {
  69. [ FloatType ]: {
  70. [ NoColorSpace ]: VK_FORMAT_R32G32_SFLOAT,
  71. [ LinearSRGBColorSpace ]: VK_FORMAT_R32G32_SFLOAT,
  72. },
  73. [ HalfFloatType ]: {
  74. [ NoColorSpace ]: VK_FORMAT_R16G16_SFLOAT,
  75. [ LinearSRGBColorSpace ]: VK_FORMAT_R16G16_SFLOAT,
  76. },
  77. [ UnsignedByteType ]: {
  78. [ NoColorSpace ]: VK_FORMAT_R8G8_UNORM,
  79. [ LinearSRGBColorSpace ]: VK_FORMAT_R8G8_UNORM,
  80. [ SRGBColorSpace ]: VK_FORMAT_R8G8_SRGB,
  81. },
  82. },
  83. [ RedFormat ]: {
  84. [ FloatType ]: {
  85. [ NoColorSpace ]: VK_FORMAT_R32_SFLOAT,
  86. [ LinearSRGBColorSpace ]: VK_FORMAT_R32_SFLOAT,
  87. },
  88. [ HalfFloatType ]: {
  89. [ NoColorSpace ]: VK_FORMAT_R16_SFLOAT,
  90. [ LinearSRGBColorSpace ]: VK_FORMAT_R16_SFLOAT,
  91. },
  92. [ UnsignedByteType ]: {
  93. [ NoColorSpace ]: VK_FORMAT_R8_UNORM,
  94. [ LinearSRGBColorSpace ]: VK_FORMAT_R8_UNORM,
  95. [ SRGBColorSpace ]: VK_FORMAT_R8_SRGB,
  96. },
  97. },
  98. };
  99. const KHR_DF_CHANNEL_MAP = [
  100. KHR_DF_CHANNEL_RGBSDA_RED,
  101. KHR_DF_CHANNEL_RGBSDA_GREEN,
  102. KHR_DF_CHANNEL_RGBSDA_BLUE,
  103. KHR_DF_CHANNEL_RGBSDA_ALPHA,
  104. ];
  105. // TODO: sampleLower and sampleUpper may change based on color space.
  106. const KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER = {
  107. [ FloatType ]: [ 0xbf800000, 0x3f800000 ],
  108. [ HalfFloatType ]: [ 0xbf800000, 0x3f800000 ],
  109. [ UnsignedByteType ]: [ 0, 255 ],
  110. };
  111. const ERROR_INPUT = 'THREE.KTX2Exporter: Supported inputs are DataTexture, Data3DTexture, or WebGLRenderer and WebGLRenderTarget.';
  112. const ERROR_FORMAT = 'THREE.KTX2Exporter: Supported formats are RGBAFormat, RGFormat, or RedFormat.';
  113. const ERROR_TYPE = 'THREE.KTX2Exporter: Supported types are FloatType, HalfFloatType, or UnsignedByteType."';
  114. const ERROR_COLOR_SPACE = 'THREE.KTX2Exporter: Supported color spaces are SRGBColorSpace (UnsignedByteType only), LinearSRGBColorSpace, or NoColorSpace.';
  115. /**
  116. * An exporter for KTX2.
  117. *
  118. * ```js
  119. * const exporter = new KTX2Exporter();
  120. * const result = await exporter.parse( dataTexture );
  121. * ```
  122. */
  123. export class KTX2Exporter {
  124. /**
  125. * This method has two variants.
  126. *
  127. * - When exporting a data texture, it receives one parameter. The data or 3D data texture.
  128. * - When exporting a render target (e.g. a PMREM), it receives two parameters. The renderer and the
  129. * render target.
  130. *
  131. * @async
  132. * @param {(DataTexture|Data3DTexture|WebGPURenderer|WebGLRenderer)} arg1 - The data texture to export or a renderer.
  133. * @param {RenderTarget} [arg2] - The render target that should be exported
  134. * @return {Promise<Uint8Array>} A Promise that resolves with the exported KTX2.
  135. */
  136. async parse( arg1, arg2 ) {
  137. let texture;
  138. if ( arg1.isDataTexture || arg1.isData3DTexture ) {
  139. texture = arg1;
  140. } else if ( ( arg1.isWebGLRenderer || arg1.isWebGPURenderer ) && arg2.isRenderTarget ) {
  141. texture = await toDataTexture( arg1, arg2 );
  142. } else {
  143. throw new Error( ERROR_INPUT );
  144. }
  145. if ( VK_FORMAT_MAP[ texture.format ] === undefined ) {
  146. throw new Error( ERROR_FORMAT );
  147. }
  148. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ] === undefined ) {
  149. throw new Error( ERROR_TYPE );
  150. }
  151. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ] === undefined ) {
  152. throw new Error( ERROR_COLOR_SPACE );
  153. }
  154. //
  155. const array = texture.image.data;
  156. const channelCount = getChannelCount( texture );
  157. const container = new KTX2Container();
  158. container.vkFormat = VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ];
  159. container.typeSize = array.BYTES_PER_ELEMENT;
  160. container.pixelWidth = texture.image.width;
  161. container.pixelHeight = texture.image.height;
  162. if ( texture.isData3DTexture ) {
  163. container.pixelDepth = texture.image.depth;
  164. }
  165. //
  166. const basicDesc = container.dataFormatDescriptor[ 0 ];
  167. basicDesc.colorModel = KHR_DF_MODEL_RGBSDA;
  168. basicDesc.colorPrimaries = texture.colorSpace === NoColorSpace
  169. ? KHR_DF_PRIMARIES_UNSPECIFIED
  170. : KHR_DF_PRIMARIES_BT709;
  171. basicDesc.transferFunction = ColorManagement.getTransfer( texture.colorSpace ) === SRGBTransfer
  172. ? KHR_DF_TRANSFER_SRGB
  173. : KHR_DF_TRANSFER_LINEAR;
  174. basicDesc.texelBlockDimension = [ 0, 0, 0, 0 ];
  175. basicDesc.bytesPlane = [
  176. container.typeSize * channelCount, 0, 0, 0, 0, 0, 0, 0,
  177. ];
  178. for ( let i = 0; i < channelCount; ++ i ) {
  179. let channelType = KHR_DF_CHANNEL_MAP[ i ];
  180. // Assign KHR_DF_SAMPLE_DATATYPE_LINEAR if the channel is linear _and_ differs from the transfer function.
  181. if ( channelType === KHR_DF_CHANNEL_RGBSDA_ALPHA && basicDesc.transferFunction !== KHR_DF_TRANSFER_LINEAR ) {
  182. channelType |= KHR_DF_SAMPLE_DATATYPE_LINEAR;
  183. }
  184. if ( texture.type === FloatType || texture.type === HalfFloatType ) {
  185. channelType |= KHR_DF_SAMPLE_DATATYPE_FLOAT;
  186. channelType |= KHR_DF_SAMPLE_DATATYPE_SIGNED;
  187. }
  188. basicDesc.samples.push( {
  189. channelType: channelType,
  190. bitOffset: i * array.BYTES_PER_ELEMENT * 8,
  191. bitLength: array.BYTES_PER_ELEMENT * 8 - 1,
  192. samplePosition: [ 0, 0, 0, 0 ],
  193. sampleLower: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 0 ],
  194. sampleUpper: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 1 ],
  195. } );
  196. }
  197. //
  198. container.levels = [ {
  199. levelData: new Uint8Array( array.buffer, array.byteOffset, array.byteLength ),
  200. uncompressedByteLength: array.byteLength,
  201. } ];
  202. //
  203. container.keyValue[ 'KTXwriter' ] = `three.js ${ REVISION }`;
  204. //
  205. return write( container, { keepWriter: true } );
  206. }
  207. }
  208. async function toDataTexture( renderer, rtt ) {
  209. const channelCount = getChannelCount( rtt.texture );
  210. let view;
  211. if ( renderer.isWebGLRenderer ) {
  212. if ( rtt.texture.type === FloatType ) {
  213. view = new Float32Array( rtt.width * rtt.height * channelCount );
  214. } else if ( rtt.texture.type === HalfFloatType ) {
  215. view = new Uint16Array( rtt.width * rtt.height * channelCount );
  216. } else if ( rtt.texture.type === UnsignedByteType ) {
  217. view = new Uint8Array( rtt.width * rtt.height * channelCount );
  218. } else {
  219. throw new Error( ERROR_TYPE );
  220. }
  221. await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height, view );
  222. } else {
  223. view = await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height );
  224. }
  225. const texture = new DataTexture( view, rtt.width, rtt.height, rtt.texture.format, rtt.texture.type );
  226. texture.colorSpace = rtt.texture.colorSpace;
  227. return texture;
  228. }
  229. function getChannelCount( texture ) {
  230. switch ( texture.format ) {
  231. case RGBAFormat:
  232. return 4;
  233. case RGFormat:
  234. case RGIntegerFormat:
  235. return 2;
  236. case RedFormat:
  237. case RedIntegerFormat:
  238. return 1;
  239. default:
  240. throw new Error( ERROR_FORMAT );
  241. }
  242. }
粤ICP备19079148号