如何:建立基本材質著色器How to: Create a basic texture shader

本文會示範如何使用著色器設計工具和有向圖形著色器語言 (DGSL),來建立單一紋理著色器。This article demonstrates how to use the Shader Designer and the Directed Graph Shader Language (DGSL) to create a single-texture shader. 這個著色器會將完稿色彩直接設定為從材質中取樣 RGB 和 Alpha 值。This shader sets the final color directly to the RGB and alpha values that are sampled from the texture.

建立基本紋理著色器Create a basic texture shader

您可以直接將材質範例的色彩與 Alpha 值直接寫入完稿輸出色彩,來實作基本的單一材質著色器。You can implement a basic, single-texture shader by writing the color and alpha values of a texture sample directly to the final output color.

開始之前,請確定已顯示 [屬性] 視窗和 [工具箱] 。Before you begin, make sure that the Properties window and the Toolbox are displayed.

  1. 建立要使用的 DGSL 著色器。Create a DGSL shader to work with. 如需有關如何將 DGSL 著色器加到專案的詳細資訊,請參閱著色器設計工具中的<使用者入門>一節。For information about how to add a DGSL shader to your project, see the Getting Started section in Shader Designer.

  2. 刪除 [點色彩] 節點。Delete the Point Color node. 在 [選取] 模式中,選取 [點色彩] 節點,然後在功能表列上,選擇 [編輯] > [刪除] 。In Select mode, select the Point Color node, and then on the menu bar, choose Edit > Delete . 這樣會替下一個步驟加入的節點留出空間。This makes room for the node that's added in the next step.

  3. 將 [材質範例] 節點加入圖形。Add a Texture Sample node to the graph. 在 [工具箱] 的 [材質] 下,選取 [材質範例] ,並將其移至設計介面。In the Toolbox , under Texture , select Texture Sample and move it to the design surface.

  4. 將 [材質座標] 節點加入圖形。Add a Texture Coordinate node to the graph. 在 [工具箱] 的 [材質] 下,選取 [材質座標] ,並將其移至設計介面。In the Toolbox , under Texture , select Texture Coordinate and move it to the design surface.

  5. 選擇要套用的材質。Choose a texture to apply. 在 [選取] 模式中,選取 [材質範例] 節點,然後在 [屬性] 視窗中,使用 [檔名] 屬性來指定您想要使用的材質。In Select mode, select the Texture Sample node, and then in the Properties window, specify the texture that you want to use by using the Filename property.

  6. 將材質設為可公開存取。Make the texture publicly accessible. 選取 [材質範例] 節點,然後在 [屬性] 視窗中,將 [存取權] 屬性設定 [公用] 。Select the Texture Sample node, and then in the Properties window, set the Access property to Public . 現在您可以從另一個工具設定材質,例如 [模型編輯器] 。Now you can set the texture from another tool, such as the Model Editor .

  7. 將材質座標連接到材質範例。Connect the texture coordinates to the texture sample. 在 [選取] 模式中,將 [材質座標] 節點的 [輸出] 端點移至 [材質範例] 節點的 [UV] 端點。In Select mode, move the Output terminal of the Texture Coordinate node to the UV terminal of the Texture Sample node. 此連接會對指定座標的材質取樣。This connection samples the texture at the specified coordinates.

  8. 將材質範例連接到完稿色彩。Connect the texture sample to the final color. 將 [材質範例] 節點的 [RGB] 端點移至 [完稿色彩] 節點的 [RGB] 端點,然後將 [材質範例] 節點的 [Alpha] 端點移至 [完稿色彩] 節點的 [Alpha] 端點。Move the RGB terminal of the Texture Sample node to the RGB terminal of the Final Color node, and then move the Alpha terminal of the Texture Sample node to the Alpha terminal of the Final Color node.

下圖顯示完成的著色器圖形和套用至立方體的著色器預覽。The following illustration shows the completed shader graph and a preview of the shader applied to a cube.

注意

在此圖中,使用一個平面當成預覽圖形,並已指定材質以更適當展現著色器的效果。In this illustration, a plane is used as the preview shape, and a texture has been specified to better demonstrate the effect of the shader.

著色器圖形及其效果預覽

某些圖形可對一些著色器提供更佳的預覽。Certain shapes might provide better previews for some shaders. 如需如何在著色器設計工具中預覽著色器的詳細資訊,請參閱著色器設計工具For more information about how to preview shaders in the Shader Designer, see Shader Designer

另請參閱See also