着色器是包含显卡执行代码和指令的资源。 材质将引用着色器并设置其参数(纹理、颜色等)。
Unity 包含一些在项目中始终可用的内置着色器(例如, 标准着色器)。您也可以编写自己的着色器并应用后期处理效果。
要创建新着色器,请从主菜单或 Project 视图__上下文菜单中 选择 Assets__ > Create > Shader。着色器是 类似于 C# 脚本的文本文件,以 Cg/HLSL 和 ShaderLab 语言的组合编写而成 (请参阅编写着色器页面以了解详细信息)。
在此检视面板部分可为着色器指定默认纹理。每当使用 此着色器创建新材质时,都会自动分配 这些纹理。
着色器检视面板显示有关着色器的基本信息(主要是着色器标签), 并允许编译和检查低级编译代码。
对于表面着色器 (Surface Shader),__Show generated code__ 按钮将显示 Unity 生成的用于处理光照和阴影的所有代码。如果确实想要 自定义生成的代码,只需将其全部复制并粘贴回原始着色器文件 并开始调整即可。
Compile and show code 按钮的弹出菜单可检查选定平台 最终编译的着色器代码(例如 Direct3D9 上的程序集,或适用于 OpenGL ES 的 低级优化 GLSL)。此功能在优化着色器性能时非常有用;通常情况下,您会想知道最后在此处生成了 多少低级指令。
生成的低级代码可用于粘贴到 GPU 着色器性能分析工具(如 AMD GPU ShaderAnalyzer 或 PVRShaderEditor)。
在着色器导入时,Unity 不会编译整个着色器。这是因为大多数着色器 内部都有很多变体,因此为所有可能的平台编译所有这些变体 将需要很长时间。实际的操作过程为:
在播放器构建时,所有“尚未编译”的着色器变体都将被编译,因此即使 Editor 不会使用这些着色器变体,它们也会存在于游戏数据中。
但是,这确实意味着着色器可能具有在着色器导入时未检测到的 错误。例如,正在使用 Direct3D 11 运行 Editor,但如果着色器是针对 OpenGL 进行编译的, 则会出错。此外,还可能着色器的某些变体不符合着色器模型 2.0 指令限制等。如果 Editor 需要了解这些错误,它们将显示在检视面板中;但是,为了检查错误,手动为所需平台 完全编译着色器也是一种很好的做法。使用着色器检视面板 中的 Compile and show code 弹出菜单即可执行此操作。
着色器编译是使用名为 UnityShaderCompiler
的后台进程执行的,该进程由 Unity
在需要编译着色器时即时启动。可启动多个编译器进程(通常在机器中每个 CPU
核心对应一个),这样在播放器构建时就可以并行完成着色器编译。当
Editor 不编译着色器时,编译器进程不执行任何操作,也不消耗计算机资源,
所以不必担心它们。Unity Editor 退出时也将关闭这些进程。
各个着色器变体编译结果将缓存在项目中的 Library/ShaderCache
文件夹下。
这意味着 100% 相同的着色器或其代码片段将重用以前编译的结果。此外还
意味着,如果有许多经常更改的着色器,着色器缓存文件夹可能会变得
非常大。删除文件夹内容通常是安全的;只会导致重新编译着色器变体。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.