Chuyển đến nội dung
Skillpedia
Lập trình chung48k · #5769

shader-programming-glsl

Skill này làm gì

Hướng dẫn này dạy cách tạo hiệu ứng hình ảnh cho game và website bằng một ngôn ngữ đặc biệt gọi là GLSL. Nó chỉ cách làm cho đồ họa đẹp và chạy nhanh.

Khi nào nên dùng

  • ✓Khi tạo hiệu ứng hình ảnh tùy chỉnh cho game hoặc website.
  • ✓Khi tối ưu tốc độ hiển thị đồ họa trên màn hình.
  • ✓Khi thêm hiệu ứng như làm mờ hoặc phát sáng cho hình ảnh.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/shader-programming-glsl/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/shader-programming-glsl/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để tạo hiệu ứng phát sáng cho một vật thể 3D bằng GLSL?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

GLSL dùng để đưa chỉ dẫn trực tiếp cho card đồ họa (GPU). Nó có hai phần chính: một phần xác định hình dạng (vertex shader) và phần còn lại xác định màu của từng điểm nhỏ trên màn hình (fragment shader).

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Shader Programming GLSL

Overview

A comprehensive guide to writing GPU shaders using GLSL (OpenGL Shading Language). Learn syntax, uniforms, varying variables, and key mathematical concepts like swizzling and vector operations for visual effects.

When to Use This Skill

  • Use when creating custom visual effects in WebGL, Three.js, or game engines.
  • Use when optimizing graphics rendering performance.
  • Use when implementing post-processing effects (blur, bloom, color correction).
  • Use when procedurally generating textures or geometry on the GPU.

Step-by-Step Guide

1. Structure: Vertex vs. Fragment

Understand the pipeline:

  • Vertex Shader: Transforms 3D coordinates to 2D screen space (gl_Position).
  • Fragment Shader: Colors individual pixels (gl_FragColor).
// Vertex Shader (basic)
attribute vec3 position;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

void main() {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
// Fragment Shader (basic)
uniform vec3 color;

void main() {
    gl_FragColor = vec4(color, 1.0);
}

2. Uniforms and Varyings

  • uniform: Data constant for all vertices/fragments (passed from CPU).
  • varying: Data interpolated from vertex to fragment shader.
// Passing UV coordinates
varying vec2 vUv;

// In Vertex Shader
void main() {
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

// In Fragment Shader
void main() {
    // Gradient based on UV
    gl_FragColor = vec4(vUv.x, vUv.y, 1.0, 1.0);
}

3. Swizzling & Vector Math

Access vector components freely: vec4 color = vec4(1.0, 0.5, 0.0, 1.0);

  • color.rgb -> vec3(1.0, 0.5, 0.0)
  • color.zyx -> vec3(0.0, 0.5, 1.0) (reordering)

Examples

Example 1: Simple Raymarching (SDF Sphere)

float sdSphere(vec3 p, float s) {
    return length(p) - s;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 0.0, -3.0); // Ray Origin
    vec3 rd = normalize(vec3(uv, 1.0)); // Ray Direction
    
    float t = 0.0;
    for(int i = 0; i < 64; i++) {
        vec3 p = ro + rd * t;
        float d = sdSphere(p, 1.0); // Sphere radius 1.0
        if(d < 0.001) break;
        t += d;
    }
    
    vec3 col = vec3(0.0);
    if(t < 10.0) {
        vec3 p = ro + rd * t;
        vec3 normal = normalize(p);
        col = normal * 0.5 + 0.5; // Color by normal
    }
    
    fragColor = vec4(col, 1.0);
}

Best Practices

  • ✅ Do: Use mix() for linear interpolation instead of manual math.
  • ✅ Do: Use step() and smoothstep() for thresholding and soft edges (avoid if branches).
  • ✅ Do: Pack data into vectors (vec4) to minimize memory access.
  • ❌ Don't: Use heavy branching (if-else) inside loops if possible; it hurts GPU parallelism.
  • ❌ Don't: Calculate constant values inside the shader; pre-calculate them on the CPU (uniforms).

Troubleshooting

Problem: Shader compiles but screen is black. Solution: Check if gl_Position.w is correct (usually 1.0). Check if uniforms are actually being set from the host application. Verify UV coordinates are within [0, 1].

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Câu hỏi thường gặp

“Uniforms” và “varyings” là gì?

“Uniforms” là thông tin không thay đổi đối với mọi vật thể, như màu tổng thể. “Varyings” là thông tin thay đổi từ điểm này sang điểm khác, như chi tiết kết cấu bề mặt.

Vì sao không nên dùng câu lệnh “if” trong shader?

Dùng “if” có thể làm chậm card đồ họa vì nó xử lý nhiều việc cùng lúc. Nên dùng các hàm khác hiệu quả hơn cho GPU.

Shader của tôi biên dịch được nhưng màn hình bị đen. Có thể sai ở đâu?

Hãy kiểm tra xem tọa độ có đúng không và thông tin bạn gửi đến shader (uniforms) có được chương trình chính thiết lập đúng cách không.

Chi tiết

Mô tả gốc của tác giả

Expert guide for writing efficient GLSL shaders (Vertex/Fragment) for web and game engines, covering syntax, uniforms, and common effects.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#5769
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự