UnityShader权威指南—中文翻译
目录:
1. Shaders 着色器
1.1 Built-in Shader Guide 内置着色器指南
1.1.1 Materials and Shaders 材质与着色器
1.1.2 Performance of Unity shaders Unity 着色器的性能 1.1.3 Normal Shader Family 标准着色器家族
1.1.3.1 Vertex Lit 顶点光照着色器 1.1.3.2 Diffuse 漫反射着色器 1.1.3.3 Specular 高光着色器 1.1.3.4 Bumped Diffuse 法线贴图漫反射着色器 1.1.3.5 Bumped Specular 法线贴图高光着色器 1.1.3.6 Parallax Diffuse 视差漫反射色器
1.1.3.7 Parallax Bumped Specular 视差法线高光着色器 1.1.3.8 Decal 贴花着色器
1.1.3.9 Diffuse Detail 漫反射细节着色器
1.1.4 Transparent Shader Family 透明着色器家族 1.1.4.1 Transparent Vertex-Lit 透明顶点着色器 1.1.4.2 1.1.4.3 1.1.4.4 1.1.4.5 1.1.4.6 1.1.4.7
1.1.5
Transparent Diffuse 透明漫反射着色器 Transparent Specular 透明高光着色器
Transparent Bumped Diffuse 透明法线漫反射着色器 Transparent Bumped Specular 透明法线高光着色器 Transparent Parallax Diffuse 透明视差漫反射着色器 Transparent Parallax Specular 透明视差高光着色器
Transparent Cutout Shader Family
1.1.5.1 Transparent Cutout Vertex-Lit 1.1.5.2 Transparent Cutout Diffuse 1.1.5.3 Transparent Cutout Specular 1.1.5.4 Transparent Cutout Bumped Diffuse 1.1.5.5 Transparent Cutout Bumped Specular 1.1.6
Self-Illuminated Shader Family 1.1.6.1 Self-Illuminated Vertex-Lit 1.1.6.2 Self-Illuminated Diffuse 1.1.6.3 Self-Illuminated Specular
1.1.6.4 Self-Illuminated Normal mapped Diffuse 1.1.6.5 Self-Illuminated Normal mapped Specular 1.1.6.6 Self-Illuminated Parallax Diffuse 1.1.6.7 Self-Illuminated Parallax Specular 1.1.7
Reflective Shader Family 1.1.7.1 Reflective Vertex-Lit 1.1.7.2 Reflective Diffuse
1 / 276
1.1.7.3 Reflective Specular 1.1.7.4 Reflective Bumped Diffuse 1.1.7.5 Reflective Bumped Specular 1.1.7.6 Reflective Parallax Diffuse 1.1.7.7 Reflective Parallax Specular 1.1.7.8 Reflective Normal Mapped Unlit 1.1.7.9 Reflective Normal mapped Vertex-lit
2 Shader Reference 着色器参考 2.1 ShaderLab syntax: Shader 着色器语法:Shader 2.1.1 2.1.2 ShaderLab syntax: Properties 着色器语法:属性 ShaderLab syntax: SubShader 着色器语法:子着色器 ShaderLab syntax: Pass 着色器语法:传递 ShaderLab syntax: Color, Material, Lighting 着色器语法:颜色,材质,光照 2.1.2.1.2 2.1.2.1.3 2.1.2.1.4 2.1.2.1.5 2.1.2.1.6 2.1.2.1.7 2.1.2.1.8 2.1.2.1.9 2.1.2.2 2.1.2.3 2.1.2.4 2.1.3 2.1.4 2.2.1 2.2.2 2.2.3 2.3.1 2.3.2 2.3.3 2.3.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 ShaderLab syntax: Culling & Depth Testing 着色器语法:剪裁和深度检测 ShaderLab syntax: Texturing 着色器语法:纹理贴图 ShaderLab syntax: Fog 着色器语法:雾 ShaderLab syntax: Alpha testing 着色器语法:透明检测 ShaderLab syntax: Blending 着色器语法:混合 ShaderLab syntax: Pass Tags 着色器语法: 渲染通道标签 ShaderLab syntax: Name 着色器语法:名字 ShaderLab syntax: BindChannels 着色器语法:绑定频道 2.1.2.1.1 2.1.2.1 ShaderLab syntax: UsePass 着色器语法:调用渲染通道 ShaderLab syntax: GrabPass 着色器语法:抓取渲染通道 ShaderLab syntax: SubShader Tags 着色器语法:子着色器标签 ShaderLab syntax: Fallback 着色器语法:Fallback ShaderLab syntax: other commands 着色器语法:其他命令 Surface Shader Examples 表面着色器示例 Custom Lighting models in Surface Shaders 表面着色器中的自定义光照模式 Surface Shader Lighting Examples 表面着色器光照示例 Accessing shader properties in Cg 在Cg中访问着色器属性 Providing vertex data to vertex programs 传递顶点数据到顶点程序 Built-in state variables in shader programs 内置着色器编程状态参数 GLSL Shader Programs OpenGL着色语言 Unity's Rendering Pipeline Unity的渲染管线 Performance Tips when Writing Shaders 优化,写着色器的窍门 Rendering with Replaced Shaders 使用代替渲染器来渲染 Using Depth Textures 使用深度贴图 Camera's Depth Texture 摄像机深度贴图 2 / 276
2.2 Writing Surface Shaders 编写表面着色器 2.3 Writing vertex and fragment shaders 编写顶点和片段着色器 2.4 Advanced ShaderLab topics
2.4.6 2.4.7 3 Tutorial Platform Specific Rendering Differences 不同平台的渲染差异 Shader Level of Detail 着色器的细节等级 2.5 ShaderLab builtin values ShaderLab内置数值 3.1 Tutorial: ShaderLab & Fixed Function Shaders 教程:ShaderLab和固定功能着色器 3.2 Tutorial: Vertex and Fragment Shaders 教程:顶点和片段着色器 3.3 Tutorial: Surface Shaders 教程:表面着色器 4
Unity 3.x Shader Conversion Guide Unity 3.x着色器转换指南
3 / 276
Shaders
All rendering in Unity is done with Shaders - small scripts that let you configure the how the graphics hardware is set up for rendering. Unity ships with 60+ built-in shaders (documented in the Built-in Shader Guide). You can extend this by making your own shaders.
Shaders in Unity can be written in one of three different ways:
? Surface Shaders will probably be your best bet. Write your shader as a surface shader if it needs to interact properly with lighting, shadows, projectors, etc. Surface shaders also make it easy to write complex shaders in a compact way - it's a higher level of abstraction. Lighting for most surface shaders can be calculated in a deferred manner (the exception is some very custom lighting models), which allows your shader to efficiently interact with many realtime lights. You write surface shaders in a couple of lines of Cg/HLSL and a lot more code gets auto-generated from that.
? Vertex and Fragment Shaders will be required, if you need some very exotic effects that the surface shaders can't handle, if your shader doesn't need to interact with lighting or if it's an image effect. Shader programs written this way are the most flexible way to create the effect you need (even surface shaders are automatically converted to a bunch of vertex and fragment shaders), but that comes at a price: you have to write more code and it's harder to make it interact with lighting. These shaders are written in Cg/HLSL as well.
? Fixed Function Shaders need to be written for old hardware that doesn't support programmable shaders. You will probably want to write fixed function shaders as an n-th fallback to your fancy fragment or surface shaders, to make sure your game still renders something sensible when run on old hardware or simpler mobile platforms. Fixed function shaders are entirely written in a language called ShaderLab, which is similar to Microsoft's .FX files or NVIDIA's CgFX.
Regardless of which type you choose, the actual meat of the shader code will always be wrapped in ShaderLab, which is used to organize the shader structure. It looks like this:
Shader \ Properties {
_MyTexture (\
// other properties like colors or vectors go here as well }
SubShader {
// here goes the 'meat' of your // - surface shader or
// - vertex and fragment shader or // - fixed function shader }
SubShader {
// here goes a simpler version of the SubShader above that can run on older graphics cards } }
We recommend that you start by reading about some basic concepts of the ShaderLab syntax in the ShaderLab reference and then move on to the tutorials listed below.
4 / 276
The tutorials include plenty of examples for the different types of shaders. For even more examples of surface shaders in particular, you can get the source of Unity's built-in shaders from the Resources section. Unity's Image Effects package contains a lot of interesting vertex and fragment shaders. Read on for an introduction to shaders, and check out the shader reference! ? ? ? Tutorial: ShaderLab & Fixed Function Shaders Tutorial: Vertex and Fragment Shaders Tutorial: Surface Shaders 着色器
所有在Unity 的渲染都用着色器 - 小脚本完成,着色器让您配置该如何设置图形硬件进行渲染。Unity 有60+内置的着色器,但你可以扩展,制作更多的自己的着色器。内置的着色器都记录在内置的着色器指南。 写Unity 的着色器可以使用三种不同的方法之一:
? 表面着色器,可能会是你最好的选择。如果它需要与灯光,阴影,投影器,等进行交互,以表面着色器方式写着色器。表面着色器也可以很容易简洁地写在复杂的着色器 - 这是一个更高的抽象层次。大多数表面着色器的光照以延迟光照的方式进行计算(唯一的例外是一些自定义的光照模型),它允许着色器有效率地与许多实时灯光互动。表面着色器用CG / HLSL语言编写几行代码和更多的代码会自动生成。
? 顶点和片段着色器,如果你需要一些非常奇特的效果,表面着色器不能处理的,如果着色器并不需要与照明互动或如果这只是图像效果。这种编写着色器方案是最灵活的方式创建你需要的效果(甚至表面着色器是自动转换成一堆顶点和片段着色器),但这有代价的:你需要写更多的代码和它很难使其与照明互动。这些着色器同样是用CG / HLSL语言编写。
? 固定功能着色器,需要为不支持可编程着色器的旧硬件编写。您可能会想要编写固定功能着色器,以确保旧的硬件或简单的移动平台上运行时,你的游戏渲染的东西仍然合理的。固定功能着色器完全是用称为ShaderLab一种语言编写的。这是类似微软的。FX文件或NVIDIA的的CgFX。
无论您选择哪种类型,实际的shader代码总是会被包裹在ShaderLab,这是用来组织着色器结构。它看起来像这样:
Shader \ Properties {
_MyTexture (\
// other properties like colors or vectors go here as well }
SubShader {
// here goes the 'meat' of your // - surface shader or
// - vertex and fragment shader or // - fixed function shader }
SubShader {
// here goes a simpler version of the SubShader above that can run on older graphics cards } }
5 / 276