Textures.js 6.8 KB

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