在实际项目中,很多次遇到在材质中需要根据index选择输出对应的数据,一个两个用if节点去连问题不大,但是数据量多了之后连起来就满眼的连线,乱的一塌糊涂,所以有了封装一个switch的想法,本质上还是if的嵌套实现,但是由代码层面去创建hlsl代码,不用在材质编辑器中连的乱七八糟了

注:这里只做了float1,float2,float3,float4的选择实现,其它的需要可以自行扩展,

     defResult为各种类别的0项,因为本质是做了循环的加法

     defResult的初始化不能省略,因为if判断里面对类别进行了判断,如果类别不符合会返回index_none

最终效果

materialSwitch

1.创建个插件,空白的就好,用来放我们的自定义节点相关的内容

2.创建switch的节点实现

MaterialExpressionSwitch.h

// Copyright 2022-2023 Ace Software. All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited#pragma once#include "CoreMinimal.h"
#include "Materials/MaterialExpression.h"
#include "MaterialExpressionIO.h"
#if WITH_EDITOR#include "MaterialCompiler.h"
#include "MaterialGraph/MaterialGraphNode_Comment.h"
#include "MaterialGraph/MaterialGraphNode.h"
#endif //WITH_EDITOR
#include "MaterialExpressionSwitch.generated.h"struct FPropertyChangedEvent;/*** */
UCLASS(collapsecategories, hidecategories=Object)
class ACEMATERIAL_API UMaterialExpressionSwitch : public UMaterialExpression
{GENERATED_UCLASS_BODY()UPROPERTY(EditAnywhere, Category=AceMaterial)TArray<FExpressionInput> Layers;UPROPERTY(EditAnywhere, Category=AceMaterial, meta=(OverridingInputProperty = "Index"))FExpressionInput Index;//~ Begin UMaterialExpression Interface#if WITH_EDITORvirtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;virtual int32 Compile(class FMaterialCompiler* Compiler, int32 OutputIndex) override;virtual void GetCaption(TArray<FString>& OutCaptions) const override;virtual const TArray<FExpressionInput*> GetInputs() override;virtual FName GetInputName(int32 InputIndex) const override;virtual FExpressionInput* GetInput(int32 InputIndex) override;
#endif // WITH_EDITOR//~ End UMaterialExpression Interface
};

MaterialExpressionSwitch.cpp

// Copyright 2022-2023 Ace Software. All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited#include "MaterialExpressionSwitch.h"#define LOCTEXT_NAMESPACE "MaterialExpression"UMaterialExpressionSwitch::UMaterialExpressionSwitch(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer)
{// Structure to hold one-time initializationstruct FConstructorStatics{FText NAME_Math;FConstructorStatics(): NAME_Math(LOCTEXT("Switch", "Switch")){}};static FConstructorStatics ConstructorStatics;#if WITH_EDITORONLY_DATAMenuCategories.Add(ConstructorStatics.NAME_Math);
#endif
}
#if WITH_EDITORvoid UMaterialExpressionSwitch::GetCaption(TArray<FString>& OutCaptions) const
{OutCaptions.Add(TEXT("Switch"));
}const TArray<FExpressionInput*> UMaterialExpressionSwitch::GetInputs()
{TArray<FExpressionInput*> Result;    for (int32 LayerIdx = 0; LayerIdx<Layers.Num(); LayerIdx++){Result.Add(&Layers[LayerIdx]);}Result.Add(&Index);return Result;
}FName UMaterialExpressionSwitch::GetInputName(int32 InputIndex) const
{if(InputIndex<Layers.Num()){return *FString::Printf(TEXT("Layer%d"), InputIndex);}return TEXT("Index");
}FExpressionInput* UMaterialExpressionSwitch::GetInput(int32 InputIndex)
{if(InputIndex<Layers.Num())return &Layers[InputIndex];return  &Index;
}void UMaterialExpressionSwitch::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{Super::PostEditChangeProperty(PropertyChangedEvent);if (UMaterialGraphNode* MatGraphNode = Cast<UMaterialGraphNode>(GraphNode)){MatGraphNode->RecreateAndLinkNode();}}int32 UMaterialExpressionSwitch::Compile(FMaterialCompiler* Compiler, int32 OutputIndex)
{int32 Result = Compiler->Constant(0);if(!Index.Expression){return  Compiler->Errorf(TEXT("请输入选择的下标!!!"));}int32 indexCode=Index.Compile(Compiler);if(Compiler->GetType(indexCode)!=MCT_Float){return  Compiler->Errorf(TEXT("value值需要输入数值,会向下取整!!!"));}if(Layers.Num()==0){return  INDEX_NONE;//Result=Compiler->Errorf(TEXT("请添加需要选择的数据!!!"));}indexCode=Compiler->Floor(indexCode);int32 defResult;switch (Compiler->GetType(Layers[0].Compile(Compiler))){case MCT_Float1:defResult=Compiler->Constant(0);break;case MCT_Float2:defResult=Compiler->Constant2(0,0);break;case MCT_Float3:defResult=Compiler->Constant3(0,0,0);break;case MCT_Float4:defResult=Compiler->Constant4(0,0,0,0);break;default:return  INDEX_NONE;}int thCode=Compiler->Constant(0.0001f);for (int32 i=0;i<Layers.Num();i++){int32 tmpCode=Compiler->Floor(Compiler->Constant(i)) ;int32 layerCode=Layers[i].Compile(Compiler);int32 ifCode=Compiler->If(indexCode,tmpCode,defResult, layerCode,defResult,thCode);Result=Compiler->Add(Result, ifCode);}return Result;
}#endif

UE4 switch材质节点相关推荐

  1. UE4 后期材质节点学习

    以下对应的效果: 材质后期在这里进行设置: 在这里调整场景的整体的饱和度 场景的对比度 灰度系数的调整 高光度/图像增益 灰阶偏移的设置 想了解这些专业名词可以看:相机gain lift gamma ...

  2. UE4材质节点的颜色分类

    目标 UE4的材质节点有各种颜色: 这些颜色其实也是一种"信息",它提示了节点本身的一种"类别". 本篇的目标是: 观察代码中决定材质节点颜色的逻辑 总结出各个 ...

  3. ue4材质节点怎么用_自学ue4材质,一大堆材质节点该如何学?

    目录 [1.基础案例]: [2.材质基础]: [3.数学工具]: [1]Desmos [2]GeoGebra [4.常用节点解读]: 一.将UV坐标系变成笛卡尔直角坐标系(锚点由左上角变换到中心) 二 ...

  4. ue4材质节点怎么用_ue4材质常用节点

    转自:http://www.unrealchina.net/portal.php?mod=view&aid=233 UE4的材质表面上看起来很简单,可是到了用的时候却总是没有办法实现好的效果. ...

  5. 【UE4 Material】使用材质节点TexCoord偏移、缩放UV

    参考博客 https://www.jianshu.com/p/be4d90163483 https://blog.csdn.net/qq_36282052/article/details/724256 ...

  6. 【UE4】材质编辑器教程笔记整理

    点我进入原教程链接 节点介绍 基本材质节点 材质的基本属性,可以通过更改着色模式切换可用的接口 节点名称 意义 Base Color 纹理 Metallic 金属度,范围0-1 Speculator ...

  7. UE4之材质参数的使用

    在UE4中想要在代码中动态改变材质的某些参数,可通过创建动态材质实例来获取指定材质参数的材质,然后将该材质实例赋给模型,以改变贴图和粗糙度为例 1.在编辑器中新建材质,这里的材质为NewMateria ...

  8. 几种常用的UE4粒子材质制作(雨水,烟雾,闪星)

    通常在UE4种制作粒子特效,有两种表达形体的方式,一种是用材质表达,一种是直接赋予模型. 几种常用又简单的粒子材质制作: 首先将Blend mode改为Translucent模式 1.Lineargr ...

  9. 尝试在UE的材质节点中进行高斯模糊

    0. 高斯模糊基础 高斯模糊的原理已有很多文章介绍.实践上讲,就是对图像做一个卷积(通俗讲,就是采样相邻的几个像素并乘算上对应的权重).我这里想采用的卷积核是5×5的(数据来自于这里的讨论): {0. ...

最新文章

  1. sklearn SVM(支持向量机)模型使用RandomSearchCV获取最优参数及可视化​​​​​​​
  2. C语言常用排序方法大全
  3. ZJOI2019一试翻车记
  4. git schnnel failed to receive handshake, SSLTLS connection failed
  5. 语义分割之Deeplabv3源码解读
  6. c# 以太坊代币_C代币
  7. Linux 系统故障修复和修复技巧
  8. 每天10个Linux命令一
  9. 呆瓜半小时入门python数据分析
  10. UltraEdit配置代码格式化工具astyle
  11. (转载)人工智能在围棋程序中的应用——复旦大学附属中学(施遥)
  12. 如何批量给 Word、Excel、PDF、PPT 文档设置文件打开密码和删除密码
  13. 计算机A级学科排名,“计算机科学与技术”学科排名出炉,上交大无缘A+吉大表现亮眼...
  14. 标准盒子模型和怪异盒子模型
  15. PVID和VID的理解
  16. 人机交互_学习总结(理论部分)
  17. 写给准备參加秋招的学弟学妹们~一定要来看哦~
  18. mezzanine 历险记
  19. 马云:曾去肯德基面试25人就我没被录用 30多年彷徨成就今天
  20. Axure与Mockplus的区别

热门文章

  1. Oracle OAF 学习小结(2)- 增删改查/LOV/Button/Sequence 的完整案例开发
  2. GDOI 2021游记
  3. 数据结构的各种排序算法稳定性比较
  4. for循环语句全示例
  5. R语言系统教程(七):数据的分布(含多种图的绘制)
  6. Python命令行参数解析
  7. div a标签的隐藏/显示
  8. Android单元测试之 Mockito
  9. SpringCloud-gateway全局GlobalFilter获取post的请求参数
  10. ACP云安全知识学习题库