Working with Material in Unreal Engine

Published on

How to work with materials in the Unreal Engine?

Platforma

Create Material Graph with C++

Creating Material Graph with C++ instead of using Blueprint nodes make sense especially to automate processes when working with a lot of materials. C++ is a way to move material data exported from any tool to the Unreal Engine and re-create them aitomatically to materials directly by code.

C++ comes it handy as well anytime there's a need to add a functionality that is not available through the common BP nodes.

  • All Material extensions
  • #include "MaterialEditingLibrary.h" // MaterialEditor (Editor Only) is required for working with Materials in C++ in the editor

All the code below is placed in UBlueprintFunctionLibrary inherited class.

Material Graph with C++

The screenshot above is the output of the code below

  • Build Material

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Build a material graph with the provided values
     *
     * @param MaterialPath	The path of the material: "/Game/Folder/MyMaterial"
     * @param TexturePath	The path of the texture to add in the material: "/Game/Folder/MyTexture"
     * @param TexCoord	Texture coordinates for the texture
     * @param Color	Color that will be multiplied by the texture
     * @param Metallic	Metallic value of the material
     * @param Specular	Specular value of the material
     * @param Roughness	Roughness value of the material
     * @param OutInfoMsg	More information about the action's result
     * 
     * @return If the action was a success or not
     */
    UFUNCTION(BlueprintCallable, Category = "")
    static bool BuildMaterial(FString MaterialPath, FString TexturePath, FVector2D TexCoord, FLinearColor Color, float Metallic, float Specular, float Roughness, FString& OutInfoMsg);
  • Add TextureParameter node to Material

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Add a texture parameter to a material graph
     *
     * @param Material	The material in which to add the expression
     * @param Texture	The texture to use
     * @param ParameterName	The name of the expression
     * @param NodePos	The XY coordinates of the node in the graph
     *
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = "")
    static class UMaterialExpressionTextureSampleParameter2D* AddTextureParameter(UMaterial* Material, UTexture* Texture, FString ParameterName, FIntPoint NodePos);
  • Add ScalarParameter node to Material

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Add a scalar parameter to a material graph
     *
     * @param Material	The material in which to add the expression
     * @param Value	The float value to use
     * @param ParameterName	The name of the expression
     * @param NodePos	The XY coordinates of the node in the graph
     *
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = " ")
    static class UMaterialExpressionScalarParameter* AddScalarParameter(UMaterial* Material, float Value, FString ParameterName, FIntPoint NodePos);
  • Add VectorParameter node to Material

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Add a vector parameter to a material graph
     *
     * @param Material	The material in which to add the expression
     * @param Color	The color value to use
     * @param ParameterName	The name of the expression
     * @param NodePos	The XY coordinates of the node in the graph
     *
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=Fd56hSN83mk" target="_blank">Alex Quevillon|10 - Build Material</a>")
    static class UMaterialExpressionVectorParameter* AddVectorParameter(UMaterial* Material, FLinearColor Color, FString ParameterName, FIntPoint NodePos);
  • Add MultiplyExpression node to Material

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Add a multiply expression to a material graph
     *
     * @param Material	The material in which to add the expression
     * @param ExpressionDesc	The description of the expression
     * @param NodePos	The XY coordinates of the node in the graph
     *
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = " ")
    static class UMaterialExpressionMultiply* AddMultiplyExpression(UMaterial* Material, FString ExpressionDesc, FIntPoint NodePos);
  • Add TexCoordExpression node to Material

    	/**
     * Editor Only - Will not work in packaged build.
     *
     * Add a texture coordinate expression to a material graph
     *
     * @param Material	The material in which to add the expression
     * @param Value	The texture coordinates value to use
     * @param ExpressionDesc	The description of the expression
     * @param NodePos	The XY coordinates of the node in the graph
     *
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=Fd56hSN83mk" target="_blank">Alex Quevillon|10 - Build Material</a>")
    static class UMaterialExpressionTextureCoordinate* AddTexCoordExpression(UMaterial* Material, FVector2D Value, FString ExpressionDesc, FIntPoint NodePos);
  • GetExistingMaterialExpressionFromName helpe function

    /**
     * Editor Only - Will not work in packaged build.
     *
     * Retrieve an existing material expression based on the provided name or description
     *
     * @param Material	The material in which the expression is
     * @param NameOrDescription	The name or description of the expression to find
     * 
     * @return The expression
     */
    UFUNCTION(BlueprintCallable, Category = " ")
    static class UMaterialExpression* GetExistingMaterialExpressionFromName(UMaterial* Material, FString NameOrDescription);

Assign Material to Mesh in C++

The code below allow to assign a material to Static Mesh as well as to Skeletal Mesh.

/**
 * Editor Only - Will not work in packaged build.
 *
 * Assign material to a mesh asset (static mesh or skeletal mesh)
 *
 * @param MeshPath	Path of the mesh: "/Game/Folder/MyMesh"
 * @param MaterialPath	Path of the material: "/Game/Folder/MyMaterial"
 * @param MaterialId	Material slot to assign material into
 * @param OutInfoMsg	More information about the action's result
 *
 * @return If the action was a success or not
 */
UFUNCTION(BlueprintCallable, Category = "<a href="https://www.youtube.com/watch?v=u3QOZUNeJOE" target="_blank">Alex Quevillon | Assign Material To Mesh</a>")
static bool AssignMaterialToMeshAsset(FString MeshPath, FString MaterialPath, int MaterialId, FString& OutInfoMsg);
Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us