程序纹理棋盘 OpenGL

Procedural Texturing Checkerboard OpenGL

我在实现棋盘的程序纹理时遇到了一些困难。这是我需要得到的:

这是我得到的:

很接近,但我的纹理在我需要得到的方面有点旋转。

这是我的着色器代码:

#version 330

in vec2 uv;

out vec3 color;

uniform sampler1D colormap;

void main() {
    float sx = sin(10*3.14*uv.x)/2 + 0.5;

    float sy = sin(10*3.14*uv.y)/2 + 0.5;

    float s = (sx + sy)/2;


    if(true){
            color = texture(colormap,s).rgb;
     }

colormap 是从 0 到 1 的映射,其中 0 对应红色,1 对应绿色。

我认为问题出在我使用的公式 (sx+sy)/2 上。我需要让正方形不旋转但与大正方形的边界对齐。 如果有人有想法得到好的公式。

谢谢。

也许是这样的:

float sx = sin(10.0 * M_PI * uv.x);
float sy = sin(10.0 * M_PI * uv.y);
float s = sx * sy / 2.0 + 0.5;

示例(无纹理): https://www.shadertoy.com/view/4sdSzn

可以给"uv"添加旋转操作:

mat2 R(float degrees){
    mat2 R = mat2(1);
    float alpha = radians(degrees);
    R[0][0] =  cos(alpha);
    R[0][1] =  sin(alpha);
    R[1][0] = -sin(alpha);
    R[1][1] =  cos(alpha);
    return R;
}

void main(){
    ver2 new_uv = R(45) * uv;
    float sx = sin(10*3.14*new_uv.x)/2 + 0.5;
    float sy = sin(10*3.14*new_uv.y)/2 + 0.5;
    float s = (sx + sy)/2;
    color = texture(colormap,s).rgb;
}