【发布时间】:2014-09-15 10:40:49
【问题描述】:
我编写了一个着色器,而不是通过高度图纹理转换顶点位置。因为几何图形正在顶点着色器上进行转换,所以我不能在 javascript 中使用传统的拾取算法,而无需对着色器进行逆向工程以将顶点置于其转换后的位置。我对 GLSL 中的 texture2D 函数的理解似乎有问题。如果忽略纹理包装,您将如何在 JS 中模拟相同的功能?这就是我目前的做法:
/**
* Gets the normalized value of an image's pixel from the x and y coordinates. The x and y coordinates expected here must be between 0 and 1
*/
sample( data, x, y, wrappingS, wrappingT )
{
var tempVec = new Vec2();
// Checks the texture wrapping and modifies the x and y accordingly
this.clampCoords( x, y, wrappingS, wrappingT, tempVec );
// Convert the normalized units into pixel coords
x = Math.floor( tempVec.x * data.width );
y = Math.floor( tempVec.y * data.height );
if ( x >= data.width )
x = data.width - 1;
if ( y >= data.height )
y = data.height - 1;
var val= data.data[ ((data.width * y) + x) * 4 ];
return val / 255.0;
}
这个函数似乎产生了正确的结果。我有一个 409 像素宽 x 434 像素高的纹理。我将图像着色为黑色,除了我将最后一个像素着色为红色(408、434)。所以当我在 JS 中调用我的采样器函数时:
this.sample(imgData, 0.9999, 0.9999. wrapS, wrapT)
结果是 1。对我来说哪个是正确的,因为它指的是红色像素。
但这似乎不是 GLSL 给我的。在 GLSL 中,我使用它(作为测试):
float coarseHeight = texture2D( heightfield, vec2( 0.9999, 0.9999 ) ).r;
我希望粗略高度也应该是 1 - 但它应该是 0。我不明白这一点...有人能给我一些关于我哪里出错的见解吗?
【问题讨论】:
标签: javascript opengl-es webgl