KTX2Exporter.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. export class KTX2Exporter {
  116. async parse( arg1, arg2 ) {
  117. let texture;
  118. if ( arg1.isDataTexture || arg1.isData3DTexture ) {
  119. texture = arg1;
  120. } else if ( ( arg1.isWebGLRenderer || arg1.isWebGPURenderer ) && arg2.isRenderTarget ) {
  121. texture = await toDataTexture( arg1, arg2 );
  122. } else {
  123. throw new Error( ERROR_INPUT );
  124. }
  125. if ( VK_FORMAT_MAP[ texture.format ] === undefined ) {
  126. throw new Error( ERROR_FORMAT );
  127. }
  128. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ] === undefined ) {
  129. throw new Error( ERROR_TYPE );
  130. }
  131. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ] === undefined ) {
  132. throw new Error( ERROR_COLOR_SPACE );
  133. }
  134. //
  135. const array = texture.image.data;
  136. const channelCount = getChannelCount( texture );
  137. const container = new KTX2Container();
  138. container.vkFormat = VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.colorSpace ];
  139. container.typeSize = array.BYTES_PER_ELEMENT;
  140. container.pixelWidth = texture.image.width;
  141. container.pixelHeight = texture.image.height;
  142. if ( texture.isData3DTexture ) {
  143. container.pixelDepth = texture.image.depth;
  144. }
  145. //
  146. const basicDesc = container.dataFormatDescriptor[ 0 ];
  147. basicDesc.colorModel = KHR_DF_MODEL_RGBSDA;
  148. basicDesc.colorPrimaries = texture.colorSpace === NoColorSpace
  149. ? KHR_DF_PRIMARIES_UNSPECIFIED
  150. : KHR_DF_PRIMARIES_BT709;
  151. basicDesc.transferFunction = ColorManagement.getTransfer( texture.colorSpace ) === SRGBTransfer
  152. ? KHR_DF_TRANSFER_SRGB
  153. : KHR_DF_TRANSFER_LINEAR;
  154. basicDesc.texelBlockDimension = [ 0, 0, 0, 0 ];
  155. basicDesc.bytesPlane = [
  156. container.typeSize * channelCount, 0, 0, 0, 0, 0, 0, 0,
  157. ];
  158. for ( let i = 0; i < channelCount; ++ i ) {
  159. let channelType = KHR_DF_CHANNEL_MAP[ i ];
  160. // Assign KHR_DF_SAMPLE_DATATYPE_LINEAR if the channel is linear _and_ differs from the transfer function.
  161. if ( channelType === KHR_DF_CHANNEL_RGBSDA_ALPHA && basicDesc.transferFunction !== KHR_DF_TRANSFER_LINEAR ) {
  162. channelType |= KHR_DF_SAMPLE_DATATYPE_LINEAR;
  163. }
  164. if ( texture.type === FloatType || texture.type === HalfFloatType ) {
  165. channelType |= KHR_DF_SAMPLE_DATATYPE_FLOAT;
  166. channelType |= KHR_DF_SAMPLE_DATATYPE_SIGNED;
  167. }
  168. basicDesc.samples.push( {
  169. channelType: channelType,
  170. bitOffset: i * array.BYTES_PER_ELEMENT * 8,
  171. bitLength: array.BYTES_PER_ELEMENT * 8 - 1,
  172. samplePosition: [ 0, 0, 0, 0 ],
  173. sampleLower: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 0 ],
  174. sampleUpper: KHR_DF_CHANNEL_SAMPLE_LOWER_UPPER[ texture.type ][ 1 ],
  175. } );
  176. }
  177. //
  178. container.levels = [ {
  179. levelData: new Uint8Array( array.buffer, array.byteOffset, array.byteLength ),
  180. uncompressedByteLength: array.byteLength,
  181. } ];
  182. //
  183. container.keyValue[ 'KTXwriter' ] = `three.js ${ REVISION }`;
  184. //
  185. return write( container, { keepWriter: true } );
  186. }
  187. }
  188. async function toDataTexture( renderer, rtt ) {
  189. const channelCount = getChannelCount( rtt.texture );
  190. let view;
  191. if ( renderer.isWebGLRenderer ) {
  192. if ( rtt.texture.type === FloatType ) {
  193. view = new Float32Array( rtt.width * rtt.height * channelCount );
  194. } else if ( rtt.texture.type === HalfFloatType ) {
  195. view = new Uint16Array( rtt.width * rtt.height * channelCount );
  196. } else if ( rtt.texture.type === UnsignedByteType ) {
  197. view = new Uint8Array( rtt.width * rtt.height * channelCount );
  198. } else {
  199. throw new Error( ERROR_TYPE );
  200. }
  201. await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height, view );
  202. } else {
  203. view = await renderer.readRenderTargetPixelsAsync( rtt, 0, 0, rtt.width, rtt.height );
  204. }
  205. const texture = new DataTexture( view, rtt.width, rtt.height, rtt.texture.format, rtt.texture.type );
  206. texture.colorSpace = rtt.texture.colorSpace;
  207. return texture;
  208. }
  209. function getChannelCount( texture ) {
  210. switch ( texture.format ) {
  211. case RGBAFormat:
  212. return 4;
  213. case RGFormat:
  214. case RGIntegerFormat:
  215. return 2;
  216. case RedFormat:
  217. case RedIntegerFormat:
  218. return 1;
  219. default:
  220. throw new Error( ERROR_FORMAT );
  221. }
  222. }
粤ICP备19079148号