| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- import DataMap from './DataMap.js';
- import { Vector3 } from '../../math/Vector3.js';
- import { DepthTexture } from '../../textures/DepthTexture.js';
- import { DepthStencilFormat, DepthFormat, UnsignedIntType, UnsignedInt248Type, EquirectangularReflectionMapping, EquirectangularRefractionMapping, CubeReflectionMapping, CubeRefractionMapping, UnsignedByteType } from '../../constants.js';
- const _size = /*@__PURE__*/ new Vector3();
- /**
- * This module manages the textures of the renderer.
- *
- * @private
- */
- class Textures extends DataMap {
- /**
- * Constructs a new texture management component.
- *
- * @param {Renderer} renderer - The renderer.
- * @param {Backend} backend - The renderer's backend.
- * @param {Info} info - Renderer component for managing metrics and monitoring data.
- */
- constructor( renderer, backend, info ) {
- super();
- /**
- * The renderer.
- *
- * @type {Renderer}
- */
- this.renderer = renderer;
- /**
- * The backend.
- *
- * @type {Backend}
- */
- this.backend = backend;
- /**
- * Renderer component for managing metrics and monitoring data.
- *
- * @type {Info}
- */
- this.info = info;
- }
- /**
- * Updates the given render target. Based on the given render target configuration,
- * it updates the texture states representing the attachments of the framebuffer.
- *
- * @param {RenderTarget} renderTarget - The render target to update.
- * @param {Number} [activeMipmapLevel=0] - The active mipmap level.
- */
- updateRenderTarget( renderTarget, activeMipmapLevel = 0 ) {
- const renderTargetData = this.get( renderTarget );
- const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
- const depthTextureMips = renderTargetData.depthTextureMips || ( renderTargetData.depthTextureMips = {} );
- const textures = renderTarget.textures;
- const size = this.getSize( textures[ 0 ] );
- const mipWidth = size.width >> activeMipmapLevel;
- const mipHeight = size.height >> activeMipmapLevel;
- let depthTexture = renderTarget.depthTexture || depthTextureMips[ activeMipmapLevel ];
- const useDepthTexture = renderTarget.depthBuffer === true || renderTarget.stencilBuffer === true;
- let textureNeedsUpdate = false;
- if ( depthTexture === undefined && useDepthTexture ) {
- depthTexture = new DepthTexture();
- depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
- depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType
- depthTexture.image.width = mipWidth;
- depthTexture.image.height = mipHeight;
- depthTextureMips[ activeMipmapLevel ] = depthTexture;
- }
- if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
- textureNeedsUpdate = true;
- if ( depthTexture ) {
- depthTexture.needsUpdate = true;
- depthTexture.image.width = mipWidth;
- depthTexture.image.height = mipHeight;
- }
- }
- renderTargetData.width = size.width;
- renderTargetData.height = size.height;
- renderTargetData.textures = textures;
- renderTargetData.depthTexture = depthTexture || null;
- renderTargetData.depth = renderTarget.depthBuffer;
- renderTargetData.stencil = renderTarget.stencilBuffer;
- renderTargetData.renderTarget = renderTarget;
- if ( renderTargetData.sampleCount !== sampleCount ) {
- textureNeedsUpdate = true;
- if ( depthTexture ) {
- depthTexture.needsUpdate = true;
- }
- renderTargetData.sampleCount = sampleCount;
- }
- //
- const options = { sampleCount };
- for ( let i = 0; i < textures.length; i ++ ) {
- const texture = textures[ i ];
- if ( textureNeedsUpdate ) texture.needsUpdate = true;
- this.updateTexture( texture, options );
- }
- if ( depthTexture ) {
- this.updateTexture( depthTexture, options );
- }
- // dispose handler
- if ( renderTargetData.initialized !== true ) {
- renderTargetData.initialized = true;
- // dispose
- const onDispose = () => {
- renderTarget.removeEventListener( 'dispose', onDispose );
- for ( let i = 0; i < textures.length; i ++ ) {
- this._destroyTexture( textures[ i ] );
- }
- if ( depthTexture ) {
- this._destroyTexture( depthTexture );
- }
- this.delete( renderTarget );
- };
- renderTarget.addEventListener( 'dispose', onDispose );
- }
- }
- /**
- * Updates the given texture. Depending on the texture state, this method
- * triggers the upload of texture data to the GPU memory. If the texture data are
- * not yet ready for the upload, it uses default texture data for as a placeholder.
- *
- * @param {Texture} texture - The texture to update.
- * @param {Object} [options={}] - The options.
- */
- updateTexture( texture, options = {} ) {
- const textureData = this.get( texture );
- if ( textureData.initialized === true && textureData.version === texture.version ) return;
- const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
- const backend = this.backend;
- if ( isRenderTarget && textureData.initialized === true ) {
- // it's an update
- backend.destroySampler( texture );
- backend.destroyTexture( texture );
- }
- //
- if ( texture.isFramebufferTexture ) {
- const renderTarget = this.renderer.getRenderTarget();
- if ( renderTarget ) {
- texture.type = renderTarget.texture.type;
- } else {
- texture.type = UnsignedByteType;
- }
- }
- //
- const { width, height, depth } = this.getSize( texture );
- options.width = width;
- options.height = height;
- options.depth = depth;
- options.needsMipmaps = this.needsMipmaps( texture );
- options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
- //
- if ( isRenderTarget || texture.isStorageTexture === true ) {
- backend.createSampler( texture );
- backend.createTexture( texture, options );
- textureData.generation = texture.version;
- } else {
- const needsCreate = textureData.initialized !== true;
- if ( needsCreate ) backend.createSampler( texture );
- if ( texture.version > 0 ) {
- const image = texture.image;
- if ( image === undefined ) {
- console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
- } else if ( image.complete === false ) {
- console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
- } else {
- if ( texture.images ) {
- const images = [];
- for ( const image of texture.images ) {
- images.push( image );
- }
- options.images = images;
- } else {
- options.image = image;
- }
- if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
- backend.createTexture( texture, options );
- textureData.isDefaultTexture = false;
- textureData.generation = texture.version;
- }
- if ( texture.source.dataReady === true ) backend.updateTexture( texture, options );
- if ( options.needsMipmaps && texture.mipmaps.length === 0 ) backend.generateMipmaps( texture );
- }
- } else {
- // async update
- backend.createDefaultTexture( texture );
- textureData.isDefaultTexture = true;
- textureData.generation = texture.version;
- }
- }
- // dispose handler
- if ( textureData.initialized !== true ) {
- textureData.initialized = true;
- textureData.generation = texture.version;
- //
- this.info.memory.textures ++;
- // dispose
- const onDispose = () => {
- texture.removeEventListener( 'dispose', onDispose );
- this._destroyTexture( texture );
- this.info.memory.textures --;
- };
- texture.addEventListener( 'dispose', onDispose );
- }
- //
- textureData.version = texture.version;
- }
- /**
- * Computes the size of the given texture and writes the result
- * into the target vector. This vector is also returned by the
- * method.
- *
- * If no texture data are available for the compute yet, the method
- * returns default size values.
- *
- * @param {Texture} texture - The texture to compute the size for.
- * @param {Vector3} target - The target vector.
- * @return {Vector3} The target vector.
- */
- getSize( texture, target = _size ) {
- let image = texture.images ? texture.images[ 0 ] : texture.image;
- if ( image ) {
- if ( image.image !== undefined ) image = image.image;
- target.width = image.width || 1;
- target.height = image.height || 1;
- target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
- } else {
- target.width = target.height = target.depth = 1;
- }
- return target;
- }
- /**
- * Computes the number of mipmap levels for the given texture.
- *
- * @param {Texture} texture - The texture.
- * @param {Number} width - The texture's width.
- * @param {Number} height - The texture's height.
- * @return {Number} The number of mipmap levels.
- */
- getMipLevels( texture, width, height ) {
- let mipLevelCount;
- if ( texture.isCompressedTexture ) {
- if ( texture.mipmaps ) {
- mipLevelCount = texture.mipmaps.length;
- } else {
- mipLevelCount = 1;
- }
- } else {
- mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
- }
- return mipLevelCount;
- }
- /**
- * Returns `true` if the given texture requires mipmaps.
- *
- * @param {Texture} texture - The texture.
- * @return {Boolean} Whether mipmaps are required or not.
- */
- needsMipmaps( texture ) {
- return this.isEnvironmentTexture( texture ) || texture.isCompressedTexture === true || texture.generateMipmaps;
- }
- /**
- * Returns `true` if the given texture is an environment map.
- *
- * @param {Texture} texture - The texture.
- * @return {Boolean} Whether the given texture is an environment map or not.
- */
- isEnvironmentTexture( texture ) {
- const mapping = texture.mapping;
- return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
- }
- /**
- * Frees internal resource when the given texture isn't
- * required anymore.
- *
- * @param {Texture} texture - The texture to destroy.
- */
- _destroyTexture( texture ) {
- this.backend.destroySampler( texture );
- this.backend.destroyTexture( texture );
- this.delete( texture );
- }
- }
- export default Textures;
|