Skip to content

Commit 04d6e5b

Browse files
committed
map LOD mesh glTF export
1 parent aa7cc01 commit 04d6e5b

12 files changed

Lines changed: 413 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ During CMake build, the following additional libraries will be fetched and inclu
4545
- [imgui_club](https://github.com/ocornut/imgui_club.git)
4646
- [ImPlot3D](https://github.com/brenocq/implot3d.git)
4747
- [FreeType](https://github.com/freetype/freetype.git)
48+
- [FP16](https://github.com/Maratyszcza/FP16.git)
4849
- [GLFW](https://github.com/glfw/glfw.git)
4950
- [GLM](https://github.com/g-truc/glm.git)
5051
- [glOwl](https://github.com/invor/glowl.git)

cmake/deps/fp16.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# fp16
2+
include_guard(GLOBAL)
3+
4+
FetchContent_Declare(fp16
5+
URL "https://github.com/Maratyszcza/FP16/archive/3d2de1816307bac63c16a297e8c4dc501b4076df.tar.gz"
6+
URL_HASH SHA256=65ace2f05fd9434b0acb7a7d3cc6cd96842ea6236b680594af932b359bedbfc1
7+
DOWNLOAD_NO_PROGRESS ON
8+
EXCLUDE_FROM_ALL
9+
SYSTEM)
10+
message(STATUS "Fetch fp16 ...")
11+
OPTION(FP16_BUILD_TESTS "" OFF)
12+
OPTION(FP16_BUILD_BENCHMARKS "" OFF)
13+
OPTION(FP16_INSTALL_LIBRARY "" OFF)
14+
FetchContent_MakeAvailable(fp16)
15+
mark_as_advanced(FORCE
16+
FETCHCONTENT_SOURCE_DIR_FP16
17+
FETCHCONTENT_UPDATES_DISCONNECTED_FP16
18+
FP16_BUILD_BENCHMARKS
19+
FP16_BUILD_COMPARATIVE_BENCHMARKS
20+
FP16_BUILD_TESTS
21+
FP16_INSTALL_LIBRARY)
22+
register_copyright(fp16 "FP16" "${fp16_SOURCE_DIR}/LICENSE")

cmake/deps/tinygltf.cmake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
include_guard(GLOBAL)
33

44
find_package(nlohmann_json REQUIRED)
5+
find_package(zlib REQUIRED)
56

67
FetchContent_Declare(tinygltf
78
URL "https://github.com/syoyo/tinygltf/archive/v2.9.6.tar.gz"
@@ -12,12 +13,14 @@ FetchContent_Declare(tinygltf
1213
SYSTEM)
1314
message(STATUS "Fetch tinygltf ...")
1415
FetchContent_MakeAvailable(tinygltf)
15-
# Build a custom tinygltf version without using the internal json.hpp. Link our json target instead.
16-
# Further, the current tinygltf CMake does not work well with add_subdirectory and building as static library.
16+
# Build a custom tinygltf version:
17+
# - Replace internal json.hpp with our json target.
18+
# - Use zlib for stb_image_write png compression
19+
# - The current tinygltf CMake does not work well with add_subdirectory and building as static library.
1720
file(COPY ${tinygltf_SOURCE_DIR}/stb_image.h DESTINATION ${tinygltf_BINARY_DIR}/src/include)
1821
file(COPY ${tinygltf_SOURCE_DIR}/stb_image_write.h DESTINATION ${tinygltf_BINARY_DIR}/src/include)
1922
file(COPY ${tinygltf_SOURCE_DIR}/tiny_gltf.h DESTINATION ${tinygltf_BINARY_DIR}/src/include)
20-
file(COPY ${tinygltf_SOURCE_DIR}/tiny_gltf.cc DESTINATION ${tinygltf_BINARY_DIR}/src/src)
23+
file(COPY ${CMAKE_SOURCE_DIR}/cmake/deps/tinygltf/tiny_gltf.cc DESTINATION ${tinygltf_BINARY_DIR}/src/src)
2124
file(COPY ${CMAKE_SOURCE_DIR}/cmake/deps/tinygltf/CMakeLists.txt DESTINATION ${tinygltf_BINARY_DIR}/src)
2225
add_subdirectory(${tinygltf_BINARY_DIR}/src ${tinygltf_BINARY_DIR}/build EXCLUDE_FROM_ALL SYSTEM)
2326
set_target_properties(tinygltf PROPERTIES FOLDER libs)

cmake/deps/tinygltf/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ target_include_directories(tinygltf PUBLIC
99
target_compile_definitions(tinygltf PRIVATE TINYGLTF_USE_CPP14)
1010
set_target_properties(tinygltf PROPERTIES
1111
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
12-
target_link_libraries(tinygltf PRIVATE nlohmann_json)
12+
target_link_libraries(tinygltf PRIVATE nlohmann_json ZLIB::ZLIBSTATIC)
1313

1414
# tinygltf includes just "json.hpp", but nlohmann_json CMake assumes "nlohmann/json.hpp" as include.
1515
# Modify include path to fix this.

cmake/deps/tinygltf/tiny_gltf.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <cstdlib>
2+
3+
#include <zlib.h>
4+
5+
static unsigned char* custom_stbiw_zlib_compress(unsigned char* data, int data_len, int* out_len, int quality) {
6+
uLong size = compressBound(static_cast<uLong>(data_len));
7+
unsigned char* buffer = static_cast<unsigned char*>(::malloc(size));
8+
if (buffer == nullptr) {
9+
return nullptr;
10+
}
11+
int state = ::compress(reinterpret_cast<Bytef*>(buffer), &size, reinterpret_cast<const Bytef*>(data),
12+
static_cast<uLong>(data_len));
13+
if (state != Z_OK) {
14+
::free(buffer);
15+
return nullptr;
16+
}
17+
*out_len = static_cast<int>(size);
18+
return buffer;
19+
}
20+
21+
#define STBIW_ZLIB_COMPRESS custom_stbiw_zlib_compress
22+
23+
#define TINYGLTF_IMPLEMENTATION
24+
#define STB_IMAGE_IMPLEMENTATION
25+
#define STB_IMAGE_WRITE_IMPLEMENTATION
26+
#include "tiny_gltf.h"

map/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ find_package(freetype REQUIRED)
1010
find_package(imgui REQUIRED)
1111
find_package(imguiclub REQUIRED)
1212
find_package(implot3d REQUIRED)
13+
find_package(fp16 REQUIRED)
1314
find_package(nlohmann_json REQUIRED)
1415
find_package(TinyGLTF REQUIRED)
1516
find_package(glowl REQUIRED)
@@ -71,6 +72,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
7172
imgui
7273
imguiclub
7374
implot3d
75+
fp16
7476
nlohmann_json::nlohmann_json
7577
tinygltf::tinygltf
7678
glowl::glowl
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#version 450
2+
3+
layout(local_size_x = 16, local_size_y = 16) in;
4+
5+
layout(binding = 0) uniform sampler2D srcTex;
6+
layout(binding = 1, rgba8) uniform writeonly image2D dstImg;
7+
8+
uniform int width;
9+
uniform int height;
10+
11+
float sRGB(float val) {
12+
if (val <= 0.0031308) {
13+
return 12.92 * val;
14+
} else {
15+
return 1.055 * pow(val, 1.0 / 2.4) - 0.055;
16+
}
17+
}
18+
19+
void main() {
20+
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
21+
if (coord.x >= width || coord.y >= height) {
22+
return;
23+
}
24+
vec4 color = texelFetch(srcTex, coord, 0);
25+
color.r = sRGB(color.r);
26+
color.g = sRGB(color.g);
27+
color.b = sRGB(color.b);
28+
imageStore(dstImg, coord, color);
29+
}

map/src/MapWindow/MapWindow.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Utils/FileDialogUtil.h"
2323
#include "Utils/GLMUtil.h"
2424
#include "Utils/ResourceUtils.h"
25+
#include "World/MapLODglTFWriter.h"
2526

2627
Satisfactory3DMap::MapWindow::MapWindow()
2728
: BaseWindow("Satisfactory3DMap"),
@@ -243,8 +244,15 @@ void Satisfactory3DMap::MapWindow::renderGui() {
243244
if (ImGui::BeginMenu("Tools")) {
244245
ImGui::MenuItem("Pak Explorer", nullptr, &pakExplorer_->show(), dataView_->pakManager() != nullptr);
245246
ImGui::Separator();
247+
if (ImGui::MenuItem("Export Map LOD", nullptr, false, dataView_->pakManager() != nullptr)) {
248+
const auto file = saveFile("Select file ...", "satisfactory-map.glb");
249+
if (file.has_value()) {
250+
MapLODglTFWriter writer(dataView_->pakManager());
251+
writer.save(file.value());
252+
}
253+
}
246254
if (ImGui::MenuItem("Export Save Text")) {
247-
auto file = saveFile("Select file ...", "savegame-export.txt");
255+
const auto file = saveFile("Select file ...", "savegame-export.txt");
248256
if (file.has_value()) {
249257
saveToTextFile(*dataView_->saveGame(), file.value());
250258
}

map/src/MapWindow/World/MapLODReader.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ Satisfactory3DMap::MapLODReader::MapLODReader(const std::shared_ptr<Satisfactory
4141
throw std::runtime_error("InstanceComponents array missing!");
4242
}
4343

44-
for (const auto& instCompRef : instCompArray->Values) {
44+
for (std::size_t instCompIdx = 0; instCompIdx < instCompArray->Values.size(); instCompIdx++) {
45+
const auto& instCompRef = instCompArray->Values[instCompIdx];
4546
if (instCompRef.pakValue() <= 0) {
4647
continue;
4748
}
@@ -58,6 +59,9 @@ Satisfactory3DMap::MapLODReader::MapLODReader(const std::shared_ptr<Satisfactory
5859
}
5960

6061
MapLODMesh LOD_mesh;
62+
LOD_mesh.actorLabel = WPHLOD_obj->ActorLabel;
63+
LOD_mesh.instanceComponentId = instCompIdx;
64+
6165
LOD_mesh.staticMesh = meshObj;
6266

6367
const auto& matProp = meshObj->Properties.get<s::ArrayProperty>("StaticMaterials");
@@ -110,7 +114,7 @@ Satisfactory3DMap::MapLODReader::MapLODReader(const std::shared_ptr<Satisfactory
110114
}
111115
}
112116
} catch (const std::exception& ex) {
113-
spdlog::warn("Error loading Pak file: {}", ex.what());
117+
spdlog::warn("Error reading Map LOD from Pak file: {}", ex.what());
114118
}
115119
}
116120
}

map/src/MapWindow/World/MapLODReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace Satisfactory3DMap {
1515
std::shared_ptr<s::UStaticMesh> staticMesh;
1616
std::shared_ptr<s::UTexture2D> baseColorTexture;
1717
std::shared_ptr<s::UTexture2D> normalTexture;
18+
std::string actorLabel;
19+
std::size_t instanceComponentId;
1820
};
1921

2022
explicit MapLODReader(const std::shared_ptr<SatisfactorySave::PakManager>& pakManager);

0 commit comments

Comments
 (0)