unity-shaders-rendering
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity Shaders and Rendering
Unity 着色器与渲染
Overview
概述
Reference for Unity's rendering systems, shader development, lighting configuration, and visual effects. Covers all three render pipelines, Shader Graph, hand-written shaders, and VFX Graph.
Unity渲染系统、着色器开发、光照配置及视觉效果参考指南。涵盖三种渲染管线、Shader Graph、手写着色器及VFX Graph。
Render Pipeline Comparison
渲染管线对比
| Feature | Built-in RP | URP | HDRP |
|---|---|---|---|
| Target | Legacy projects | Mobile, VR, wide range | High-end PC/console |
| Shader language | Surface shaders + HLSL | HLSL (no surface shaders) | HLSL |
| Shader Graph | Yes | Yes | Yes |
| SRP Batcher | No | Yes | Yes |
| Render Features | No | Yes (ScriptableRendererFeature) | Custom Pass |
| Post-processing | Post Processing Stack v2 | Volume system (built-in) | Volume system (built-in) |
| Ray tracing | No | No (probe-based) | Yes (DXR) |
| Performance | Moderate | Optimized for scale | Highest fidelity |
Recommendation: Use URP for new projects unless targeting high-end visuals exclusively (HDRP). Built-in RP is legacy -- migrate when possible.
| 特性 | 内置管线(Built-in RP) | URP | HDRP |
|---|---|---|---|
| 目标场景 | 遗留项目 | 移动端、VR及广泛场景 | 高端PC/主机 |
| 着色器语言 | Surface着色器 + HLSL | HLSL(无Surface着色器) | HLSL |
| Shader Graph支持 | 是 | 是 | 是 |
| SRP批处理 | 否 | 是 | 是 |
| 渲染功能 | 否 | 是(ScriptableRendererFeature) | 自定义通道(Custom Pass) |
| 后处理 | Post Processing Stack v2 | 内置Volume系统 | 内置Volume系统 |
| 光线追踪 | 否 | 否(基于探针) | 是(DXR) |
| 性能表现 | 中等 | 针对规模化场景优化 | 最高画质 |
建议: 新项目优先使用URP,除非专门针对高端视觉效果选择HDRP。内置管线已属遗留技术,建议尽可能迁移。
Shader Graph
Shader Graph
Getting Started
入门指南
- Right-click in Project: Create > Shader Graph > URP > Lit Shader Graph
- Double-click to open Shader Graph editor
- Build node network connecting to Master Stack outputs
- Create a Material using the shader, assign to renderers
- 在项目窗口右键:创建 > Shader Graph > URP > 光照着色器图(Lit Shader Graph)
- 双击打开Shader Graph编辑器
- 构建节点网络并连接至主栈输出
- 使用该着色器创建材质,分配给渲染器
Master Stack Outputs (URP Lit)
URP光照主栈输出
| Output | Type | Purpose |
|---|---|---|
| Base Color | Color (RGB) | Albedo/diffuse color |
| Normal | Vector3 | Tangent-space normal map |
| Metallic | Float (0-1) | Metal vs. dielectric |
| Smoothness | Float (0-1) | Roughness inverse |
| Emission | Color (RGB) | Self-illumination |
| Alpha | Float (0-1) | Transparency |
| Alpha Clip Threshold | Float | Cutoff for alpha testing |
| 输出项 | 类型 | 用途 |
|---|---|---|
| 基础颜色(Base Color) | 颜色(RGB) | 反照率/漫反射颜色 |
| 法线(Normal) | 向量3(Vector3) | 切线空间法线贴图 |
| 金属度(Metallic) | 浮点数(0-1) | 金属材质 vs 绝缘材质 |
| 光滑度(Smoothness) | 浮点数(0-1) | 粗糙度的倒数 |
| 自发光(Emission) | 颜色(RGB) | 自发光效果 |
| 透明度(Alpha) | 浮点数(0-1) | 透明度 |
| Alpha裁剪阈值(Alpha Clip Threshold) | 浮点数 | Alpha测试的截断值 |
Common Node Patterns
常见节点模式
| Effect | Key Nodes |
|---|---|
| Dissolve | Noise > Step > Alpha Clip + Edge emission |
| Scrolling UV | Time > Multiply > Add to UV |
| Fresnel glow | Fresnel Effect > Multiply color > Emission |
| Triplanar mapping | Triplanar node (avoids UV stretching) |
| Color shift | Lerp between colors using parameter or time |
| Vertex displacement | Noise > Multiply > Add to Position |
| Outline | Two-pass: inverted hull in custom render feature |
| 效果 | 核心节点 |
|---|---|
| 溶解效果 | 噪声(Noise)> 阶跃(Step)> Alpha裁剪 + 边缘自发光 |
| UV滚动 | 时间(Time)> 乘法(Multiply)> 叠加至UV |
| 菲涅尔发光 | 菲涅尔效果(Fresnel Effect)> 颜色乘法 > 自发光 |
| 三平面映射 | 三平面节点(避免UV拉伸) |
| 颜色偏移 | 使用参数或时间在颜色间插值(Lerp) |
| 顶点位移 | 噪声(Noise)> 乘法(Multiply)> 叠加至位置(Position) |
| 轮廓线 | 双通道:在自定义渲染功能中使用反转外壳 |
Shader Graph Sub Graphs
Shader Graph子图
Extract reusable node groups into Sub Graphs (Create > Shader Graph > Sub Graph). Use for shared noise functions, UV transformations, or custom lighting models.
将可复用的节点组提取为子图(创建 > Shader Graph > 子图)。适用于共享噪声函数、UV变换或自定义光照模型。
Hand-Written Shaders (ShaderLab + HLSL)
手写着色器(ShaderLab + HLSL)
URP Shader Structure
URP着色器结构
hlsl
Shader "Custom/SimpleUnlit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _Color;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
return tex * _Color;
}
ENDHLSL
}
}
}Key differences from Built-in shaders:
- Use /
HLSLPROGRAM(notENDHLSL/CGPROGRAM)ENDCG - Include URP shader library, not UnityCG.cginc
- Use /
TEXTURE2Dmacros (notSAMPLER)sampler2D - Wrap properties in for SRP Batcher compatibility
CBUFFER_START(UnityPerMaterial)
hlsl
Shader "Custom/SimpleUnlit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _Color;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
return tex * _Color;
}
ENDHLSL
}
}
}与内置着色器的主要差异:
- 使用/
HLSLPROGRAM(而非ENDHLSL/CGPROGRAM)ENDCG - 引入URP着色器库,而非UnityCG.cginc
- 使用/
TEXTURE2D宏(而非SAMPLER)sampler2D - 将属性包裹在中,以兼容SRP批处理
CBUFFER_START(UnityPerMaterial)
Lighting
光照
Light Types
光照类型
| Type | Use For | Shadow Cost |
|---|---|---|
| Directional | Sun, global illumination | Low (cascaded shadow maps) |
| Point | Torches, lamps | Medium |
| Spot | Flashlights, stage lights | Medium |
| Area (baked only) | Soft window light, panels | High (bake only) |
| 类型 | 适用场景 | 阴影开销 |
|---|---|---|
| 方向光 | 太阳、全局光照 | 低(级联阴影贴图) |
| 点光源 | 火把、灯具 | 中 |
| 聚光灯 | 手电筒、舞台灯光 | 中 |
| 区域光(仅烘焙) | 柔和窗户光、面板光 | 高(仅烘焙) |
Lighting Modes
光照模式
| Mode | Description | Best For |
|---|---|---|
| Realtime | Computed every frame | Dynamic objects, few lights |
| Baked | Pre-computed into lightmaps | Static environments |
| Mixed | Baked indirect + realtime direct | Best balance |
| 模式 | 描述 | 最佳适用场景 |
|---|---|---|
| 实时 | 每帧计算 | 动态物体、少量光源 |
| 烘焙 | 预计算至光照贴图 | 静态场景 |
| 混合 | 烘焙间接光照 + 实时直接光照 | 最佳平衡方案 |
Lightmap Baking Tips
光照贴图烘焙技巧
- Set lightmap resolution based on scene scale (10-40 texels/unit for indoor)
- Use Light Probes for dynamic objects in baked scenes
- Use Reflection Probes for metallic/reflective surfaces
- Enable GPU Lightmapper for faster bake times
- Mark objects as Contribute GI in the Static flags
- 根据场景比例设置光照贴图分辨率(室内场景建议10-40 texels/单位)
- 在烘焙场景中为动态物体使用光照探针(Light Probes)
- 为金属/反光表面使用反射探针(Reflection Probes)
- 启用GPU光照贴图器以加快烘焙速度
- 在静态标记中设置物体为"贡献GI(Contribute GI)"
Post-Processing (Volume System)
后处理(Volume系统)
text
Setup:
1. Add a Volume (Global or Local) to the scene
2. Create a Volume Profile asset
3. Add overrides: Bloom, Color Adjustments, Tonemapping, etc.
4. Camera must have Post Processing enabled (URP Camera settings)| Effect | Performance | Notes |
|---|---|---|
| Bloom | Low | Use threshold to control intensity |
| Color Adjustments | Very Low | Saturation, contrast, color filter |
| Tonemapping | Very Low | ACES for cinematic look |
| Vignette | Very Low | Frame darkening |
| Ambient Occlusion (SSAO) | Medium | Disable on mobile |
| Depth of Field | High | Use Bokeh only for cinematics |
| Motion Blur | Medium | Can cause motion sickness in VR |
text
设置步骤:
1. 向场景添加Volume(全局或局部)
2. 创建Volume Profile资源
3. 添加覆盖效果:Bloom、颜色调整、色调映射等
4. 相机需启用后处理(URP相机设置)| 效果 | 性能开销 | 说明 |
|---|---|---|
| Bloom | 低 | 使用阈值控制强度 |
| 颜色调整 | 极低 | 饱和度、对比度、颜色滤镜 |
| 色调映射 | 极低 | 使用ACES实现电影级效果 |
| 暗角 | 极低 | 画面边缘暗化 |
| 环境光遮蔽(SSAO) | 中 | 移动端建议禁用 |
| 景深 | 高 | 仅在影视化场景中使用散景效果 |
| 运动模糊 | 中 | VR场景中可能引发晕动症 |
VFX Graph vs Particle System
VFX Graph vs 粒子系统
| Feature | Particle System (Shuriken) | VFX Graph |
|---|---|---|
| Execution | CPU | GPU (compute shader) |
| Particle count | Thousands | Millions |
| Complexity | Component-based, simple | Node-based, complex |
| Platform | All | Compute shader capable only |
| Integration | Physics, collision | Limited physics |
Use Particle System for gameplay-integrated effects (physics collisions, small counts). Use VFX Graph for visual spectacles (rain, fire, magic, ambient particles).
| 特性 | 粒子系统(Shuriken) | VFX Graph |
|---|---|---|
| 执行方式 | CPU | GPU(计算着色器) |
| 粒子数量 | 数千级 | 数百万级 |
| 复杂度 | 组件化、简单 | 节点化、复杂 |
| 支持平台 | 所有平台 | 仅支持具备计算着色器能力的平台 |
| 集成度 | 物理、碰撞 | 物理集成有限 |
粒子系统适用于与游戏玩法集成的效果(物理碰撞、少量粒子)。VFX Graph适用于视觉奇观(雨、火、魔法、环境粒子)。
URP Render Features
URP渲染功能
Extend URP rendering with custom ScriptableRendererFeatures:
csharp
public class OutlineFeature : ScriptableRendererFeature
{
OutlinePass _pass;
public override void Create()
{
_pass = new OutlinePass();
_pass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data)
{
renderer.EnqueuePass(_pass);
}
}Common uses: custom outlines, screen-space effects, render texture generation, stencil-based effects.
通过自定义ScriptableRendererFeature扩展URP渲染:
csharp
public class OutlineFeature : ScriptableRendererFeature
{
OutlinePass _pass;
public override void Create()
{
_pass = new OutlinePass();
_pass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data)
{
renderer.EnqueuePass(_pass);
}
}常见用途:自定义轮廓线、屏幕空间效果、渲染纹理生成、基于模板的效果。
Additional Resources
额外资源
Reference Files
参考文件
- -- Complete shader implementations: toon/cel shading, water, dissolve, hologram, force field, procedural skybox, stencil portal, vertex animation, custom lighting models
references/shader-recipes.md - -- Advanced lighting setups, GI troubleshooting, VFX Graph cookbook (fire, smoke, electricity, portals), Scriptable Render Pipeline customization, custom render passes
references/lighting-vfx-detail.md
- -- 完整着色器实现:卡通着色、水效果、溶解、全息图、力场、程序化天空盒、模板传送门、顶点动画、自定义光照模型
references/shader-recipes.md - -- 高级光照设置、GI故障排查、VFX Graph食谱(火焰、烟雾、电流、传送门)、可脚本化渲染管线定制、自定义渲染通道
references/lighting-vfx-detail.md