Shaders: vertex and fragment programs
着色器:顶点与片断程序
本文档主要是对Unity官方手册的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于Unity2018.2版本
参考链接:https://docs.unity3d.com/Manual/ShaderTut2.html
This tutorial will teach you the basics of how to write vertex and fragment programs in Unity shaders. For a basic introduction to ShaderLab see the Getting Started tutorial. If you want to write shaders that interact with lighting, read about Surface Shaders instead.
本教程将向您介绍如何在Unity着色器中编写顶点和片段程序的基础知识。对于ShaderLab的基本介绍,请参阅入门教程。如果你想要写与灯光交互的着色器,那就去阅读表面着色器吧。
Lets start with a small recap of the general structure of a shader:
Shader "MyShaderName"
{
Properties
{
// material properties here
}
SubShader // subshader for graphics hardware A
{
Pass
{
// pass commands ...
}
// more passes if needed
}
// more subshaders if needed
FallBack "VertexLit" // optional fallback
}
Here at the end we introduce a new command: FallBack “VertexLit”. The Fallback command can be used at the end of the shader; it tells which shader should be used if no SubShaders from the current shader can run on user’s graphics hardware. The effect is the same as including all SubShaders from the fallback shader at the end. For example, if you were to write a fancy normal-mapped shader, then instead of writing a very basic non-normal-mapped subshader for old graphics cards you can just fallback to built-in VertexLit shader.
在这里,我们引入了一个新的命令:回退(FallBack) “VertexLit”。回退命令可以在着色器的末端使用;它告诉我们,如果当前着色器中没有子材质可以在用户的图形硬件上运行,那么应该使用哪个着色器。其效果在所有子着色器是一样的被包含在最后的回退着色器中。例如,如果您要编写一个漂亮的法线映射着色器,而不是为旧的图形卡编写一个非常基本的无法线映射的子着色器,您可以直接回退到内置的VertexLit着色器。
The basic building blocks of the shader are introduced in the first shader tutorial while the full documentation of Properties, SubShaders and Passes are also available.
着色器的基本构建块是在第一个着色器教程中引入的,而属性、SubShaders和Passes的完整文档也是可用的。
A quick way of building SubShaders is to use passes defined in other shaders. The command UsePass does just that, so you can reuse shader code in a neat fashion. As an example the following command uses the pass with the name “FORWARD” from the built-in Specular shader: UsePass “Specular/FORWARD”.
构建子着色器的一种快速方法是使用在其他着色器中定义的passes。UsePass的命令就是这样做的,所以您可以以一种整洁的方式重用着色器代码。作为一个例子,下面的命令使用来自内置的高光着色器的“FORWARD”这个名称