顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

顶点着色器 像素着色器

We created an example interactive vertex displacement effect with Shader Graph and the Lightweight Render Pipeline to help you use these features to design effects. This post will walk you through our process. Get the demo project with the Shader Graph shader, example scene, and some example game assets from the 3D Game Kit, and follow along!

我们使用Shader GraphLightweight Render Pipeline创建了一个示例性的交互式顶点位移效果,以帮助您使用这些功能来设计效果。 这篇文章将引导您完成我们的过程。 从3D游戏套件中获取带有Shader Graph着色器,示例场景和一些示例游戏资源的演示项目 ,然后继续学习!

The sphere in the video example below has a shader-based displacement effect that activates when we hit the space bar. In your game, you would assign this to some relevant gameplay event. In this article, we will look at how to create this shader using the Shader Graph package, and integrate the spacebar keypress trigger. The goal is to help you understand how to design effects in Shader Graph, and interact with them from your other C# scripts. The demo project contains the shader, the script that controls the shader,  a preconfigured Lightweight Scriptable Render Pipeline (LWRP) Asset, and an example scene for you to get started with. If you prefer to view this tutorial as a video instead of text you can find it on the Unity YouTube channel.

以下视频示例中的球体具有基于着色器的位移效果,当我们按下空格键时会**该效果。 在您的游戏中,您可以将其分配给一些相关的游戏事件。 在本文中,我们将研究如何使用Shader Graph包创建此着色器,并集成空格键按键触发器。 目的是帮助您了解如何在Shader Graph中设计效果,以及如何从其他C#脚本与它们进行交互。 演示项目包含着色器,控制着色器的脚本,预配置的轻量级可脚本化渲染管道(LWRP)资产以及供您入门的示例场景。 如果您想以视频而非文本的形式观看本教程,可以在Unity YouTube频道上找到它。

演示地址

安装着色器图和LWRP软件包 (Installing Shader Graph and LWRP Packages)

First, let’s look at how to set up Shader Graph and the Lightweight Render Pipeline. Open the Package Manager and select install on the Lightweight RP package. This will automatically install the correct version of Shader Graph.

首先,让我们看一下如何设置Shader Graph和Lightweight Render Pipeline。 打开软件包管理器,然后选择在Lightweight RP软件包上安装。 这将自动安装正确版本的Shader Graph。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Once we’ve installed the Lightweight RP, we need to create a new Pipeline asset in the Project. Select Create->Rendering->Lightweight Render Pipeline Asset.

一旦安装了Lightweight RP,就需要在项目中创建一个新的Pipeline资产。 选择创建->渲染->轻型渲染管道资产。

We can then activate this pipeline asset by going to Edit->Project Settings->Graphics, and dragging the LightweightRenderPipelineAsset into the Scriptable Render Pipeline Settings field. If you are following along with the downloaded assets, this step has already been completed for you.

然后,我们可以通过以下方法来**该管道资产:转到“编辑”->“项目设置”->“图形”,然后将LightweightRenderPipelineAsset拖动到“可脚本渲染管道设置”字段中。 如果您正在跟踪下载的资产,那么此步骤已经为您完成。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Now that the Lightweight Render Pipeline is installed, we can look at creating a new Shader Graph. Let’s create a new graph in our project by selecting Create->Shader->PBR Graph. The PBR Graph allows us to create a new shader that takes inputs from Unity’s Physically Based Rendering system,so that our shader can use features such as shadows and reflections. Once we have created this shader, we add it to a new Material and attach the Material to a Sphere in our example scene by dragging and dropping the material onto the sphere.

现在已经安装了轻量级渲染管线,我们可以看看创建一个新的Shader Graph。 通过选择Create-> Shader-> PBR Graph在我们的项目中创建一个新图。 PBR图允许我们创建一个新的着色器,该着色器从Unity的基于物理的渲染系统中获取输入,以便我们的着色器可以使用诸如阴影和反射之类的功能。 创建此着色器后,我们将其添加到新材质中,然后通过将材质拖放到球体上,将其附加到示例场景中的“球体”上。

着色器图的顶点位移 (Vertex Displacement with Shader Graph)

To achieve the effect, we will displace the vertices in our mesh along its normals by changing the output Position in the PBR Master output node. We will displace by using an Add node on the base Object Position of each vertex. By adding the Normal Vector to the base Object Position, we can see all the vertices become extruded, making the sphere appear bigger. To vary this displacement, we will multiply this normal vector displacement semi-randomly by using a Simple Noise node.

为了达到这种效果,我们将通过更改“ PBR主”输出节点中的输出“ Position”来沿其法线移动网格中的顶点。 我们将通过在每个顶点的基本“对象位置”上使用“添加”节点进行置换。 通过将法线向量添加到基本对象位置,我们可以看到所有顶点都被拉伸,从而使球体看起来更大。 要更改此位移,我们将使用“简单噪声”节点将此法向矢量位移半随机地乘以。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

When we click Save Asset, we can see in the Scene View that the sphere is now displaced based on Simple Noise.

单击“保存资产”时,我们可以在“场景视图”中看到,球现在已基于“简单噪波”进行了位移。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Unfortunately, there are seams in the displacement because the Simple Noise is being sampled based on UV space. To fix the seams by using Object Space for the Simple Noise instead of UV Space, we can simply add a Position node set to Object.

不幸的是,由于基于UV空间对简单噪声进行采样,因此位移中存在接缝。 要通过使用对象空间代替简单空间来替代接缝,只需将“位置”节点集添加到对象即可。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

To create the pulsation effect, we will scroll this Position output by adding it to a Time node, before sending it to the Simple Noise node. We can also use a Multiply with the Time node to vary the speed of the scroll.

为了创建脉动效果,我们将通过将此位置输出添加到“时间”节点来滚动此位置输出,然后再将其发送到“简单噪声”节点。 我们还可以将“与时间”节点相乘来改变滚动速度。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

在C#中控制着色器图属性 (Controlling Shader Graph Properties in C#)

To control our displacement, we expose a new Shader Property in our Shader Graph. Shader Properties allow us to provide inputs to our shader via values entered in the Inspector, or via our own C# scripts as in this case. We will create a new Vector1 property named Amount and changed the Reference to _Amount. The reference field is the string name by which we will access and change the displacement via script. If we do not change this, it will use an auto-generated value. If the string does not match exactly, we will not be able to address our property via script so double check that both match, including capitalization.

为了控制位移,我们在“明暗器图形”中公开了一个新的“明暗器属性”。 着色器属性允许我们通过在检查器中输入的值或通过本例中自己的C#脚本为着色器提供输入。 我们将创建一个新的Vector1属性,名为Amount,并将Reference更改为_Amount。 参考字段是字符串名称,我们将通过该字符串名称来访问和更改脚本的位移。 如果我们不更改此设置,它将使用自动生成的值。 如果字符串不完全匹配,我们将无法通过脚本访问属性,因此请仔细检查两者是否匹配,包括大小写。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

We use this Amount shader property in a Multiply node with the Simple Noise before it gets multiplied with the normal vector. This allows us to scale the noise before it’s applied to the vertex positions. Now, the Amount variable controls how much we displace each vertex in the mesh.

在将其与法线向量相乘之前,我们在“简单噪声”的“乘以”节点中使用此“数量”着色器属性。 这使我们能够在将噪波应用于顶点位置之前对其进行缩放。 现在,Amount变量控制我们在网格中移动每个顶点的程度。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

To control this Amount variable, we have created a C# script called DisplacementControl and attached it to the DisplacementSphere GameObject. This script controls the _Amount variable by interacting with the property we created in our material which is assigned to the MeshRenderer component. We store a reference to the MeshRenderer component in the variable meshRender, and declare a new float variable displacementAmount.

为了控制此Amount变量,我们创建了一个名为DisplacementControl的C#脚本并将其附加到DisplacementSphere GameObject。 该脚本通过与我们在材质中创建的属性(分配给MeshRenderer组件)进行交互来控制_Amount变量。 我们将对MeshRenderer组件的引用存储在变量meshRender中,并声明一个新的float变量displacementAmount。

We use a simple lerp in the Update function to interpolate the displacementAmount variable to the value of 0. We then set the shader variable _Amount to the value stored in displacementAmount variable. This will update the Shader Graph’s _Amount variable, smoothing it over time to 0.

我们在Update函数中使用一个简单的lerp来将置换量Amount插入值0。然后,将着色器变量_Amount设置为置换量中存储的值。 这将更新Shader Graph的_Amount变量,将其随时间平滑为0。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

We are using Unity’s default “Jump” Input Axis (which is assigned to the space bar by default) to set the value of displacementAmount to 1 when pressed.

我们使用Unity的默认“跳转”输入轴(默认情况下分配给空格键)将按下时将置换量值设置为1。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Now, when we enter Play Mode in the scene, we can see that by pressing the spacebar, displacementAmount gets set to the value of 1, and then slowly interpolates back to 0.

现在,当我们在场景中进入“播放模式”时,可以看到通过按空格键,displacementAmount的值设置为1,然后缓慢地插值回0。

使用明暗器图形创建Voronoi发光效果 (Creating the Voronoi Glow Effect with Shader Graph)

To create the adjustable glow effect, we will output to the Emission in the PBR Master node. We use a Voronoi Noise node and Multiply it to a Color node. This will create a little modulation in the glow effect with some dark spots. Then, we use a Lerp node with another Color node as the base color, and use the Amount variable in the T input. This will allow us to blend between a base Color node and the Voronoi Noise color node using the Amount variable.

要创建可调整的发光效果,我们将在PBR主节点中输出到“发射”。 我们使用Voronoi噪波节点并将其乘以一个Color节点。 这会在发光效果中产生一些暗点的调制。 然后,我们将Lerp节点与另一个Color节点用作基础颜色,并在T输入中使用Amount变量。 这将允许我们使用Amount变量在基本Color节点和Voronoi Noise颜色节点之间进行混合。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Then, we will scroll the glow by using a similar setup as before. We use a Position node set to Object and add it to a Time node, and connect the output into the UV slot of our Voronoi Noise node.

然后,我们将使用与以前类似的设置来滚动发光。 我们使用一个设置为“对象”的“位置”节点,并将其添加到“时间”节点,然后将输出连接到我们的Voronoi噪波节点的UV插槽中。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

We can also add a second layer of glow for additional variation by using a Simple Noise node scrolled by an additional layer of Time, and multiplying the two Noise outputs together. This gives us a second layer of noise, which adds visual interest.

我们还可以通过使用“简单噪声”节点滚动一个附加的时间层,并将两个“噪声”输出相乘,来添加第二个发光层,以增加变化。 这给了我们第二层噪音,增加了视觉吸引力。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

Because this glow effect is adjusted by the Amount variable in the Lerp node, we can see in Play Mode that when we press the spacebar, the sphere will activate both effects, vertex displacement and glow, and then slowly go back to its normal state.

由于此发光效果是由Lerp节点中的Amount变量调整的,因此我们可以在“播放模式”中看到,当按下空格键时,球体将同时**顶点位移和发光这两种效果,然后慢慢返回其正常状态。

顶点着色器 像素着色器_使用着色器图创建交互式顶点效果

To add a little extra touch, we have also linked up a simple particle system in the DisplacementControl script.

为了增加一点额外的感觉,我们还在DisplacementControl脚本中链接了一个简单的粒子系统。

And here is our finished product:

这是我们的成品:

演示地址

download here. We look forward to seeing what cool stuff you create with it! 在这里下载 。 我们期待看到您用它创造了什么很棒的东西!

To learn more about Unity, check out some of our available courses on our Learn website. To check out some other cool effects, feel free to also browse the Unity Asset Store.

要了解有关Unity的更多信息,请在学习网站上查看一些可用的课程。 要查看其他一些很酷的效果,请随时浏览Unity Asset Store

翻译自: https://blogs.unity3d.com/2019/02/12/creating-an-interactive-vertex-effect-using-shader-graph/

顶点着色器 像素着色器

相关文章: