How to clear a rectangle area in WebGL?

こ雲淡風輕ζ 提交于 2019-12-24 03:05:07

问题


WebGL has a clear method that clears an entire surface. What's the best way to clear just a particular rectangle of the surface? For example I want to set a 100x100 box of pixels starting at (50, 50) to all zeroes (ARGB 0, 0, 0, 0). All I can think of right now is drawing a quad with a fragment shader that writes zeroes. Is there not an easier way?


回答1:


You can use the SCISSOR test to constrain clearing (and rendering in general) to a rectangle.

// 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);



回答2:


anything outside the viewport if within the webgl context will be drawn, anything outside scissor will be clipped and not drawn at all !



来源:https://stackoverflow.com/questions/11544608/how-to-clear-a-rectangle-area-in-webgl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!