shader-techniques
26
总安装量
18
周安装量
#14076
全站排名
安装命令
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-game-developer --skill shader-techniques
Agent 安装分布
claude-code
17
gemini-cli
13
opencode
13
antigravity
12
codex
11
cursor
10
Skill 文档
Shader Techniques
Shader Pipeline Overview
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â RENDERING PIPELINE â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â CPU (Game Logic) â
â â â
â VERTEX SHADER â
â ââ Transform vertices to clip space â
â ââ Calculate normals, tangents â
â ââ Pass data to fragment shader â
â â â
â RASTERIZATION (Fixed function) â
â ââ Triangle setup â
â ââ Pixel coverage â
â ââ Interpolation â
â â â
â FRAGMENT/PIXEL SHADER â
â ââ Sample textures â
â ââ Calculate lighting â
â ââ Output final color â
â â â
â OUTPUT (Framebuffer) â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Basic Shader Structure
// â
Production-Ready: Basic Surface Shader (Unity HLSL)
Shader "Custom/BasicSurface"
{
Properties
{
_MainTex ("Albedo", 2D) = "white" {}
_Color ("Color Tint", Color) = (1,1,1,1)
_Metallic ("Metallic", Range(0,1)) = 0.0
_Smoothness ("Smoothness", Range(0,1)) = 0.5
_NormalMap ("Normal Map", 2D) = "bump" {}
_NormalStrength ("Normal Strength", Range(0,2)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
sampler2D _NormalMap;
half4 _Color;
half _Metallic;
half _Smoothness;
half _NormalStrength;
struct Input
{
float2 uv_MainTex;
float2 uv_NormalMap;
};
void surf(Input IN, inout SurfaceOutputStandard o)
{
// Albedo
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Normal mapping
half3 normal = UnpackNormal(tex2D(_NormalMap, IN.uv_NormalMap));
normal.xy *= _NormalStrength;
o.Normal = normalize(normal);
// PBR properties
o.Metallic = _Metallic;
o.Smoothness = _Smoothness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Advanced Effects
Toon/Cel Shading
// â
Production-Ready: Toon Shader
Shader "Custom/Toon"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_RampTex ("Ramp Texture", 2D) = "white" {}
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_OutlineWidth ("Outline Width", Range(0, 0.1)) = 0.02
}
SubShader
{
Tags { "RenderType"="Opaque" }
// Outline Pass
Pass
{
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
};
float _OutlineWidth;
float4 _OutlineColor;
v2f vert(appdata v)
{
v2f o;
// Expand vertices along normals
float3 scaled = v.vertex.xyz + v.normal * _OutlineWidth;
o.pos = UnityObjectToClipPos(float4(scaled, 1.0));
return o;
}
half4 frag(v2f i) : SV_Target
{
return _OutlineColor;
}
ENDCG
}
// Main Toon Pass
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
sampler2D _RampTex;
float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
return o;
}
half4 frag(v2f i) : SV_Target
{
// Sample albedo
half4 col = tex2D(_MainTex, i.uv) * _Color;
// Calculate diffuse with ramp
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
float NdotL = dot(i.worldNormal, lightDir) * 0.5 + 0.5;
float3 ramp = tex2D(_RampTex, float2(NdotL, 0.5)).rgb;
col.rgb *= ramp;
return col;
}
ENDCG
}
}
}
Dissolve Effect
// â
Production-Ready: Dissolve Shader
Shader "Custom/Dissolve"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_NoiseTex ("Noise Texture", 2D) = "white" {}
_DissolveAmount ("Dissolve Amount", Range(0, 1)) = 0
_EdgeColor ("Edge Color", Color) = (1, 0.5, 0, 1)
_EdgeWidth ("Edge Width", Range(0, 0.2)) = 0.05
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
sampler2D _NoiseTex;
float _DissolveAmount;
float4 _EdgeColor;
float _EdgeWidth;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
half4 frag(v2f i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
float noise = tex2D(_NoiseTex, i.uv).r;
// Discard dissolved pixels
clip(noise - _DissolveAmount);
// Add edge glow
float edge = 1 - smoothstep(0, _EdgeWidth, noise - _DissolveAmount);
col.rgb = lerp(col.rgb, _EdgeColor.rgb, edge);
return col;
}
ENDCG
}
}
}
Post-Processing Effects
// â
Production-Ready: Screen-Space Vignette
Shader "PostProcess/Vignette"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Intensity ("Intensity", Range(0, 1)) = 0.5
_Smoothness ("Smoothness", Range(0.01, 1)) = 0.5
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
sampler2D _MainTex;
float _Intensity;
float _Smoothness;
half4 frag(v2f_img i) : SV_Target
{
half4 col = tex2D(_MainTex, i.uv);
// Calculate vignette
float2 center = i.uv - 0.5;
float dist = length(center);
float vignette = smoothstep(0.5, 0.5 - _Smoothness, dist);
vignette = lerp(1, vignette, _Intensity);
col.rgb *= vignette;
return col;
}
ENDCG
}
}
}
Shader Optimization
OPTIMIZATION TECHNIQUES:
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PRECISION: â
â ⢠Use half instead of float where possible â
â ⢠Mobile: Always prefer half/fixed â
â ⢠PC: float for positions, half for colors â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â TEXTURE SAMPLING: â
â ⢠Minimize texture samples â
â ⢠Use texture atlases â
â ⢠Avoid dependent texture reads â
â ⢠Use lower mipmap for distant objects â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â MATH OPERATIONS: â
â ⢠Replace div with mul (x/2 â x*0.5) â
â ⢠Use MAD operations (a*b+c) â
â ⢠Avoid branching in fragment shaders â
â ⢠Pre-compute constants â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â VARIANTS: â
â ⢠Minimize shader keywords â
â ⢠Use multi_compile_local â
â ⢠Strip unused variants â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
COST COMPARISON (Relative):
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Operation â Cost â Notes â
âââââââââââââââââââââââ¼ââââââââ¼ââââââââââââââââââââââââââââââââ¤
â Add/Multiply â 1x â Baseline â
â Divide â 4x â Avoid in loops â
â Sqrt â 4x â Use rsqrt when possible â
â Sin/Cos â 8x â Use lookup tables on mobile â
â Texture Sample â 4-8x â Varies by hardware â
â Pow â 8x â Use exp2(x*log2(y)) â
â Normalize â 4x â Pre-normalize in vertex â
âââââââââââââââââââââââ´ââââââââ´ââââââââââââââââââââââââââââââââ
ð§ Troubleshooting
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Shader not compiling â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â SOLUTIONS: â
â â Check for syntax errors (missing semicolons) â
â â Verify all variables are declared â
â â Check target platform compatibility â
â â Look for mismatched semantics â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Pink/magenta material â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â SOLUTIONS: â
â â Shader failed to compile - check console â
â â Missing shader on target platform â
â â Add fallback shader â
â â Check render pipeline compatibility â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Shader too slow â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â SOLUTIONS: â
â â Profile with GPU debugger (RenderDoc) â
â â Reduce texture samples â
â â Use lower precision â
â â Simplify math operations â
â â Move calculations to vertex shader â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â PROBLEM: Too many shader variants â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¤
â SOLUTIONS: â
â â Use shader_feature instead of multi_compile â
â â Strip unused variants in build settings â
â â Combine keywords where possible â
â â Use material property blocks â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
Shader Languages by Engine
| Engine | Language | Notes |
|---|---|---|
| Unity URP/HDRP | HLSL + ShaderGraph | Visual + code |
| Unity Built-in | CG/HLSL | Legacy surface shaders |
| Unreal | HLSL + Material Editor | Node-based preferred |
| Godot | Godot Shading Language | Similar to GLSL |
| OpenGL | GLSL | Cross-platform |
| DirectX | HLSL | Windows/Xbox |
| Vulkan | SPIR-V (from GLSL) | Low-level |
Use this skill: When creating custom visuals, optimizing rendering, or implementing advanced effects.