🌐
[Unreal Engine 5] 実行時に mesh の頂点座標を変化させる
結論
MyDynamicMeshActor.cpp
#include "MyDynamicMeshActor.h"
#include "DynamicMesh/DynamicMesh3.h"
#include <numbers>
#include <cmath>
namespace {
double PI2 = 2.0 * std::numbers::pi;
const FVector3d vertex[] = { {0,0,0},{0,1,0},{1,0,0},{1,1,0.5} };
const double scale = 200.0;
}
void AMyDynamicMeshActor::BeginPlay()
{
Super::BeginPlay();
auto meshCmp = GetDynamicMeshComponent();
auto mesh = meshCmp->GetMesh();
mesh->AppendVertex(vertex[0] * scale);
mesh->AppendVertex(vertex[1] * scale);
mesh->AppendVertex(vertex[2] * scale);
mesh->AppendVertex(vertex[3] * scale);
mesh->AppendTriangle(0, 1, 2);
mesh->AppendTriangle(3, 2, 1);
meshCmp->NotifyMeshUpdated();
meshCmp->EnableComplexAsSimpleCollision();
meshCmp->UpdateCollision();
}
void AMyDynamicMeshActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
auto meshCmp = GetDynamicMeshComponent();
auto mesh = meshCmp->GetMesh();
double w = 0.5;
phase += (w * DeltaSeconds);
if (phase >= PI2) {
phase -= PI2;
}
auto h1 = (std::sin(phase) + 1.0) * 0.3;
auto h2 = (std::cos(phase) + 1.0) * 0.3;
const FVector3d v1 = { scale, scale, h1*scale };
const FVector3d v2 = { scale, 0, h2*scale };
UE::Geometry::FIndex3i idx = {3,2,1};
mesh->SetVertex(3, v1);
mesh->SetVertex(2, v2);
meshCmp->NotifyMeshUpdated();
meshCmp->UpdateCollision(false);
}
補足
-
BeginPlay()
でSuper::BeginPlay()
を呼ばないとTick()
が呼ばれない -
Tick()
内のUpdateCollision()
は引数false
が必要
Discussion