diff --git a/cocos/3d/assets/texture-cube.ts b/cocos/3d/assets/texture-cube.ts index 524210ff39..b12a215444 100644 --- a/cocos/3d/assets/texture-cube.ts +++ b/cocos/3d/assets/texture-cube.ts @@ -69,20 +69,6 @@ enum FaceIndex { export class TextureCube extends TrivialTexture { public static FaceIndex = FaceIndex; - /** - * 此贴图的像素宽度。 - */ - public get width (): number { - return this._width; - } - - /** - * 此贴图的像素高度。 - */ - public get height (): number { - return this._height; - } - /** * 所有层级 Mipmap,注意,这里不包含自动生成的 Mipmap。 * 当设置 Mipmap 时,贴图的尺寸以及像素格式可能会改变。 @@ -168,8 +154,6 @@ export class TextureCube extends TrivialTexture { @property public _mipmaps: ITextureCubeMipmap[] = []; - private _width: number = 0; - private _height: number = 0; constructor () { super(); diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index 07b8539320..e08496b894 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -2,12 +2,9 @@ import { Asset } from '../../assets/asset'; import { ImageAsset } from '../../assets/image-asset'; import { SpriteFrame } from '../../assets/sprite-frame'; import { Texture2D } from '../../assets/texture-2d'; -import { Rect } from '../../core/math'; +import { Rect, Size } from '../../core/math'; import { GFXDevice } from '../../gfx/device'; -import { customizationManager } from '../../renderer/scene/customization-manager'; -import { Model } from '../../renderer/scene/model'; import { TextureCube } from '../assets/texture-cube'; -import { aabb } from '../geom-utils'; import effects from './effects'; class BuiltinResMgr { @@ -121,11 +118,8 @@ class BuiltinResMgr { resources[defaultCubeTexture._uuid] = defaultCubeTexture; const spriteFrame = new SpriteFrame(); + (spriteFrame.texture as Texture2D).image = imgAsset; spriteFrame._uuid = 'default-spriteframe'; - spriteFrame.setOriginalSize(cc.size(imgAsset.width, imgAsset.height)); - spriteFrame.setRect(new Rect(0, 0, imgAsset.width, imgAsset.height)); - spriteFrame.image = imgAsset; - spriteFrame.onLoaded(); resources[spriteFrame._uuid] = spriteFrame; // builtin effects diff --git a/cocos/3d/ui/assembler/label/bmfont.ts b/cocos/3d/ui/assembler/label/bmfont.ts index fe38349161..b8ebd3ed65 100644 --- a/cocos/3d/ui/assembler/label/bmfont.ts +++ b/cocos/3d/ui/assembler/label/bmfont.ts @@ -50,7 +50,7 @@ export const bmfont: IAssembler = { fillMeshVertices3D(node, renderer, comp.renderData!, comp.color); }, - appendQuad (comp: LabelComponent, texture: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number, scale: number) { + appendQuad (comp: LabelComponent, spriteframe: SpriteFrame, rect: Rect, rotated: boolean, x: number, y: number, scale: number) { const renderData = comp.renderData; if (!renderData){ return; @@ -63,9 +63,8 @@ export const bmfont: IAssembler = { renderData.indiceCount = renderData.dataLength / 2 * 3; const datas = renderData.datas; - const origin = texture.originalSize; - const texw = origin.width; - const texh = origin.height; + const texw = spriteframe.width; + const texh = spriteframe.height; const rectWidth = rect.width; const rectHeight = rect.height; diff --git a/cocos/3d/ui/assembler/label/letter-font.ts b/cocos/3d/ui/assembler/label/letter-font.ts index 5362f1fc92..632aa1eaa6 100644 --- a/cocos/3d/ui/assembler/label/letter-font.ts +++ b/cocos/3d/ui/assembler/label/letter-font.ts @@ -82,7 +82,7 @@ class FontLetterDefinition { const _backgroundStyle = 'rgba(255, 255, 255, 0.005)'; class LetterTexture { - public texture: SpriteFrame | null = null; + public spriteframe: SpriteFrame | null = null; public labelInfo: ILabelInfo; public char: string; public data: ISharedLabelData | null = null; @@ -103,13 +103,13 @@ class LetterTexture { } public destroy () { - this.texture!.destroy(); - this.texture = null; + this.spriteframe!.destroy(); + this.spriteframe = null; // LabelComponent._canvasPool.put(this._data); } private _updateProperties () { - this.texture = new SpriteFrame(); + this.spriteframe = new SpriteFrame(); this.data = LabelComponent.CanvasPool.get(); this.canvas = this.data.canvas; this.context = this.data.context; @@ -130,7 +130,7 @@ class LetterTexture { // this.texture.initWithElement(this.canvas); const image = new ImageAsset(this.canvas); - this.texture.image = image; + (this.spriteframe.texture as Texture2D).image = image; } private _updateTexture () { @@ -166,7 +166,8 @@ class LetterTexture { context.fillText(this.char, startX, startY); // this.texture.handleLoadedTexture(); - this.texture!.updateImage(); + (this.spriteframe!.texture as Texture2D).updateImage(); + } } @@ -265,7 +266,7 @@ export class LetterAtlas { } public insertLetterTexture (letterTexture: LetterTexture) { - const texture = letterTexture.texture; + const texture = letterTexture.spriteframe; const device = cc.director.root.device; if (!texture || !this.texture || !device || !texture.image) { return null; diff --git a/cocos/3d/ui/assembler/label/ttfUtils.ts b/cocos/3d/ui/assembler/label/ttfUtils.ts index f153b36f14..5be4c88ae0 100644 --- a/cocos/3d/ui/assembler/label/ttfUtils.ts +++ b/cocos/3d/ui/assembler/label/ttfUtils.ts @@ -27,7 +27,7 @@ * @hidden */ -import { SpriteFrame } from '../../../../assets'; +import { SpriteFrame, Texture2D } from '../../../../assets'; import { Component } from '../../../../components'; import { fragmentText, safeMeasureText } from '../../../../core/utils'; import { Color, Size } from '../../../../core/math'; @@ -261,7 +261,12 @@ export const ttfUtils = { // _texture.handleLoadedTexture(); if (_texture) { - _texture.image = _texture.image; + if (_texture instanceof SpriteFrame) { + const tex = (_texture.texture as Texture2D); + tex.image = tex.image; + } else { + _texture.image = _texture.image; + } } }, diff --git a/cocos/3d/ui/components/label-component.ts b/cocos/3d/ui/components/label-component.ts index 288c9a0dfc..c9d3ce087c 100644 --- a/cocos/3d/ui/components/label-component.ts +++ b/cocos/3d/ui/components/label-component.ts @@ -28,7 +28,7 @@ * @category ui */ -import { BitmapFont, Font, ImageAsset, SpriteFrame } from '../../../assets'; +import { BitmapFont, Font, ImageAsset, SpriteFrame, Texture2D } from '../../../assets'; import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { ccenum } from '../../../core/value-types/enum'; import { UI } from '../../../renderer/ui/ui'; @@ -448,7 +448,7 @@ export class LabelComponent extends UIRenderComponent { // } if (this._cacheMode === CacheMode.CHAR) { - this._ttfTexture = null; + this._ttfSpriteFrame = null; } this._cacheMode = value; @@ -600,7 +600,7 @@ export class LabelComponent extends UIRenderComponent { // 这个保存了旧项目的 file 数据 private _N$file: Font | null = null; private _texture: SpriteFrame | LetterRenderTexture | null = null; - private _ttfTexture: SpriteFrame | null = null; + private _ttfSpriteFrame: SpriteFrame | null = null; private _userDefinedFont: Font | null = null; private _assemblerData: ISharedLabelData | null = null; private _fontAtlas: FontAtlas | null = null; @@ -612,7 +612,7 @@ export class LabelComponent extends UIRenderComponent { this._userDefinedFont = null; } - this._ttfTexture = null; + this._ttfSpriteFrame = null; } public onEnable () { @@ -640,9 +640,9 @@ export class LabelComponent extends UIRenderComponent { } this._assemblerData = null; - if (this._ttfTexture) { - this._ttfTexture.destroy(); - this._ttfTexture = null; + if (this._ttfSpriteFrame) { + this._ttfSpriteFrame.destroy(); + this._ttfSpriteFrame = null; } super.onDestroy(); @@ -705,7 +705,7 @@ export class LabelComponent extends UIRenderComponent { private _flushMaterial () { const material = this._material; // Setup blend function for premultiplied ttf label texture - // if (this._texture === this._ttfTexture) { + // if (this._texture === this._ttfSpriteFrame) { // this._srcBlendFactor = macro.BlendFactor.ONE; // } else { // this._srcBlendFactor = macro.BlendFactor.SRC_ALPHA; @@ -736,17 +736,19 @@ export class LabelComponent extends UIRenderComponent { if (this.cacheMode === CacheMode.CHAR && cc.sys.browserType !== cc.sys.BROWSER_TYPE_WECHAT_GAME_SUB) { this._letterTexture = this._assembler!.getAssemblerData(); this._texture = this._letterTexture; - } else if (!this._ttfTexture) { - this._ttfTexture = new SpriteFrame(); + } else if (!this._ttfSpriteFrame) { + this._ttfSpriteFrame = new SpriteFrame(); this._assemblerData = this._assembler!.getAssemblerData(); - // this._ttfTexture.initWithElement(this._assemblerData.canvas); + // this._ttfSpriteFrame.initWithElement(this._assemblerData.canvas); const image = new ImageAsset(this._assemblerData!.canvas); - this._ttfTexture.image = image; + const tex = new Texture2D(); + tex.image = image; + this._ttfSpriteFrame.texture = tex; } if (this.cacheMode !== CacheMode.CHAR) { // this._frame._refreshTexture(this._texture); - this._texture = this._ttfTexture; + this._texture = this._ttfSpriteFrame; } this._flushMaterial(); diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 5ba553287a..808c2dc7a4 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -401,7 +401,7 @@ export class SpriteComponent extends UIRenderComponent { } if(this._spriteFrame){ - this._spriteFrame.on('change-texture-view', this._markForUpdateUvDirty, this); + this._spriteFrame.on('updated', this._markForUpdateUvDirty, this); } } @@ -438,7 +438,7 @@ export class SpriteComponent extends UIRenderComponent { } if (this._spriteFrame) { - this._spriteFrame.off('change-texture-view'); + this._spriteFrame.off('updated'); } } @@ -603,11 +603,11 @@ export class SpriteComponent extends UIRenderComponent { if(this._renderData){ if(oldFrame){ - oldFrame.off('change-texture-view'); + oldFrame.off('updated'); } if(spriteFrame){ - spriteFrame.on('change-texture-view', this._markForUpdateUvDirty, this); + spriteFrame.on('updated', this._markForUpdateUvDirty, this); } if (oldFrame && spriteFrame) { diff --git a/cocos/assets/render-texture.ts b/cocos/assets/render-texture.ts index 1fecaae54d..0ce5377788 100644 --- a/cocos/assets/render-texture.ts +++ b/cocos/assets/render-texture.ts @@ -26,12 +26,6 @@ export class RenderTexture extends TextureBase { public static DepthStencilFormat = DepthStencilFormat; private _window: GFXWindow | null = null; - @property - private _width: number = 0; - - @property - private _height: number = 0; - @property private _depthStencilFormat: DepthStencilFormat = DepthStencilFormat.NONE; diff --git a/cocos/assets/simple-texture.ts b/cocos/assets/simple-texture.ts index f80e575102..56f66521b4 100644 --- a/cocos/assets/simple-texture.ts +++ b/cocos/assets/simple-texture.ts @@ -139,6 +139,7 @@ export class SimpleTexture extends TextureBase { source = data; } this.uploadData(source, level, arrayIndex); + this._checkTextureLoaded(); }; if (image.loaded) { upload(); @@ -154,6 +155,15 @@ export class SimpleTexture extends TextureBase { } } + protected _checkTextureLoaded(){ + this._textureReady(); + } + + protected _textureReady(){ + this.loaded = true; + this.emit('load'); + } + /** * Set mipmap level of this texture. * The value is passes as presumed info to `this._getGfxTextureCreateInfo()`. diff --git a/cocos/assets/sprite-frame.ts b/cocos/assets/sprite-frame.ts index 7e2b8b7d6d..3d66d1363b 100644 --- a/cocos/assets/sprite-frame.ts +++ b/cocos/assets/sprite-frame.ts @@ -33,13 +33,12 @@ import { ccclass } from '../core/data/class-decorator'; import { Rect, Size, Vec2 } from '../core/math'; import { ImageAsset } from './image-asset'; -import { ITexture2DSerializeData } from './texture-2d'; -import * as textureUtil from './texture-util'; import { murmurhash2_32_gc } from '../core/utils/murmurhash2_gc'; -import { SimpleTexture, PresumedGFXTextureInfo, PresumedGFXTextureViewInfo } from './simple-texture'; -import { IGFXTextureInfo } from '../gfx/texture'; -import { IGFXTextureViewInfo, GFXTextureView } from '../gfx/texture-view'; -import { GFXTextureType, GFXTextureViewType } from '../gfx/define'; +import { GFXTextureView } from '../gfx/texture-view'; +import { TextureBase } from './texture-base'; +import { Asset } from './asset'; +import { Texture2D } from './texture-2d'; +import { RenderTexture } from './render-texture'; const INSET_LEFT = 0; const INSET_TOP = 1; @@ -82,9 +81,9 @@ interface ISpriteFrameOriginal { interface ISpriteFrameInitInfo { /** - * @zh ImageAsset 资源。 + * @zh Texture 对象资源。 */ - image: ImageAsset | null; + texture?: TextureBase; /** * @zh 精灵帧原始尺寸。 */ @@ -118,9 +117,9 @@ interface ISpriteFrameInitInfo { */ isRotate?: boolean; /** - * @zh 是否保持原尺寸大小。 + * @zh 是否转置 UV。 */ - keepOriginSize?: boolean; + isFlipUv?: boolean; } const temp_uvs: IUV[] = [{ u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, v: 0 }]; @@ -140,26 +139,58 @@ const temp_uvs: IUV[] = [{ u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, v: 0 }, { u: 0, * * @example * ```typescript - * const self = this; + * // First way to use a SpriteFrame * const url = "assets/PurpleMonster/icon/spriteFrame"; - * cc.loader.loadRes(url, function (err, spriteFrame) { + * cc.loader.loadRes(url, (err, spriteFrame) => { * const node = new Node("New Sprite"); * const sprite = node.addComponent(SpriteComponent); * sprite.spriteFrame = spriteFrame; * node.parent = self.node; * }); + * + * // Second way to use a SpriteFrame + * const self = this; + * const url = "assets/PurpleMonster/icon"; + * cc.loader.loadRes(url, (err, imageAsset) => { + * const node = new Node("New Sprite"); + * const sprite = node.addComponent(SpriteComponent); + * const spriteFrame = new SpriteFrame(); + * spriteFrame.initialize({ + * originalSize: new Size(imageAsset.width, imageAsset.height), + * rect: new Rect(0, 0, imageAsset.width, imageAsset.height), + * }); + * (spriteFrame.texture as Texture2D).image = imageAsset; + * sprite.spriteFrame = spriteFrame; + * node.parent = self.node; + * }); + * + * // Third way to use a SpriteFrame + * const self = this; + * const cameraComp = this.getComponent(CameraComponent); + * const renderTexture = new RenderTexture(); + * rendetTex.reset({ + * width: 512, + * height: 512, + * depthStencilFormat: RenderTexture.DepthStencilFormat.DEPTH_24_STENCIL_8 + * }); + * + * cameraComp.targetTexture = renderTexture; + * const spriteFrame = new SpriteFrame(); + * spriteFrame.texture = renderTexture; * ``` */ @ccclass('cc.SpriteFrame') -export class SpriteFrame extends SimpleTexture { +export class SpriteFrame extends Asset { /** * @zh * 一键创建 spriteframe * + * @deprecated v1.0.0 beta10 后移除,新方案请参考头部。 * @param config - 图片源 */ public static create (config: ISpriteFrameInitInfo){ - const spriteframe = new SpriteFrame(config); + const spriteframe = new SpriteFrame(); + spriteframe.initialize(config); spriteframe.onLoaded(); return spriteframe; } @@ -176,9 +207,7 @@ export class SpriteFrame extends SimpleTexture { set insetTop (value) { this._capInsets[INSET_TOP] = value; - if (this.image) { - this._calculateSlicedUV(); - } + this._calculateSlicedUV(); } /** @@ -274,14 +303,43 @@ export class SpriteFrame extends SimpleTexture { } /** - * 图片源 ImageAsset。使用 FrameBuffer 绘制不设置次参数。 + * 图片源 ImageAsset。用户不要设置此参数。 + * @deprecated v1.0.0 beta10 废弃。 */ get image () { return this._image; } set image (value) { - this.reset({image: value}); + this._image = this.image; + let tex = this._texture; + if(value instanceof RenderTexture){ + tex = new Texture2D(); + } + + (tex as Texture2D).image = value; + this.reset({texture: tex}, true); + } + + set _imageSource(value: ImageAsset) { + this._image = value; + const tex = this._texture as Texture2D; + tex.image = this._image; + this._calculateUV(); + } + + get texture (){ + return this._texture; + } + + set texture(value){ + if(!value){ + console.warn(`Error Texture in ${this.name}`); + return; + } + + this.reset({ texture: value }, true); + this.emit('updated'); } get atlasUuid () { @@ -297,11 +355,11 @@ export class SpriteFrame extends SimpleTexture { } get width () { - return this._rect.width; + return this._texture.width; } get height () { - return this._rect.height; + return this._texture.height; } public vertices: IVertices | null = null; @@ -338,8 +396,12 @@ export class SpriteFrame extends SimpleTexture { protected _original: ISpriteFrameOriginal | null = null; protected _atlasUuid: string = ''; + // @ts-ignore + protected _texture: TextureBase; - constructor (info?: ISpriteFrameInitInfo) { + protected _flipUv = false; + + constructor () { super(); if (CC_EDITOR) { @@ -347,8 +409,17 @@ export class SpriteFrame extends SimpleTexture { this._atlasUuid = ''; } - // 初始化只初始化基础配置,gfxtexture 等留到 onLoaded 完成。 - this._resetData(info); + this._texture = new Texture2D(); + this._texture.on('load', this._textureLoaded, this); + } + + /** + * 初始化配置 + * @param info 配置。 + */ + public initialize(info?: ISpriteFrameInitInfo){ + this.reset(info); + this.onLoaded(); } /** @@ -356,7 +427,7 @@ export class SpriteFrame extends SimpleTexture { * Returns whether the texture have been loaded. * * @zh - * 返回是否已加载纹理。 + * 返回是否已加载精灵帧。 */ public textureLoaded () { return this.loaded; @@ -475,31 +546,32 @@ export class SpriteFrame extends SimpleTexture { } public getGFXTextureView (){ - return this._gfxTextureView; + return this._texture.getGFXTextureView(); } /** * 外置 GFX 贴图视图,一般提供给 FrameBuffer 使用。 * 注意: 需要取消使用外置 GFXTextureView 可以使用 set image 方法替换新视图。 + * @deprecated v1.0.0 beta10 后移除 * @param value GFX 贴图视图 */ public setGFXTextureView (value: GFXTextureView) { - this._tryDestroyTexture(); - this._image = null; - const tex = this._gfxTexture = value.texture; - this._resetData({ - originalSize: new Size(tex.width, tex.height), - rect: new Rect(0, 0, tex.width, tex.height), - image: null, - borderBottom: 0, - borderLeft: 0, - borderRight: 0, - borderTop: 0, - }); - this._calculateUV(true); - this._gfxTextureView = value; - this._gfxTexture = value.texture; - this.emit('change-texture-view'); + // this._tryDestroyTexture(); + // this._image = null; + // const tex = this._gfxTexture = value.texture; + // this._resetData({ + // originalSize: new Size(tex.width, tex.height), + // rect: new Rect(0, 0, tex.width, tex.height), + // image: null, + // borderBottom: 0, + // borderLeft: 0, + // borderRight: 0, + // borderTop: 0, + // }); + // this._calculateUV(true); + // this._gfxTextureView = value; + // this._gfxTexture = value.texture; + // this.emit('change-texture-view'); } /** @@ -509,14 +581,10 @@ export class SpriteFrame extends SimpleTexture { * @zh * 克隆 SpriteFrame。 * + * @deprecated 建议重新创建,v1.0.0 beta10 后移除 * @returns - 复制后的精灵帧 */ public clone () { - if (!this.image){ - console.warn(`No image ${this.name}`); - return; - } - const cap = this._capInsets; const config: ISpriteFrameInitInfo = { originalSize: this._originalSize, @@ -526,11 +594,16 @@ export class SpriteFrame extends SimpleTexture { borderLeft: cap[INSET_LEFT], borderRight: cap[INSET_RIGHT], offset: this._offset, - image: this.image, }; - const cloneSprite = new SpriteFrame(config); + + if(this._texture instanceof RenderTexture){ + config.texture = this._texture; + } + + const cloneSprite = new SpriteFrame(); cloneSprite.name = this.name; cloneSprite.atlasUuid = this.atlasUuid; + cloneSprite.initialize(config); cloneSprite.onLoaded(); return cloneSprite; } @@ -553,7 +626,7 @@ export class SpriteFrame extends SimpleTexture { * 当加载中的场景或 Prefab 被标记为 `asyncLoadAssets` 时,用户在场景中由自定义组件关联到的所有 SpriteFrame 的贴图都不会被提前加载。 * 只有当 Sprite 组件要渲染这些 SpriteFrame 时,才会检查贴图是否加载。如果你希望加载过程提前,你可以手工调用这个方法。 * - * @method ensureLoadTexture + * @deprecated v1.0.0 beta10 后移除 * @example * ```ts * spriteFrame.once('load', this._onTextureLoaded, this); @@ -561,31 +634,93 @@ export class SpriteFrame extends SimpleTexture { * ``` */ public ensureLoadTexture () { - const image = this.image; - if (image && !image.loaded) { - // load exists texture - this._refreshTexture(image); - textureUtil.postLoadImage(image); - } + // const image = this.image; + // if (image && !image.loaded) { + // // load exists texture + // this._refreshTexture(image); + // textureUtil.postLoadImage(image); + // } } /** - * 重新更新 + * 重新更新 ImageAsset + * @deprecated v1.0.0 beta10 后移除 */ public updateImage () { // 上传图片源数据。 - this._assignImage(this._image!, 0); + if(this._texture instanceof Texture2D){ + // @ts-ignore + (this._texture as Texture2D)._assignImage(this._image!, 0); + } } /** * 重置 SpriteFrame 数据。 * @param info SpriteFrame 初始化数据。 */ - public reset (info?: ISpriteFrameInitInfo) { - this._resetData(info); - this._calculateUV(); - this._tryReset(); - this.updateImage(); + public reset (info?: ISpriteFrameInitInfo, clearData = false) { + let calUV = false; + if(clearData){ + this._originalSize.set(0, 0); + this._rect.set(0, 0, 0 ,0); + this._offset.set(0, 0); + this._capInsets = [0 , 0, 0, 0]; + this._rotated = false; + calUV = true; + } + + if (info) { + if (info.texture) { + this._rect.x = this._rect.y = 0; + this._rect.width = info.texture.width; + this._rect.height = info.texture.height; + this._texture.off('load'); + this._texture = info.texture; + this.checkRect(this._texture); + this._texture.on('load', this._textureLoaded, this); + } + + if (info.originalSize) { + this._originalSize.set(info.originalSize); + } + + if (info.rect) { + this._rect.set(info.rect); + } + + if (info.offset) { + this._offset.set(info.offset); + } + + if (info.borderTop !== undefined) { + this._capInsets[INSET_TOP] = info.borderTop; + } + + if (info.borderBottom !== undefined) { + this._capInsets[INSET_BOTTOM] = info.borderBottom; + } + + if (info.borderLeft !== undefined) { + this._capInsets[INSET_LEFT] = info.borderLeft; + } + + if (info.borderRight !== undefined) { + this._capInsets[INSET_RIGHT] = info.borderRight; + } + + this._rotated = !!info.isRotate; + this._flipUv = !!info.isFlipUv; + // hack + if (!this._texture['image']) { + this._flipUv = true; + } + + calUV = true; + } + + if(calUV){ + this._calculateUV() + } } /** @@ -594,7 +729,7 @@ export class SpriteFrame extends SimpleTexture { * * @param texture */ - public checkRect (texture: ImageAsset) { + public checkRect (texture: TextureBase) { const rect = this._rect; let maxX = rect.x; let maxY = rect.y; @@ -607,32 +742,14 @@ export class SpriteFrame extends SimpleTexture { } if (maxX > texture.width) { - cc.errorID(3300, texture.url + '/' + this.name, maxX, texture.width); + cc.errorID(3300, this.name + '/' + texture.name, maxX, texture.width); } if (maxY > texture.height) { - cc.errorID(3400, texture.url + '/' + this.name, maxY, texture.height); + cc.errorID(3400, this.name + '/' + texture.name, maxY, texture.height); } } - public destroy (){ - this.uv.length = 0; - this.uvSliced.length = 0; - this._capInsets.length = 0; - this.vertices = null; - return super.destroy(); - } - public onLoaded (){ - if(!this._image){ - return; - } - - if (!this._image.loaded){ - this.ensureLoadTexture(); - return; - } - - this.reset(); this.loaded = true; this.emit('load'); } @@ -642,15 +759,18 @@ export class SpriteFrame extends SimpleTexture { * * @zh 设置使用的纹理实例。 * + * @deprecated v1.0.0 beta10 后移除 * @param image */ public _refreshTexture (image: ImageAsset) { - this.image = image; - if (image.loaded) { - this.onLoaded(); - } else { - image.once('load', this._refreshTexture, this); - } + // this._image = image; + // if (image.loaded) { + // if(this._texture instanceof Texture2D){ + // (this._texture as Texture2D).image = this._image; + // } + // } else { + // image.once('load', this.ensureLoadTexture, this); + // } } /** @@ -659,9 +779,9 @@ export class SpriteFrame extends SimpleTexture { */ public _calculateSlicedUV () { const rect = this._rect; - const texture = this._getCalculateTarget()!; - const atlasWidth = texture.width; - const atlasHeight = texture.height; + // const texture = this._getCalculateTarget()!; + const atlasWidth = this._texture.width; + const atlasHeight = this._texture.height; const leftWidth = this._capInsets[INSET_LEFT]; const rightWidth = this._capInsets[INSET_RIGHT]; const centerWidth = rect.width - leftWidth - rightWidth; @@ -727,7 +847,7 @@ export class SpriteFrame extends SimpleTexture { this._rect.x = frame._rect.x; this._rect.y = frame._rect.y; - this.image = frame.image; + this._image = frame.image; this._calculateUV(); } @@ -738,7 +858,7 @@ export class SpriteFrame extends SimpleTexture { this._rect.x = this._original.x; this._rect.y = this._original.y; - this.image = this._original.spriteframe.image; + this._image = this._original.spriteframe.image; this._original = null; this._calculateUV(); } @@ -747,16 +867,11 @@ export class SpriteFrame extends SimpleTexture { * @zh * 计算 UV。 */ - public _calculateUV (flip = false) { + public _calculateUV () { const rect = this._rect; - const texture = this._getCalculateTarget(); - if(!texture){ - console.warn(`There is no texture to calculate in ${this.name}`); - return; - } const uv = this.uv; - const texw = texture.width; - const texh = texture.height; + const texw = this._texture.width; + const texh = this._texture.height; if (this._rotated) { const l = texw === 0 ? 0 : rect.x / texw; @@ -776,7 +891,7 @@ export class SpriteFrame extends SimpleTexture { const r = texw === 0 ? 0 : (rect.x + rect.width) / texw; const b = texh === 0 ? 0 : (rect.y + rect.height) / texh; const t = texh === 0 ? 0 : rect.y / texh; - if (flip) { + if (this._flipUv) { uv[0] = l; uv[1] = t; uv[2] = r; @@ -841,7 +956,6 @@ export class SpriteFrame extends SimpleTexture { } const serialize = { - base: super._serialize(exporting), image: this._image ? exporting ? Editor.Utils.UuidUtils.compressUuid(this._image._uuid, true) : this._image._uuid : undefined, name: this._name, atlas: exporting ? undefined : this._atlasUuid, // strip from json if exporting @@ -859,11 +973,6 @@ export class SpriteFrame extends SimpleTexture { public _deserialize (serializeData: any, handle: any) { const data = serializeData as ISpriteFramesSerializeData; - super._deserialize(data.base, handle); - if(data.image.length > 0){ - this._image = new ImageAsset(); - handle.result.push(this, '_image', data.image); - } const rect = data.rect; if (rect) { this.setRect(new Rect(rect.x, rect.y, rect.width, rect.height)); @@ -889,6 +998,8 @@ export class SpriteFrame extends SimpleTexture { this._capInsets[INSET_BOTTOM] = capInsets[INSET_BOTTOM]; } + handle.result.push(this, '_imageSource', data.image); + if (CC_EDITOR) { this._atlasUuid = data.atlas ? data.atlas : ''; } @@ -901,93 +1012,13 @@ export class SpriteFrame extends SimpleTexture { } } - /** - * GFX 贴图信息。 - * @param presumed The presumed GFX texture info. - */ - protected _getGfxTextureCreateInfo (presumed: PresumedGFXTextureInfo): IGFXTextureInfo | null { - if(!this._image){ - return null; - } - - return Object.assign({ - type: GFXTextureType.TEX2D, - width: this._image.width, - height: this._image.height, - }, presumed); - } - - /** - * GFX 贴图视图信息。 - * @param presumed The presumed GFX texture view info. - */ - protected _getGfxTextureViewCreateInfo (presumed: PresumedGFXTextureViewInfo): IGFXTextureViewInfo | null { - return Object.assign({ - type: GFXTextureViewType.TV2D, - }, presumed); - } - - /** - * 重制精灵帧数据。 - * @param 重置数据。 - */ - protected _resetData (info?: ISpriteFrameInitInfo) { - if (info) { - if (info.image) { - this._rect.x = this._rect.y = 0; - this._rect.width = info.image.width; - this._rect.height = info.image.height; - const keepOriginSize = !!info.keepOriginSize; - if (!keepOriginSize) { - this._originalSize.width = this._rect.width; - this._originalSize.height = this._rect.height; - } - this._image = info.image; - this.checkRect(this._image); - } - - if (info.originalSize) { - this._originalSize.set(info.originalSize); - } - - if (info.rect) { - this._rect.set(info.rect); - } - - if (info.offset) { - this._offset.set(info.offset); - } - - if (info.borderTop !== undefined) { - this._capInsets[INSET_TOP] = info.borderTop; - } - - if (info.borderBottom !== undefined) { - this._capInsets[INSET_BOTTOM] = info.borderBottom; - } - - if (info.borderLeft !== undefined) { - this._capInsets[INSET_LEFT] = info.borderLeft; - } - - if (info.borderRight !== undefined) { - this._capInsets[INSET_RIGHT] = info.borderRight; - } - - this._rotated = !!info.isRotate; - } - - if(this._image){ - this._setGFXFormat(this._image.format); - } - } - - protected _getCalculateTarget(){ - if (this._image) { - return this._image; - } else { - return this._gfxTexture; - } + protected _textureLoaded() { + const tex = this._texture; + this.reset({ + originalSize: new Size(tex.width, tex.height), + rect: new Rect(0, 0, tex.width, tex.height), + }); + this.emit('updated'); } } diff --git a/cocos/assets/texture-2d.ts b/cocos/assets/texture-2d.ts index 0a57d90d87..22564723ad 100644 --- a/cocos/assets/texture-2d.ts +++ b/cocos/assets/texture-2d.ts @@ -68,20 +68,6 @@ export interface ITexture2DCreateInfo { */ @ccclass('cc.Texture2D') export class Texture2D extends SimpleTexture { - /** - * 此贴图的像素宽度。 - */ - public get width (): number { - return this._width; - } - - /** - * 此贴图的像素高度。 - */ - public get height (): number { - return this._height; - } - /** * 所有层级 Mipmap,注意,这里不包含自动生成的 Mipmap。 * 当设置 Mipmap 时,贴图的尺寸以及像素格式可能会改变。 @@ -127,8 +113,6 @@ export class Texture2D extends SimpleTexture { @property([ImageAsset]) public _mipmaps: ImageAsset[] = []; - private _width: number = 0; - private _height: number = 0; constructor () { super(true); @@ -136,8 +120,6 @@ export class Texture2D extends SimpleTexture { public onLoaded () { this.initialize(); - this.loaded = true; - this.emit('load'); } /** @@ -269,6 +251,21 @@ export class Texture2D extends SimpleTexture { type: GFXTextureViewType.TV2D, }, presumed); } + + protected _checkTextureLoaded() { + let ready = true; + for (let i = 0; i < this._mipmaps.length; ++i) { + const image = this._mipmaps[i]; + if(!image.loaded){ + ready = false; + break; + } + } + + if(ready){ + super._textureReady(); + } + } } cc.Texture2D = Texture2D; diff --git a/cocos/assets/texture-base.ts b/cocos/assets/texture-base.ts index 7f79f8727b..3e438cd373 100644 --- a/cocos/assets/texture-base.ts +++ b/cocos/assets/texture-base.ts @@ -53,6 +53,20 @@ export class TextureBase extends Asset { return this._format >= PixelFormat.RGB_ETC1 && this._format <= PixelFormat.RGBA_PVRTC_4BPPV1; } + /** + * 此贴图的像素宽度。 + */ + public get width(): number { + return this._width; + } + + /** + * 此贴图的像素高度。 + */ + public get height(): number { + return this._height; + } + public static PixelFormat = PixelFormat; public static WrapMode = WrapMode; @@ -89,11 +103,15 @@ export class TextureBase extends Asset { @property protected _anisotropy = 16; + protected _width: number = 0; + protected _height: number = 0; + private _id: string; private _samplerInfo: Array = []; private _samplerHash: number = 0; - protected constructor (flipY: boolean = false) { + + constructor (flipY: boolean = false) { super(); this._flipY = flipY;