Boat Attack使用了Universal RP的许多新图形功能,可以用于探索 Universal RP 的使用方式和技巧。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

6.4 KiB

My first Shader Graph

Before you begin, make sure that your project is set up properly, and the graphs are loading correctly. See Getting started with Shader Graph for more information.

Create a new node

Use the Create Node menu to create new nodes. There are two ways to open the menu:

  1. Right click, and select Create Node from the context menu.
  2. Press the spacebar.

In the menu, you can type in the search bar to look for specific nodes, or browse all nodes in the library. In this example, we'll create a Color node. First, type "color" in the Create Node menu's search bar. Then, click Color, or highlight Color and press Enter to create a Color node.

Connect nodes

To build a graph, you need to connect nodes together. To do so, click the Output Slot of a node, and drag that connection into the Input Slot of another node.

Start by connecting the Color node to the Albedo slot of our master node.

Change node output

Notice that the connection updated the master preview, and the 3D Object in the Main Preview is now black, which is the color specified in the Color node. You can click on the color bar in that node, and use the color picker to change the color. Any changes you make on the node updates the object in the Main Preview in real time.

For example, if you pick red, the 3D Object immediately reflects this change.

Save the graph

Currently, Shader Graphs do not automatically save. There are two ways to save your changes:

  1. Click the Save Asset button in the top left corner of the window.
  2. Close the graph. If Unity detects any unsaved changes, a pop-up window appears, and asks if you want to save those changes.

Create a Material

After saving your graph, use the shader to create a new Material. The process of creating a new Material and assigning it a Shader Graph shader is the same as that for regular shaders. In either the main menu or the Project View context menu, select Assets > Create > Material. Select the Material you just created. In its Inspector window, select the Shader drop-down menu, click Shader Graphs, and choose the Shader Graph shader you wish to apply to the Material.

You can also right-click the Shader Graph shader, and select Create > Material. This method automatically assigns that Shader Graph shader to the newly created Material.

Put the Material in the Scene

Now that you have assigned your shader to a Material, you can apply it to objects in the Scene. Drag and drop the Material onto an object in the Scene. Alternatively, in the object's Inspector window, locate Mesh Renderer > Materials, and apply the Material to the Element.

Use properties to edit the graph

You can also use properties to alter your shader's appearance. Properties are options that are visible from the Material's Inspector, which lets others change settings in your shader without the need to open the Shader Graph.

To create a new property, use the Add (+) button on the top right corner of the Blackboard, and select the type of property to create. In this example, we'll select Color.

This adds a new property in the Blackboard with the following options.

Option Description
Property button To change the name of the property, right-click the button, select Rename, then enter a new property name. To delete the property, right-click the button, and select Delete.
Exposed Enable this checkbox to make the property visible from the Material's Inspector.
Reference The property's name that appears in C# scripts. To change the Reference name, enter a new string.
Default The default value of the property.
Mode The mode of the property. Each property has different modes. For Color, you can select either Default or HDR.
Precision The default precision of the property.

There are two ways to reference a property in your graph:

  1. Drag the property from the Blackboard onto the graph.
  2. Right-click and select Create Node. The property is listed in the Properties category.

Try connecting the property to the Albedo output slot. The object immediately changes to black.

Save your graph, and return to the Material's Inspector. The property now appears in the Inspector. Any changes you make to the property in the Inspector affects all objects that use this Material.

More Tutorials

To keep exploring how to use Shader Graph to author shaders, check out these blog posts:

You can also visit the Unity YouTube Channel and look for video tutorials on Shader Graph, or head to our user forum to find the latest information and conversations about Shader Graph.