Forge Shader Gridのロゴ

Forge Shader Grid

コミュニティ
Nebulavenus
forge-shader-grid

Add a procedural anti-aliased grid floor to an SDL3 GPU scene using fwidth()/smoothstep() in the fragment shader. Covers procedural rendering, screen-space derivatives for anti-aliasing, distance fade, and using multiple graphics pipelines in a single render pass.

概要

公開者Nebulavenus
リポジトリforge-gpu
スキル名forge-shader-grid
スター
46
フォーク
7
同梱ファイル
指示のみ
ライセンスZlib
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    NebulavenusがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキル「Forge Shader Grid」をTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/Nebulavenus/forge-gpu/tree/main/.claude/skills/forge-shader-grid
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Nebulavenus/forge-gpu.git /tmp/forge-gpu
mkdir -p .claude/skills
cp -r /tmp/forge-gpu/.claude/skills/forge-shader-grid .claude/skills/forge-shader-grid
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでForge Shader Gridを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあるとすぐに、モデルが自動的にForge Shader Gridを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Forge Shader Gridは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、スキルを使わない間は指示の読み込みにトークンを消費しません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Shader Grid

When to use

  • You need a ground grid or floor plane in a 3D scene
  • You want procedural patterns (no texture) with anti-aliasing
  • You need a debug visualization plane for positioning objects
  • You want to combine multiple rendering techniques in one render pass
  • You need to render objects with different vertex formats in the same pass

Core technique: anti-aliased procedural grid

The grid is rendered on a flat quad. The fragment shader computes grid lines procedurally from the world-space position using screen-space derivatives.

HLSL fragment shader pattern

hlsl
/* Step 1: Scale world position to grid space */
float2 grid_uv = world_pos.xz / grid_spacing;

/* Step 2: Distance to nearest grid line (0 = on line, 0.5 = between) */
float2 dist = abs(frac(grid_uv - 0.5) - 0.5);

/* Step 3: Screen-space rate of change (pixel footprint in grid space) */
float2 fw = fwidth(grid_uv);

/* Step 4: Anti-aliased line mask (smooth edge, not hard step) */
float2 aa_line = 1.0 - smoothstep(line_width, line_width + fw, dist);

/* Step 5: Combine X and Z lines */
float grid = max(aa_line.x, aa_line.y);

/* Step 6a: Frequency-based fade (prevent moiré at low angles) */
/* When fwidth >= 0.5, a pixel spans more than half a grid cell —
 * the grid pattern exceeds the Nyquist limit and cannot be resolved. */
float max_fw = max(fw.x, fw.y);
grid *= 1.0 - smoothstep(0.3, 0.5, max_fw);

/* Step 6b: Distance fade (secondary limit for extreme distances) */
float cam_dist = length(world_pos - eye_pos.xyz);
float fade = 1.0 - smoothstep(fade_distance * 0.5, fade_distance, cam_dist);
grid *= fade;

/* Mix line and background colors */
float3 surface = lerp(bg_color.rgb, line_color.rgb, grid);

Why this works

  • fwidth() uses the same derivative hardware as mip selection (Lesson 05)
  • The transition width is always one pixel, so lines look crisp at any distance
  • Frequency-based fade detects when grid cells become sub-pixel (Nyquist limit) and fades the grid regardless of viewing angle — this prevents moiré at low grazing angles where the distance fade alone is insufficient
  • Distance fade provides a secondary limit for extreme distances

Multiple pipelines pattern

Different objects in a scene often need different pipeline configurations. You can switch pipelines within a single render pass:

c
/* Begin one render pass with color + depth targets */
SDL_GPURenderPass *pass = SDL_BeginGPURenderPass(cmd, &color, 1, &depth);

/* Pipeline 1: grid (position-only vertices, no sampler, CULL_NONE) */
SDL_BindGPUGraphicsPipeline(pass, grid_pipeline);
SDL_PushGPUVertexUniformData(cmd, 0, &grid_vert_uniforms, sizeof(...));
SDL_PushGPUFragmentUniformData(cmd, 0, &grid_frag_uniforms, sizeof(...));
/* bind grid VB/IB, draw */

/* Pipeline 2: model (pos+normal+uv vertices, sampler, CULL_BACK) */
SDL_BindGPUGraphicsPipeline(pass, model_pipeline);
/* push model uniforms, bind model VB/IB, draw */

SDL_EndGPURenderPass(pass);

Both pipelines share the same render targets. The depth buffer handles occlusion between them automatically.

Grid pipeline setup

c
/* Vertex format: position only (float3, 12 bytes) */
SDL_GPUVertexAttribute grid_attr;
SDL_zero(grid_attr);
grid_attr.location = 0;
grid_attr.format   = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
grid_attr.offset   = 0;

/* Pipeline: no backface culling (visible from both sides) */
pipeline_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;

/* Fragment shader: 0 samplers, 1 uniform buffer */
/* Vertex shader: 0 samplers, 1 uniform buffer (VP matrix) */

Grid geometry

A simple quad on the XZ plane:

c
#define GRID_HALF_SIZE 50.0f

float vertices[] = {
    -GRID_HALF_SIZE, 0.0f, -GRID_HALF_SIZE,
     GRID_HALF_SIZE, 0.0f, -GRID_HALF_SIZE,
     GRID_HALF_SIZE, 0.0f,  GRID_HALF_SIZE,
    -GRID_HALF_SIZE, 0.0f,  GRID_HALF_SIZE,
};
Uint16 indices[] = { 0, 1, 2, 0, 2, 3 };

Grid fragment uniforms (96 bytes)

c
typedef struct GridFragUniforms {
    float line_color[4];    /* grid line color (RGBA, linear space)    */
    float bg_color[4];      /* background color (RGBA, linear space)   */
    float light_dir[4];     /* light direction (xyz, w unused)         */
    float eye_pos[4];       /* camera position (xyz, w unused)         */
    float grid_spacing;     /* world units between lines (e.g. 1.0)   */
    float line_width;       /* line thickness in grid space (e.g. 0.02)*/
    float fade_distance;    /* distance for fade-out (e.g. 40.0)      */
    float ambient;          /* ambient intensity [0..1]                */
    float shininess;        /* specular exponent                       */
    float specular_str;     /* specular intensity [0..1]               */
    float _pad0, _pad1;     /* pad to 16-byte alignment                */
} GridFragUniforms;

Key API calls (in order)

  1. SDL_CreateGPUGraphicsPipeline — create grid pipeline (position-only vertex format, CULL_NONE, 1 vertex uniform, 1 fragment uniform, 0 samplers)
  2. SDL_CreateGPUBuffer + SDL_CreateGPUTransferBuffer — upload grid quad vertices and indices
  3. SDL_BeginGPURenderPass — begin pass with color + depth targets
  4. SDL_BindGPUGraphicsPipeline(pass, grid_pipeline) — bind grid pipeline
  5. SDL_PushGPUVertexUniformData(cmd, 0, &vp_matrix, ...) — push view-projection matrix
  6. SDL_PushGPUFragmentUniformData(cmd, 0, &grid_frag_uniforms, ...) — push grid parameters
  7. SDL_BindGPUVertexBuffers / SDL_BindGPUIndexBuffer — bind grid geometry
  8. SDL_DrawGPUIndexedPrimitives — draw grid quad
  9. SDL_BindGPUGraphicsPipeline(pass, model_pipeline) — switch to model pipeline (if needed)
  10. SDL_EndGPURenderPass

Ready-to-use template

Minimal vertex shader (grid.vert.hlsl)

hlsl
cbuffer VertUniforms : register(b0, space1)
{
    float4x4 vp_matrix;
};

struct VSInput  { float3 pos : TEXCOORD0; };
struct VSOutput { float4 clip_pos : SV_Position; float3 world_pos : TEXCOORD0; };

VSOutput main(VSInput input)
{
    VSOutput output;
    output.world_pos = input.pos;
    output.clip_pos  = mul(vp_matrix, float4(input.pos, 1.0));
    return output;
}

Minimal fragment shader (grid.frag.hlsl)

hlsl
cbuffer FragUniforms : register(b0, space3)
{
    float4 line_color;
    float4 bg_color;
    float4 eye_pos;
    float  grid_spacing;
    float  line_width;
    float  fade_distance;
    float  _pad0;
};

float4 main(float4 clip_pos : SV_Position, float3 world_pos : TEXCOORD0) : SV_Target
{
    float2 grid_uv = world_pos.xz / grid_spacing;
    float2 dist    = abs(frac(grid_uv - 0.5) - 0.5);
    float2 fw      = fwidth(grid_uv);
    float2 aa_line = 1.0 - smoothstep(line_width, line_width + fw, dist);
    float  grid    = max(aa_line.x, aa_line.y);

    /* Frequency-based fade: prevent moire at low grazing angles */
    float max_fw = max(fw.x, fw.y);
    grid *= 1.0 - smoothstep(0.3, 0.5, max_fw);

    float cam_dist = length(world_pos - eye_pos.xyz);
    float fade     = 1.0 - smoothstep(fade_distance * 0.5, fade_distance, cam_dist);
    grid *= fade;

    float3 surface = lerp(bg_color.rgb, line_color.rgb, grid);
    return float4(surface, 1.0);
}

Minimal C setup

c
/* Grid geometry — flat quad on XZ plane */
#define GRID_HALF_SIZE 50.0f
float grid_verts[] = {
    -GRID_HALF_SIZE, 0.0f, -GRID_HALF_SIZE,
     GRID_HALF_SIZE, 0.0f, -GRID_HALF_SIZE,
     GRID_HALF_SIZE, 0.0f,  GRID_HALF_SIZE,
    -GRID_HALF_SIZE, 0.0f,  GRID_HALF_SIZE,
};
Uint16 grid_indices[] = { 0, 1, 2, 0, 2, 3 };

/* Grid fragment uniforms */
typedef struct GridFragUniforms {
    float line_color[4];
    float bg_color[4];
    float eye_pos[4];
    float grid_spacing;
    float line_width;
    float fade_distance;
    float _pad0;
} GridFragUniforms;

/* Pipeline switch in render pass */
SDL_BindGPUGraphicsPipeline(pass, grid_pipeline);
SDL_PushGPUVertexUniformData(cmd, 0, &vp_matrix, sizeof(vp_matrix));
SDL_PushGPUFragmentUniformData(cmd, 0, &grid_frag, sizeof(grid_frag));
/* bind VB/IB, draw indexed */
SDL_BindGPUGraphicsPipeline(pass, model_pipeline);
/* push model uniforms, bind model VB/IB, draw */

Common mistakes

  1. Forgetting fwidth() — without it, lines alias badly at distance
  2. No frequency-based fade — distance fade alone is not enough; at low grazing angles, grid cells become sub-pixel before the distance fade kicks in. Use max(fwidth(grid_uv).x, fwidth(grid_uv).y) with smoothstep to fade when pixels span more than ~30–50% of a grid cell (the Nyquist limit)
  3. Using frac() directly — frac(grid_uv) puts the discontinuity at the grid line; frac(grid_uv - 0.5) - 0.5 centers the smooth region on the line, which is what you want
  4. Wrong color space — with SDR_LINEAR swapchain, all colors must be in linear space (not sRGB). Convert hex colors: (value/255)^2.2
  5. Missing depth write on grid — the grid must write to the depth buffer for correct occlusion with other objects
  6. Culling the grid — use CULL_NONE so the grid is visible from below too

Reference

  • Lesson: GPU Lesson 12 — Shader Grid
  • Math: screen-space derivatives (Lesson 05), Blinn-Phong (Lesson 10)
  • Concept: fwidth() = abs(ddx()) + abs(ddy()) — pixel footprint

よくある質問

AIスキル「Forge Shader Grid」では何ができますか?

Add a procedural anti-aliased grid floor to an SDL3 GPU scene using fwidth()/smoothstep() in the fragment shader. Covers procedural rendering, screen-space derivatives for anti-aliasing, distance fade, and using multiple graphics pipelines in a single render pass.

TypingMindでForge Shader Gridを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Forge Shader Gridはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにForge Shader Gridをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/Nebulavenus/forge-gpu/tree/main/.claude/skills/forge-shader-gridを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Forge Shader Gridを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストの内容がこのスキルの用途に合ったときに、このスキルを読み込めます。

Forge Shader Gridで使えるAIモデルの数に制限はありますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもForge Shader Gridを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Forge Shader Grid」は無料ですか?

はい。NebulavenusがZlibライセンスのもとでGitHubに公開しています。ご利用のAIプロバイダーに支払うのは、使用したトークンの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇