使用模板缓冲区的Three.js

时间:2016-01-26 20:25:11

标签: three.js webgl

我无法得到以下内容将场景绘制为作为模板蒙版创建的形状。相反,代码似乎只是将模板本身渲染为黑色物体。

http://signaturefloors.dev.flooradvisor.com.au/productapp/floor_align.php

我的渲染功能是:

var gl = floor_align.renderer.domElement.getContext('webgl') || floor_align.renderer.domElement.getContext('experimental-webgl');
gl.clearStencil(0);
gl.clear(gl.STENCIL_BUFFER_BIT);
gl.enable(gl.STENCIL_TEST);
gl.stencilFunc(gl.ALWAYS, 1, 1);
gl.stencilOp(gl.KEEP, gl.REPLACE, gl.REPLACE);
gl.colorMask(0, 0, 0, 0);

// Floor Mask (Create a stencil that we render the next pass into)
floor_align.renderer.render(floor_align.maskScene, floor_align.maskCamera);

gl.colorMask(1, 1, 1, 1);
gl.stencilFunc(gl.NOTEQUAL, 1, 1);
gl.stencilOp(gl.KEEP, gl.REPLACE, gl.REPLACE);

// Render a floor pass
floor_align.renderer.render(floor_align.scene, floor_align.camera);

gl.disable(gl.STENCIL_TEST);

渲染器有autoClear = false;

2 个答案:

答案 0 :(得分:3)

接受的答案不适用于最新的threejs。如果您有兴趣,可以使用以下最新版本(使用r111):


        gl.enable(gl.STENCIL_TEST);
        gl.stencilOp(gl.KEEP, gl.KEEP, gl.REPLACE);

        //renderer.clear(); <-- works without this too

        gl.stencilFunc(gl.ALWAYS, 1, 0xFF);
        gl.stencilMask(0xFF);
        renderer.render(maskScene, camera);

        gl.stencilFunc(gl.EQUAL, 1, 0xFF);
        gl.stencilMask(0x00);

        renderer.render(scene, camera);
        gl.stencilMask(0xFF);
        gl.disable(gl.STENCIL_TEST);

在我的情况下,我也没有禁用自动清除功能,但仍然可以使用。

答案 1 :(得分:1)

通过反复试验,我将我的代码更新为此,现在可以使用了。清除深度缓冲区似乎特别重要,所以我猜我的面具一定是隐藏了更远的地板碎片。

// Render the scene
function fla_render() {

    floor_align.renderer.clear();

    // Background
    //floor_align.renderer.render(floor_align.scene, floor_align.camera);

    floor_align.renderer.clearDepth();

    var gl = floor_align.renderer.domElement.getContext('webgl') || floor_align.renderer.domElement.getContext('experimental-webgl');

    // Clear the stencil buffer
    gl.clearStencil(0);
    gl.clear(gl.STENCIL_BUFFER_BIT);

    // Replacing the values at the stencil buffer to 1 on every pixel we draw
    gl.stencilFunc(gl.ALWAYS, 1, 1);
    gl.stencilOp(gl.KEEP, gl.REPLACE, gl.REPLACE);

    // Disable color (u can also disable here the depth buffers)
    gl.colorMask(false, false, false, false);

    // Write to stencil
    gl.enable(gl.STENCIL_TEST);

    // Floor Mask (Create a stencil that we render the next pass into)
    floor_align.renderer.render(floor_align.maskScene, floor_align.maskCamera);

    // Telling the stencil now to draw/keep only pixels that equals 1 - which we set earlier
    gl.stencilFunc(gl.EQUAL, 1, 1);
    gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);

    // Clear depth buffer (seems important)
    floor_align.renderer.clearDepth();

    // Enable color
    gl.colorMask(true, true, true, true);

    // Render a floor pass
    floor_align.renderer.render(floor_align.scene, floor_align.camera);

    // Disable stencil test;
    gl.disable(gl.STENCIL_TEST);

}