LoginSignup
4
5

More than 3 years have passed since last update.

はじめに

こんにちは。UE4超初心者(UE4歴、数十日)のがっちょです。

今回は、新卒の就活作品用に作った
C++地形生成ライブラリを使用して、UE4上に孤島を生成します。

BP


完成した作品

>> Unreal Engine 4 Viewer Download [Windows/.exe/(x86/x64)] 📥

孤島を駆け巡れます。


クラスの概要

>> dtl::shape::PerlinIsland (形状クラス)


導入方法

0. 前準備を行う

>> クラス in Unreal Engine

上記のリンクに飛び、DTLのダウンロードから(プロジェクト名)Build.csの追加記述を行う。


1. BlueprintFunctionLibraryを継承したC++クラスを作成

PerlinIsland.h

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "PerlinIsland.generated.h"

UCLASS()
class (プロジェクト名マクロ)_API UPerlinIsland : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

        UFUNCTION(BlueprintCallable, Category = DTL, meta = (HidePin = "worldContextObject_", DefaultToSelf = "worldContextObject_"))
        static bool perlinIsland(const UObject* worldContextObject_, float frequency, int32 octaves, int32 max_value, int32 min_value);
};

(プロジェクト名マクロ)_API の部分は適宜変更する。

PerlinIsland.cpp

#include "PerlinIsland.h"

#include "EngineUtils.h"
#include "Landscape.h"
#include "LandscapeInfo.h"
#include "LandscapeEditor/Public/LandscapeEditorUtils.h"

#include <DTL.hpp>

bool UPerlinIsland::perlinIsland(const UObject* worldContextObject_, float frequency, int32 octaves, int32 max_value, int32 min_value) {

    dtl::shape::PerlinIsland<uint16> generator(
        static_cast<double>(frequency),
        static_cast<size_t>(octaves),
        static_cast<uint16>(max_value),
        static_cast<uint16>(min_value));

    ::UWorld* world{ worldContextObject_->GetWorld() };
    for (TActorIterator<ALandscape> actorItr(world); actorItr; ++actorItr) {
        ::ALandscape* landscape{ *actorItr };
        if (landscape == nullptr) continue;
        ::ULandscapeInfo::RecreateLandscapeInfo(world, false);

        ::FIntRect rect{ landscape->GetBoundingRect() };
        const int32 w{ rect.Width() + 1 };
        const int32 h{ rect.Height() + 1 };

        ::TArray<uint16> Data;
        Data.Init(0, w * h);
        generator.draw(Data, w, h);
        LandscapeEditorUtils::SetHeightmapData(landscape, Data);
        return true;
    }
    return false;
}

2.Actorを継承したブループリントクラスを作成

BP

4つの変数を定義する。

変数名 デフォルト値
frequency Float 2.0
octaves Integer 10
max value Integer 20000
min value Integer 0

BP

EventPerlinIslandは エディタで呼び出す(Call in Editor) にチェックを入れる。

BP

今回定義する変数は インスタンス編集可能(Instance Editable)スポーン時に公開(Expose on Spawn) にチェックを入れる。

BP


3. 完成

作成したActorをテキトーにレベルに配置する。
その後に Landscape を作成する。

View


4. より詳しい解説記事

1~3を見てもよくわからなかったひとは

dtl::shape::PerlinIsland in Unreal Engine (形状クラス) 詳しい解説 のページを参考にしてください。


さいごに

今回はUE4のランドスケープ機能をC++コードから呼び出して地形生成を行いました。
もう少し工夫すればより多く、バリエーション豊かな地形が創れそうですね。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5