版本:2.4.13,怎么将多个cc.SpriteFrame绘制到一张cc.SpriteFrame上

引擎里不直接使用texture,直接使用的是精灵叠加在一起,进行渲染排序。最终结果是不同精灵进行不同的渲染顺序,形成了同一张图显示在屏幕上。

引擎以前都能合,以前是有cc.RenderTexture().bgain()和end()函数了,不知道后来为什么给移除了,还不给平替方案 :sob:

这个大多数是用来截图读取屏幕像素的,现在是
initRender() {

        let texture = new cc.RenderTexture();

        var size = cc.view.getFrameSize();

        texture.initWithSize(size.width, size.height, cc.gfx.RB_FMT_S8);

        this.camera_result.targetTexture = texture;

        this.camera_result.zoomRatio = cc.view.getVisibleSize().height / (size.height / cc.view.getScaleY());

        this.texture_result = texture;

    },

    initImage() {

        let data = this.texture_result.readPixels();

        this._width = this.texture_result.width;

        this._height = this.texture_result.height;

        let picData = this.filpYImage(data, this._width, this._height);

        return picData;

    },

用相机实现截图功能

相机截图效率太低了吧,怎么将两个let data = this.texture_result.readPixels();混合并渲染出来呢? :sob:

一个相机有分组,相机只渲染相同分组的节点,读取的texture_result就是相机的当前画面。

相机确实简单,但是感觉绕远了 :rofl:

你要的功能本应该通过cocos的框架去实现,而不是自己去模拟相机渲染。

所以说这引擎没做的嘛

引擎的渲染框架追求的是通用,通用的渲染需要依托框架实现,你这个拿底层数据,模拟底层渲染,需要看cocos的底层代码实现。合并texture这件事,也可以自己按照数组进行合并。对于alpha空白的像素使用背景像素,对于alpha不空白的像素使用顶层像素,如果有透明,合并alpha

顶一顶顶一顶顶一顶顶一顶

顶一顶顶一顶顶一顶顶一顶

你应该是要混合吧,shader里面采样多张纹理混合后输出

就是要在不用shader的前提下做到这点 :sob:

那就是读取像素值然后根据rgba值比例权重计算新的值

自己算像素的话,js这性能怕不是要算几分钟,还不如用摄像机截图,而这截图我都嫌慢 :sob:

用rendertexture 解决

用了的,后面绘制的会擦除前面绘制的东西像我一楼那样背景都给擦没了

创建一个render texture不就好了。在缓冲区中进行

创建了的 你看一楼就是这么干的,后面drawTextureAt的东西会覆盖前面的,大佬有啥好的想法能麻烦给下代码吗?有偿

/**

 * 获取像素数据

 * @param node 节点

 * @param flipY 垂直翻转数据

 */

public static getPixelsData(node: cc.Node, flipY: boolean = true) {

    if (!cc.isValid(node)) {

        return null;

    }

    // 节点宽高

    const width = Math.floor(node.width),

        height = Math.floor(node.height);

    // 创建临时摄像机用于渲染目标节点

    const cameraNode = new cc.Node();

    cameraNode.parent = node;

    const camera = cameraNode.addComponent(cc.Camera);

    camera.clearFlags |= cc.Camera.ClearFlags.COLOR;

    camera.backgroundColor = cc.color(0, 0, 0, 0);

    camera.zoomRatio = cc.winSize.height / height;

    // 将节点渲染到 RenderTexture 中

    const renderTexture = new cc.RenderTexture();

    renderTexture.initWithSize(width, height, cc.RenderTexture.DepthStencilFormat.RB_FMT_S8);

    camera.targetTexture = renderTexture;

    camera.render(node);

    // 获取像素数据

    const pixelsData = renderTexture.readPixels();

    // 销毁临时对象并返回数据

    renderTexture.destroy();

    cameraNode.destroy();

    // 垂直翻转数据

    if (flipY) {

        const length = pixelsData.length,

            lineWidth = width * 4,

            data = new Uint8Array(length);

        for (let i = 0, j = length - lineWidth; i < length; i += lineWidth, j -= lineWidth) {

            for (let k = 0; k < lineWidth; k++) {

                data[i + k] = pixelsData[j + k];

            }

        }

        return data;

    }

    return pixelsData;

}