image asset shared texture2d in texture map (#5296)

save
This commit is contained in:
JoneLau
2019-08-30 16:59:23 +08:00
committed by pandamicro
parent d6301e14a1
commit edd5ef308f
6 changed files with 85 additions and 59 deletions
+2 -1
View File
@@ -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;
+17
View File
@@ -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;
+34 -52
View File
@@ -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;
+1
View File
@@ -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;
}
}
+16 -3
View File
@@ -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 () {
+15 -3
View File
@@ -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) {