我们建议您阅读在 ShaderLab参考(ShaderLab reference)中有关ShaderLab语法的一些基本概念开始,然后到下面列出的教程。
该教程包括大量不同类型的着色器的例子。更多的例子,尤其是表面着色器的例子,你可以在资源部分(Resources section)得到Unity内置着色器的源代码。Unity的图像效果(Image Effects)资源包中包含了很多有趣的顶点和片段着色器。 阅读着色器介绍,以及着色器参考:
? ? ?
教程:ShaderLab和固定功能着色器 教程:顶点和片段着色器 教程:表面着色器
Built-in Shader Guide
If you're looking for the best information for using Unity's built-in shaders, you've come to the right place. Unity includes more than 40 built-in shaders, and of course you can write many more on your own! This guide will explain each family of the built-in shaders, and go into detail for each specific shader. With this guide, you'll be able to make the most out of Unity's shaders, to achieve the effect you're aiming for.
Using Shaders
Shaders in Unity are used through Materials, which essentially combine shader code with parameters like textures. An in-depth explanation of the Shader/Material relationship can be read here.
Material properties will appear in the Inspector when either the Material itself or a GameObject that uses the Material is selected. The Material Inspector looks like this:
6 / 276
Each Material will look a little different in the Inspector, depending on the specific shader it is using. The shader iself determines what kind of properties will be available to adjust in the Inspector. Material inspector is described in detail in Material reference page. Remember that a shader is implemented through a Material. So while the shader defines the properties that will be shown in the Inspector, each Material actually contains the adjusted data from sliders, colors, and textures. The most important thing to remember about this is that a single shader can be used in multiple Materials, but a single Material cannot use multiple shaders.
内置着色器指南
假如要了解关于Unity内置shader的更多重要信息,你已经进入正确的地方。Unity包括超过40种内置的shader,当然也可以自己编写更多属于自己的shader!本指导将依次解释每个内置的shader,并讲述每个指定shader的详细信息。跟着此向导可以创建置更多Unity之外的shader,完成预期的效果。
使用着色器
在Unity中shader是通过Materials(材质)来应用的,实际上每个shader关联的代码和参数类似Textures(纹理)一样,关于一个更全面的Shader/Material之间的解释可以到这里阅读。 当Material(材质)自身或者应用了此材质的GameObject(游戏对象)被选择时材质属性会显示在Inspector(检视面板)中,这个材质检视面板就像下面这样:
在检视面板中每个材质都看似有些不同,这依赖于其使用的特定Shader,这个Shader自身将确定在检视面板中匹配哪些属性是可用的,关于材质检视面板详细信息可以到材质参考页面查看。牢记shader是
7 / 276
通过Material(材质)才能执行。于此同时这个Shader定义的属性将显示在检视面板,每个材质实际包含通过滑块,颜色及材质调整过的数据信息。要牢记非常重要的事情是一个单一的Shader能够被应用在多维材质中,但是一个单一的材质不能应用在多维Shader中
Materials and Shaders
There is a close relationship between Materials and Shaders in Unity. Shaders contain code that defines what kind of properties and assets to use. Materials allow you to adjust properties and assign assets.
A Shader is implemented through a Material
To create a new Material, use Assets->Create->Material from the main menu or the Project
View context menu. Once the Material has been created, you can apply it to an object and tweak all of its properties in the Inspector. To apply it to an object, just drag it from the Project View to any object in the Scene orHierarchy.
Setting Material Properties
You can select which Shader you want any particular Material to use. Simply expand
the Shader drop-down in the Inspector, and choose your new Shader. The Shader you choose will dictate the available properties to change. The properties can be colors, sliders, textures, numbers, or
8 / 276
vectors. If you have applied the Material to an active object in the Scene, you will see your property changes applied to the object in real-time.
There are two ways to apply a Texture to a property.
1. Drag it from the Project View on top of the Texture square
2. Click the Select button, and choose the texture from the drop-down list that appears Two placement options are available for each Texture:
Tiling Offset
Scales the texture along the different. Slides the texture around.
Built-in Shaders There is a library of built-in Shaders that come standard with every installation of Unity. There are over 30 of these built-in Shaders, and six basic families. ? ? ? ? ? Normal: For opaque textured objects. Transparent: For partly transparent objects. The texture's alpha channel defines the level of transparency. TransparentCutOut: For objects that have only fully opaque and fully transparent areas, like fences. Self-Illuminated: For objects that have light emitting parts. Reflective: For opaque textured objects that reflect an environment Cubemap. In each group, built-in shaders range by complexity, from the simple VertexLit to the complex Parallax Bumped with Specular. For more information about performance of Shaders, please read the built-in Shader performance page
This grid displays a thumbnail of all built-in Shaders:
9 / 276
The builtin Unity shaders matrix
Shader technical details
Unity has an extensive Shader system, allowing you to tweak the look of all in-game graphics. It works like this:
A Shader basically defines a formula for how the in-game shading should look. Within any given Shader is a number of properties (typically textures). Shaders are implemented through Materials, which are attached directly to individual GameObjects. Within a Material, you will choose a Shader, then define the properties (usually textures and colors, but properties can vary) that are used by the Shader.
This is rather complex, so let's look at a workflow diagram:
On the left side of the graph is the Carbody Shader. 2 different Materials are created from this: Blue car Material and Red car Material. Each of these Materials have 2 textures assigned; the Car
Texture defines the main texture of the car, and a Color FX texture. These properties are used by the shader to make the car finish look like 2-tone paint. This can be seen on the front of the red car: it is yellow where it faces the camera and then fades towards purple as the angle increases. The car materials are attached to the 2 cars. The car wheels, lights and windows don't have the color change effect, and must hence use a different Material. At the bottom of the graph there is a Simple Metal Shader. The Wheel Material is using this Shader. Note that even though the same Car Texture is reused here, the end result is quite different from the car body, as the Shader used in the Material is different.
To be more specific, a Shader defines:
10 / 276