opengl-es - WebGL2 是否自动内置了抗锯齿功能?

标签 opengl-es webgl webgl2

我一直在阅读这些 examples 的源代码我继续看到这个选项,但是,我无法在任何地方找到这是否是受支持的功能。你只是通过打开这个标志来获得 antialias 吗?有关此功能的更多详细信息吗?

最佳答案

Do you simply get antialias by turning on this flag?

不是,只是要求,不是要求

来自规范:

5.2.1 Context creation parameters

...

antialias

If the value is true and the implementation supports antialiasing the drawing buffer will perform antialiasing using its choice of technique (multisample/supersample) and quality. If the value is false or the implementation does not support antialiasing, no antialiasing is performed.

还有这个

2.2 The Drawing Buffer

...

The depth, stencil and antialias attributes, when set to true, are requests, not requirements. The WebGL implementation should make a best effort to honor them. When any of these attributes is set to false, however, the WebGL implementation must not provide the associated functionality.

通过将其设置为 false,您告诉浏览器“不要打开抗锯齿”期间。例如,如果您正在制作像素化游戏,您可能希望告诉浏览器不要抗锯齿。

通过不设置标志,浏览器通常会尝试使用抗锯齿。通过将标志设置为 true,浏览器可能会将其视为提示,但抗锯齿是否发生以及它如何发生(它使用什么设置或技术等)仍然取决于浏览器。通常存在与抗锯齿相关的错误,因此浏览器通常被迫不支持某些 GPU。浏览器也可能会根据性能拒绝。例如,当不设置标志时,浏览器可能决定不使用抗锯齿来支持智能手机的性能,然后设置标志可能会暗示应用程序更喜欢抗锯齿而不是性能,但这仍然由浏览器决定。

这是一个测试

test("webgl");
test("webgl2");

function test(webglVersion) {
  antialiasTest(webglVersion, {}, "default");
  antialiasTest(webglVersion, {antialias: true}, "true");
  antialiasTest(webglVersion, {antialias: false}, "false");
}

function antialiasTest(webglVersion, options, desc) {
  const canvas = document.createElement("canvas");
  canvas.width = 2;
  canvas.height = 2;
  const gl = canvas.getContext(webglVersion, options);
  if (!gl) {
    log(webglVersion, 'not supported');
    return;
  }
  
  const vs = `
  attribute vec4 position;
  void main() {
     gl_Position = position;
  }
  `;
  const fs = `
  void main() {
    gl_FragColor = vec4(1, 0, 0, 1);
  }
  `;
  const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
  const bufferInfo = twgl.createBufferInfoFromArrays(gl, {
    position: {
      numComponents: 2,
      data: [
        -1, -1, 
         1, -1,
        -1,  1,
      ],
    },
  });
  gl.useProgram(programInfo.program);
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  gl.drawArrays(gl.TRIANGLES, 0, 3);
  const pixels = new Uint8Array(2 * 2 * 4);
  gl.readPixels(0, 0, 2, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
  const isNotAntialiased = 
    isRedOrBlack(pixels[ 0]) && 
    isRedOrBlack(pixels[ 4]) && 
    isRedOrBlack(pixels[ 8]) && 
    isRedOrBlack(pixels[12]) ; 
  log(webglVersion, 'with antialias =', desc, 'was', isNotAntialiased ? 'NOT' : '', 'antialiased');
}

function isRedOrBlack(r) {
  return r === 255 || r === 0;
}
function log(...args) {
  const elem = document.createElement("div");
  elem.textContent = [...args].join(' ');
  document.body.appendChild(elem);
}
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>

虽然与切线相关,WebGL2 允许您使用 renderbufferStorageMultisample 创建抗锯齿渲染缓冲区,并使用 blitFramebuffer 解析它们,这是 WebGL1 中不可用的功能。渲染到抗锯齿帧缓冲区,然后将其 blit 到 Canvas 是强制抗锯齿的一种方式,至少在 WebGL2 中是这样。

关于opengl-es - WebGL2 是否自动内置了抗锯齿功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50255282/

相关文章:

qt 4.8.2 qpa QGraphicsView(QGLWidget)渲染产生黑屏

oop - 在面向对象的设置中使用OpenGL绘图操作?

javascript - 在 ThreeJs 中围绕中心点上下移动 3D 相机

javascript - 如何从 Canvas 合成渲染大比例图像

javascript - 在 webgl 中渲染到纹理

reactjs - 显示: none on Deck. gl React组件导致巨大的性能问题

android - libgdx SpriteBatch 渲染到纹理

google-chrome - Firefox 和 Chrome 之间的 webgl 性能差异

javascript - 使用 webgl 2.0、延迟着色、帧缓冲区对带有三 Angular 形网格的场景进行光线追踪

javascript - webgl、纹理坐标和obj