Textures.js 7.1 KB

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