From edd5ef308f94c2486ad2d887ee84697bdd1771e2 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Fri, 30 Aug 2019 16:59:23 +0800 Subject: [PATCH] image asset shared texture2d in texture map (#5296) save --- cocos/core/3d/builtin/init.ts | 3 +- cocos/core/assets/image-asset.ts | 17 +++++ cocos/core/assets/sprite-frame.ts | 86 ++++++++++--------------- cocos/core/assets/texture-2d.ts | 1 + cocos/ui/assembler/label/letter-font.ts | 19 +++++- cocos/ui/components/label-component.ts | 18 +++++- 6 files changed, 85 insertions(+), 59 deletions(-) diff --git a/cocos/core/3d/builtin/init.ts b/cocos/core/3d/builtin/init.ts index 8bd1b96e78..39aff6ed59 100644 --- a/cocos/core/3d/builtin/init.ts +++ b/cocos/core/3d/builtin/init.ts @@ -118,7 +118,8 @@ class BuiltinResMgr { resources[defaultCubeTexture._uuid] = defaultCubeTexture; const spriteFrame = new SpriteFrame(); - (spriteFrame.texture as Texture2D).image = imgAsset; + const texture = imgAsset._texture; + spriteFrame.texture = texture; spriteFrame._uuid = 'default-spriteframe'; resources[spriteFrame._uuid] = spriteFrame; diff --git a/cocos/core/assets/image-asset.ts b/cocos/core/assets/image-asset.ts index 91a264b252..af0cd984ea 100644 --- a/cocos/core/assets/image-asset.ts +++ b/cocos/core/assets/image-asset.ts @@ -32,6 +32,7 @@ import {ccclass, property} from '../data/class-decorator'; import { GFXDevice, GFXFeature } from '../gfx/device'; import { Asset } from './asset'; import { PixelFormat } from './asset-enum'; +import { Texture2D } from './texture-2d'; /** * 内存图像源。 @@ -112,10 +113,26 @@ export class ImageAsset extends Asset { return this._url; } + set _texture (tex) { + this._tex = tex; + } + + get _texture () { + if (!this._tex) { + let tex = new cc.Texture2D(); + tex.name = this._url; + tex.image = this; + this._tex = tex; + } + return this._tex; + } + private static extnames = ['.png', '.jpg', '.jpeg', '.bmp', '.webp', '.pvr', '.pkm']; private _nativeData: ImageSource; + private _tex; + private _url: string; private _exportedExts: string[] | null | undefined = undefined; diff --git a/cocos/core/assets/sprite-frame.ts b/cocos/core/assets/sprite-frame.ts index ef50b14a47..af1b0881dc 100644 --- a/cocos/core/assets/sprite-frame.ts +++ b/cocos/core/assets/sprite-frame.ts @@ -154,7 +154,8 @@ const temp_uvs: IUV[] = [{ u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, * const node = new Node("New Sprite"); * const sprite = node.addComponent(SpriteComponent); * const spriteFrame = new SpriteFrame(); - * (spriteFrame.texture as Texture2D).image = imageAsset; + * const tex = imageAsset._texture; + * spriteFrame.texture = tex; * sprite.spriteFrame = spriteFrame; * node.parent = self.node; * }); @@ -274,18 +275,29 @@ export class SpriteFrame extends Asset { this._calculateUV(); } - set _imageSource(value: ImageAsset) { + set _imageSource (value: ImageAsset) { this._image = value; - const tex = this._texture as Texture2D; - tex.image = this._image; + // const tex = this._texture as Texture2D; + // @ts-ignore + if (window.Editor && Editor.isBuilder){ + return; + } + this._texture = value._texture; + this._texture.on('load', this._textureLoaded, this); this._calculateUV(); } - get texture (){ + get texture () { + if (!this._texture) { + // @ts-ignore + this._texture = this._image && !(window.Editor && Editor.isBuilder) ? this._image._texture : new Texture2D(); + this._texture.on('load', this._textureLoaded, this); + } + return this._texture; } - set texture(value){ + set texture (value){ if(!value){ console.warn(`Error Texture in ${this.name}`); return; @@ -305,10 +317,6 @@ export class SpriteFrame extends Asset { this._atlasUuid = value; } - get original (){ - return this._original; - } - get width () { return this._texture.width; } @@ -346,9 +354,6 @@ export class SpriteFrame extends Asset { protected _capInsets = [0, 0, 0, 0]; - // store original info before packed to dynamic atlas - protected _original: ISpriteFrameOriginal | null = null; - protected _atlasUuid: string = ''; // @ts-ignore protected _texture: TextureBase; @@ -362,9 +367,6 @@ export class SpriteFrame extends Asset { // Atlas asset uuid this._atlasUuid = ''; } - - this._texture = new Texture2D(); - this._texture.on('load', this._textureLoaded, this); } /** @@ -521,7 +523,9 @@ export class SpriteFrame extends Asset { this._rect.x = this._rect.y = 0; this._rect.width = info.texture.width; this._rect.height = info.texture.height; - this._texture.off('load'); + if(this._texture){ + this._texture.off('load'); + } this._texture = info.texture; this.checkRect(this._texture); this._texture.on('load', this._textureLoaded, this); @@ -606,40 +610,16 @@ export class SpriteFrame extends Asset { return true; } - public onLoaded (){ - this.loaded = true; - this.emit('load'); + public onLoaded () { + this.loaded = true; + this.emit('load'); } - public _setDynamicAtlasFrame (frame: SpriteFrame) { - if (!frame || this._texture instanceof RenderTexture) { - return; + public destroy (){ + if(this._texture){ + this._texture.off('load'); } - - this._original = { - spriteframe: this, - x: this._rect.x, - y: this._rect.y, - }; - - this._rect.x = frame._rect.x; - this._rect.y = frame._rect.y; - this._image = frame._image; - (this._texture as Texture2D).image = this._image; - // this._calculateUV(); - } - - public _resetDynamicAtlasFrame () { - if (!this._original || this._texture instanceof RenderTexture) { - return; - } - - this._rect.x = this._original.x; - this._rect.y = this._original.y; - this._image = this._original.spriteframe._image; - (this._texture as Texture2D).image = this._image; - this._original = null; - // this._calculateUV(); + return super.destroy(); } /* @@ -649,8 +629,9 @@ export class SpriteFrame extends Asset { public _calculateSlicedUV () { const rect = this._rect; // const texture = this._getCalculateTarget()!; - const atlasWidth = this._texture.width; - const atlasHeight = this._texture.height; + const tex = this.texture; + const atlasWidth = tex.width; + const atlasHeight = tex.height; const leftWidth = this._capInsets[INSET_LEFT]; const rightWidth = this._capInsets[INSET_RIGHT]; const centerWidth = rect.width - leftWidth - rightWidth; @@ -710,8 +691,9 @@ export class SpriteFrame extends Asset { public _calculateUV () { const rect = this._rect; const uv = this.uv; - const texw = this._texture.width; - const texh = this._texture.height; + const tex = this.texture; + const texw = tex.width; + const texh = tex.height; if (this._rotated) { const l = texw === 0 ? 0 : rect.x / texw; diff --git a/cocos/core/assets/texture-2d.ts b/cocos/core/assets/texture-2d.ts index 3b370f1674..ee2bd04fe9 100644 --- a/cocos/core/assets/texture-2d.ts +++ b/cocos/core/assets/texture-2d.ts @@ -237,6 +237,7 @@ export class Texture2D extends SimpleTexture { } const mipmapUUID = data.mipmaps[i]; handle.result.push(this._mipmaps, `${i}`, mipmapUUID); + this._mipmaps[i]._texture = this; } } diff --git a/cocos/ui/assembler/label/letter-font.ts b/cocos/ui/assembler/label/letter-font.ts index a96c332cd6..91d38679df 100644 --- a/cocos/ui/assembler/label/letter-font.ts +++ b/cocos/ui/assembler/label/letter-font.ts @@ -92,6 +92,7 @@ class LetterTexture { public width = 0; public height = 0; public hash: string; + private _lastImage: ImageAsset | null = null; constructor (char: string, labelInfo: ILabelInfo) { this.char = char; this.labelInfo = labelInfo; @@ -104,7 +105,12 @@ class LetterTexture { } public destroy () { - // this.spriteframe!.destroy(); + if (!this.spriteframe) { + return; + } + + this.spriteframe.texture.destroy(); + this.spriteframe.destroy(); this.spriteframe = null; // LabelComponent._canvasPool.put(this._data); } @@ -129,9 +135,16 @@ class LetterTexture { this.canvas.height = this.height; } - // this.texture.initWithElement(this.canvas); + // Note: after the optimization + if (this._lastImage) { + this._lastImage._texture.destroy(); + this._lastImage.destroy(); + } + const image = new ImageAsset(this.canvas); - (this.spriteframe.texture as Texture2D).image = image; + this._lastImage = image; + const tex = image._texture; + this.spriteframe.texture = tex; } private _updateTexture () { diff --git a/cocos/ui/components/label-component.ts b/cocos/ui/components/label-component.ts index 621940eff7..2356f1f56b 100644 --- a/cocos/ui/components/label-component.ts +++ b/cocos/ui/components/label-component.ts @@ -643,10 +643,22 @@ export class LabelComponent extends UIRenderComponent { this._assemblerData = null; if (this._ttfSpriteFrame) { - // this._ttfSpriteFrame.destroy(); + const tex = this._ttfSpriteFrame.texture; + if (tex) { + const tex2d = tex as Texture2D; + if (tex2d.image) { + tex2d.image.destroy(); + } + tex.destroy(); + } this._ttfSpriteFrame = null; } + if (this._letterTexture) { + this._letterTexture.destroy(); + this._letterTexture = null; + } + super.onDestroy(); } @@ -744,9 +756,9 @@ export class LabelComponent extends UIRenderComponent { } else if (!this._ttfSpriteFrame) { this._ttfSpriteFrame = new SpriteFrame(); this._assemblerData = this._assembler!.getAssemblerData(); - // this._ttfSpriteFrame.initWithElement(this._assemblerData.canvas); const image = new ImageAsset(this._assemblerData!.canvas); - (this._ttfSpriteFrame.texture as Texture2D).image = image; + const tex = image._texture; + this._ttfSpriteFrame.texture = tex; } if (this.cacheMode !== CacheMode.CHAR) {