代码之家  ›  专栏  ›  技术社区  ›  rubyquartz

在细胞中产生噪音-cpp和glsl

  •  1
  • rubyquartz  · 技术社区  · 6 年前

    我的代码旨在创建类似于此图像的东西。 https://thebookofshaders.com/edit.php#12/cellnoise-01.frag

    在我的实现中,我将图像分割为8x8网格,目的是实现类似的效果。调试之后,我正在努力找出为什么我的点会在每个单元格中绘制,但是它们周围的其他点都是白色的,并且不会随着距离而改变。注意网格中有64个独特的点(它们还没有被随机分配,但是每个单元格有一个)。

    以下是我的主要方法:

    uniform ivec2 u_Dimensions;
    uniform int u_Time;
    
    in vec2 fs_UV;
    
    out vec3 color;
    
    void main()
    {
    
    //divide the image into a grid
    
    float xpos = (fs_UV.x * u_Dimensions.x);
    float ypos = (fs_UV.y * u_Dimensions.y);
    
    float width = float(u_Dimensions.x);
    float height = float(u_Dimensions.y);
    
    float cell_height = u_Dimensions.y / 8.f;
    float cell_width = u_Dimensions.x / 8.f;
    
    //arrays to contain the x and y for each placed point
    
    float px[64];
    float py[64];
    
    int p_count = 0;
    
    for(float i = 0; i < u_Dimensions.y; i+= cell_height){
        for(float j = 0; j < u_Dimensions.x; j+= cell_width){
    
            px[p_count] = int(j + cell_width / 5.f);    //x and y
            py[p_count] = int(i + cell_height / 2.f);   //placed manually for now
    
            p_count++;
        }
    }
    
    
    int cellx = int(float(xpos / cell_width));
    int celly = int(float(ypos / cell_height));
    int index = (cellx) + (8 * celly);
    
    for(int i = 0; i < 64; i++){
    
        if (px[i] == int(xpos) && py[i] == int(ypos)){
            color = vec3(0, 0, 0);
            break;                      //all dots successfully draw in black
    
        } else {
    
            float close_x = (px[index]);    //these values are registering fine
            float close_y = (py[index]);
    
            color = vec3(255, 255, 255) / vec3(distance(vec2(xpos, ypos), vec2(close_x, close_y)));   
            //final color always appears 100% white, no gradient?
        }
    
    }
    
    }
    

    有什么帮助或指导吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Makogan    6 年前

    您的代码不能按预期工作的原因是因为glsl中的有效值范围是[0,1],但是您的voronoi单元算法使用的值范围是[0,255]。

    正如评论中指出的。