mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 08:42:43 +08:00
support RenderTexture
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
const renderer = require('../renderer');
|
||||
const renderEngine = require('../renderer/render-engine');
|
||||
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]
|
||||
* @method initWithSize
|
||||
*/
|
||||
initWithSize (width, height) {
|
||||
this.width = Math.floor(width || cc.visibleRect.width);
|
||||
this.height = Math.floor(height || cc.visibleRect.height);
|
||||
|
||||
let opts = Texture2D._getSharedOptions();
|
||||
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 ]
|
||||
};
|
||||
this._framebuffer = new renderEngine.gfx.FrameBuffer(renderer.device, width, height, opts);
|
||||
|
||||
this.loaded = true;
|
||||
this.emit("load");
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Get pixels from render texture
|
||||
* !#zh
|
||||
* 从 render texture 读取像素数据
|
||||
* @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;
|
||||
}
|
||||
});
|
||||
|
||||
cc.RenderTexture = module.exports = RenderTexture;
|
||||
@@ -238,7 +238,9 @@ var Texture2D = cc.Class({
|
||||
WrapMode: WrapMode,
|
||||
Filter: Filter,
|
||||
// predefined most common extnames
|
||||
extnames: ['.png', '.jpg', '.jpeg', '.bmp', '.webp']
|
||||
extnames: ['.png', '.jpg', '.jpeg', '.bmp', '.webp'],
|
||||
|
||||
_getSharedOptions: _getSharedOptions
|
||||
},
|
||||
|
||||
ctor () {
|
||||
|
||||
@@ -33,6 +33,7 @@ require('./CCScripts');
|
||||
require('./CCSceneAsset');
|
||||
require('./CCSpriteFrame');
|
||||
require('./CCTexture2D');
|
||||
require('./CCRenderTexture');
|
||||
require('./CCTTFFont');
|
||||
require('./CCSpriteAtlas');
|
||||
require('./CCBitmapFont');
|
||||
|
||||
@@ -81,7 +81,7 @@ let Camera = cc.Class({
|
||||
camera.dirty = true;
|
||||
|
||||
this._matrixDirty = true;
|
||||
|
||||
this._inited = false;
|
||||
this._camera = camera;
|
||||
},
|
||||
|
||||
@@ -97,6 +97,7 @@ let Camera = cc.Class({
|
||||
_backgroundColor: cc.color(51, 77, 120, 255),
|
||||
_depth: 0,
|
||||
_zoomRatio: 1,
|
||||
_targetTexture: null,
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -154,6 +155,7 @@ let Camera = cc.Class({
|
||||
* The color with which the screen will be cleared.
|
||||
* !#zh
|
||||
* 摄像机用于清除屏幕的背景色。
|
||||
* @property {Color} backgroundColor
|
||||
*/
|
||||
backgroundColor: {
|
||||
get () {
|
||||
@@ -170,6 +172,7 @@ let Camera = cc.Class({
|
||||
* Camera's depth in the camera rendering order.
|
||||
* !#zh
|
||||
* 摄像机深度,用于决定摄像机的渲染顺序。
|
||||
* @property {Number} depth
|
||||
*/
|
||||
depth: {
|
||||
get () {
|
||||
@@ -179,6 +182,25 @@ let Camera = cc.Class({
|
||||
this._depth = value;
|
||||
this._camera.setDepth(value);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Destination render texture.
|
||||
* Usually cameras render directly to screen, but for some effects it is useful to make a camera render into a texture.
|
||||
* !#zh
|
||||
* 摄像机渲染的目标 RenderTexture。
|
||||
* 一般摄像机会直接渲染到屏幕上,但是有一些效果可以使用摄像机渲染到 RenderTexture 上再对 RenderTexture 进行处理来实现。
|
||||
* @property {RenderTexture} targetTexture
|
||||
*/
|
||||
targetTexture: {
|
||||
get () {
|
||||
return this._targetTexture;
|
||||
},
|
||||
set (value) {
|
||||
this._targetTexture = value;
|
||||
this._updateTargetTexture();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -242,16 +264,29 @@ let Camera = cc.Class({
|
||||
);
|
||||
},
|
||||
|
||||
_updateTargetTexture () {
|
||||
let texture = this._targetTexture;
|
||||
this._camera._framebuffer = texture ? texture._framebuffer : null;
|
||||
},
|
||||
|
||||
_onMatrixDirty () {
|
||||
this._matrixDirty = true;
|
||||
},
|
||||
|
||||
onLoad () {
|
||||
_init () {
|
||||
if (this._inited) return;
|
||||
this._inited = true;
|
||||
|
||||
this._camera.setNode(this.node);
|
||||
this._camera.setClearFlags(this._clearFlags);
|
||||
this._camera.setDepth(this._depth);
|
||||
this._updateBackgroundColor();
|
||||
this._updateCameraMask();
|
||||
this._updateTargetTexture();
|
||||
},
|
||||
|
||||
onLoad () {
|
||||
this._init();
|
||||
},
|
||||
|
||||
onEnable () {
|
||||
@@ -380,6 +415,23 @@ let Camera = cc.Class({
|
||||
return node._cullingMask === this.cullingMask;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Render the camera manually.
|
||||
* !#zh
|
||||
* 手动渲染摄像机。
|
||||
* @method render
|
||||
* @param {Node} root
|
||||
*/
|
||||
render (root) {
|
||||
root = root || cc.director.getScene();
|
||||
if (!root) return null;
|
||||
|
||||
this.lateUpdate();
|
||||
renderer._walker.visit(root);
|
||||
renderer._forward.renderCamera(this._camera, renderer.scene);
|
||||
},
|
||||
|
||||
lateUpdate: !CC_EDITOR && function () {
|
||||
let node = this.node;
|
||||
node.getWorldMatrix(_mat4_temp_1);
|
||||
@@ -390,11 +442,19 @@ let Camera = cc.Class({
|
||||
let fov = Math.atan(Math.tan(this._fov/2) / this.zoomRatio)*2;
|
||||
camera.setFov(fov);
|
||||
|
||||
let width = cc.visibleRect.width;
|
||||
let height = cc.visibleRect.height;
|
||||
|
||||
let targetTexture = this._targetTexture;
|
||||
if (targetTexture) {
|
||||
height = targetTexture.height;
|
||||
}
|
||||
|
||||
_vec3_temp_1.x = _mat4_temp_1.m12;
|
||||
_vec3_temp_1.y = _mat4_temp_1.m13;
|
||||
_vec3_temp_1.z = 0;
|
||||
|
||||
node.z = cc.visibleRect.height / 1.1566;
|
||||
node.z = height / 1.1566;
|
||||
node.lookAt(_vec3_temp_1);
|
||||
|
||||
this._matrixDirty = false;
|
||||
|
||||
@@ -13577,14 +13577,6 @@ module.exports = (function () {
|
||||
render (scene) {
|
||||
this._reset();
|
||||
|
||||
const canvas = this._device._gl.canvas;
|
||||
|
||||
scene._cameras.sort((a, b) => {
|
||||
if (a._depth > b._depth) return 1;
|
||||
else if (a._depth < b._depth) return -1;
|
||||
else return 0;
|
||||
});
|
||||
|
||||
scene._cameras.sort((a, b) => {
|
||||
if (a._depth > b._depth) return 1;
|
||||
else if (a._depth < b._depth) return -1;
|
||||
@@ -13593,19 +13585,31 @@ module.exports = (function () {
|
||||
|
||||
for (let i = 0; i < scene._cameras.length; ++i) {
|
||||
let camera = scene._cameras.data[i];
|
||||
let view = camera.view;
|
||||
let dirty = camera.dirty;
|
||||
if (!view) {
|
||||
view = this._requestView();
|
||||
dirty = true;
|
||||
}
|
||||
if (dirty) {
|
||||
camera.extractView(view, canvas.width, canvas.height);
|
||||
}
|
||||
this._render(view, scene);
|
||||
this.renderCamera(camera, scene);
|
||||
}
|
||||
}
|
||||
|
||||
renderCamera (camera, scene) {
|
||||
const canvas = this._device._gl.canvas;
|
||||
|
||||
let view = camera.view;
|
||||
let dirty = camera.dirty;
|
||||
if (!view) {
|
||||
view = this._requestView();
|
||||
dirty = true;
|
||||
}
|
||||
if (dirty) {
|
||||
let width = canvas.width;
|
||||
let height = canvas.height;
|
||||
if (camera._framebuffer) {
|
||||
width = camera._framebuffer._width;
|
||||
height = camera._framebuffer._height;
|
||||
}
|
||||
camera.extractView(view, width, height);
|
||||
}
|
||||
this._render(view, scene);
|
||||
}
|
||||
|
||||
_transparentStage (view, items) {
|
||||
// update uniforms
|
||||
this._device.setUniform('view', mat4.array(_a16_view, view._matView));
|
||||
|
||||
Reference in New Issue
Block a user