Textures.js 7.2 KB

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