小编Der*_*ney的帖子

WebGL图像渲染质量差

我在WebGL中进行图像渲染时遇到问题:我有一个画布,有一个可以容纳整个画布的四边形,还有一个应该映射到整个四边形的纹理(我已经设置了正确的纹理坐标)。

该图像是包含RGB数据(按此顺序)的Uint8array,图像为1024 * 768像素。我有正确的缓冲区。这是图像的链接。

原始图片

问题如下:当渲染到WebGL中时,即使我的画布大小等于图像大小,我的图片也变得模糊不清。看到下面的结果:

通过WebGL渲染

现在输入代码:这是我用来创建纹理句柄的代码:

//texture
that.Texture = that.gl.createTexture();         
that.gl.bindTexture(that.gl.TEXTURE_2D, that.Texture);                          

// Set the parameters so we can render any size image.
that.gl.texParameteri(that.gl.TEXTURE_2D, that.gl.TEXTURE_WRAP_S, that.gl.CLAMP_TO_EDGE);
that.gl.texParameteri(that.gl.TEXTURE_2D, that.gl.TEXTURE_WRAP_T, that.gl.CLAMP_TO_EDGE);
that.gl.texParameteri(that.gl.TEXTURE_2D, that.gl.TEXTURE_MIN_FILTER, that.gl.NEAREST);
that.gl.texParameteri(that.gl.TEXTURE_2D, that.gl.TEXTURE_MAG_FILTER, that.gl.NEAREST);
Run Code Online (Sandbox Code Playgroud)

数据被加载到纹理上,如下所示:

this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGB, this.m_iImageWidth, this.m_iImageHeight,
0, this.gl.RGB, this.gl.UNSIGNED_BYTE, this.m_aImage);
Run Code Online (Sandbox Code Playgroud)

最后,这是我使用的片段着色器:

precision mediump float;
uniform sampler2D u_image;
varying vec2 v_texCoord;
void main() 
{
   gl_FragColor = texture2D(u_image, v_texCoord);
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多选项,从过滤到设置样式选项,将图像渲染为像素化,将图像转换为RGBA并为其提供RGBA值,结果始终是糟糕的纹理渲染。即使画布与纹理的大小完全相同,WebGL似乎也无法正确插入数据。

有人提示吗?

提前致谢。

javascript canvas webgl image-rendering

2
推荐指数
1
解决办法
2953
查看次数

标签 统计

canvas ×1

image-rendering ×1

javascript ×1

webgl ×1