mirror of
https://github.com/billsonnn/nitro-renderer.git
synced 2024-11-22 23:50:52 +01:00
Add TexturePool
This commit is contained in:
parent
036a03f7d0
commit
49fe53e48d
5
packages/utils/src/GetTexturePool.ts
Normal file
5
packages/utils/src/GetTexturePool.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { TexturePool } from './TexturePool';
|
||||
|
||||
const texturePool = new TexturePool();
|
||||
|
||||
export const GetTexturePool = () => texturePool;
|
105
packages/utils/src/TexturePool.ts
Normal file
105
packages/utils/src/TexturePool.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import { NitroLogger, TextureUtils } from '@nitrots/utils';
|
||||
import { Texture } from 'pixi.js';
|
||||
|
||||
export class TexturePool
|
||||
{
|
||||
private static MAX_IDLE: number = 3600;
|
||||
|
||||
private _textures: { [index: string]: { [index: string]: Texture[] } } = {};
|
||||
private _totalTextures: number = 0;
|
||||
private _runCount: number = 0;
|
||||
|
||||
public getTotalTextures(): number
|
||||
{
|
||||
let total = 0;
|
||||
|
||||
for(const width in this._textures)
|
||||
{
|
||||
for(const height in this._textures[width])
|
||||
{
|
||||
total += this._textures[width][height].length;
|
||||
}
|
||||
}
|
||||
|
||||
this._totalTextures = total;
|
||||
|
||||
return this._totalTextures;
|
||||
}
|
||||
|
||||
public getTexture(width: number, height: number): Texture
|
||||
{
|
||||
if(!this._textures[width]) this._textures[width] = {};
|
||||
|
||||
if(!this._textures[width][height]) this._textures[width][height] = [];
|
||||
|
||||
if(this._textures[width][height].length)
|
||||
{
|
||||
const texture = this._textures[width][height].shift();
|
||||
|
||||
if(texture)
|
||||
{
|
||||
this._totalTextures--;
|
||||
|
||||
return texture;
|
||||
}
|
||||
}
|
||||
|
||||
return TextureUtils.createRenderTexture(width, height);
|
||||
}
|
||||
|
||||
public putTexture(texture: Texture)
|
||||
{
|
||||
if(!texture) return;
|
||||
|
||||
if(!this._textures[texture.width]) this._textures[texture.width] = {};
|
||||
|
||||
if(!this._textures[texture.width][texture.height]) this._textures[texture.width][texture.height] = [];
|
||||
|
||||
//@ts-ignore
|
||||
delete texture.source.hitMap;
|
||||
|
||||
this._textures[texture.width][texture.height].push(texture);
|
||||
|
||||
this._totalTextures++;
|
||||
}
|
||||
|
||||
public run(): void
|
||||
{
|
||||
this._runCount++;
|
||||
|
||||
if(!this._totalTextures) return;
|
||||
|
||||
for(const width in this._textures)
|
||||
{
|
||||
for(const height in this._textures[width])
|
||||
{
|
||||
const textures = this._textures[width][height];
|
||||
|
||||
for(let i = textures.length - 1; i >= 0; i--)
|
||||
{
|
||||
const texture = textures[i];
|
||||
const source = texture.source;
|
||||
|
||||
if((source._touched > -1) && (this._runCount - source._touched) > TexturePool.MAX_IDLE)
|
||||
{
|
||||
//@ts-ignore
|
||||
delete texture.source.hitMap;
|
||||
|
||||
if(!source.destroyed) texture.destroy(true);
|
||||
|
||||
this._textures[texture.width][texture.height].splice(i, 1);
|
||||
|
||||
this._totalTextures--;
|
||||
|
||||
NitroLogger.log(`[TexturePool] Texture disposed: ${texture.width}x${texture.height}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public get textures(): { [index: string]: { [index: string]: Texture[] } }
|
||||
{
|
||||
return this._textures;
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ export * from './FurniId';
|
||||
export * from './GetPixi';
|
||||
export * from './GetRenderer';
|
||||
export * from './GetStage';
|
||||
export * from './GetTexturePool';
|
||||
export * from './GetTicker';
|
||||
export * from './GetTickerFPS';
|
||||
export * from './GetTickerTime';
|
||||
@ -23,6 +24,7 @@ export * from './Node3D';
|
||||
export * from './NumberBank';
|
||||
export * from './PointMath';
|
||||
export * from './RoomId';
|
||||
export * from './TexturePool';
|
||||
export * from './TextureUtils';
|
||||
export * from './Vector3d';
|
||||
export * from './filters';
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { GetRoomEngine, RoomEngine } from '@nitrots/room';
|
||||
import { GetRenderer } from '@nitrots/utils';
|
||||
import { TextureSource } from 'pixi.js';
|
||||
import { GetRenderer, GetTexturePool } from '@nitrots/utils';
|
||||
import { Texture, TextureGCSystem, TextureSource } from 'pixi.js';
|
||||
export { };
|
||||
|
||||
declare global
|
||||
@ -9,13 +9,17 @@ declare global
|
||||
{
|
||||
NitroDevTools?:
|
||||
{
|
||||
getRoomEngine(): RoomEngine;
|
||||
showTextureCache(): TextureSource<any>[];
|
||||
roomEngine(): RoomEngine;
|
||||
textureCache(): TextureSource<any>[];
|
||||
texturePool(): { [index: string]: { [index: string]: Texture[] } };
|
||||
textureGC(): TextureGCSystem;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
window.NitroDevTools = {
|
||||
getRoomEngine: () => GetRoomEngine(),
|
||||
showTextureCache: () => GetRenderer().texture.managedTextures,
|
||||
roomEngine: () => GetRoomEngine(),
|
||||
textureCache: () => GetRenderer().texture.managedTextures,
|
||||
texturePool: () => GetTexturePool().textures,
|
||||
textureGC: () => GetRenderer().textureGC
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user