KTX2Exporter.js 6.7 KB

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