我已经使用width=16height=16创建了一个 Canvas 。然后,我使用WebGL为其渲染图像。看起来是这样的:

javascript - 如何拉伸(stretch)WebGL Canvas 而不会模糊?样式 “image-rendering”不起作用-LMLPHP

之后,我使用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;
      }

结果如下:

javascript - 如何拉伸(stretch)WebGL Canvas 而不会模糊?样式 “image-rendering”不起作用-LMLPHP

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

最佳答案

Safari尚未在WebGL上支持image-rendering: pixelated;Filed a bug

同样crisp-edges不是!= pixelatedcrisp-edges可以是多种算法。这并不意味着pixelated。这意味着应用一些算法可以保持边缘清晰,其中有很多算法。

The spec itself显示示例:

鉴于此图像:

javascript - 如何拉伸(stretch)WebGL Canvas 而不会模糊?样式 “image-rendering”不起作用-LMLPHP

这是pixelated:

javascript - 如何拉伸(stretch)WebGL Canvas 而不会模糊?样式 “image-rendering”不起作用-LMLPHP

在允许浏览器对crisp-edges使用各种算法的情况下,例如,结果可能是

javascript - 如何拉伸(stretch)WebGL Canvas 而不会模糊?样式 “image-rendering”不起作用-LMLPHP

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

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

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取决于浏览器来解释。

07-28 05:33