简体   繁体   English

Webgl具有简单纹理的怪异渲染

[英]Webgl weird rendering with simple texture

I'm having trouble rendering textures on my custom 3D shape. 我在自定义3D形状上渲染纹理时遇到问题。

With the following parameters: 具有以下参数:

gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);

It gives me this result: 它给了我这个结果:

结果

Changing the following parameters: 更改以下参数:

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

It gives me this : 它给了我这个:

结果2

And with these parameters: 并使用以下参数:

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

结果3

I tried changing the texture coordinates but without success the one used for each face here was: 0.0, 0.0, 0.0, 10.0, 10.0, 10.0, 10.0, 0.0 我尝试更改纹理坐标,但未成功,这里用于每个面的坐标是: 0.0, 0.0, 0.0, 10.0, 10.0, 10.0, 10.0, 0.0

Any idea why one triangle(actually 2 the one on the parallel face) behaves weirdly? 知道为什么一个三角形(实际上是平行面上的2个三角形)表现得怪异吗?

The issue is that you're using faces with varying amount of points - but still using a fixed number (4) of texture coords for a face. 问题是您使用的点的数量各不相同-但仍要为面孔使用固定数量(4)的纹理坐标。 As some faces have 5 points, at some point the texture coords are "shifted". 由于某些面有5个点,因此在某些点纹理坐标会“移动”。

Instead, you should add them just like the vertices: 相反,您应该像添加顶点一样添加它们:

for (var j=0; j < face.length; j++){
    vertices...

     //generate texture coords, they could also be stored like the points
     textureCoords.push(points[face[j]][0] / 4);
     textureCoords.push((points[face[j]][1]+points[face[j]][2]) / 4);

http://jsfiddle.net/bNTkK/9/ http://jsfiddle.net/bNTkK/9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM