【问题标题】:Emulate texture2D in Javascript在 Javascript 中模拟 texture2D
【发布时间】: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


    【解决方案1】:

    您可能已经注意到任何渲染的纹理都是 y 镜像的。

    OpenGL 并且 WebGL 纹理原点位于左下角,而使用 canvas 2d 方法加载时的缓冲区数据具有左上角原点。

    因此,您要么需要重写缓冲区,要么反转 v 坐标。

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 2015-04-24
      • 2014-02-20
      • 2010-11-21
      • 2011-12-23
      • 2015-07-12
      • 1970-01-01
      • 2010-09-16
      • 2015-09-19
      相关资源
      最近更新 更多