Mix - Bilinear Interpolation

Task

To fill the screen using bilinear interpolation, follow these steps:

  1. Assign the color blue to the left-top corner of the screen.

  2. Assign the color red to the left-bottom corner of the screen.

  3. Assign the color green to the right-top corner of the screen.

  4. Assign the color black to the right-bottom corner of the screen.

  5. Use bilinear interpolation to blend the colors in between the corners and fill the screen with a smooth gradient.
    使用双线性的方式铺满屏幕,如下步骤实现:

  6. 屏幕左上角设置为蓝色

  7. 屏幕左下角设置为红色

  8. 屏幕右上角设置为绿色

  9. 屏幕右下角设置为黑色

  10. 使用双线性渲染的方式,两个角直接通过线性插值实现

Theory

Bilinear interpolation 是一种决定每个像素颜色的一种特定的方法。分别是XY轴方向进行线性插值

具体实现步骤

  1. Horizontal Interpolation. 先做一次横向的线性插值.
  2. Vertical Interpolation. 在做一次纵向的线性插值。

注意:因为是一个面有4个点,所以横向插值时需要做上下两边。

Answer

glsl 复制代码
uniform vec2 iResolution;

void main() {
  // Normalized pixel coordinates (from 0 to 1)
  vec2 uv = gl_FragCoord.xy / iResolution.xy;
  
  vec3 red = vec3(1.0, 0.0, 0.0);
  vec3 black = vec3(0.0, 0.0, 0.0);
  vec3 blue = vec3(0.0, 0.0, 1.0);
  vec3 green = vec3(0.0, 1.0, 0.0);
  vec3 t1 = mix(red, black, uv.x);
  vec3 t2 = mix(blue, green, uv.x);
  vec3 color = mix(t1, t2, uv.y);
  
  gl_FragColor = vec4(color, 1.0);
}

效果

练习

Mix - Bilinear Interpolation

最后

如果你觉得这篇文章有用,记得点赞、关注、收藏,学Shader更轻松!!

相关推荐
天***885219 分钟前
Edge 浏览器离线绿色增强版+官方安装包,支持win7等系统
前端·edge
漫游的渔夫28 分钟前
别再直接 `json.loads` 了!AI 返回的 JSON 坑位指南
前端·人工智能
软件工程师文艺39 分钟前
从0到1:Claude Code如何用React构建CLI应用
前端·react.js·前端框架
M ? A1 小时前
Vue 迁移 React 实战:VuReact 一键自动化转换方案
前端·vue.js·经验分享·react.js·开源·自动化·vureact
yuki_uix1 小时前
重排、重绘与合成——浏览器渲染性能的底层逻辑
前端·javascript·面试
沃尔威武1 小时前
调试黑科技:Chrome DevTools时间旅行调试实战
前端·科技·chrome devtools
yuki_uix1 小时前
虚拟 DOM 与 Diff 算法——React 性能优化的底层逻辑
前端·react.js·面试
yuki_uix1 小时前
从输入 URL 到页面显示——浏览器工作原理全解析
前端·面试
weixin_408099672 小时前
【完整教程】天诺脚本如何调用 OCR 文字识别 API?自动识别屏幕文字实战(附代码)
前端·人工智能·后端·ocr·api·天诺脚本·自动识别文字脚本
吴声子夜歌2 小时前
ES6——Generator函数详解
前端·javascript·es6