Textures.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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( 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 = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
  30. depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType;
  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. if ( texture.isFramebufferTexture ) {
  90. const renderer = this.backend.renderer;
  91. const renderTarget = renderer.getRenderTarget();
  92. if ( renderTarget ) {
  93. texture.type = renderTarget.texture.type;
  94. } else {
  95. texture.type = UnsignedByteType;
  96. }
  97. }
  98. //
  99. const { width, height, depth } = this.getSize( texture );
  100. options.width = width;
  101. options.height = height;
  102. options.depth = depth;
  103. options.needsMipmaps = this.needsMipmaps( texture );
  104. options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
  105. //
  106. if ( isRenderTarget || texture.isStorageTexture === true ) {
  107. backend.createSampler( texture );
  108. backend.createTexture( texture, options );
  109. } else {
  110. const needsCreate = textureData.initialized !== true;
  111. if ( needsCreate ) backend.createSampler( texture );
  112. if ( texture.version > 0 ) {
  113. const image = texture.image;
  114. if ( image === undefined ) {
  115. console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
  116. } else if ( image.complete === false ) {
  117. console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
  118. } else {
  119. if ( texture.images ) {
  120. const images = [];
  121. for ( const image of texture.images ) {
  122. images.push( image );
  123. }
  124. options.images = images;
  125. } else {
  126. options.image = image;
  127. }
  128. if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
  129. backend.createTexture( texture, options );
  130. textureData.isDefaultTexture = false;
  131. }
  132. backend.updateTexture( texture, options );
  133. if ( options.needsMipmaps && texture.mipmaps.length === 0 ) backend.generateMipmaps( texture );
  134. }
  135. } else {
  136. // async update
  137. backend.createDefaultTexture( texture );
  138. textureData.isDefaultTexture = true;
  139. }
  140. }
  141. // dispose handler
  142. if ( textureData.initialized !== true ) {
  143. textureData.initialized = true;
  144. //
  145. this.info.memory.textures ++;
  146. // dispose
  147. const onDispose = () => {
  148. texture.removeEventListener( 'dispose', onDispose );
  149. this._destroyTexture( texture );
  150. this.info.memory.textures --;
  151. };
  152. texture.addEventListener( 'dispose', onDispose );
  153. }
  154. //
  155. textureData.version = texture.version;
  156. }
  157. getSize( texture, target = _size ) {
  158. let image = texture.images ? texture.images[ 0 ] : texture.image;
  159. if ( image ) {
  160. if ( image.image !== undefined ) image = image.image;
  161. target.width = image.width;
  162. target.height = image.height;
  163. target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
  164. } else {
  165. target.width = target.height = target.depth = 1;
  166. }
  167. return target;
  168. }
  169. getMipLevels( texture, width, height ) {
  170. let mipLevelCount;
  171. if ( texture.isCompressedTexture ) {
  172. mipLevelCount = texture.mipmaps.length;
  173. } else {
  174. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  175. }
  176. return mipLevelCount;
  177. }
  178. needsMipmaps( texture ) {
  179. if ( this.isEnvironmentTexture( texture ) ) return true;
  180. return ( texture.isCompressedTexture === true ) || ( ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter ) );
  181. }
  182. isEnvironmentTexture( texture ) {
  183. const mapping = texture.mapping;
  184. return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
  185. }
  186. _destroyTexture( texture ) {
  187. this.backend.destroySampler( texture );
  188. this.backend.destroyTexture( texture );
  189. this.delete( texture );
  190. }
  191. }
  192. export default Textures;