KTX2Exporter.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {
  2. FloatType,
  3. HalfFloatType,
  4. UnsignedByteType,
  5. RGBAFormat,
  6. RGFormat,
  7. RGIntegerFormat,
  8. RedFormat,
  9. RedIntegerFormat,
  10. LinearEncoding,
  11. sRGBEncoding,
  12. DataTexture,
  13. REVISION,
  14. } from 'three';
  15. import {
  16. write,
  17. KTX2Container,
  18. KHR_DF_CHANNEL_RGBSDA_ALPHA,
  19. KHR_DF_CHANNEL_RGBSDA_BLUE,
  20. KHR_DF_CHANNEL_RGBSDA_GREEN,
  21. KHR_DF_CHANNEL_RGBSDA_RED,
  22. KHR_DF_MODEL_RGBSDA,
  23. KHR_DF_PRIMARIES_BT709,
  24. KHR_DF_SAMPLE_DATATYPE_FLOAT,
  25. KHR_DF_SAMPLE_DATATYPE_LINEAR,
  26. KHR_DF_SAMPLE_DATATYPE_SIGNED,
  27. KHR_DF_TRANSFER_LINEAR,
  28. KHR_DF_TRANSFER_SRGB,
  29. VK_FORMAT_R16_SFLOAT,
  30. VK_FORMAT_R16G16_SFLOAT,
  31. VK_FORMAT_R16G16B16A16_SFLOAT,
  32. VK_FORMAT_R32_SFLOAT,
  33. VK_FORMAT_R32G32_SFLOAT,
  34. VK_FORMAT_R32G32B32A32_SFLOAT,
  35. VK_FORMAT_R8_SRGB,
  36. VK_FORMAT_R8_UNORM,
  37. VK_FORMAT_R8G8_SRGB,
  38. VK_FORMAT_R8G8_UNORM,
  39. VK_FORMAT_R8G8B8A8_SRGB,
  40. VK_FORMAT_R8G8B8A8_UNORM,
  41. } from '../libs/ktx-parse.module.js';
  42. const VK_FORMAT_MAP = {
  43. [ RGBAFormat ]: {
  44. [ FloatType ]: {
  45. [ LinearEncoding ]: VK_FORMAT_R32G32B32A32_SFLOAT,
  46. },
  47. [ HalfFloatType ]: {
  48. [ LinearEncoding ]: VK_FORMAT_R16G16B16A16_SFLOAT,
  49. },
  50. [ UnsignedByteType ]: {
  51. [ LinearEncoding ]: VK_FORMAT_R8G8B8A8_UNORM,
  52. [ sRGBEncoding ]: VK_FORMAT_R8G8B8A8_SRGB,
  53. },
  54. },
  55. [ RGFormat ]: {
  56. [ FloatType ]: {
  57. [ LinearEncoding ]: VK_FORMAT_R32G32_SFLOAT,
  58. },
  59. [ HalfFloatType ]: {
  60. [ LinearEncoding ]: VK_FORMAT_R16G16_SFLOAT,
  61. },
  62. [ UnsignedByteType ]: {
  63. [ LinearEncoding ]: VK_FORMAT_R8G8_UNORM,
  64. [ sRGBEncoding ]: VK_FORMAT_R8G8_SRGB,
  65. },
  66. },
  67. [ RedFormat ]: {
  68. [ FloatType ]: {
  69. [ LinearEncoding ]: VK_FORMAT_R32_SFLOAT,
  70. },
  71. [ HalfFloatType ]: {
  72. [ LinearEncoding ]: VK_FORMAT_R16_SFLOAT,
  73. },
  74. [ UnsignedByteType ]: {
  75. [ LinearEncoding ]: VK_FORMAT_R8_SRGB,
  76. [ sRGBEncoding ]: VK_FORMAT_R8_UNORM,
  77. },
  78. },
  79. };
  80. const KHR_DF_CHANNEL_MAP = {
  81. 0: KHR_DF_CHANNEL_RGBSDA_RED,
  82. 1: KHR_DF_CHANNEL_RGBSDA_GREEN,
  83. 2: KHR_DF_CHANNEL_RGBSDA_BLUE,
  84. 3: KHR_DF_CHANNEL_RGBSDA_ALPHA,
  85. };
  86. const ERROR_INPUT = 'THREE.KTX2Exporter: Supported inputs are DataTexture, Data3DTexture, or WebGLRenderer and WebGLRenderTarget.';
  87. const ERROR_FORMAT = 'THREE.KTX2Exporter: Supported formats are RGBAFormat, RGFormat, or RedFormat.';
  88. const ERROR_TYPE = 'THREE.KTX2Exporter: Supported types are FloatType, HalfFloatType, or UnsignedByteType."';
  89. const ERROR_ENCODING = 'THREE.KTX2Exporter: Supported encodings are sRGB (UnsignedByteType only) or Linear.';
  90. export class KTX2Exporter {
  91. parse( arg1, arg2 ) {
  92. let texture;
  93. if ( arg1.isDataTexture || arg1.isData3DTexture ) {
  94. texture = arg1;
  95. } else if ( arg1.isWebGLRenderer && arg2.isWebGLRenderTarget ) {
  96. texture = toDataTexture( arg1, arg2 );
  97. } else {
  98. throw new Error( ERROR_INPUT );
  99. }
  100. if ( VK_FORMAT_MAP[ texture.format ] === undefined ) {
  101. throw new Error( ERROR_FORMAT );
  102. }
  103. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ] === undefined ) {
  104. throw new Error( ERROR_TYPE );
  105. }
  106. if ( VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.encoding ] === undefined ) {
  107. throw new Error( ERROR_ENCODING );
  108. }
  109. //
  110. const array = texture.image.data;
  111. const channelCount = getChannelCount( texture );
  112. const container = new KTX2Container();
  113. container.vkFormat = VK_FORMAT_MAP[ texture.format ][ texture.type ][ texture.encoding ];
  114. container.typeSize = array.BYTES_PER_ELEMENT;
  115. container.pixelWidth = texture.image.width;
  116. container.pixelHeight = texture.image.height;
  117. if ( texture.isData3DTexture ) {
  118. container.pixelDepth = texture.image.depth;
  119. }
  120. //
  121. const basicDesc = container.dataFormatDescriptor[ 0 ];
  122. // TODO: After `texture.encoding` is replaced, distinguish between
  123. // non-color data (unspecified model and primaries) and sRGB or Linear-sRGB colors.
  124. basicDesc.colorModel = KHR_DF_MODEL_RGBSDA;
  125. basicDesc.colorPrimaries = KHR_DF_PRIMARIES_BT709;
  126. basicDesc.transferFunction = texture.encoding === sRGBEncoding
  127. ? KHR_DF_TRANSFER_SRGB
  128. : KHR_DF_TRANSFER_LINEAR;
  129. basicDesc.texelBlockDimension = [ 0, 0, 0, 0 ];
  130. basicDesc.bytesPlane = [
  131. container.typeSize * channelCount, 0, 0, 0, 0, 0, 0, 0,
  132. ];
  133. for ( let i = 0; i < channelCount; ++ i ) {
  134. let channelType = KHR_DF_CHANNEL_MAP[ i ];
  135. if ( texture.encoding === LinearEncoding ) {
  136. channelType |= KHR_DF_SAMPLE_DATATYPE_LINEAR;
  137. }
  138. if ( texture.type === FloatType || texture.type === HalfFloatType ) {
  139. channelType |= KHR_DF_SAMPLE_DATATYPE_FLOAT;
  140. channelType |= KHR_DF_SAMPLE_DATATYPE_SIGNED;
  141. }
  142. basicDesc.samples.push( {
  143. channelType: channelType,
  144. bitOffset: i * array.BYTES_PER_ELEMENT,
  145. bitLength: array.BYTES_PER_ELEMENT * 8 - 1,
  146. samplePosition: [ 0, 0, 0, 0 ],
  147. sampleLower: texture.type === UnsignedByteType ? 0 : - 1,
  148. sampleUpper: texture.type === UnsignedByteType ? 255 : 1,
  149. } );
  150. }
  151. //
  152. container.levels = [ {
  153. levelData: new Uint8Array( array.buffer, array.byteOffset, array.byteLength ),
  154. uncompressedByteLength: array.byteLength,
  155. } ];
  156. //
  157. container.keyValue[ 'KTXwriter' ] = `three.js ${ REVISION }`;
  158. //
  159. return write( container, { keepWriter: true } );
  160. }
  161. }
  162. function toDataTexture( renderer, rtt ) {
  163. const channelCount = getChannelCount( rtt.texture );
  164. let view;
  165. if ( rtt.texture.type === FloatType ) {
  166. view = new Float32Array( rtt.width * rtt.height * channelCount );
  167. } else if ( rtt.texture.type === HalfFloatType ) {
  168. view = new Uint16Array( rtt.width * rtt.height * channelCount );
  169. } else if ( rtt.texture.type === UnsignedByteType ) {
  170. view = new Uint8Array( rtt.width * rtt.height * channelCount );
  171. } else {
  172. throw new Error( ERROR_TYPE );
  173. }
  174. renderer.readRenderTargetPixels( rtt, 0, 0, rtt.width, rtt.height, view );
  175. return new DataTexture( view, rtt.width, rtt.height, rtt.texture.format, rtt.texture.type );
  176. }
  177. function getChannelCount( texture ) {
  178. switch ( texture.format ) {
  179. case RGBAFormat:
  180. return 4;
  181. case RGFormat:
  182. case RGIntegerFormat:
  183. return 2;
  184. case RedFormat:
  185. case RedIntegerFormat:
  186. return 1;
  187. default:
  188. throw new Error( ERROR_FORMAT );
  189. }
  190. }