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.
着色器应避免在其代码中使用分支或条件语句,而是依靠fract和step函数来确定每个像素的颜色。

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更轻松!!

相关推荐
花开盛夏^.^4 小时前
电脑打开多个浏览器Web窗口占用较大的运行内存的原因
前端·浏览器
馒头不想说话5 小时前
实体店老板的24小时AI运营员工,写、剪、发全自动,省下一整个团队工资
前端
椒哥8 小时前
一天被问几十次"允许",我统计了 517 次,然后写了个插件
前端·javascript·github
风骏时光牛马8 小时前
生成一个AI稳定性治理及疑难问题分析标题,只需要一个标题,不需要其他的任何文本。
前端
莫西干式秃头8 小时前
用 AI + threejs 写了个浏览器里的 3D 动作游戏《武来》:砍怪、放技能、开 SU7
前端·人工智能·three.js
kyriewen8 小时前
我测了 6 种防请求竞态的写法:只有 1 种真能打
前端·javascript·程序员
孟健8 小时前
用 WorkBuddy 做个人名片网页,挂上开源设计技能后效果完全不同了
前端·人工智能·ai编程
IT_陈寒8 小时前
SpringBoot自动配置的坑,这次终于爬出来了
前端·人工智能·后端
LuHang9 小时前
从天地图切换到高德地图:一次真实的底图迁移实战
前端·vue.js·cesium
用户360055579009 小时前
Kestrel_VLLM - 27 · 寄存器常驻与融合发射:两次「少发一次」
前端