ue4中使用c++实现自定义网格体

这篇具有很好参考价值的文章主要介绍了ue4中使用c++实现自定义网格体。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • 有两个类可以在ue4中实现自定义网格体,分别是UCustomMeshComponent和UProceduralMeshComponent,实现的方法都是构建三角形以实现不同的网格体。
  • 网上的教程以蓝图为主,但我想用c++实现。
    我找到了一篇用UProceduralMeshComponent实现的博客,链接如下:https://blog.csdn.net/yb0022/article/details/103034588
  • 本篇博客换一种方法,用UCustomMeshComponent实现自定义网格体。因为是例子,命名都挺不讲究的。

一、整体流程

步骤一:创建actor的c++类
步骤二:引入CustomMeshComponent组件
步骤三:给actor添加CustomMeshComponent组件
步骤四:调用生成网格体的api

步骤一就正常创建即可,不再赘述。说明一下文件的名字,项目叫custommesh,添加的actor叫MyActor。

二、引入ue4内置插件CustomMeshComponent

1、修改cs文件

custommesh.Build.cs

// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class custommesh : ModuleRules
{
	public custommesh(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" ,  "CustomMeshComponent" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

直接在对应位置添加模块名即可

2、添加头文件

MyActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CustomMeshComponent.h" //注意要加载generated.h上面
#include "MyActor.generated.h"

三、插件使用

1、给actor添加CustomMeshComponent组件


	UFUNCTION(BlueprintCallable, Category = "Custom")  //让函数蓝图中可以调用,这个程序中是在构造函数调用的,其实没用上蓝图
	void custom();//生成网格体函数
	
	UPROPERTY(VisibleAnywhere)
	class UCustomMeshComponent* MeshComponent;  //组件实例

2、生成mesh

	TArray<FCustomMeshTriangle> vertices;
	FCustomMeshTriangle mesh_node1;
	mesh_node1.Vertex0 = FVector(0, 0, 0);   //注意把这几个点设置大一点。我开始设置的值太小,生成后招不到,浪费不少时间
	mesh_node1.Vertex1 = FVector(0, 100, 0);
	mesh_node1.Vertex2 = FVector(100, 0, 0);
	vertices.Add(mesh_node1);

	MeshComponent->AddCustomMeshTriangles(vertices);  //关键就是调用这个函数,根据一组或若干组的三个点生成网格体

四、完整代码

  • 我是先按照用UProceduralMeshComponent实现的那篇实现了一次,所以代码中UProceduralMeshComponent和UCustomMeshComponent两种实现都有。
    custommesh.Build.cs
// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class custommesh : ModuleRules
{
	public custommesh(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" , "ProceduralMeshComponent", "CustomMeshComponent" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

MyActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyActor.h"


// Sets default values
AMyActor::AMyActor()
{
	PrimaryActorTick.bCanEverTick = true;
	//DrawMeshComponent = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("DrawMeshComponent"));
	//DrawMeshComponent->SetupAttachment(RootComponent);
	//DrawTriangle();
	MeshComponent = CreateDefaultSubobject<UCustomMeshComponent>(TEXT("CustomMeshComponent"));
	MeshComponent->SetupAttachment(RootComponent);
	custom();
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


void AMyActor::DrawTriangle(UMaterialInterface* InMaterial)
{

	TArray<FVector> vertices;
	TArray<int32> triangles;
	vertices.Add(FVector(0, 0, 0));
	vertices.Add(FVector(0, 100, 0));
	vertices.Add(FVector(100, 0, 0));
	triangles.Add(0);
	triangles.Add(1);
	triangles.Add(2);

	TArray<FVector> normals;
	TArray<FProcMeshTangent> tangents;
	TArray<FLinearColor> vertexColors;
	TArray<FVector2D> uvs;
	DrawMeshComponent->CreateMeshSection_LinearColor(0, vertices, triangles, normals, uvs, vertexColors, tangents, true);
	//static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialAsset(TEXT("/Game/StarterContent/Materials/M_Basic_Wall"));
	//DrawMeshComponent->SetMaterial(0, MaterialAsset);

	//UE_LOG(LogTemp, Warning, TEXT("1234"));

}



void AMyActor::custom() 
{
	TArray<FCustomMeshTriangle> vertices;
	FCustomMeshTriangle qwe;
	qwe.Vertex0 = FVector(0, 0, 0);
	qwe.Vertex1 = FVector(0, 100, 0);
	qwe.Vertex2 = FVector(100, 0, 0);
	vertices.Add(qwe);
	FCustomMeshTriangle asd;
	asd.Vertex0 = FVector(0, 100, 0);
	asd.Vertex1 = FVector(100, 100, 0);
	asd.Vertex2 = FVector(100, 0, 0);
	vertices.Add(asd);
	FCustomMeshTriangle zxc;
	zxc.Vertex0 = FVector(110, 0, 0);
	zxc.Vertex1 = FVector(0, 100, 0);
	zxc.Vertex2 = FVector(0, 0, 0);
	vertices.Add(zxc);

	MeshComponent->AddCustomMeshTriangles(vertices);
}

MyActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include"ProceduralMeshComponent.h"
#include "CustomMeshComponent.h"
#include "MyActor.generated.h"




UCLASS()
class CUSTOMMESH_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();
	UFUNCTION(BlueprintCallable, Category = "MyActorFonctionFromC")
	void DrawTriangle(UMaterialInterface* InMaterial);
	UFUNCTION(BlueprintCallable, Category = "Custom")
	void custom();

	UPROPERTY(VisibleAnywhere)
	class UProceduralMeshComponent* DrawMeshComponent;
	UPROPERTY(VisibleAnywhere)
	class UCustomMeshComponent* MeshComponent;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

五、效果展示

效果图是上面完整代码的结果
正面
ue4 程序化网格体,c++,ue4,开发语言

反面
ue4 程序化网格体,c++,ue4,开发语言
我总共生成了三个网格体,正面组成正方形,反面只有一个三角形,网格体只有一面可以被看到。文章来源地址https://www.toymoban.com/news/detail-728892.html

到了这里,关于ue4中使用c++实现自定义网格体的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【用unity实现100个游戏之16】Unity中程序化生成的2D地牢5(附项目源码,完结)

    【视频】:https://www.youtube.com/watch?app=desktopv=-QOCX6SVFsklist=PLcRSafycjWFenI87z7uZHFv6cUG2Tzu9vindex=1 注意 :本文为学习笔记记录,推荐支持原作者,去看原视频自己手敲代码理解更加深入

    2024年02月03日
    浏览(36)
  • 什么是程序化交易

    大到量化、程序化、高频交易、套利交易、主观投资这些基本的概念,小到网格交易、条件单、T+0、ETF套利、期现套利、算法拆单交易、打板策略等具体的投资方式。如果没有接触过这些,很容易混淆。 程序化交易: 指通过既定程序或特定软件,自动生成或执行交易指令的

    2024年02月06日
    浏览(40)
  • 【毕业设计】基于程序化生成和音频检测的生态仿真与3D内容生成系统----程序化生成地形算法设计

    Input: Output: 程序化生成地形算法是一种在计算机中生成地形的方法,通常用于游戏开发和虚拟现实应用。下面是几种常见的程序化生成地形算法: Diamond-Square Algorithm(钻石-正方形算法) 该算法通过随机值填充网格的四个角落,然后计算中间点的高度值,不断重复直到整个网

    2024年02月01日
    浏览(33)
  • 基于URP的程序化天空盒

    参考来源:   天空盒教程第 1 部分 |开尔文·范·霍恩 (kelvinvanhoorn.com) 【程序化天空盒】过程记录02:云扰动 边缘光 消散效果_九九345的博客-CSDN博客 程序化天空盒实现昼夜变换 - 知乎 (zhihu.com) 一、太阳          目标:改变光的方向,使天空球旋转(日夜交替);光的正方

    2024年02月15日
    浏览(49)
  • 【UE4 C++】根据指定路径生成静态网格体

    在上一篇博客中(【UE C++】蓝图调用C++函数),我们用C++创建了一个蓝图函数库,本篇文章在这个蓝图函数库基础上增加一个方法,该方法只需输入一个文件目录路径,就可在场景中生成该目录下得所有静态网格体。(如果不想写C++的话,可以用这篇文章的方法:UE4 | BP | 使

    2024年02月15日
    浏览(64)
  • 分享股票量化交易程序化模型的设计思路

    一个股票量化交易程序化模型的入市设计往往伴随着设计者的偏好和交易时间框架等。主要分为震荡交易、套利交易以及趋势跟踪等。当然在近些年的发展中,也出现了类似遗传算法、人工智能神经网络等许多种类的系统模型。 但是对于大多数投资者来说,趋势跟踪系统可以

    2024年02月03日
    浏览(42)
  • 程序化交易接口策略过滤器–九宫格

    不同的程序化交易接口策略适用于不同的市场情况,有些交易策略使用于均值回归,有些则试用于方向明显的时候,有些试用于方向不明显的时候,因此,我们需要根据不同的市场情况,综合考虑方向和波动率,市场成交量来选择合适的交易策略。 本文介绍了一种选择程序化

    2023年04月09日
    浏览(33)
  • CityGML程序化建模开源引擎及数据集

    在攻读博士学位期间,我在 3D GIS 研究中遇到了以下缺点: 包含多个细节级别的 CityGML 数据集很少。 不存在程序化生成的 CityGML 格式的数据。 没有免费的程序化建模引擎。 公开可用的 CityGML 模型通常包含大量(拓扑)错误。 推荐:用 NSDT编辑器 快速搭建可编程3D场景 为了解

    2024年02月13日
    浏览(26)
  • Three.js程序化3D城市建模【OpenStreetMap】

    对于我在 Howest 的研究项目,我决定构建一个 3D 版本的 Lucas Bebber 的“交互式讲故事的动画地图路径”项目。 我将使用 OSM 中的矢量轮廓来挤出建筑物的形状并将它们添加到 3js 场景中,随后我将对其进行动画处理 推荐:用 NSDT编辑器 快速搭建可编程3D场景 为了使用 Node 和

    2024年02月11日
    浏览(30)
  • 【程序化天空盒】过程记录01:日月 天空渐变 大气散射

    昼夜的话肯定少不了太阳和月亮,太阳和月亮实现的道理是一样的,只不过是月亮比太阳多了一个需要控制月牙程度(or添加贴图)的细节~ 太阳的话很简单,直接在shader里实现一个太阳跟随平行光旋转而旋转的样子就行。实现这个效果需要用到Unity内置变量 _WorldSpaceLightPos0

    2024年01月18日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包