Textures.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import DataMap from './DataMap.js';
  2. import { Vector3, DepthTexture, DepthStencilFormat, UnsignedInt248Type, LinearFilter, NearestFilter, EquirectangularReflectionMapping, EquirectangularRefractionMapping, CubeReflectionMapping, CubeRefractionMapping } from 'three';
  3. const _size = new Vector3();
  4. class Textures extends DataMap {
  5. constructor( backend, info ) {
  6. super();
  7. this.backend = backend;
  8. this.info = info;
  9. }
  10. updateRenderTarget( renderTarget ) {
  11. const renderTargetData = this.get( renderTarget );
  12. const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
  13. const texture = renderTarget.texture;
  14. const size = this.getSize( texture );
  15. let depthTexture = renderTarget.depthTexture || renderTargetData.depthTexture;
  16. if ( depthTexture === undefined ) {
  17. depthTexture = new DepthTexture();
  18. depthTexture.format = DepthStencilFormat;
  19. depthTexture.type = UnsignedInt248Type;
  20. depthTexture.image.width = size.width;
  21. depthTexture.image.height = size.height;
  22. }
  23. if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
  24. texture.needsUpdate = true;
  25. depthTexture.needsUpdate = true;
  26. depthTexture.image.width = size.width;
  27. depthTexture.image.height = size.height;
  28. }
  29. renderTargetData.width = size.width;
  30. renderTargetData.height = size.height;
  31. renderTargetData.texture = texture;
  32. renderTargetData.depthTexture = depthTexture;
  33. if ( renderTargetData.sampleCount !== sampleCount ) {
  34. texture.needsUpdate = true;
  35. depthTexture.needsUpdate = true;
  36. renderTargetData.sampleCount = sampleCount;
  37. }
  38. const options = { sampleCount };
  39. this.updateTexture( texture, options );
  40. this.updateTexture( depthTexture, options );
  41. // dispose handler
  42. if ( renderTargetData.initialized !== true ) {
  43. renderTargetData.initialized = true;
  44. // dispose
  45. const onDispose = () => {
  46. renderTarget.removeEventListener( 'dispose', onDispose );
  47. this._destroyTexture( texture );
  48. this._destroyTexture( depthTexture );
  49. };
  50. renderTarget.addEventListener( 'dispose', onDispose );
  51. }
  52. }
  53. updateTexture( texture, options = {} ) {
  54. const textureData = this.get( texture );
  55. if ( textureData.initialized === true && textureData.version === texture.version ) return;
  56. const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
  57. const backend = this.backend;
  58. if ( isRenderTarget && textureData.initialized === true ) {
  59. // it's an update
  60. backend.destroySampler( texture );
  61. backend.destroyTexture( texture );
  62. }
  63. //
  64. const { width, height, depth } = this.getSize( texture );
  65. options.width = width;
  66. options.height = height;
  67. options.depth = depth;
  68. options.needsMipmaps = this.needsMipmaps( texture );
  69. options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
  70. //
  71. if ( isRenderTarget || options.store === true ) {
  72. //if ( options.store === true ) options.levels = 1; /* no mipmaps? */
  73. backend.createSampler( texture );
  74. backend.createTexture( texture, options );
  75. } else {
  76. const needsCreate = textureData.initialized !== true;
  77. if ( needsCreate ) backend.createSampler( texture );
  78. if ( texture.version > 0 ) {
  79. const image = texture.image;
  80. if ( image === undefined ) {
  81. console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
  82. } else if ( image.complete === false ) {
  83. console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
  84. } else {
  85. if ( texture.images ) {
  86. const images = [];
  87. for ( const image of texture.images ) {
  88. images.push( this._getUploadImage( image ) );
  89. }
  90. options.images = images;
  91. } else {
  92. options.image = this._getUploadImage( image );
  93. }
  94. if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
  95. backend.createTexture( texture, options );
  96. textureData.isDefaultTexture = false;
  97. }
  98. backend.updateTexture( texture, options );
  99. if ( options.needsMipmaps ) backend.generateMipmaps( texture );
  100. }
  101. } else {
  102. // async update
  103. backend.createDefaultTexture( texture );
  104. textureData.isDefaultTexture = true;
  105. }
  106. }
  107. // dispose handler
  108. if ( textureData.initialized !== true ) {
  109. textureData.initialized = true;
  110. //
  111. this.info.memory.textures ++;
  112. // dispose
  113. const onDispose = () => {
  114. texture.removeEventListener( 'dispose', onDispose );
  115. this._destroyTexture( texture );
  116. this.info.memory.textures --;
  117. };
  118. texture.addEventListener( 'dispose', onDispose );
  119. }
  120. //
  121. textureData.version = texture.version;
  122. }
  123. getSize( texture, target = _size ) {
  124. let image = texture.images ? texture.images[ 0 ] : texture.image;
  125. if ( image ) {
  126. if ( image.image !== undefined ) image = image.image;
  127. target.width = image.width;
  128. target.height = image.height;
  129. target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
  130. } else {
  131. target.width = target.height = target.depth = 1;
  132. }
  133. return target;
  134. }
  135. getMipLevels( texture, width, height ) {
  136. let mipLevelCount;
  137. if ( texture.isCompressedTexture ) {
  138. mipLevelCount = texture.mipmaps.length;
  139. } else {
  140. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  141. }
  142. return mipLevelCount;
  143. }
  144. needsMipmaps( texture ) {
  145. if ( this.isEnvironmentTexture( texture ) ) return true;
  146. return ( texture.isCompressedTexture !== true ) /*&& ( texture.generateMipmaps === true )*/ && ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter );
  147. }
  148. isEnvironmentTexture( texture ) {
  149. const mapping = texture.mapping;
  150. return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
  151. }
  152. _getUploadImage( image ) {
  153. if ( this._isHTMLImage( image ) ) {
  154. return this._imageToCanvas( image );
  155. }
  156. return image;
  157. }
  158. _imageToCanvas( image ) {
  159. const { width, height } = image;
  160. // eslint-disable-next-line compat/compat
  161. const canvas = new OffscreenCanvas( width, height );
  162. const context = canvas.getContext( '2d' );
  163. context.drawImage( image, 0, 0, width, height );
  164. return canvas;
  165. }
  166. _isHTMLImage( image ) {
  167. return ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement );
  168. }
  169. _destroyTexture( texture ) {
  170. this.backend.destroySampler( texture );
  171. this.backend.destroyTexture( texture );
  172. this.delete( texture );
  173. }
  174. }
  175. export default Textures;