UE 【材质编辑】自定义材质节点

使用UE的材质编辑器,蓝图提供了大量的节点函数:

实际上,这是一段封装好的包含一串HLSL代码的容器。打开"Source/Runtime/Engine/Classes/Material",可以看到很多不同节点的头文件:

照葫芦画瓢

以UMaterialExpressionArccossine为例,我们可以定义自己的一个节点:

.h

cpp 复制代码
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "UObject/ObjectMacros.h"
#include "MaterialExpressionIO.h"
#include "Materials/MaterialExpression.h"
#include "MaterialExpressionMultiVec.generated.h"

UCLASS(MinimalAPI, collapsecategories, hidecategories=Object)
class UMaterialExpressionMultiVec : public UMaterialExpression
{
    GENERATED_UCLASS_BODY()

        //材质节点的输入
    UPROPERTY()
        FExpressionInput Input1;
    UPROPERTY()
        FExpressionInput Input2;
    UPROPERTY()
        FExpressionInput Input3;
    UPROPERTY()
        FExpressionInput Input4;
    UPROPERTY()
        FExpressionInput Input5;

    UPROPERTY(EditAnywhere, Category = "MyMaterial")
        float myIndex;

    //~ Begin UMaterialExpression Interface
#if WITH_EDITOR
    //调用HLSLTranlator的函数来翻译HLSL代码
    virtual int32 Compile(class FMaterialCompiler* Compiler, int32 OutputIndex) override;
    //材质节点的名字
    virtual void GetCaption(TArray<FString>& OutCaptions) const override;
    //鼠标悬停提示
    virtual void GetExpressionToolTip(TArray<FString>& OutToolTip) override;
    virtual FText GetKeywords() const override {return FText::FromString(TEXT("multi-vec"));}
#endif
    //~ End UMaterialExpression Interface
};

.cpp

cpp 复制代码
//
//    UMaterialExpressionMultiVec
//
UMaterialExpressionMultiVec::UMaterialExpressionMultiVec(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    // Structure to hold one-time initialization
    struct FConstructorStatics
    {
        FText NAME_Math;
        FConstructorStatics()
            : NAME_Math(LOCTEXT("Math", "Math"))
        {
        }
    };
    static FConstructorStatics ConstructorStatics;

    myIndex = 0.0f;

#if WITH_EDITORONLY_DATA
    MenuCategories.Add(ConstructorStatics.NAME_Math);
#endif
}

#if WITH_EDITOR
int32 UMaterialExpressionMultiVec::Compile(class FMaterialCompiler* Compiler, int32 OutputIndex)
{
    int32 Result = INDEX_NONE;

    if (!Input1.GetTracedInput().Expression)
    {
        // 当输入口没有连线时报错,这个输出可以在材质编辑器里看到
        return Compiler->Errorf(TEXT("哇,报错了耶!!你的节点第一根线没连好!!!"));
    }
    if (!Input2.GetTracedInput().Expression)
    {
        // 当输入口没有连线时报错,这个输出可以在材质编辑器里看到
        return Compiler->Errorf(TEXT("哇,报错了耶!!你的节点第二根线没连好!!!"));
    }
    if (!Input3.GetTracedInput().Expression)
    {
        // 当输入口没有连线时报错,这个输出可以在材质编辑器里看到
        return Compiler->Errorf(TEXT("哇,报错了耶!!你的节点第三根线没连好!!!"));
    }
    if (!Input4.GetTracedInput().Expression)
    {
        // 当输入口没有连线时报错,这个输出可以在材质编辑器里看到
        return Compiler->Errorf(TEXT("哇,报错了耶!!你的节点第四根线没连好!!!"));
    }
    if (!Input5.GetTracedInput().Expression)
    {
        // 当输入口没有连线时报错,这个输出可以在材质编辑器里看到
        return Compiler->Errorf(TEXT("哇,报错了耶!!你的节点第五根线没连好!!!"));
    }

    int32 newIndex = myIndex;

    if (newIndex > 5 || newIndex < 0)
    {
        return Compiler->Errorf(TEXT("index指数不对,应该在0到5之间"));
    }

    switch (newIndex)
    {
    case 0:
        return Input1.Compile(Compiler);
    case 1:
        return Input2.Compile(Compiler);
    case 2:
        return Input3.Compile(Compiler);
    case 3:
        return Input4.Compile(Compiler);
    case 4:
        return Input5.Compile(Compiler);
    }

    return Result;
}

void UMaterialExpressionMultiVec::GetCaption(TArray<FString>& OutCaptions) const
{
    OutCaptions.Add(TEXT("MultiVecExpression"));
}

void UMaterialExpressionMultiVec::GetExpressionToolTip(TArray<FString>& OutToolTip)
{
    ConvertToMultilineToolTip(TEXT("Multi Vec to Select."), 40, OutToolTip);
}
#endif // WITH_EDITOR

编译之后,我们在材质编辑器中可找到我们节点:

注意:

1.悬停提示:Multi Vec to Select

2.节点名称:MultiVecExpression

3.内置参数:myIndex

相关推荐
技术小甜甜16 小时前
[Blender] 高质量材质推荐第四弹:25-30号精选纹理资源详解
blender·游戏开发·材质·建模·资源
bug总结2 天前
记录下three.js学习过程中不理解问题----材质(material)⑤
学习·材质
在下胡三汉6 天前
glb/gltf格式批量转换fbx/obj,材质贴图在,批量转换stl/dae等其他格式,无需一个个打开
材质·贴图
技术小甜甜9 天前
【Blender Texture】【游戏开发】高质感 Blender 4K 材质资源推荐合集 —— 提升场景真实感与美术表现力
blender·游戏开发·材质·texture
北冥没有鱼啊11 天前
UE 材质基础第三天
材质
qq_5895681011 天前
threejsPBR材质与纹理贴图
three.js·材质·贴图
中金快讯20 天前
塑料杯子什么材质最好,用起来是不是安全?
材质
北冥没有鱼啊23 天前
UE 材质几个输出向量节点
ue5·游戏引擎·ue4·虚幻·材质
ue星空1 个月前
UE5用TextureGraph制作瓷砖贴图材质
ue5·材质
三天不学习1 个月前
一文讲透 Vue3 + Three.js 材质属性之皮革篇【扫盲篇】
javascript·webgl·three.js·材质