如何在不模糊的情况下拉伸 WebGL canvas?样式 "image-rendering" 不起作用

How to stretch a WebGL canvas without blurring? The style "image-rendering" doesn't work

我用 width=16height=16 创建了一个 canvas。然后我使用 WebGL 为它渲染图像。这是它的样子:

之后,我使用 width: 256pxheight: 256px 缩放了 canvas。我还将 image-rendering 设置为 pixelated:

      canvas {
        image-rendering: optimizeSpeed;             /* STOP SMOOTHING, GIVE ME SPEED  */
        image-rendering: -moz-crisp-edges;          /* Firefox                        */
        image-rendering: -o-crisp-edges;            /* Opera                          */
        image-rendering: -webkit-optimize-contrast; /* Chrome (and eventually Safari) */
        image-rendering: pixelated; /* Chrome */
        image-rendering: optimize-contrast;         /* CSS3 Proposed                  */
        -ms-interpolation-mode: nearest-neighbor;   /* IE8+                           */
        width: 256px;
        height: 256px;
      }

这是结果:

图像模糊。为什么?我在 OSX Mojave 上使用 Safari 12.0.2。

这是一个非常古老的 Webkit bug,在 Blink 分叉发生之前。 此后,Blink fixed it,Webkit 仍然没有。
您可能希望通过对仍未解决的问题发表评论来让他们知道这仍然是一个问题。

至于解决方法,有几种,但没有一个是完美的。

  • 第一个是直接以正确的尺寸绘制场景并自己进行像素化。
  • 另一种方法是在 2d canvas 上渲染您的 webgl canvas(您可以使用 CSS 技巧调整大小,或者使用二维上下文 imageSmoothingEnabled 属性.
  • CSS-Houdini 可能会让我们自己解决这个问题。

但真正的问题是确定您是否需要此解决方法。我看不出对这种情况进行功能测试的任何意义(至少没有 Houdini),所以这意味着您要么必须进行丑陋的用户代理检测,要么将解决方法应用于所有人。

Safari 尚不支持 image-rendering: pixelated; WebGL。 Filed a bug

crisp-edges不!=pixelatedcrisp-edges 可以是任意数量的算法。它并不意味着 pixelated。这意味着应用一些保持清晰边缘的算法,其中有大量算法。

The spec itself 显示示例:

给定这张图片:

这是pixelated:

允许浏览器对 crisp-edges 使用多种算法,因此例如结果可能是

换句话说,您的 CSS 可能不会产生您期望的结果。如果浏览器不支持像素化但支持清晰边缘,并且如果他们使用上述算法,那么您将不会看到像素化的外观。

在没有 image-rendering: pixelated 的情况下绘制像素化图形的最有效方法是绘制到一个小纹理,然后将该纹理绘制到 canvas 并使用 NEAREST 过滤。

const vs = `
attribute vec4 position;
void main() {
  gl_Position = position;
}
`;
const fs = `
precision mediump float;
void main() {
  gl_FragColor = vec4(1, 0, 0, 1);
}
`;

const screenVS = `
attribute vec4 position;
varying vec2 v_texcoord;
void main() {
  gl_Position = position;
  // because we know position goes from -1 to 1
  v_texcoord = position.xy * 0.5 + 0.5;
}
`;
const screenFS = `
precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D u_tex;
void main() {
  gl_FragColor = texture2D(u_tex, v_texcoord);
}
`;

const gl = document.querySelector('canvas').getContext('webgl', {antialias: false});

// compile shaders, link programs, look up locations
const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
const screenProgramInfo = twgl.createProgramInfo(gl, [screenVS, screenFS]);


const width = 16;
const height = 16;
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_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);

const fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);

// create buffers and put data in
const quadBufferInfo = twgl.createBufferInfoFromArrays(gl, {
  position: { 
    numComponents: 2,
    data: [
      -1, -1, 
       1, -1,
      -1,  1,
      -1,  1, 
       1, -1,
       1,  1,
    ],
  }
});


render();

function render() {
  // draw at 16x16 to texture
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  gl.viewport(0, 0, width, height);
  gl.useProgram(programInfo.program);
  // bind buffers and set attributes
  twgl.setBuffersAndAttributes(gl, programInfo, quadBufferInfo);
  
  gl.drawArrays(gl.TRIANGLES, 0, 3);  // only draw the first triangle
  
  // draw texture to canvas
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  gl.useProgram(screenProgramInfo.program);
  // bind buffers and set attributes
  twgl.setBuffersAndAttributes(gl, screenProgramInfo, quadBufferInfo);
  // uniforms default to 0 so in this simple case
  // no need to bind texture or set uniforms since
  // we only have 1 texture, it's on texture unit 0
  // and the uniform defaults to 0
  
  gl.drawArrays(gl.TRIANGLES, 0, 6);
}
<canvas width="256" height="256"></canvas>
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>

注意:如果您正在渲染 3D 或出于某些其他原因需要深度缓冲区,您需要将深度渲染缓冲区附件添加到帧缓冲区。

请注意,optimizeSpeed 也不是真正的选项。它早就被弃用了,就像 crisp-edges 由浏览器来解释。