跳到主要内容
← Noise

Noise

Tiling

shader-learning 自学第 12 课:Tiling

实时渲染 · 2D 片段 GLSL

第 12 课:Tiling

实现

uniform vec2 iResolution;
uniform float iTime;

vec2 points[100];

void init() {
  for (int i = 0; i < 100; i++) {
    points[i] = vec2(sin(float(i)), cos(float(i)));
  }
}

vec2 getPoint(int index) {
  return sin(iTime + points[index] * 6.28) * 0.5 + 0.5;
}

void main() {
  init();

  vec2 uv = gl_FragCoord.xy / iResolution.xy;
  uv *= 10.0;
  vec2 uv_i = floor(uv);

  float m_dist = 1.0;
  for(int y = -1; y <= 1; y++){
    for(int x = -1; x <= 1; x++){
      float index_f = uv_i.x + uv_i.y * 10.0 + float(x) + float(y) * 10.0;
      index_f = mod(index_f + 100.0, 100.0);
      int index = int(index_f);

      vec2 point = getPoint(index) ;
      point = point + vec2(float(x), float(y)) + uv_i;

      float dist = distance(uv, point);
      m_dist = min(m_dist, dist);
    }
  }

  gl_FragColor = vec4(0.0, m_dist, 0.0, 1.0);
}