统一的Unigine着色器语言
The Unified UNIGINE shader language (UUSL) allows you to create a single shader file for both 3D graphics APIs: OpenGL and Direct3D11. The language brings pre-defined data types and implemented functions that simplifies and unifies shaders writing process.统一UNIGINE着色器语言(UUSL)允许您为两个3D图形API(OpenGL和Direct3D11)创建单个着色器文件。该语言带来了预定义的数据类型和实现的功能,这些功能简化并统一了着色器的编写过程。
This section of the Unigine documentation contains information about the UUSL syntax (unified data types, intrinsic functions, parameters, textures, semantics, etc.) and tutorials on creating shaders. Unigine文档的本部分包含有关UUSL语法(统一的数据类型,内部函数,参数,纹理,语义等)的信息,以及有关创建着色器的教程。
Frequently Asked Questions经常问的问题#
What is UUSL?什么是UUSL?#
The Unified UNIGINE shader language (UUSL) is a wrapper, which unifies the most part of GLSL and HLSL syntax. You can use the UUSL instructions to write shader code without graphic API defining: it will work on both APIs.统一UNIGINE着色器语言(UUSL)是包装器,它统一了GLSL和HLSL语法的大部分内容。您可以使用UUSL指令来编写着色器代码,而无需定义图形API:它将在两个API上都可以使用。
Should I write shaders by using UUSL only?我是否应该仅使用UUSL编写着色器?#
Definitely not. UUSL is just a wrapper which facilitates the shader-writing process. You can also use an old-school approach by writing separately part of the shader for corresponding graphic API with GLSL and HLSL shading languages.绝对不是。 UUSL只是一个有助于着色器编写过程的包装器。您也可以使用老式的方法,分别使用 GLSL 和 HLSL 着色语言为相应的图形API编写着色器的一部分。
Which shader stages does the Unigine engine have?Unigine引擎具有哪些着色器阶段?#
Unigine engine has the standard shaders pipeline: Vertex-Shader stage → Geometry-Shader stage → Fragment-Shader stage. If you want to use tessellation, the pipeline will have 3 additional stages between vertex-shader and geometry-shader stages: Control-Shader stage, tessellation stage, Evaluation-Shader stage. Unigine引擎具有标准的着色器管道: Vertex-Shader阶段→ Geometry-Shader阶段→ Fragment-Shader阶段。如果要使用细分,则管线在顶点着色器阶段和几何着色器阶段之间还有3个附加阶段: Control-Shader阶段,镶嵌细分阶段,评估-着色器阶段。
How are materials and shaders interrelated?材质和着色器如何相互关联?#
Material specifies which shaders for defined rendering pass it will use to render. You can define vertex, geometry and fragment shaders for a material. When you assign the material, the engine starts using its shaders to render the image. A piece of cake! 材质指定将用于渲染定义的 rendering pass 的着色器。您可以为材料定义顶点,几何和片段着色器。分配材质时,引擎将开始使用其着色器来渲染图像。一块蛋糕!
We have a neat article about the materials files formats, check it out.我们有一篇关于 materials文件格式的简洁文章,请查看。
How to start using UUSL?如何开始使用UUSL?#
When you start writing shaders, include the UUSL headers and start coding!开始编写着色器时,请包含UUSL标头并开始编码!
Use the following commands to include the necessary headers:使用以下命令包括必要的标题:
// Include Unified Unigine Shader Language (UUSL) common header
#include <core/materials/shaders/api/common.h>
Do shaders need compilation?着色器需要编译吗?#
Yes, they do. But the engine does the job: just use the materials_reload console command if you made some changes in shader files. It is really handy: you don't need to compile the shader every time you made any changes, you see the result of the changes in runtime without engine restart!是的,他们这样做。但是引擎可以完成工作:如果对着色器文件进行了一些更改,只需使用 materials_reload 控制台命令。这真的很方便:您不需要每次进行任何更改都编译着色器,无需运行引擎即可看到运行时的更改结果!
Can I debug the UUSL shader code?我可以调试UUSL着色器代码吗?#
Sure. If there is a shader error, the engine shows it in the console: 当然。如果存在着色器错误,引擎会在控制台中显示该错误:
The message contains information about which file has errors and their description (including the number of line).该消息包含有关哪个文件有错误及其描述(包括行数)的信息。
Export the shader file to the file and find this code line to figure out what's happen. Shader will be exported in a file with the graphic API shader format (.hlsl or .glsl) located relatively to the data folder. 将着色器文件导出到该文件,并找到此代码行以了解发生了什么。着色器将导出为相对于data文件夹具有图形API着色器格式(.hlsl或.glsl)的文件。
How to create custom shader?如何创建自定义着色器?#
您可以使用相应主题的教程来了解如何为它们创建材质和着色器: