Stanford UE4 + C++ Course Study Record 15: Introduction to Materials

Table of contents

1. Basic operation

2. Blueprint Control


A material         in UE can be simply understood as a resource applied to a mesh to control its appearance. The teaching of this part of the course will not focus on how to make exquisite and real materials, and how to build beautiful scenes, etc. It will focus on the use of materials and gameplay.

1. Basic operation

        Create a Materials folder under the Content directory, and create a material in it, named M_PBRDemo, PBR is the abbreviation of Physical Based Rendering (physically based rendering), as the name implies, it uses physical laws to achieve graphics rendering. Double-click to enter the editor, you can see that the material does have many physical properties that can be set. Drag a "Constant3Vector" from the palette into the "Base Color" pin and choose any color. Save the material and apply it to any geometry in the scene by dragging:

Figure 15-1 Applying a material

        Then, you can set the interface layout as follows, and view the corresponding effects while setting various properties of the material. Please explore and try this part yourself.

Figure 15-2 Change properties and view them in real time

        After trying a few times, we will find that every time we adjust the parameters, we need to click Apply or Save, and wait for a few seconds to see the effect, which will undoubtedly reduce the efficiency of our material adjustment. So we right-click any attribute node and "convert it to parameter". After saving, right-click the material in the content browser, create an instance of the material, name it MI_PBRDemo_Inst and open it, and then you can see the adjustment results immediately.

Figure 15-3 Adjust roughness in real time

        In addition, we can also use mathematical calculations to achieve some changing effects. Open M_PBRDemo again, add a "LinearInterpolate" node, and set the AB pins to red and blue respectively, and then set it as the basic color of the material, and you will find that the final color is purple obtained by mixing blue and red. Adjust the Alpha value of the node, and you will find that it controls the ratio of the fusion of the two.

        Add the Sine node, which is the sine function in mathematics, connected to the Alpha pin of Lerp, and the input of Sine is the Time node. Doing so, we created a material that alternates between red and blue using a simple math formula. However, the effective value of Alpha is 0~1, and the value range of sin is -1~1, so add 1 and multiply by 0.5 after the Sine node. When we create a mathematical operation node in the blueprint, we can directly enter the + - operator.

Figure 15-4 Material color switching

        We can also open the "Real-time Node" button at the top, and click on the detailed information of the node to preview the intermediate process in real time:

Figure 15-5 Real-time preview

        If you only need to preview some nodes in real time, you can right-click and select "Start preview node", so that the nodes before this node will be compiled. Figure 15-6 shows the result of previewing from the multiplication node:

Figure 15-6 Preview from the Multiply node

        All the nodes added above in the editor are called expressions, and they output a specific value. In addition to expressions, there is another type called functions, which are blueprint functions that can be reused. An intuitive difference between them is that functions can enter the detailed blueprint program by double-clicking, while expressions cannot. Add a DebugScalarValue node as shown below, and set it as the basic color, switch the mesh body to a cube in the preview, and you can see the number of the sine function displayed on the material:

Figure 15-7 Display numbers
Figure 15-8 Display numbers

2. Blueprint Control

        Further, we want to set the properties in the material as parameters and use the blueprint to control the style of the material.

        Copy a copy of M_PBRDemo and add the suffix of BP to the name to indicate the blueprint version. Open the material of the blueprint version, replace the Time node with Constant, set it as a parameter, name it GameTime, and save the material.

Figure 15-9 Conversion parameters

        Create a blueprint class from Actor in the Content directory and name it SineWaveBP. Open SineWaveBP, create a static mesh as the root node, and set it as a cube Cube (or any mesh), and then add the newly created M_PBRDemoBP material to it. Then we can use the blueprint to set the GameTime variable:

Figure 15-10 Setting GameTime
Figure 15-11 Running effect

        The above effects can be achieved with only materials or with blueprints, but the underlying processing of the two is different. According to the course description, if only materials are used, they will be compiled into HLSL (High Level Shader Language) and run directly on the GPU; while blueprint control will be calculated through the CPU. Generally speaking, we prefer to put these graphics calculations on the GPU for processing.

Guess you like

Origin blog.csdn.net/surkea/article/details/127248567