GS DynamicMeshActor

GS DynamicMeshActor

GeometryScript DynamicMeshActor。

ADynamicMeshActor与AStaticMeshActor类似之处在于,它主要是UDynamicMeshComponent的容器。但是,DynamicMeshActor确实为想要基于UDynamicMesh实现程序化网格体生成的Actor蓝图提供了一些特定支持。

之前只能创建新StaticMesh资产的建模模式工具现在还可以创建新DynamicMeshActor,并且编辑工具可以编辑任一类型的网格体Actor。

UE5 程序化生成相关的DynamicMesh解析。

ADynamicMeshActor is an Actor that has a USimpleDynamicMeshComponent as it’s RootObject.

在这里插入图片描述

AActor
ADynamicMeshActor
UDynamicMeshComponent
UBaseDynamicMeshComponent
UMeshComponent
UPrimitiveComponent
USceneComponent
UActorComponent
UDynamicMesh
FDynamicMesh3

FDynamicMesh3 is a dynamic triangle mesh class. The mesh has has connectivity,
is an indexed mesh, and allows for gaps in the index space.

FDynamicMesh3 是一个动态三角形网格类。网格具有连接性,是一个索引网格,除了保存基本的几何数据,还保存一些拓扑连接关系,便于快速查询等等相关几何操作。

Engine\Source\Runtime\GeometryFramework\Public\DynamicMeshActor.h

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

#pragma once

#include "CoreMinimal.h"
#include "UDynamicMesh.h"
#include "Components/DynamicMeshComponent.h"

#include "DynamicMeshActor.generated.h"


/**
 * ADynamicMeshActor is an Actor that has a USimpleDynamicMeshComponent as it's RootObject.
 */
UCLASS(ConversionRoot, ComponentWrapperClass, ClassGroup=DynamicMesh, meta = (ChildCanTick))
class GEOMETRYFRAMEWORK_API ADynamicMeshActor : public AActor
{
    
    
	GENERATED_UCLASS_BODY()

protected:
	UPROPERTY(Category = DynamicMeshActor, VisibleAnywhere, BlueprintReadOnly, meta = (ExposeFunctionCategories = "Mesh,Rendering,Physics,Components|StaticMesh", AllowPrivateAccess = "true"))
	TObjectPtr<class UDynamicMeshComponent> DynamicMeshComponent;

public:
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	UDynamicMeshComponent* GetDynamicMeshComponent() const {
    
     return DynamicMeshComponent; }



	//
	// Mesh Pool support. Meshes can be locally allocated from the Mesh Pool
	// in Blueprints, and then released back to the Pool and re-used. This
	// avoids generating temporary UDynamicMesh instances that need to be
	// garbage-collected. See UDynamicMeshPool for more details.
	//

public:
	/** Control whether the DynamicMeshPool will be created when requested via GetComputeMeshPool() */
	UPROPERTY(Category = DynamicMeshActor, EditAnywhere, BlueprintReadWrite)
	bool bEnableComputeMeshPool = true;
protected:
	/** The internal Mesh Pool, for use in DynamicMeshActor BPs. Use GetComputeMeshPool() to access this, as it will only be created on-demand if bEnableComputeMeshPool = true */
	UPROPERTY(Transient)
	TObjectPtr<UDynamicMeshPool> DynamicMeshPool;

public:
	/** Access the compute mesh pool */
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	UDynamicMeshPool* GetComputeMeshPool();

	/** Request a compute mesh from the Pool, which will return a previously-allocated mesh or add and return a new one. If the Pool is disabled, a new UDynamicMesh will be allocated and returned. */
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	UDynamicMesh* AllocateComputeMesh();

	/** Release a compute mesh back to the Pool */
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	bool ReleaseComputeMesh(UDynamicMesh* Mesh);

	/** Release all compute meshes that the Pool has allocated */
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	void ReleaseAllComputeMeshes();

	/** Release all compute meshes that the Pool has allocated, and then release them from the Pool, so that they will be garbage-collected */
	UFUNCTION(BlueprintCallable, Category = DynamicMeshActor)
	void FreeAllComputeMeshes();
};



UE5 AGeneratedDynamicMeshActor 对象

首先开启Geometry Script 插件,才可以使用GeneratedDynamicMeshActor动态生成Mesh。

AActor
ADynamicMeshActor
GeneratedDynamicMeshActor

AGeneratedDynamicMeshActor is an Editor-only subclass of ADynamicMeshActor that provides special support for dynamic procedural generation of meshes in the Editor, eg via Blueprints. Expensive procedural generation implemented via BP can potentially cause major problems in the Editor, in particular with interactive performance. AGeneratedDynamicMeshActor provides special infrastructure for this use case. Essentially, instead of doing procedural generation in the Construction Script, a BP-implementable event OnRebuildGeneratedMesh is available,and doing the procedural mesh regeneration when that function fires will generally provide better in-Editor interactive performance.

AGeneratedDynamicMeshActor 是 ADynamicMeshActor 的子类,仅在Editor模式下使用,ADynamicMeshActor用途是对Editor模式下动态程序化生成网格体提供了支持,例如使用蓝图。在Editor编辑模式下通过蓝图大量动态程序化生成网格体可能会在交互式性能方面产生很大的问题。AGeneratedDynamicMeshActor针对这种问题提供专门的解决方案。本质上,在Editor编辑模式下进行交互式生成网格体,OnRebuildGeneratedMesh基于蓝图实现的事件中进行程序化生成要比在蓝图Construction Script进行程序化生成有更好的效果。

示例

程序化生成一个Box
在这里插入图片描述
拖动右侧Box Size图标可动态缩放Box
在这里插入图片描述
添加一个Box与第一Box进行Bool运算substract
在这里插入图片描述

在这里插入图片描述

GS Function

提供的相关的蓝图函数
UGeometryScriptLibrary_MeshPrimitiveFunctions::AppendSimpleExtrudePolygon
多边形挤出
UGeometryScriptLibrary_MeshSpatial::BuildBVHForMesh
BVH空间索引
FGeometryScriptDynamicMeshBVH->FindNearestPointOnMesh
使用BVH空间索引查找网格体上最近的点

自定义DynamicMeshActor

自定义Runtime运行时可用的DynamicMeshActor对象。

TObjectPtr<UDynamicMeshPool> DynamicMeshPool = NewObject<UDynamicMeshPool>();
UDynamicMesh* OutMesh = DynamicMeshPool->RequestMesh();
OutMesh->EditMesh([&](FDynamicMesh3& EditMesh)
		{
    
    
			FVector newVertex;
			FIndex3i newTriangle;
			int32 ConstantGroupID = EditMesh.AllocateTriangleGroup();

			for (size_t i = 0; i < VertexCount; i++)
			{
    
    
				newVertex.X;
				newVertex.Y;
				newVertex.Z;
				EditMesh.AppendVertex(newVertex);
			}

			for (size_t i =0; i < TriangleCount; i++)
			{
    
    
				newTriangle.A ;
				newTriangle.B;
				newTriangle.C;

				EditMesh.AppendTriangle(newTriangle, ConstantGroupID);
			}

		}, EDynamicMeshChangeType::GeneralEdit, EDynamicMeshAttributeChangeFlags::Unknown, false);

AActor* Actor = Cast<AActor>(GetWorld()->SpawnActor(AActor::StaticClass()));
UDynamicMeshComponent* DMCom = NewObject<UDynamicMeshComponent>(Actor);
DMCom->RegisterComponent();
DMCom->SetMesh(MoveTemp(OutMesh->GetMeshRef()));
Actor->SetRootComponent(DMCom);
Actor->SetActorLabel(TEXT("F6"));

DynamicMeshPool->ReturnMesh(OutMesh);

在这里插入图片描述

参考

  1. https://docs.unrealengine.com/5.0/zh-CN/geometry-script-users-guide/
  2. https://blog.csdn.net/mrbaolong/article/details/131543522?spm=1001.2014.3001.5501

猜你喜欢

转载自blog.csdn.net/mrbaolong/article/details/131543522
gs