glsl by martinholovsky/claude-skills-generator
npx skills add https://github.com/martinholovsky/claude-skills-generator --skill glsl文件组织:本技能采用拆分结构。高级着色器模式请参阅
references/目录。
本技能提供 GLSL 着色器专业知识,用于在 JARVIS AI 助手 HUD 中创建全息视觉效果。它专注于实时渲染的高效 GPU 编程。
风险等级:低 - GPU 端代码攻击面有限,但可能导致性能问题
主要用例:
// tests/shaders/holographic-panel.test.ts
import { describe, it, expect, beforeEach } from 'vitest'
import { WebGLTestContext, captureFramebuffer, compareImages } from '../utils/webgl-test'
describe('HolographicPanelShader', () => {
let ctx: WebGLTestContext
beforeEach(() => {
ctx = new WebGLTestContext(256, 256)
})
// 单元测试:着色器编译
it('should compile without errors', () => {
const shader = ctx.compileShader(holoFragSource, ctx.gl.FRAGMENT_SHADER)
expect(shader).not.toBeNull()
expect(ctx.getShaderErrors()).toEqual([])
})
// 单元测试:Uniform 变量可访问
it('should have required uniforms', () => {
const program = ctx.createProgram(vertSource, holoFragSource)
expect(ctx.getUniformLocation(program, 'uTime')).not.toBeNull()
expect(ctx.getUniformLocation(program, 'uColor')).not.toBeNull()
expect(ctx.getUniformLocation(program, 'uOpacity')).not.toBeNull()
})
// 视觉回归测试
it('should render scanlines correctly', async () => {
ctx.renderShader(holoFragSource, { uTime: 0, uColor: [0, 0.5, 1], uOpacity: 1 })
const result = captureFramebuffer(ctx)
const baseline = await loadBaseline('holographic-scanlines.png')
expect(compareImages(result, baseline, { threshold: 0.01 })).toBeLessThan(0.01)
})
// 边界情况测试
it('should handle extreme UV values', () => {
const testCases = [
{ uv: [0, 0], expected: 'no crash' },
{ uv: [1, 1], expected: 'no crash' },
{ uv: [0.5, 0.5], expected: 'no crash' }
]
testCases.forEach(({ uv }) => {
expect(() => ctx.renderAtUV(holoFragSource, uv)).not.toThrow()
})
})
})
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// 从通过测试的最小着色器开始
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
uniform float uOpacity;
in vec2 vUv;
out vec4 fragColor;
void main() {
// 通过编译测试的最小实现
fragColor = vec4(uColor, uOpacity);
}
// 测试通过后扩展为完整实现
void main() {
vec2 uv = vUv;
float scanline = sin(uv.y * 100.0) * 0.1 + 0.9;
float pulse = sin(uTime * 2.0) * 0.1 + 0.9;
vec3 color = uColor * scanline * pulse;
fragColor = vec4(color, uOpacity);
}
# 运行所有着色器测试
npm run test:shaders
# 视觉回归测试
npm run test:visual -- --update-snapshots # 仅首次运行
npm run test:visual
# 性能基准测试
npm run bench:shaders
# 跨浏览器编译检查
npm run test:webgl-compat
| 版本 | 上下文 | 特性 |
|---|---|---|
| GLSL ES 3.00 | WebGL 2.0 | 现代特性,更好的精度 |
| GLSL ES 1.00 | WebGL 1.0 | 遗留支持 |
#version 300 es
precision highp float;
precision highp int;
// WebGL 2.0 着色器头部
// ❌ 错误 - GPU 分支发散
vec3 getColor(float value) {
if (value < 0.3) {
return vec3(1.0, 0.0, 0.0); // 红色
} else if (value < 0.7) {
return vec3(1.0, 1.0, 0.0); // 黄色
} else {
return vec3(0.0, 1.0, 0.0); // 绿色
}
}
// ✅ 正确 - 使用 mix/step 实现无分支
vec3 getColor(float value) {
vec3 red = vec3(1.0, 0.0, 0.0);
vec3 yellow = vec3(1.0, 1.0, 0.0);
vec3 green = vec3(0.0, 1.0, 0.0);
vec3 color = mix(red, yellow, smoothstep(0.3, 0.31, value));
color = mix(color, green, smoothstep(0.7, 0.71, value));
return color;
}
// ❌ 错误 - 多个纹理绑定
uniform sampler2D uIcon1;
uniform sampler2D uIcon2;
uniform sampler2D uIcon3;
vec4 getIcon(int id) {
if (id == 0) return texture(uIcon1, vUv);
if (id == 1) return texture(uIcon2, vUv);
return texture(uIcon3, vUv);
}
// ✅ 正确 - 单个图集纹理
uniform sampler2D uIconAtlas;
uniform vec4 uAtlasOffsets[3]; // 每个图标的 [x, y, 宽度, 高度]
vec4 getIcon(int id) {
vec4 offset = uAtlasOffsets[id];
vec2 atlasUV = offset.xy + vUv * offset.zw;
return texture(uIconAtlas, atlasUV);
}
// ❌ 错误 - 无论距离远近都使用相同质量
const int NOISE_OCTAVES = 8;
float noise(vec3 p) {
float result = 0.0;
for (int i = 0; i < NOISE_OCTAVES; i++) {
result += snoise(p * pow(2.0, float(i)));
}
return result;
}
// ✅ 正确 - 根据距离减少八度音阶
uniform float uCameraDistance;
float noise(vec3 p) {
// 距离远时使用更少的八度音阶(细节不可见)
int octaves = int(mix(2.0, 8.0, 1.0 - smoothstep(10.0, 100.0, uCameraDistance)));
float result = 0.0;
for (int i = 0; i < 8; i++) {
if (i >= octaves) break;
result += snoise(p * pow(2.0, float(i)));
}
return result;
}
// ❌ 错误 - 许多单独的 uniform 变量
uniform float uPosX;
uniform float uPosY;
uniform float uPosZ;
uniform float uRotX;
uniform float uRotY;
uniform float uRotZ;
uniform float uScaleX;
uniform float uScaleY;
uniform float uScaleZ;
// ✅ 正确 - 打包到向量/矩阵中
uniform vec3 uPosition;
uniform vec3 uRotation;
uniform vec3 uScale;
// 或者更好:
uniform mat4 uTransform;
// ❌ 错误 - 所有内容都使用 highp(浪费 GPU 周期)
precision highp float;
highp vec3 color;
highp float alpha;
highp vec2 uv;
// ✅ 正确 - 根据数据需求匹配精度
precision highp float; // 计算的默认精度
mediump vec3 color; // 0-1 范围,mediump 足够
mediump float alpha; // 0-1 范围
highp vec2 uv; // 纹理坐标需要高精度
lowp int flags; // 类似布尔值的变量
// ❌ 错误 - 冗余的纹理获取
void main() {
vec3 diffuse = texture(uTexture, vUv).rgb;
// ... 一些代码 ...
float alpha = texture(uTexture, vUv).a; // 相同的查找!
// ... 更多代码 ...
vec3 doubled = texture(uTexture, vUv).rgb * 2.0; // 又一次!
}
// ✅ 正确 - 缓存结果
void main() {
vec4 texSample = texture(uTexture, vUv);
vec3 diffuse = texSample.rgb;
float alpha = texSample.a;
vec3 doubled = texSample.rgb * 2.0;
}
// shaders/holographic-panel.frag
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
uniform float uOpacity;
uniform vec2 uResolution;
in vec2 vUv;
out vec4 fragColor;
const int SCANLINE_COUNT = 50;
void main() {
vec2 uv = vUv;
// 扫描线效果
float scanline = 0.0;
for (int i = 0; i < SCANLINE_COUNT; i++) {
float y = float(i) / float(SCANLINE_COUNT);
scanline += smoothstep(0.0, 0.002, abs(uv.y - y));
}
scanline = 1.0 - scanline * 0.3;
// 边缘发光
float edge = 1.0 - smoothstep(0.0, 0.05, min(
min(uv.x, 1.0 - uv.x),
min(uv.y, 1.0 - uv.y)
));
// 动画脉冲
float pulse = sin(uTime * 2.0) * 0.1 + 0.9;
vec3 color = uColor * scanline * pulse;
color += vec3(0.0, 0.5, 1.0) * edge * 0.5;
fragColor = vec4(color, uOpacity);
}
// shaders/energy-field.frag
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
in vec2 vUv;
in vec3 vNormal;
in vec3 vViewPosition;
out vec4 fragColor;
float snoise(vec3 v) {
return fract(sin(dot(v, vec3(12.9898, 78.233, 45.543))) * 43758.5453);
}
void main() {
vec3 viewDir = normalize(-vViewPosition);
float fresnel = pow(1.0 - abs(dot(viewDir, vNormal)), 3.0);
float noise = snoise(vec3(vUv * 5.0, uTime * 0.5));
vec3 color = uColor * fresnel;
color += uColor * noise * 0.2;
float alpha = fresnel * 0.8 + noise * 0.1;
fragColor = vec4(color, alpha);
}
// shaders/data-bar.frag
#version 300 es
precision highp float;
uniform float uValue;
uniform float uThreshold;
uniform vec3 uColorLow;
uniform vec3 uColorHigh;
uniform vec3 uColorWarning;
in vec2 vUv;
out vec4 fragColor;
void main() {
float fill = step(vUv.x, uValue);
vec3 color = mix(uColorLow, uColorHigh, uValue);
color = mix(color, uColorWarning, step(uThreshold, uValue));
float gradient = vUv.y * 0.3 + 0.7;
fragColor = vec4(color * gradient * fill, fill);
}
| 风险 | 缓解措施 |
|---|---|
| 无限循环 | 始终使用常量循环边界 |
| GPU 挂起 | 首先使用小数据集测试着色器 |
| 内存耗尽 | 限制纹理大小 |
// ❌ 错误 - 动态循环边界
for (int i = 0; i < int(uCount); i++) { }
// ✅ 正确 - 常量循环边界
const int MAX_ITERATIONS = 100;
for (int i = 0; i < MAX_ITERATIONS; i++) {
if (i >= int(uCount)) break;
}
// ❌ 危险 - 可能导致 GPU 挂起
for (int i = 0; i < uniformValue; i++) { }
// ✅ 安全 - 常量边界配合提前退出
const int MAX = 100;
for (int i = 0; i < MAX; i++) {
if (i >= uniformValue) break;
}
// ❌ 危险 - 除以零
float result = value / divisor;
// ✅ 安全 - 防止除零
float result = value / max(divisor, 0.0001);
npm run test:shadersnpm run test:visualnpm run bench:shadersGLSL 着色器为 JARVIS HUD 中的视觉效果提供动力:
请记住:着色器在 GPU 上运行 - 单个错误的着色器可能导致整个系统冻结。
参考资料:
references/advanced-patterns.md - 复杂着色器技术每周安装次数
137
代码仓库
GitHub 星标数
29
首次出现
2026年1月20日
安全审计
安装于
gemini-cli120
codex119
opencode117
github-copilot108
cursor104
claude-code102
File Organization : This skill uses split structure. See
references/for advanced shader patterns.
This skill provides GLSL shader expertise for creating holographic visual effects in the JARVIS AI Assistant HUD. It focuses on efficient GPU programming for real-time rendering.
Risk Level : LOW - GPU-side code with limited attack surface, but can cause performance issues
Primary Use Cases :
// tests/shaders/holographic-panel.test.ts
import { describe, it, expect, beforeEach } from 'vitest'
import { WebGLTestContext, captureFramebuffer, compareImages } from '../utils/webgl-test'
describe('HolographicPanelShader', () => {
let ctx: WebGLTestContext
beforeEach(() => {
ctx = new WebGLTestContext(256, 256)
})
// Unit test: Shader compiles
it('should compile without errors', () => {
const shader = ctx.compileShader(holoFragSource, ctx.gl.FRAGMENT_SHADER)
expect(shader).not.toBeNull()
expect(ctx.getShaderErrors()).toEqual([])
})
// Unit test: Uniforms are accessible
it('should have required uniforms', () => {
const program = ctx.createProgram(vertSource, holoFragSource)
expect(ctx.getUniformLocation(program, 'uTime')).not.toBeNull()
expect(ctx.getUniformLocation(program, 'uColor')).not.toBeNull()
expect(ctx.getUniformLocation(program, 'uOpacity')).not.toBeNull()
})
// Visual regression test
it('should render scanlines correctly', async () => {
ctx.renderShader(holoFragSource, { uTime: 0, uColor: [0, 0.5, 1], uOpacity: 1 })
const result = captureFramebuffer(ctx)
const baseline = await loadBaseline('holographic-scanlines.png')
expect(compareImages(result, baseline, { threshold: 0.01 })).toBeLessThan(0.01)
})
// Edge case test
it('should handle extreme UV values', () => {
const testCases = [
{ uv: [0, 0], expected: 'no crash' },
{ uv: [1, 1], expected: 'no crash' },
{ uv: [0.5, 0.5], expected: 'no crash' }
]
testCases.forEach(({ uv }) => {
expect(() => ctx.renderAtUV(holoFragSource, uv)).not.toThrow()
})
})
})
// Start with minimal shader that passes tests
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
uniform float uOpacity;
in vec2 vUv;
out vec4 fragColor;
void main() {
// Minimal implementation to pass compilation test
fragColor = vec4(uColor, uOpacity);
}
// Expand to full implementation after tests pass
void main() {
vec2 uv = vUv;
float scanline = sin(uv.y * 100.0) * 0.1 + 0.9;
float pulse = sin(uTime * 2.0) * 0.1 + 0.9;
vec3 color = uColor * scanline * pulse;
fragColor = vec4(color, uOpacity);
}
# Run all shader tests
npm run test:shaders
# Visual regression tests
npm run test:visual -- --update-snapshots # First time only
npm run test:visual
# Performance benchmark
npm run bench:shaders
# Cross-browser compilation check
npm run test:webgl-compat
| Version | Context | Features |
|---|---|---|
| GLSL ES 3.00 | WebGL 2.0 | Modern features, better precision |
| GLSL ES 1.00 | WebGL 1.0 | Legacy support |
#version 300 es
precision highp float;
precision highp int;
// WebGL 2.0 shader header
// ❌ BAD - GPU branch divergence
vec3 getColor(float value) {
if (value < 0.3) {
return vec3(1.0, 0.0, 0.0); // Red
} else if (value < 0.7) {
return vec3(1.0, 1.0, 0.0); // Yellow
} else {
return vec3(0.0, 1.0, 0.0); // Green
}
}
// ✅ GOOD - Branchless with mix/step
vec3 getColor(float value) {
vec3 red = vec3(1.0, 0.0, 0.0);
vec3 yellow = vec3(1.0, 1.0, 0.0);
vec3 green = vec3(0.0, 1.0, 0.0);
vec3 color = mix(red, yellow, smoothstep(0.3, 0.31, value));
color = mix(color, green, smoothstep(0.7, 0.71, value));
return color;
}
// ❌ BAD - Multiple texture bindings
uniform sampler2D uIcon1;
uniform sampler2D uIcon2;
uniform sampler2D uIcon3;
vec4 getIcon(int id) {
if (id == 0) return texture(uIcon1, vUv);
if (id == 1) return texture(uIcon2, vUv);
return texture(uIcon3, vUv);
}
// ✅ GOOD - Single atlas texture
uniform sampler2D uIconAtlas;
uniform vec4 uAtlasOffsets[3]; // [x, y, width, height] for each icon
vec4 getIcon(int id) {
vec4 offset = uAtlasOffsets[id];
vec2 atlasUV = offset.xy + vUv * offset.zw;
return texture(uIconAtlas, atlasUV);
}
// ❌ BAD - Same quality regardless of distance
const int NOISE_OCTAVES = 8;
float noise(vec3 p) {
float result = 0.0;
for (int i = 0; i < NOISE_OCTAVES; i++) {
result += snoise(p * pow(2.0, float(i)));
}
return result;
}
// ✅ GOOD - Reduce octaves based on distance
uniform float uCameraDistance;
float noise(vec3 p) {
// Fewer octaves when far away (detail not visible)
int octaves = int(mix(2.0, 8.0, 1.0 - smoothstep(10.0, 100.0, uCameraDistance)));
float result = 0.0;
for (int i = 0; i < 8; i++) {
if (i >= octaves) break;
result += snoise(p * pow(2.0, float(i)));
}
return result;
}
// ❌ BAD - Many individual uniforms
uniform float uPosX;
uniform float uPosY;
uniform float uPosZ;
uniform float uRotX;
uniform float uRotY;
uniform float uRotZ;
uniform float uScaleX;
uniform float uScaleY;
uniform float uScaleZ;
// ✅ GOOD - Packed into vectors/matrices
uniform vec3 uPosition;
uniform vec3 uRotation;
uniform vec3 uScale;
// Or even better:
uniform mat4 uTransform;
// ❌ BAD - Everything highp (wastes GPU cycles)
precision highp float;
highp vec3 color;
highp float alpha;
highp vec2 uv;
// ✅ GOOD - Match precision to data needs
precision highp float; // Default for calculations
mediump vec3 color; // 0-1 range, mediump sufficient
mediump float alpha; // 0-1 range
highp vec2 uv; // Need precision for texture coords
lowp int flags; // Boolean-like values
// ❌ BAD - Redundant texture fetches
void main() {
vec3 diffuse = texture(uTexture, vUv).rgb;
// ... some code ...
float alpha = texture(uTexture, vUv).a; // Same lookup!
// ... more code ...
vec3 doubled = texture(uTexture, vUv).rgb * 2.0; // Again!
}
// ✅ GOOD - Cache the result
void main() {
vec4 texSample = texture(uTexture, vUv);
vec3 diffuse = texSample.rgb;
float alpha = texSample.a;
vec3 doubled = texSample.rgb * 2.0;
}
// shaders/holographic-panel.frag
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
uniform float uOpacity;
uniform vec2 uResolution;
in vec2 vUv;
out vec4 fragColor;
const int SCANLINE_COUNT = 50;
void main() {
vec2 uv = vUv;
// Scanline effect
float scanline = 0.0;
for (int i = 0; i < SCANLINE_COUNT; i++) {
float y = float(i) / float(SCANLINE_COUNT);
scanline += smoothstep(0.0, 0.002, abs(uv.y - y));
}
scanline = 1.0 - scanline * 0.3;
// Edge glow
float edge = 1.0 - smoothstep(0.0, 0.05, min(
min(uv.x, 1.0 - uv.x),
min(uv.y, 1.0 - uv.y)
));
// Animated pulse
float pulse = sin(uTime * 2.0) * 0.1 + 0.9;
vec3 color = uColor * scanline * pulse;
color += vec3(0.0, 0.5, 1.0) * edge * 0.5;
fragColor = vec4(color, uOpacity);
}
// shaders/energy-field.frag
#version 300 es
precision highp float;
uniform float uTime;
uniform vec3 uColor;
in vec2 vUv;
in vec3 vNormal;
in vec3 vViewPosition;
out vec4 fragColor;
float snoise(vec3 v) {
return fract(sin(dot(v, vec3(12.9898, 78.233, 45.543))) * 43758.5453);
}
void main() {
vec3 viewDir = normalize(-vViewPosition);
float fresnel = pow(1.0 - abs(dot(viewDir, vNormal)), 3.0);
float noise = snoise(vec3(vUv * 5.0, uTime * 0.5));
vec3 color = uColor * fresnel;
color += uColor * noise * 0.2;
float alpha = fresnel * 0.8 + noise * 0.1;
fragColor = vec4(color, alpha);
}
// shaders/data-bar.frag
#version 300 es
precision highp float;
uniform float uValue;
uniform float uThreshold;
uniform vec3 uColorLow;
uniform vec3 uColorHigh;
uniform vec3 uColorWarning;
in vec2 vUv;
out vec4 fragColor;
void main() {
float fill = step(vUv.x, uValue);
vec3 color = mix(uColorLow, uColorHigh, uValue);
color = mix(color, uColorWarning, step(uThreshold, uValue));
float gradient = vUv.y * 0.3 + 0.7;
fragColor = vec4(color * gradient * fill, fill);
}
| Risk | Mitigation |
|---|---|
| Infinite loops | Always use constant loop bounds |
| GPU hangs | Test shaders with small datasets first |
| Memory exhaustion | Limit texture sizes |
// ❌ BAD - Dynamic loop bound
for (int i = 0; i < int(uCount); i++) { }
// ✅ GOOD - Constant loop bound
const int MAX_ITERATIONS = 100;
for (int i = 0; i < MAX_ITERATIONS; i++) {
if (i >= int(uCount)) break;
}
// ❌ DANGEROUS - May cause GPU hang
for (int i = 0; i < uniformValue; i++) { }
// ✅ SAFE - Constant bound with early exit
const int MAX = 100;
for (int i = 0; i < MAX; i++) {
if (i >= uniformValue) break;
}
// ❌ DANGEROUS - Division by zero
float result = value / divisor;
// ✅ SAFE - Guard against zero
float result = value / max(divisor, 0.0001);
npm run test:shadersnpm run test:visualnpm run bench:shadersGLSL shaders power the visual effects in JARVIS HUD:
Remember : Shaders run on GPU - a single bad shader can freeze the entire system.
References :
references/advanced-patterns.md - Complex shader techniquesWeekly Installs
137
Repository
GitHub Stars
29
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli120
codex119
opencode117
github-copilot108
cursor104
claude-code102
测试策略完整指南:单元/集成/E2E测试金字塔与自动化实践
11,200 周安装