跳到主要内容
← Ray Tracing

Ray Tracing

Interpolation in barycentric coordinates

shader-learning 自学第 4 课:Interpolation in barycentric coordinates

实时渲染 · 2D 片段 GLSL

第 4 课:Interpolation in barycentric coordinates

实现

uniform vec2 iResolution;

const float c_Epsilon = 0.001;

void main() {
  vec3 P = vec3(gl_FragCoord.xy / iResolution, 1.0);

  vec3 P1 = vec3(0.25, 0.25, 1.0);
  vec3 P2 = vec3(0.50, 0.75, 1.0);
  vec3 P3 = vec3(0.75, 0.25, 1.0);

  mat3 m = mat3(P1, P2, P3);

  vec3 w = inverse(m) * P;
  float sum_w = w.x + w.y + w.z;

  vec3 C1 = vec3(1.0, 0.0, 0.0);
  vec3 C2 = vec3(0.0, 1.0, 0.0);
  vec3 C3 = vec3(0.0, 0.0, 1.0);

  vec3 color = C1 * w.x + C2 * w.y + C3 * w.z;
  color *= step(0.0, w.x);
  color *= step(0.0, w.y);
  color *= step(0.0, w.z);

  gl_FragColor = vec4(color, 1.0);
}