Textures.js 7.2 KB

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