虚幻地形高度图生成及测试

虚幻地形高度图生成及测试

虚幻引擎地形系统将高度数据存储在高度图中,这是一个灰阶图像,使用黑白色值来存储地貌高程。在高度图中,纯黑色值表示最低点,纯白色值表示最高点。支持16位灰阶PNG、8位灰阶r8及16位灰阶r16格式。

本文测试使用开源的opencvcgal库将地形网格体采样插值生成栅格,并写入到16位灰阶PNG图片中,结果可支持导入虚幻UE Landscape

示例代码

cpp 复制代码
#include<iostream>
#include<cmath>

#include<CGAL/Surface_mesh.h>
#include<CGAL/Surface_mesh/IO/PLY.h>

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Projection_traits_xy_3.h>
#include <CGAL/Delaunay_triangulation_2.h>

#include <CGAL/Polygon_mesh_processing/locate.h>

#include <opencv2/opencv.hpp>

using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Projection_traits = CGAL::Projection_traits_xy_3<Kernel>;
using Point_2 = Kernel::Point_2;
using Point_3 = Kernel::Point_3;

// Triangulated Irregular Network
using TIN = CGAL::Delaunay_triangulation_2<Projection_traits>;
using Mesh = CGAL::Surface_mesh<Point_3>;

int main(){

    Mesh mesh;
    CGAL::IO::read_PLY("test.ply",mesh);
   
    CGAL::Bbox_3 bbox = CGAL::bbox_3(mesh.points().begin(), mesh.points().end());
    std::cout << "with:" << bbox.x_span() << "\nheight:" << bbox.y_span() << std::endl;
    int width = std::ceil(bbox.x_span());
    int height = std::ceil(bbox.y_span());
    width = std::max(width, height);
    height = width;
    // 特殊处理,虚幻地形表示的范围为512单位 (可略)
  //  int max_multiple = std::ceil(bbox.zmax() / 512);
  //  int min_multiple = std::ceil(std::abs(bbox.zmin()) / 512);
  //  min_multiple = bbox.zmin() < 0 ? min_multiple : 0;
   int maxHeight = max_multiple * 512;
   int minHeight = min_multiple * 512;

    int maxGray = (1 << 16) - 1;//65535 
    
    cv::Mat image(height, width, CV_16UC1);

    TIN tin (mesh.points().begin(), mesh.points().end());
    TIN::Face_handle location;

    for (std::size_t y = 0; y < height; ++y)
        for (std::size_t x = 0; x < width; ++x)
        {
            Point_3 query(bbox.xmin() + x * (bbox.xmax() - bbox.xmin()) / double(width),
                bbox.ymin() + (height - y) * (bbox.ymax() - bbox.ymin()) / double(height),
                0); // not relevant for location in 2D
            location = tin.locate(query, location);
            // Points outside the convex hull will be colored black
            if (!tin.is_infinite(location))
            {
                std::array<double, 3> barycentric_coordinates
                    = CGAL::Polygon_mesh_processing::barycentric_coordinates
                    (Point_2(location->vertex(0)->point().x(), location->vertex(0)->point().y()),
                        Point_2(location->vertex(1)->point().x(), location->vertex(1)->point().y()),
                        Point_2(location->vertex(2)->point().x(), location->vertex(2)->point().y()),
                        Point_2(query.x(), query.y()),
                        Kernel());
                double height_at_query
                    = (barycentric_coordinates[0] * location->vertex(0)->point().z()
                        + barycentric_coordinates[1] * location->vertex(1)->point().z()
                        + barycentric_coordinates[2] * location->vertex(2)->point().z());
                // 重新映射高度值到0~65535
                double height_ratio = (height_at_query + minHeight) / (maxHeight+minHeight);
                image.at<uint16_t>(y,x)=(uint16_t)(height_ratio* maxGray);
            }
            else {
                image.at<uint16_t>(y, x) = (uint16_t)0;
            }
        }
    cv::imwrite("output.png", image);
    return 0;
}

测试运行

cpp 复制代码
cmake_minimum_required(VERSION 3.1...3.23)
project(main)
# cgal
find_package(CGAL REQUIRED)
# opencv 
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )

create_single_source_cgal_program("main.cpp")
target_link_libraries(main PRIVATE ${OpenCV_LIBS})

编译&构建
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=D:\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake --build build --config Debug

结果

测试地形网格

生成的灰度图

导入虚幻Landscape

参考

  1. https://dev.epicgames.com/documentation/zh-cn/unreal-engine/importing-and-exporting-landscape-heightmaps-in-unreal-engine
  2. https://blog.csdn.net/mrbaolong/article/details/141643001?spm=1001.2014.3001.5501
相关推荐
charon87784 天前
虚幻引擎 | (类恐鬼症)玩家和NPC语音聊天(中)
游戏引擎·虚幻
北冥没有鱼啊5 天前
UE5 贝塞尔曲线导弹
游戏·ue5·游戏引擎·ue4·虚幻
北冥没有鱼啊5 天前
ue5 伤害插件
游戏·ue5·ue4·游戏开发·虚幻
EdSheeran乀6 天前
虚幻中的c++(持续更新)
开发语言·c++·ue5·游戏引擎·ue4·虚幻
qwq!67 天前
虚幻5|使用F插值到,击打敌人使UI血条缓慢缩减|小知识(3)
c++·笔记·ue5·游戏引擎·虚幻
北冥没有鱼啊8 天前
ue5 AI追角色后失去目标解决办法
游戏·ue5·游戏引擎·ue4·虚幻
花生糖@9 天前
使用批处理脚本自动化启动Unreal Engine项目
运维·游戏·自动化·虚幻·bat
charon877811 天前
虚幻引擎VR游戏开发03| 键位映射
游戏引擎·vr·虚幻
吉凶以情迁13 天前
unreal engine骨骼绑定重定向实现自定义人物替换游戏中小白人,但是用小白人或者某超人现有的移动等功能再次折腾笔记...
笔记·游戏·游戏引擎·虚幻·骨骼绑定
charon877814 天前
虚幻引擎VR游戏开发02 | 性能优化设置
游戏·游戏引擎·vr·游戏开发·虚幻·技术美术