? ? ? ?
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.
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中材质与着色器之间有着密切的关系。着色器包含着定义了属性和资源使用种类的代码。材质允许你调整属性和分配资源。
色器通过材
质来实现
11 / 276
要创建新材质,从主菜单或项目视图的环境菜单中,选择Assets->Create->Material来创建。一旦材质已经创建,你可以将它应用到一个对象并在检视视图中调整其所有属性。要将它应用到一个对象,只需将它从项目视图中拖到场景视图或层次视图的某个对象上。
设置材质属性
你可以选择你想要用于任何特殊材质的着色器。只需在检视视图中展开Shader下拉菜单,并选择你的新着色器。你选择的着色器将支配可用的属性发生变化。这些属性可以是颜色、滑块、纹理、数字、或向量。在场景中如果你已经将材质应用到一个活动对象,你会实时看到属性的更改应用到了对象。 有两种方法可以将一个纹理应用到一个属性。
1. 从项目视图中拖动它到方形纹理上面
2. 单击Select(选择)按钮,然后从出现的下拉列表中选择纹理 每种贴图有两种贴图方式:
? 平铺 沿着不同的(方向)缩放纹理 ? 偏移 纹理偏移 内置着色器
Unity的每个标准安装包都有一个内置着色器的库。有超过30个内置着色器和六个基本族。
? ? ? ? ?
标准的:对于不透明纹理对象
透明的:对于部分透明的对象。纹理的alpha通道定义透明度 透明切块:对于拥有完全不透明和完全透明的区域的对象,比如栅栏 自身发光的:对于有发光部件的对象
反射的:对于能反射外界立方体贴图的不透明纹理对象(比如镜子,本身不透明,但其反映的是外界环境的贴图——个人理解)
在每一组中,内置的着色器按复杂性排列,从简单的顶点光亮到复杂的视差凸起镜面。如需更多有关着色器性能的信息,请阅读内置着色器性能页面。 此网格显示所有内置着色器的缩略图:
12 / 276
内置shader列表
着色技术细节
Unity有一个广泛的着色系统,让你来调整游戏中的所有图形外观。它的工作原理是这样的:
一个着色器本质上定义了游戏中的明暗应该如何表现的规则。在任意给定的着色器里是一个属性(通常是纹理)的数量。着色器通过材质执行,直接附属到特定的游戏对象。在一个材质里,你可以选择一个着色器,然后定义属性(通常是纹理和色彩,但性质可能不同)由该着色器使用。 这是相当复杂的,所以让我们看看一个工作流程图:
图表的左边是车体着色器。 2个不同的材质是这样创建的:蓝色的汽车材质和红色的汽车材质。这些材质的每一个都有2个给定的纹理;汽车纹理定义汽车的主要纹理,和颜色特效纹理。这些属性被着色器所使
13 / 276
用,使完成的汽车看上去像喷涂了两种色调。可以看到在红色车前面:面对相机的地方是黄色,然后随角度的增加消退成紫色。车材质附着到了两辆车上。而车轮、车灯和车窗没有颜色变化效果,因此必须使用不同的材质。在图表的底部有一个简单金属着色器。车轮材质使用这个着色器。请注意,即使同样的汽车纹理在这里被重用,最终车身产生的结果是完全不同的,因为在材质中使用的着色器是不同的。 更多具体细节,一个着色器定义了如下内容:
? ? ? ?
方法用以呈现(渲染)一个对象。这包括根据最终用户的图形卡使用不同的方法。 任意顶点和用于渲染的片段程序 可指派到材质中的一些纹理属性 可指派到材质中的颜色和编号设置
一个材质定义了:
? ? ?
用于渲染的纹理 用于渲染的颜色
一些其它资源,例如一个着色器渲染所必需的立方体贴图
着色器是图形程序员写的。他们创建使用ShaderLab语言,这很简单。但是要让一个着色器在各种图形卡上运行良好却是一个复杂的工作,需要对图形卡如何工作有一个相当全面的了解。
许多着色器直接内置在Unity中,有些作为标准资源库。如果你喜欢,更多有关着色器的信息参见内置着色器指导。
Performance of Unity shaders
There are a number of factors that can affect the overall performance of your game. This page will talk specifically about the performance considerations forBuilt-in Shaders. Performance of a shader mostly depends on two things: shader itself and which Rendering Path is used by the project or specific camera. For performance tips when writing your own shaders, see ShaderLab Shader Performance page.
Rendering Paths and shader performance
From the rendering paths Unity supports, Deferred Lighting and Vertex Lit paths have the most
predictable performance. In Deferred lighting, each object is generally drawn twice, no matter what lights are affecting it. Similarly, in Vertex Lit each object is generally drawn once. So then, the performance differences in shaders mostly depend on how many textures they use and what calculations they do.
Shader Performance in Forward rendering path
In Forward rendering path, performance of a shader depends on both the shader itself and the lights in the scene. The following section explains the details. There are two basic categories of shaders from a performance perspective, Vertex-Lit, and Pixel-Lit.
14 / 276
Vertex-Lit shaders in Forward rendering path are always cheaper than Pixel-Lit shaders. These shaders work by calculating lighting based on the mesh vertices, using all lights at once. Because of this, no matter how many lights are shining on the object, it will only have to be drawn once.
Pixel-Lit shaders calculate final lighting at each pixel that is drawn. Because of this, the object has to be drawn once to get the ambient & main directional light, and once for each additional light that is shining on it. Thus the formula is N rendering passes, where N is the final number of pixel lights shining on the object. This increases the load on the CPU to process and send off commands to the graphics card, and on the graphics card to process the vertices and draw the pixels. The size of the Pixel-lit object on the screen will also affect the speed at which it is drawn. The larger the object, the slower it will be drawn.
So pixel lit shaders come at performance cost, but that cost allows for some spectacular effects: shadows, normal-mapping, good looking specular highlights and light cookies, just to name a few.
Remember that lights can be forced into a pixel (\vertex lights shining on a Pixel-Lit shader will be calculated based on the object's vertices or whole object, and will not add to the rendering cost or visual effects that are associated with pixel lights.
General shader performance
Out of Built-in Shaders, they come roughly in this order of increasing complexity:
? ? ? ? ? ? ? ?
Unlit. This is just a texture, not affected by any lighting. VertexLit. Diffuse.
Normal mapped. This is a bit more expensive than Diffuse: it adds one more texture (normal map), and a couple of shader instructions. Specular. This adds specular highlight calculation.
Normal Mapped Specular. Again, this is a bit more expensive than Specular. Parallax Normal mapped. This adds parallax normal-mapping calculation.
Parallax Normal Mapped Specular. This adds both parallax normal-mapping and specular highlight calculation.
Additionally, Unity has several simplified shaders targeted at mobile platforms, under \These shaders work on other platforms as well, so if you can live with their simplifications (e.g. approximate specular, no per-material color support etc.), try using them!
15 / 276