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;

};

五、效果展示

效果图是上面完整代码的结果
正面
在这里插入图片描述

反面
在这里插入图片描述
我总共生成了三个网格体,正面组成正方形,反面只有一个三角形,网格体只有一面可以被看到。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值