如何清除WebGL中的矩形区域?

Ash*_*ain 2 javascript html5 canvas webgl

WebGL有一种clear清除整个表面的方法.清除表面特定矩形的最佳方法是什么?例如,我想将从(50,50)开始的100x100像素盒设置为全零(ARGB 0,0,0,0).我现在能想到的就是用一个写入零的着色器着色器绘制四边形.有没有更简单的方法?

gma*_*man 14

您可以使用SCISSOR测试将清除(和一般渲染)约束为矩形.

// turn on the scissor test.
gl.enable(gl.SCISSOR_TEST);

// set the scissor rectangle.
gl.scissor(x, y, width, height);

// clear.
gl.clearColor(r, g, b, a);
gl.clear(gl.COLOR_BUFFER_BIT ...);

// turn off the scissor test so you can render like normal again.
gl.disable(gl.SCISSOR_TEST);
Run Code Online (Sandbox Code Playgroud)