Fract - Grid

Task

Write a shader that draws a grid. Each cell in the grid should be 50x50 pixels in size, with a margin of 10 pixels between cells.
编写一个绘制网格的着色器。网格中的每个单元格应为50x50像素大小,单元格之间的间距为10像素。

Requirements

The shader should avoid using branching or conditional statements in its code, and instead rely on the fract and step functions to determine the color of each pixel.
着色器应避免在其代码中使用分支或条件语句,而是依靠fractstep函数来确定每个像素的颜色。

Theory

把一个单元格和一个间距做为一个整体, 开始10像素作为间距,[10, 60]作为单元格,宽和高使用同样的处理方式,使用step处理各自区域之间的值。

Answer

glsl 复制代码
void main() {
  float cellSize = 50.0f;
  float margin = 10.0f;
  vec2 cell = gl_FragCoord.xy/(cellSize + margin);
  float x = step(margin / (cellSize + margin), fract(cell.x));
  float y = step(margin / (cellSize + margin), fract(cell.y));

  gl_FragColor = vec4(1.0 * x * y, 0.0, 0.0, 1.0);
}

效果

练习

Fract - Grid

最后

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

相关推荐
GreenTea2 小时前
GrokBot 核心成员 Lauren Tan:每月交付 2000 个 PR 的人,是怎么用 AI 的
前端·后端·架构
mCell4 小时前
用 Knip 清理 AI Coding 留下的冗余代码
前端·ai编程·前端工程化
笃行3504 小时前
使用Rokid AIUI做了一个童年推箱子游戏
前端
excel5 小时前
前端加密的作用与使用场景
前端
计算机魔术师6 小时前
纽约时报版权诉讼披露:微软高管内部称训练 AI 是人类历史上最大规模劳动窃取
前端
去伪存真7 小时前
大模型的参数量为什么那么大?
前端·人工智能
IT_陈寒7 小时前
Java空指针这次真把我坑惨了
前端·人工智能·后端
八荒启·交互动画8 小时前
# Web特效020—让 Web 特效真正动起来:时间循环应该怎么接
前端·webgl·网页特效·八荒启-交互动画·八荒启
动恰客流统计8 小时前
线下零售数字化浪潮下,客流统计的3个核心发展趋势
大数据·前端·人工智能
用户938515635079 小时前
从小米前端面试题,彻底搞懂闭包、作用域链与 useState 惰性初始化
前端·面试