From d400a93099fb113ce4632f6c65d8b873e285e51c Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Thu, 18 Apr 2019 16:14:31 +0800 Subject: [PATCH] restore RenderTexture, label picture merge used LetterRenderTexture (#4244) --- cocos/3d/ui/assembler/label/letter-font.ts | 110 ++++++++--- cocos/assets/render-texture.ts | 219 +++++++++------------ 2 files changed, 173 insertions(+), 156 deletions(-) diff --git a/cocos/3d/ui/assembler/label/letter-font.ts b/cocos/3d/ui/assembler/label/letter-font.ts index d09306dbd0..d02bb2ed2e 100644 --- a/cocos/3d/ui/assembler/label/letter-font.ts +++ b/cocos/3d/ui/assembler/label/letter-font.ts @@ -23,11 +23,11 @@ THE SOFTWARE. ****************************************************************************/ -import { ImageAsset, RenderTexture, SpriteFrame } from '../../../../assets'; +import { ImageAsset, SpriteFrame, Texture2D } from '../../../../assets'; import { Color, Rect, Size, Vec2 } from '../../../../core'; import { mixin } from '../../../../core/utils/js'; import { isUnicodeCJK, isUnicodeSpace, safeMeasureText} from '../../../../core/utils/text-utils'; -import { GFXBufferTextureCopy } from '../../../../gfx/define'; +import { GFXBufferTextureCopy, GFXFormat, GFXTextureType } from '../../../../gfx/define'; import { LabelComponent } from '../../components/label-component'; import { LabelOutlineComponent } from '../../components/label-outline-component'; // import { UIComponent } from '../../components/ui-component'; @@ -68,7 +68,7 @@ class FontLetterDefinition { public v = 0; public w = 0; public h = 0; - public texture: RenderTexture | null = null; + public texture: LetterRenderTexture | null = null; public offsetX = 0; public offsetY = 0; public valid = false; @@ -166,6 +166,81 @@ class LetterTexture { } } +export class LetterRenderTexture extends Texture2D { + /** + * !#en + * Init the render texture with size. + * !#zh + * 初始化 render texture + * @param [width] + * @param [height] + * @param [string] + * @method initWithSize + */ + public initWithSize (width: number, height: number, format: GFXFormat = GFXFormat.RGBA8) { + this.destroy(); + + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + console.warn('Unable to get device'); + return; + } + + const texInfo = this._getTextureCreateInfo(); + texInfo.width = width; + texInfo.height = height; + texInfo.format = format; + this._texture = gfxDevice.createTexture(texInfo); + this._textureView = gfxDevice.createTextureView(this._getTextureViewCreateInfo()); + + this.loaded = true; + this.emit('load'); + } + + /** + * !#en Draw a texture to the specified position + * !#zh 将指定的图片渲染到指定的位置上 + * @param {Texture2D} texture + * @param {Number} x + * @param {Number} y + */ + public drawTextureAt (texture: SpriteFrame, x: number, y: number) { + if (!texture.image || !this._texture) { + return; + } + + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + console.warn('Unable to get device'); + return; + } + + const region: GFXBufferTextureCopy = { + buffOffset: 0, + buffStride: 0, + buffTexHeight: 0, + texOffset: { + x, + y, + z: 0, + }, + texExtent: { + width: texture.image.width, + height: texture.image.height, + depth: 1, + }, + texSubres: { + baseMipLevel: 0, + levelCount: 1, + baseArrayLayer: 0, + layerCount: 1, + }, + }; + + gfxDevice.copyTexImagesToTexture([texture.image.data as HTMLCanvasElement], this._texture, [region]); + } +} + export class LetterAtlas { get width () { return this._width; @@ -175,7 +250,7 @@ export class LetterAtlas { return this._height; } - public texture: RenderTexture; + public texture: LetterRenderTexture; private _x = space; private _y = space; private _nexty = space; @@ -186,7 +261,7 @@ export class LetterAtlas { private _dirty = false; constructor (width: number, height: number) { - this.texture = new RenderTexture(); + this.texture = new LetterRenderTexture(); this.texture.initWithSize(width, height); this._width = width; @@ -217,7 +292,6 @@ export class LetterAtlas { return null; } - console.log('drawtexture ' + this._x + ' ' + this._y); this.texture.drawTextureAt(texture, this._x, this._y); this._dirty = true; @@ -280,7 +354,7 @@ export class LetterAtlas { public beforeSceneLoad () { this.destroy(); - const texture = new RenderTexture(); + const texture = new LetterRenderTexture(); texture.initWithSize(this._width, this._height); // texture.update(); @@ -335,24 +409,6 @@ export class LetterAtlas { return letterDefinition; } - - private _makeFrameBuffer (){ - if (!cc.director.root || !cc.director.root.device){ - return null; - } - - return cc.director.root.device.createFramebuffer({}); - } - - private _makeContainer (width: number, height: number){ - const texture = new SpriteFrame(); - const canvas = document.createElement('canvas') as HTMLCanvasElement; - canvas.width = width; - canvas.height = height; - const image = new ImageAsset(canvas); - texture.image = image; - return texture; - } } const _tmpRect = new Rect(); @@ -388,8 +444,8 @@ let _isWrapText = false; let _labelWidth = 0; let _labelHeight = 0; let _maxLineWidth = 0; -const _atlasWidth = 256; -const _atlasHeight = 256; +const _atlasWidth = 1024; +const _atlasHeight = 1024; let _fontFamily = ''; let _isBold = false; const _labelInfo: ILabelInfo = { diff --git a/cocos/assets/render-texture.ts b/cocos/assets/render-texture.ts index b59319a97c..278af6709c 100644 --- a/cocos/assets/render-texture.ts +++ b/cocos/assets/render-texture.ts @@ -1,19 +1,19 @@ import { ccclass } from '../core/data/class-decorator'; -// import { IRect2D, normalizeRect2D } from '../core/vmath/rect'; -import { GFXBufferTextureCopy, GFXFormat } from '../gfx/define'; +import { IRect2D, normalizeRect2D } from '../core/vmath/rect'; +import { GFXFormat, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType } from '../gfx/define'; import { GFXFramebuffer } from '../gfx/framebuffer'; import { GFXTexture } from '../gfx/texture'; import { GFXTextureView } from '../gfx/texture-view'; -// import { RenderPassStage } from '../pipeline/define'; -import { SpriteFrame } from './sprite-frame'; +import { RenderPassStage } from '../pipeline/define'; +import { ImageAsset } from './image-asset'; import { Texture2D } from './texture-2d'; -// type DSFormat = 'D24S8' | 'S8' | 'D16'; -// const _formatMap = { -// D24S8: GFXFormat.D24S8, -// S8: GFXFormat.R8UI, -// D16: GFXFormat.D16, -// }; +type DSFormat = 'D24S8' | 'S8' | 'D16'; +const _formatMap = { + D24S8: GFXFormat.D24S8, + S8: GFXFormat.R8UI, + D16: GFXFormat.D16, +}; /** * Render textures are textures that can be rendered to. @@ -22,7 +22,7 @@ import { Texture2D } from './texture-2d'; */ @ccclass('cc.RenderTexture') export class RenderTexture extends Texture2D { - private _frameBuffer: GFXFramebuffer | null = null; + private _framebuffer: GFXFramebuffer | null = null; private _depthStencilTexture: GFXTexture | null = null; @@ -42,105 +42,67 @@ export class RenderTexture extends Texture2D { * @param [string] * @method initWithSize */ - public initWithSize (width: number, height: number, format: GFXFormat = GFXFormat.RGBA8) { + public initWithSize (width: number, height: number, format: DSFormat = 'D24S8') { this.destroy(); const gfxDevice = this._getGlobalDevice(); if (!gfxDevice) { - console.warn('Unable to get device'); return; } - const texInfo = this._getTextureCreateInfo(); - texInfo.width = width; - texInfo.height = height; - texInfo.format = format; - this._texture = gfxDevice.createTexture(texInfo); - this._textureView = gfxDevice.createTextureView(this._getTextureViewCreateInfo()); + const image = new ImageAsset({ + width, + height, + format: GFXFormat.RGBA8UI, + _data: new Uint8Array(width * height * 4), + _compressed: false, + }); - // TOOD: - // if (format !== undefined) { - // this._depthStencilTexture = gfxDevice.createTexture({ - // type: GFXTextureType.TEX2D, - // usage: GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, - // format: _formatMap[format], - // width, - // height, - // }); - // this._depthStencilTextureView = gfxDevice.createTextureView({ - // texture: this._depthStencilTexture, - // type: GFXTextureViewType.TV2D, - // format: _formatMap[format], - // }); - // } + this.image = image; - // this._frameBuffer = gfxDevice.createFramebuffer({ - // renderPass: cc.director.root.pipeline.getRenderPass(RenderPassStage.DEFAULT), - // colorViews: [ this._textureView ], - // depthStencilView: this._depthStencilTextureView, - // isOffscreen: true, - // }); + if (!this._textureView) { + return; + } + + if (format !== undefined) { + this._depthStencilTexture = gfxDevice.createTexture({ + type: GFXTextureType.TEX2D, + usage: GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT, + format: _formatMap[format], + width, + height, + }); + this._depthStencilTextureView = gfxDevice.createTextureView({ + texture: this._depthStencilTexture, + type: GFXTextureViewType.TV2D, + format: _formatMap[format], + }); + } + + this._framebuffer = gfxDevice.createFramebuffer({ + renderPass: cc.director.root.pipeline.getRenderPass(RenderPassStage.DEFAULT), + colorViews: [this._textureView], + depthStencilView: this._depthStencilTextureView!, + isOffscreen: true, + }); this.loaded = true; this.emit('load'); } - /** - * !#en Draw a texture to the specified position - * !#zh 将指定的图片渲染到指定的位置上 - * @param {Texture2D} texture - * @param {Number} x - * @param {Number} y - */ - public drawTextureAt (texture: SpriteFrame, x: number, y: number) { - if (!texture.image || !this._texture) { - return; - } - - const gfxDevice = this._getGlobalDevice(); - if (!gfxDevice) { - console.warn('Unable to get device'); - return; - } - - const region: GFXBufferTextureCopy = { - buffOffset: 0, - buffStride: 0, - buffTexHeight: 0, - texOffset: { - x, - y, - z: 0, - }, - texExtent: { - width: texture.image.width, - height: texture.image.height, - depth: 1, - }, - texSubres: { - baseMipLevel: 0, - levelCount: 1, - baseArrayLayer: 0, - layerCount: 1, - }, - }; - - gfxDevice.copyTexImagesToTexture([texture.image.data as HTMLCanvasElement], this._texture, [region]); - } - public destroy () { - // if (this._frameBuffer) { - // this._frameBuffer.destroy(); - // this._frameBuffer = null; - // } - // if (this._depthStencilTextureView) { - // this._depthStencilTextureView.destroy(); - // this._depthStencilTextureView = null; - // } - // if (this._depthStencilTexture) { - // this._depthStencilTexture.destroy(); - // this._depthStencilTexture = null; - // } + if (this._framebuffer) { + this._framebuffer.destroy(); + this._framebuffer = null; + } + if (this._depthStencilTextureView) { + this._depthStencilTextureView.destroy(); + this._depthStencilTextureView = null; + } + if (this._depthStencilTexture) { + this._depthStencilTexture.destroy(); + this._depthStencilTexture = null; + } return super.destroy(); } @@ -169,45 +131,44 @@ export class RenderTexture extends Texture2D { * @param [data] * @param [rect] */ - // TODO: - // public readPixels (data?: Uint8Array, rect: IRect2D = {}) { - // if (!this._frameBuffer || !this._texture) { - // return undefined; - // } + public readPixels (data?: Uint8Array, rect: IRect2D = {}) { + if (!this._framebuffer || !this._texture) { + return undefined; + } - // const gfxDevice = this._getGlobalDevice(); - // if (!gfxDevice) { - // return; - // } + const gfxDevice = this._getGlobalDevice(); + if (!gfxDevice) { + return; + } - // const normalizedRect = normalizeRect2D(rect, this.width, this.height); + const normalizedRect = normalizeRect2D(rect, this.width, this.height); - // data = data || new Uint8Array(normalizedRect.width * normalizedRect.height * 4); + data = data || new Uint8Array(normalizedRect.width * normalizedRect.height * 4); - // gfxDevice.copyFramebufferToBuffer(this._frameBuffer, data.buffer, [{ - // buffOffset: 0, - // buffStride: 0, - // buffTexHeight: 0, - // texOffset: { - // x: normalizedRect.x, - // y: normalizedRect.y, - // z: 0, - // }, - // texExtent: { - // width: normalizedRect.width, - // height: normalizedRect.height, - // depth: 1, - // }, - // texSubres: { - // baseMipLevel: 0, - // levelCount: 1, - // baseArrayLayer: 0, - // layerCount: 1, - // }, - // }]); + gfxDevice.copyFramebufferToBuffer(this._framebuffer, data.buffer, [{ + buffOffset: 0, + buffStride: 0, + buffTexHeight: 0, + texOffset: { + x: normalizedRect.x, + y: normalizedRect.y, + z: 0, + }, + texExtent: { + width: normalizedRect.width, + height: normalizedRect.height, + depth: 1, + }, + texSubres: { + baseMipLevel: 0, + levelCount: 1, + baseArrayLayer: 0, + layerCount: 1, + }, + }]); - // return data; - // } + return data; + } } cc.RenderTexture = RenderTexture;