动态更新渲染目标的宽度和高度

时间:2017-06-26 15:42:01

标签: webgl

我创建了一个纹理和一个渲染缓冲区,用于将现有的三维场景渲染为一个纹理,并将该纹理用作另一个webgl程序的输入。

下面是处理将场景渲染到纹理的类的伪代码。

根据需要更新纹理宽度和高度的最佳方法是什么(例如,如果浏览器调整大小)?我是否每次都需要创建一个新的纹理/渲染缓冲区?

我想知道我能用比现在更少的代码来做这件事吗?

class Renderer {
    constructor() {
        // creates the texture and framebuffer for the first time 
        this.updateRTT(128, 128);
    }

    updateRTT(width, height) {
        const gl = getContext();

        this.rttwidth = width;
        this.rttheight = height;
        console.log('update RTT', this.rttwidth, this.rttheight);

        this.frameBuffer = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);

        this.texture = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, this.texture);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.rttwidth, this.rttheight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

        this.renderBuffer = gl.createRenderbuffer();
        gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer);
        gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, this.rttwidth, this.rttheight);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.texture, 0);
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this.renderBuffer);

        gl.bindTexture(gl.TEXTURE_2D, null);
        gl.bindRenderbuffer(gl.RENDERBUFFER, null);
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    }

    render_to_target(width, height) {
        // is there a better way to just update the texture/framebuffer?
        if (this.rttwidth !== width || this.rttheight !== height) {
           // if the width or height is different from the previous one, update the texture and framebuffer
           this.updateRTT(width, height);
        }

        gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);

        // draw my scene here


        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, this.texture);
        gl.generateMipmap(gl.TEXTURE_2D);

        // clear
        gl.bindTexture(gl.TEXTURE_2D, null);
        gl.bindRenderbuffer(gl.RENDERBUFFER, null);
        gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    }
}

1 个答案:

答案 0 :(得分:2)

足以重置纹理/ renderbuffer属性,如下所示:

resize(width, height) {
    // resize color attachment
    gl.bindTexture(gl.TEXTURE_2D, this.texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.bindTexture(gl.TEXTURE_2D, null);

    // resize depth attachment
    gl.bindRenderbuffer(gl.RENDERBUFFER, this.renderBuffer);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
    gl.bindRenderbuffer(gl.RENDERBUFFER, null);

    // update internal dimensions
    this.rttwidth=width;
    this.rttheight=height;
}