mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
remove RenderTexture (#4247)
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
const renderer = require('../renderer');
|
||||
const renderEngine = require('../renderer/render-engine');
|
||||
const gfx = renderEngine.gfx;
|
||||
const Texture2D = require('./CCTexture2D');
|
||||
|
||||
/**
|
||||
* Render textures are textures that can be rendered to.
|
||||
* @class RenderTexture
|
||||
* @extends Texture2D
|
||||
*/
|
||||
let RenderTexture = cc.Class({
|
||||
name: 'cc.RenderTexture',
|
||||
extends: Texture2D,
|
||||
|
||||
ctor () {
|
||||
this._framebuffer = null;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Init the render texture with size.
|
||||
* !#zh
|
||||
* 初始化 render texture
|
||||
* @param {Number} [width]
|
||||
* @param {Number} [height]
|
||||
* @param {Number} [depthStencilFormat]
|
||||
* @method initWithSize
|
||||
*/
|
||||
initWithSize (width, height, depthStencilFormat) {
|
||||
this.width = Math.floor(width || cc.visibleRect.width);
|
||||
this.height = Math.floor(height || cc.visibleRect.height);
|
||||
|
||||
let opts = {};
|
||||
opts.format = this._format;
|
||||
opts.width = width;
|
||||
opts.height = height;
|
||||
opts.images = undefined;
|
||||
opts.wrapS = this._wrapS;
|
||||
opts.wrapT = this._wrapT;
|
||||
|
||||
if (!this._texture) {
|
||||
this._texture = new renderer.Texture2D(renderer.device, opts);
|
||||
}
|
||||
else {
|
||||
this._texture.update(opts);
|
||||
}
|
||||
|
||||
opts = {
|
||||
colors: [ this._texture ],
|
||||
};
|
||||
if (depthStencilFormat) {
|
||||
let depthStencilBuffer = new gfx.RenderBuffer(renderer.device, depthStencilFormat, width, height);
|
||||
if (depthStencilFormat === gfx.RB_FMT_D24S8) {
|
||||
opts.depth = opts.stencil = depthStencilBuffer;
|
||||
}
|
||||
else if (depthStencilFormat === gfx.RB_FMT_S8) {
|
||||
opts.stencil = depthStencilBuffer;
|
||||
}
|
||||
else if (depthStencilFormat === gl.RB_FMT_D16) {
|
||||
opts.depth = depthStencilBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._framebuffer) {
|
||||
this._framebuffer.destroy();
|
||||
}
|
||||
this._framebuffer = new renderEngine.gfx.FrameBuffer(renderer.device, width, height, opts);
|
||||
|
||||
this.loaded = true;
|
||||
this.emit("load");
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Draw a texture to the specified position
|
||||
* !#zh 将指定的图片渲染到指定的位置上
|
||||
* @param {Texture2D} texture
|
||||
* @param {Number} x
|
||||
* @param {Number} y
|
||||
*/
|
||||
drawTextureAt (texture, x, y) {
|
||||
if (!texture._image) return;
|
||||
|
||||
this._texture.updateSubImage({
|
||||
x, y,
|
||||
image: texture._image,
|
||||
width: texture.width,
|
||||
height: texture.height,
|
||||
level: 0,
|
||||
flipY: false
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Get pixels from render texture, the pixels data stores in a RGBA Uint8Array.
|
||||
* It will return a new (width * height * 4) length Uint8Array by default。
|
||||
* You can specify a data to store the pixels to reuse the data,
|
||||
* you and can specify other params to specify the texture region to read.
|
||||
* !#zh
|
||||
* 从 render texture 读取像素数据,数据类型为 RGBA 格式的 Uint8Array 数组。
|
||||
* 默认每次调用此函数会生成一个大小为 (长 x 高 x 4) 的 Uint8Array。
|
||||
* 你可以通过传入 data 来接收像素数据,也可以通过传参来指定需要读取的区域的像素。
|
||||
* @method readPixels
|
||||
* @param {Uint8Array} [data]
|
||||
* @param {Number} [x]
|
||||
* @param {Number} [y]
|
||||
* @param {Number} [w]
|
||||
* @param {Number} [h]
|
||||
* @return {Uint8Array}
|
||||
*/
|
||||
readPixels (data, x, y, w, h) {
|
||||
if (!this._framebuffer || !this._texture) return data;
|
||||
|
||||
x = x || 0;
|
||||
y = y || 0;
|
||||
let width = w || this.width;
|
||||
let height = h || this.height
|
||||
data = data || new Uint8Array(width * height * 4);
|
||||
|
||||
let gl = renderer._forward._device._gl;
|
||||
let oldFBO = gl.getParameter(gl.FRAMEBUFFER_BINDING);
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, this._framebuffer._glID);
|
||||
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._texture._glID, 0);
|
||||
gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, data);
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, oldFBO);
|
||||
|
||||
return data;
|
||||
},
|
||||
|
||||
destroy () {
|
||||
this._super();
|
||||
if (this._framebuffer) {
|
||||
this._framebuffer.destroy();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
cc.RenderTexture = module.exports = RenderTexture;
|
||||
@@ -24,7 +24,6 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { RenderTexture } from '../../../assets';
|
||||
import { BitmapFont } from '../../../assets/bitmap-font';
|
||||
import { Font} from '../../../assets/font';
|
||||
import { ImageAsset } from '../../../assets/image-asset';
|
||||
@@ -34,6 +33,7 @@ import { ccenum } from '../../../core/value-types/enum';
|
||||
import { UI } from '../../../renderer/ui/ui';
|
||||
import { FontAtlas } from '../assembler/label/bmfontUtils';
|
||||
import { CanvasPool, ISharedLabelData } from '../assembler/label/font-utils';
|
||||
import { LetterRenderTexture } from '../assembler/label/letter-font';
|
||||
import { UIRenderComponent } from './ui-render-component';
|
||||
|
||||
/**
|
||||
@@ -595,12 +595,12 @@ export class LabelComponent extends UIRenderComponent {
|
||||
// don't need serialize
|
||||
// 这个保存了旧项目的 file 数据
|
||||
private _N$file: Font | null = null;
|
||||
private _texture: SpriteFrame | RenderTexture | null = null;
|
||||
private _texture: SpriteFrame | LetterRenderTexture | null = null;
|
||||
private _ttfTexture: SpriteFrame | null = null;
|
||||
private _userDefinedFont: Font | null = null;
|
||||
private _assemblerData: ISharedLabelData | null = null;
|
||||
private _fontAtlas: FontAtlas | null = null;
|
||||
private _letterTexture: RenderTexture | null = null;
|
||||
private _letterTexture: LetterRenderTexture | null = null;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
@@ -36,5 +36,4 @@ export {default as JsonAsset} from './CCJsonAsset';
|
||||
export {default as AssetLibrary} from './CCAssetLibrary';
|
||||
export { ImageAsset } from './image-asset';
|
||||
export { Texture2D } from './texture-2d';
|
||||
export { RenderTexture } from './render-texture';
|
||||
export { TTFFont } from './ttf-font';
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
import { ccclass } from '../core/data/class-decorator';
|
||||
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 { 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,
|
||||
};
|
||||
|
||||
/**
|
||||
* Render textures are textures that can be rendered to.
|
||||
* @class RenderTexture
|
||||
* @extends Texture2D
|
||||
*/
|
||||
@ccclass('cc.RenderTexture')
|
||||
export class RenderTexture extends Texture2D {
|
||||
private _framebuffer: GFXFramebuffer | null = null;
|
||||
|
||||
private _depthStencilTexture: GFXTexture | null = null;
|
||||
|
||||
private _depthStencilTextureView: GFXTextureView | null = null;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* !#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: DSFormat = 'D24S8') {
|
||||
this.destroy();
|
||||
|
||||
const gfxDevice = this._getGlobalDevice();
|
||||
if (!gfxDevice) {
|
||||
return;
|
||||
}
|
||||
|
||||
const image = new ImageAsset({
|
||||
width,
|
||||
height,
|
||||
format: GFXFormat.RGBA8UI,
|
||||
_data: new Uint8Array(width * height * 4),
|
||||
_compressed: false,
|
||||
});
|
||||
|
||||
this.image = image;
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return super.destroy();
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// public updateSize (width, height) {
|
||||
// this.width = Math.floor(width || cc.visibleRect.width);
|
||||
// this.height = Math.floor(height || cc.visibleRect.height);
|
||||
// this._resetUnderlyingMipmaps();
|
||||
|
||||
// const rbo = this.depthStencilBuffer;
|
||||
// if (rbo) { rbo.update(this.width, this.height); }
|
||||
// this._framebuffer._width = width;
|
||||
// this._framebuffer._height = height;
|
||||
// }
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Get pixels from render texture, the pixels data stores in a RGBA Uint8Array.
|
||||
* It will return a new (width * height * 4) length Uint8Array by default.
|
||||
* You can specify a data to store the pixels to reuse the data,
|
||||
* you and can specify other params to specify the texture region to read.
|
||||
* !#zh
|
||||
* 从 render texture 读取像素数据,数据类型为 RGBA 格式的 Uint8Array 数组。
|
||||
* 默认每次调用此函数会生成一个大小为 (长 x 高 x 4) 的 Uint8Array。
|
||||
* 你可以通过传入 data 来接收像素数据,也可以通过传参来指定需要读取的区域的像素。
|
||||
* @param [data]
|
||||
* @param [rect]
|
||||
*/
|
||||
public readPixels (data?: Uint8Array, rect: IRect2D = {}) {
|
||||
if (!this._framebuffer || !this._texture) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const gfxDevice = this._getGlobalDevice();
|
||||
if (!gfxDevice) {
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedRect = normalizeRect2D(rect, this.width, this.height);
|
||||
|
||||
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,
|
||||
},
|
||||
}]);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
cc.RenderTexture = RenderTexture;
|
||||
Reference in New Issue
Block a user