《Unity着色器圣经》2.0.1 | 什么是着色器(shader)?

目录索引

译文

欢迎来到第二章,本章我们将进入 Unity 着色器(shader)专题。着色器是一种带有“.shader”后缀(如 color.shader)的程序,经过编译它们可以在项目中实现有趣的效果。着色器的内部包含了一系列的数学计算与指令,可以对屏幕上模型覆盖区域内的每个像素的颜色进行处理。

着色器允许我们根据多边形物体的属性通过坐标系统绘制元素。程序在GPU上执行,因为GPU 是由数千个小型、高效的内核所组成的,非常适合解决并行任务,而 CPU 则是专门为处理串行任务而设计的。

Unity 有三种与着色器有关的文件类型。第一种是扩展名直接为“.shader”的文件,该类型的程序能够在不同类型的渲染管线中编译。

第二种是扩展名为”.shadergraph “的文件,该类型的程序只能在通用渲染管线(URP)或高清渲染管线(HDRP)中编译。除此之外,还有一些扩展名为”.hlsl “的文件,里面包含了我们自定义的着色器函数。这些函数通常以 “自定义函数 “节点的形式出现在shader graph中。

还有一种扩展名为”.cginc “的程序,我们将在稍后的小节中详细介绍。现在,我们仅需要了解以下联系:“.cginc ”与“.shader ”程序中的 CGPROGRAM 关联,而“.hlsl ”与“.shadergraph ”程序中的 HLSLPROGRAM 关联。了解这种联系是非常重要的,因为每个扩展名都有不同的功能,并在特定的上下文环境中使用。

图片[1]-《Unity着色器圣经》2.0.1 | 什么是着色器(shader)?-软件开发学习笔记
Fig. 2.0.1a. Unity中不同shader的图标

Unity 至少定义了四种类型的着色器结构,其中包括顶点着色器和片元着色器的组合、用于自动光照计算的表面着色器以及用于更高级场合的计算着色器。每种类型的着色器都有先前描述过的属性和功能,便于编译过程。虽然 Unity 会自动加入这些结构,我们也可以十分轻松地对其进行自定义。


原文对照

Considering some previous knowledge, we will go into the topic of shaders in Unity. A shader is a small program with a “.shader” extension (e.g. color.shader), which can be used to generate interesting effects in our projects. Inside, it has mathematical calculations and lists of instructions (commands) that allow color processing for each pixel within the area covering an object on our computer screen.

This program allows us to draw elements (using coordinate systems) based on the properties of a polygonal object. The shaders are executed by the GPU since they have a parallel architecture that consists of thousands of small, efficient cores designed to solve tasks simultaneously, while the CPU has been designed for sequential serial processing.

Note that Unity has three types of files associated with shaders. Firstly, we have programs with the “.shader” extension that are capable of compiling in the different types of render pipelines.

Secondly, we have programs with the “.shadergraph” extension that can only compile in either Universal RP or High Definition RP. In addition, we have files with the “.hlsl” extension that allow us to create customized functions; generally used within a node type called Custom Function, found in Shader Graph.

There is also another type of program with the extension “.cginc” which we will review in detail later on. For now, we will limit ourselves to making the following association: “.cginc” is linked to “.shader” CGPROGRAM, and “.hlsl” is linked to “.shadergraph” HLSLPROGRAM. Knowing this analogy is fundamental because each extension fulfills a different function and is used in specific contexts.

图片[1]-《Unity着色器圣经》2.0.1 | 什么是着色器(shader)?-软件开发学习笔记
Fig. 2.0.1a. Reference icons for shaders in Unity

In Unity, there are at least four types of structures defined to generate shaders, among which we can find the combination of vertex shader and fragment shader then surface shader for automatic lighting calculation and compute shader for more advanced concepts. Each of these structures has previously described properties and functions that facilitate the compilation process; we can also easily define our operations, since the software adds these structures automatically.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容