ue4讀取灰度圖生成三維地形mesh

轉自:http://www.javashuo.com/article/p-grjnrhmv-mv.htmlhtml

新建ue c++工程。
在Build.cs中添加"ProceduralMeshComponent"模塊。
在 uproject中添加"ProceduralMeshComponent"模塊。
建立材質,傳入grass貼圖
導入灰度圖資源
建立繼承自Actor的類 ATerrainCreateActor,並建立藍圖類對象
將藍圖對象拖入場景,設置其灰度貼圖參數、Z值縮放比例參數、材質參數
最終效果
ATerrainCreateActor類代碼以下
 
頭文件

 

#pragma once
 
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "TerrainCreateActor.generated.h"
 
UCLASS()
class UETERRAIN_API ATerrainCreateActor : public AActor
{
    GENERATED_BODY()
     
public: 
    ATerrainCreateActor();
private:
    UPROPERTY(VisibleAnywhere)
        UProceduralMeshComponent * mesh;//自定義mesh
    UPROPERTY(EditAnywhere)
        UTexture2D * grayTexture;//傳入灰度圖
    UPROPERTY(EditAnywhere)
        float zScale;//z值係數
    UPROPERTY(EditAnywhere)
        UMaterial* meshMat;//材質
 
protected:
      
    virtual void BeginPlay() override; 
public:
      
    virtual void Tick(float DeltaTime) override;
};

源文件c++

#include "TerrainCreateActor.h"
  
ATerrainCreateActor::ATerrainCreateActor()
{
    PrimaryActorTick.bCanEverTick = true; 
    mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("terrainMesh"));
    RootComponent = mesh;
    mesh->bUseAsyncCooking = true;
}
  
void ATerrainCreateActor::BeginPlay()
{
    Super::BeginPlay();
 
    //讀取灰度圖像素信息
    FTexture2DMipMap* MyMipMap = &grayTexture->PlatformData->Mips[0];
    FByteBulkData* RawImageData = &MyMipMap->BulkData;
    FColor* FormatedImageData = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));
    uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
    //mesh基礎信息
    TArray<FVector> vertices;
    TArray<int32> Triangles;
    TArray<FVector> normals;
    TArray<FVector2D> UV0;
    TArray<FProcMeshTangent> tangents;
    TArray<FLinearColor> vertexColors;
    for (size_t i = 0; i < TextureWidth; i++)
    {
        for (size_t j = 0; j < TextureHeight; j++)
        {
            //根據顏色設定頂點z值
            FColor PixelColor = FormatedImageData[j * TextureWidth + i];
            float tempZ = (PixelColor .B* 299 + PixelColor .G* 587 + PixelColor.R * 114 + 500) / 1000;//rgb轉灰度
            tempZ *= zScale;
            vertices.Add(FVector(i*5, j*5, tempZ));  //頂點
            normals.Add(FVector(0, 0, 1));//法線
            UV0.Add(FVector2D((float)i/(float)TextureWidth, (float)j/(float)TextureHeight));//uv
            //UV0.Add(FVector2D(i,j));//uv
            tangents.Add(FProcMeshTangent(1, 0, 0));//切線
            vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0)); //頂點顏色
 
            if (j < TextureHeight - 1 && i < TextureWidth - 1)
            {
                //三角索引  此處按照vertice的添加順序肯定索引
                Triangles.Add(i*TextureHeight + j);
                Triangles.Add(i*TextureHeight + j + 1);
                Triangles.Add(i*TextureHeight + j + TextureHeight);
 
                Triangles.Add(i*TextureHeight + j + TextureHeight);
                Triangles.Add(i*TextureHeight + j + 1);
                Triangles.Add(i*TextureHeight + j + TextureHeight + 1);
            }
        }
    }
      
 
    RawImageData->Unlock(); 
 
    //建立mesh
    mesh->CreateMeshSection_LinearColor(0, vertices, Triangles, normals, UV0, vertexColors, tangents, true);
    mesh->ContainsPhysicsTriMeshData(true);
    mesh->SetMaterial(0, meshMat);
}
  
void ATerrainCreateActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}
相關文章
相關標籤/搜索