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.
在Unity中材质与着色器之间有着密切的关系。着色器包含着定义了属性和资源使用种类的代码。材质允许你调整属性和分配资源。
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 or Hierarchy.
要创建新材质,从主菜单或项目视图的环境菜单中,选择Assets->Create->Material来创建。一旦材质已经创建,你可以将它应用到一个对象并在检视视图中调整其所有属性。要将它应用到一个对象,只需将它从项目视图中拖到场景视图或层次视图的某个对象上。
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 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.
你可以选择你想要用于任何特殊材质的着色器。只需在检视视图中展开Shader下拉菜单,并选择你的新着色器。你选择的着色器将支配可用的属性发生变化。这些属性可以是颜色、滑块、纹理、数字、或向量。在场景中如果你已经将材质应用到一个活动对象,你会实时看到属性的更改应用到了对象。
There are two ways to apply a Texture to a property.
有两种方法可以将一个纹理应用到一个属性。
- Drag it from the Project View on top of the Texture square
从项目视图中拖动它到方形纹理上面 - Click the Select button, and choose the texture from the drop-down list that appears
单击Select(选择)按钮,然后从出现的下拉列表中选择纹理
Two placement options are available for each Texture:
- Tiling 平铺 Scales the texture along the different. 沿着不同的(方向)缩放纹理
- Offset 偏移 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.
Unity的每个标准安装包都有一个内置着色器的库。有超过30个内置着色器和六个基本族。
- Normal: For opaque textured objects.
标准的:对于不透明纹理对象 - Transparent: For partly transparent objects. The texture's alpha channel defines the level of transparency.
透明的:对于部分透明的对象。纹理的alpha通道定义透明度 - 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:
此网格显示所有内置着色器的缩略图:
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:
Unity有一个广泛的着色系统,让你来调整游戏中的所有图形外观。它的工作原理是这样的:
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.
图表的左边是车体着色器。 2个不同的材质是这样创建的:蓝色的汽车材质和红色的汽车材质。这些材质的每一个都有2个给定的纹理;汽车纹理定义汽车的主要纹理,和颜色特效纹理。这些属性被着色器所使用,使完成的汽车看上去像喷涂了两种色调。可以看到在红色车前面:面对相机的地方是黄色,然后随角度的增加消退成紫色。车材质附着到了两辆车上。而车轮、车灯和车窗没有颜色变化效果,因此必须使用不同的材质。在图表的底部有一个简单金属着色器。车轮材质使用这个着色器。请注意,即使同样的汽车纹理在这里被重用,最终车身产生的结果是完全不同的,因为在材质中使用的着色器是不同的。
To be more specific, a Shader defines:
更多具体细节,一个着色器定义了如下内容:
- The method to render an object. This includes using different methods depending on the graphics card of the end user.
方法用以呈现(渲染)一个对象。这包括根据最终用户的图形卡使用不同的方法。 - Any vertex and fragment programs used to render. 任意顶点和用于渲染的片段程序
- Some texture properties that are assignable within Materials. 可指派到材质中的一些纹理属性
- Color and number settings that are assignable within Materials.
可指派到材质中的颜色和编号设置
A Material defines:
一个材质定义了:
- Which textures to use for rendering. 用于渲染的纹理
- Which colors to use for rendering. 用于渲染的颜色
- Any other assets, such as a Cubemap that is required by the shader for rendering.
一些其它资源,例如一个着色器渲染所必需的立方体贴图
Shaders are meant to be written by graphics programmers. They are created using the ShaderLab language, which is quite simple. However, getting a shader to work well on a variety graphics cards is an involved job and requires a fairly comprehensive knowledge of how graphics cards work.
着色器是图形程序员写的。他们创建使用ShaderLab语言,这很简单。但是要让一个着色器在各种图形卡上运行良好却是一个复杂的工作,需要对图形卡如何工作有一个相当全面的了解。
A number of shaders are built into Unity directly, and some more come in the Standard Assets Library. If you like, there is plenty more shader information in the Built-in Shader Guide.
许多着色器直接内置在Unity中,有些作为标准资源库。如果你喜欢,更多有关着色器的信息参见内置着色器指导。