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

相关推荐
IT从业者张某某21 分钟前
less 工具 OpenHarmony PC适配实践
前端·microsoft·less
行走的陀螺仪1 小时前
vue3-封装权限按钮组件和自定义指令
前端·vue3·js·自定义指令·权限按钮
isyuah1 小时前
vite-plugin-openapi-ts CLI 使用指南
前端·vite
qq_398586541 小时前
浏览器中内嵌一个浏览器
前端·javascript·css·css3
Mapmost2 小时前
地图引擎性能优化:解决3DTiles加载痛点的六大核心策略
前端
San30.2 小时前
Ajax 数据请求:从 XMLHttpRequest 到现代前端数据交互的演进
前端·ajax·交互
西西西西胡萝卜鸡2 小时前
虚拟列表(Virtual List)组件实现与优化铁臂猿版(简易版)
前端·vue.js
宇余2 小时前
Unibest:新一代uni-app工程化最佳实践指南
前端·vue.js
性野喜悲2 小时前
ts+uniapp小程序时间日期选择框(分开选择)
前端·javascript·vue.js
P***25394 小时前
前端构建工具缓存清理,npm cache与yarn cache
前端·缓存·npm