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

相关推荐
我是苏苏1 小时前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端
无羡仙1 小时前
Vue插槽
前端·vue.js
用户6387994773052 小时前
每组件(Per-Component)与集中式(Centralized)i18n
前端·javascript
SsunmdayKT2 小时前
React + Ts eslint配置
前端
开始学java2 小时前
useEffect 空依赖 + 定时器 = 闭包陷阱?count 永远停在 1 的坑我踩透了
前端
zerosrat2 小时前
从零实现 React Native(2): 跨平台支持
前端·react native
狗哥哥2 小时前
🔥 Vue 3 项目深度优化之旅:从 787KB 到极致性能
前端·vue.js
青莲8432 小时前
RecyclerView 完全指南
android·前端·面试
青莲8432 小时前
Android WebView 混合开发完整指南
android·前端·面试
GIS之路2 小时前
GDAL 实现矢量数据转换处理(全)
前端