Textures.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import DataMap from './DataMap.js';
  2. import { Vector3 } from '../../math/Vector3.js';
  3. import { DepthTexture } from '../../textures/DepthTexture.js';
  4. import { DepthStencilFormat, DepthFormat, UnsignedIntType, UnsignedInt248Type, EquirectangularReflectionMapping, EquirectangularRefractionMapping, CubeReflectionMapping, CubeRefractionMapping, UnsignedByteType } from '../../constants.js';
  5. const _size = /*@__PURE__*/ new Vector3();
  6. /**
  7. * This module manages the textures of the renderer.
  8. *
  9. * @private
  10. */
  11. class Textures extends DataMap {
  12. /**
  13. * Constructs a new texture management component.
  14. *
  15. * @param {Renderer} renderer - The renderer.
  16. * @param {Backend} backend - The renderer's backend.
  17. * @param {Info} info - Renderer component for managing metrics and monitoring data.
  18. */
  19. constructor( renderer, backend, info ) {
  20. super();
  21. /**
  22. * The renderer.
  23. *
  24. * @type {Renderer}
  25. */
  26. this.renderer = renderer;
  27. /**
  28. * The backend.
  29. *
  30. * @type {Backend}
  31. */
  32. this.backend = backend;
  33. /**
  34. * Renderer component for managing metrics and monitoring data.
  35. *
  36. * @type {Info}
  37. */
  38. this.info = info;
  39. }
  40. /**
  41. * Updates the given render target. Based on the given render target configuration,
  42. * it updates the texture states representing the attachments of the framebuffer.
  43. *
  44. * @param {RenderTarget} renderTarget - The render target to update.
  45. * @param {Number} [activeMipmapLevel=0] - The active mipmap level.
  46. */
  47. updateRenderTarget( renderTarget, activeMipmapLevel = 0 ) {
  48. const renderTargetData = this.get( renderTarget );
  49. const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
  50. const depthTextureMips = renderTargetData.depthTextureMips || ( renderTargetData.depthTextureMips = {} );
  51. const textures = renderTarget.textures;
  52. const size = this.getSize( textures[ 0 ] );
  53. const mipWidth = size.width >> activeMipmapLevel;
  54. const mipHeight = size.height >> activeMipmapLevel;
  55. let depthTexture = renderTarget.depthTexture || depthTextureMips[ activeMipmapLevel ];
  56. const useDepthTexture = renderTarget.depthBuffer === true || renderTarget.stencilBuffer === true;
  57. let textureNeedsUpdate = false;
  58. if ( depthTexture === undefined && useDepthTexture ) {
  59. depthTexture = new DepthTexture();
  60. depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
  61. depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType
  62. depthTexture.image.width = mipWidth;
  63. depthTexture.image.height = mipHeight;
  64. depthTextureMips[ activeMipmapLevel ] = depthTexture;
  65. }
  66. if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
  67. textureNeedsUpdate = true;
  68. if ( depthTexture ) {
  69. depthTexture.needsUpdate = true;
  70. depthTexture.image.width = mipWidth;
  71. depthTexture.image.height = mipHeight;
  72. }
  73. }
  74. renderTargetData.width = size.width;
  75. renderTargetData.height = size.height;
  76. renderTargetData.textures = textures;
  77. renderTargetData.depthTexture = depthTexture || null;
  78. renderTargetData.depth = renderTarget.depthBuffer;
  79. renderTargetData.stencil = renderTarget.stencilBuffer;
  80. renderTargetData.renderTarget = renderTarget;
  81. if ( renderTargetData.sampleCount !== sampleCount ) {
  82. textureNeedsUpdate = true;
  83. if ( depthTexture ) {
  84. depthTexture.needsUpdate = true;
  85. }
  86. renderTargetData.sampleCount = sampleCount;
  87. }
  88. //
  89. const options = { sampleCount };
  90. for ( let i = 0; i < textures.length; i ++ ) {
  91. const texture = textures[ i ];
  92. if ( textureNeedsUpdate ) texture.needsUpdate = true;
  93. this.updateTexture( texture, options );
  94. }
  95. if ( depthTexture ) {
  96. this.updateTexture( depthTexture, options );
  97. }
  98. // dispose handler
  99. if ( renderTargetData.initialized !== true ) {
  100. renderTargetData.initialized = true;
  101. // dispose
  102. const onDispose = () => {
  103. renderTarget.removeEventListener( 'dispose', onDispose );
  104. for ( let i = 0; i < textures.length; i ++ ) {
  105. this._destroyTexture( textures[ i ] );
  106. }
  107. if ( depthTexture ) {
  108. this._destroyTexture( depthTexture );
  109. }
  110. this.delete( renderTarget );
  111. };
  112. renderTarget.addEventListener( 'dispose', onDispose );
  113. }
  114. }
  115. /**
  116. * Updates the given texture. Depending on the texture state, this method
  117. * triggers the upload of texture data to the GPU memory. If the texture data are
  118. * not yet ready for the upload, it uses default texture data for as a placeholder.
  119. *
  120. * @param {Texture} texture - The texture to update.
  121. * @param {Object} [options={}] - The options.
  122. */
  123. updateTexture( texture, options = {} ) {
  124. const textureData = this.get( texture );
  125. if ( textureData.initialized === true && textureData.version === texture.version ) return;
  126. const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
  127. const backend = this.backend;
  128. if ( isRenderTarget && textureData.initialized === true ) {
  129. // it's an update
  130. backend.destroySampler( texture );
  131. backend.destroyTexture( texture );
  132. }
  133. //
  134. if ( texture.isFramebufferTexture ) {
  135. const renderTarget = this.renderer.getRenderTarget();
  136. if ( renderTarget ) {
  137. texture.type = renderTarget.texture.type;
  138. } else {
  139. texture.type = UnsignedByteType;
  140. }
  141. }
  142. //
  143. const { width, height, depth } = this.getSize( texture );
  144. options.width = width;
  145. options.height = height;
  146. options.depth = depth;
  147. options.needsMipmaps = this.needsMipmaps( texture );
  148. options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
  149. //
  150. if ( isRenderTarget || texture.isStorageTexture === true ) {
  151. backend.createSampler( texture );
  152. backend.createTexture( texture, options );
  153. textureData.generation = texture.version;
  154. } else {
  155. const needsCreate = textureData.initialized !== true;
  156. if ( needsCreate ) backend.createSampler( texture );
  157. if ( texture.version > 0 ) {
  158. const image = texture.image;
  159. if ( image === undefined ) {
  160. console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
  161. } else if ( image.complete === false ) {
  162. console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
  163. } else {
  164. if ( texture.images ) {
  165. const images = [];
  166. for ( const image of texture.images ) {
  167. images.push( image );
  168. }
  169. options.images = images;
  170. } else {
  171. options.image = image;
  172. }
  173. if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
  174. backend.createTexture( texture, options );
  175. textureData.isDefaultTexture = false;
  176. textureData.generation = texture.version;
  177. }
  178. if ( texture.source.dataReady === true ) backend.updateTexture( texture, options );
  179. if ( options.needsMipmaps && texture.mipmaps.length === 0 ) backend.generateMipmaps( texture );
  180. }
  181. } else {
  182. // async update
  183. backend.createDefaultTexture( texture );
  184. textureData.isDefaultTexture = true;
  185. textureData.generation = texture.version;
  186. }
  187. }
  188. // dispose handler
  189. if ( textureData.initialized !== true ) {
  190. textureData.initialized = true;
  191. textureData.generation = texture.version;
  192. //
  193. this.info.memory.textures ++;
  194. // dispose
  195. const onDispose = () => {
  196. texture.removeEventListener( 'dispose', onDispose );
  197. this._destroyTexture( texture );
  198. this.info.memory.textures --;
  199. };
  200. texture.addEventListener( 'dispose', onDispose );
  201. }
  202. //
  203. textureData.version = texture.version;
  204. }
  205. /**
  206. * Computes the size of the given texture and writes the result
  207. * into the target vector. This vector is also returned by the
  208. * method.
  209. *
  210. * If no texture data are available for the compute yet, the method
  211. * returns default size values.
  212. *
  213. * @param {Texture} texture - The texture to compute the size for.
  214. * @param {Vector3} target - The target vector.
  215. * @return {Vector3} The target vector.
  216. */
  217. getSize( texture, target = _size ) {
  218. let image = texture.images ? texture.images[ 0 ] : texture.image;
  219. if ( image ) {
  220. if ( image.image !== undefined ) image = image.image;
  221. target.width = image.width || 1;
  222. target.height = image.height || 1;
  223. target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
  224. } else {
  225. target.width = target.height = target.depth = 1;
  226. }
  227. return target;
  228. }
  229. /**
  230. * Computes the number of mipmap levels for the given texture.
  231. *
  232. * @param {Texture} texture - The texture.
  233. * @param {Number} width - The texture's width.
  234. * @param {Number} height - The texture's height.
  235. * @return {Number} The number of mipmap levels.
  236. */
  237. getMipLevels( texture, width, height ) {
  238. let mipLevelCount;
  239. if ( texture.isCompressedTexture ) {
  240. if ( texture.mipmaps ) {
  241. mipLevelCount = texture.mipmaps.length;
  242. } else {
  243. mipLevelCount = 1;
  244. }
  245. } else {
  246. mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
  247. }
  248. return mipLevelCount;
  249. }
  250. /**
  251. * Returns `true` if the given texture requires mipmaps.
  252. *
  253. * @param {Texture} texture - The texture.
  254. * @return {Boolean} Whether mipmaps are required or not.
  255. */
  256. needsMipmaps( texture ) {
  257. return this.isEnvironmentTexture( texture ) || texture.isCompressedTexture === true || texture.generateMipmaps;
  258. }
  259. /**
  260. * Returns `true` if the given texture is an environment map.
  261. *
  262. * @param {Texture} texture - The texture.
  263. * @return {Boolean} Whether the given texture is an environment map or not.
  264. */
  265. isEnvironmentTexture( texture ) {
  266. const mapping = texture.mapping;
  267. return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
  268. }
  269. /**
  270. * Frees internal resource when the given texture isn't
  271. * required anymore.
  272. *
  273. * @param {Texture} texture - The texture to destroy.
  274. */
  275. _destroyTexture( texture ) {
  276. this.backend.destroySampler( texture );
  277. this.backend.destroyTexture( texture );
  278. this.delete( texture );
  279. }
  280. }
  281. export default Textures;
粤ICP备19079148号