2
0

Textures.js 7.3 KB

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