2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-11-22 21:02:07 +00:00

Another refactor to clean up comments and code

This commit is contained in:
LJW-Dev
2025-10-08 21:09:15 +08:00
parent 94ccd961cb
commit 0662f2adbe
13 changed files with 70 additions and 2981 deletions

View File

@@ -5,7 +5,7 @@ Heavily modified version of https://github.com/sudeshnapal12/Space-Partitioning-
Credit to sudeshnapal12
Precalculated, evenly sized BSPs are much more efficient and smaller compared to dynamically creating them
BSP leaf sizes are precalculated, evenly sized BSPs are much more efficient and smaller compared to dynamically creating them
*/
#include <algorithm>
@@ -16,7 +16,7 @@ Precalculated, evenly sized BSPs are much more efficient and smaller compared to
#include <sstream>
#include <vector>
#include "CustomMapConsts.h"
#define MAX_AABB_SIZE 512 // maximum size a BSP node can be before it becomes a leaf
enum PlaneAxis
{

View File

@@ -1,46 +1,26 @@
#pragma once
#include <string>
// do not change any values in this header file
#define DYN_ENT_COUNT 0
// the clipMap->cmodels[0].leaf.terrainContents takes precendence over leaf and material terrain contents
// material flags determine the features of the surface
// unsure which flag type changes what right now
// -1 results in: no running, water splashes all the time, low friction, slanted angles make you slide very fast
// 1 results in: normal surface features, grenades work, seems normal
#define MATERIAL_SURFACE_FLAGS 1
#define MATERIAL_CONTENT_FLAGS 1
// terrain/world flags: does not change the type of terrain or what features they have
// from testing, as long at it isn't 0 things will work correctly
#define LEAF_TERRAIN_CONTENTS 1
#define WORLD_TERRAIN_CONTENTS 1
std::string missingImageName = "missing_image";
std::string colorOnlyImageName = "color_only_image";
#define MAX_AABB_SIZE 512 // maximum size a BSP node can be before it becomes a leaf
#define LIGHTGRID_COLOUR 128 // global lighting colour
// do not change
#define MAX_COL_VERTS (UINT16_MAX - 1) // max amount of collision verts a map can have
// do not change
#define DEFAULT_LIGHT_INDEX 0
#define SUN_LIGHT_INDEX 1
// do not change
enum SMODEL_FLAGS
{
SMODEL_FLAG_NO_SHADOW = 1,
SMODEL_FLAG_IS_LIT = 2
};
// do not change
enum GFX_SURFACE_FLAGS
{
GFX_SURFACE_CASTS_SUN_SHADOW = 0x1,

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +1,15 @@
#pragma once
#include "Game/T6/Material/JsonMaterialLoaderT6.h"
#include "BinarySpacePartitionTreePreCalc.h"
#include "TriangleSort.h"
#include "CustomMapConsts.h"
#include "CustomMapOptions.h"
#include "Util.h"
#include "Game/T6/Material/JsonMaterialLoaderT6.h"
#include "Utils/Pack.h"
#include <float.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace nlohmann;
class CustomMapLinker
{

View File

@@ -0,0 +1,17 @@
#pragma once
// These options can be edited
// material flags determine the features of the surface
// unsure which flag type changes what right now
// -1 results in: no running, water splashes all the time, low friction, slanted angles make you slide very fast
// 1 results in: normal surface features, grenades work, seems normal
#define MATERIAL_SURFACE_FLAGS 1
#define MATERIAL_CONTENT_FLAGS 1
// terrain/world flags: does not change the type of terrain or what features they have
// from testing, as long at it isn't 0 things will work correctly
#define LEAF_TERRAIN_CONTENTS 1
#define WORLD_TERRAIN_CONTENTS 1
#define LIGHTGRID_COLOUR 128 // global lighting colour

View File

@@ -22,26 +22,20 @@ namespace
AssetCreationResult CreateAsset(const std::string& assetName, AssetCreationContext& context) override
{
// custom maps must have a map_gfx file
auto mapGfxFile = m_search_path.Open("custom_map/map_gfx.fbx");
if (!mapGfxFile.IsOpen())
return AssetCreationResult::NoAction();
printf("Loading map data...\n");
// create map info from the obj file
customMapInfo* mapInfo = CustomMapInfo::createCustomMapInfo(m_zone.m_name, m_search_path);
// create map info from the fbx file
customMapInfo* mapInfo = ProjectCreator::createCustomMapInfo(m_zone.m_name, m_search_path);
if (mapInfo == NULL)
return AssetCreationResult::Failure();
printf("Creating map from data...\n");
// linker will add all the assets needed
CustomMapLinker* linker = new CustomMapLinker(m_memory, m_search_path, m_zone, context);
bool result = linker->linkCustomMap(mapInfo);
//auto gfxWorldAsset = context.LoadDependency<AssetFootstepTable>("default_1st_person");
//return AssetCreationResult::Success(gfxWorldAsset);
if (result)
{
auto gfxWorldAsset = context.LoadDependency<AssetGfxWorld>(mapInfo->bspName);

File diff suppressed because it is too large Load Diff

View File

@@ -44,6 +44,8 @@ bool loadFBXMesh(ufbx_node* node)
if (mesh->vertex_tangent.exists == false)
hasTangentSpace = false;
// Fix the target_unit_meters opt not working
// UFBX stores the transform data in units that are 100x larger than what blender uses, so this converts them back
ufbx_transform origTransform = node->local_transform;
origTransform.translation.x /= 100.0f;
@@ -113,6 +115,11 @@ bool loadFBXMesh(ufbx_node* node)
customMapVertex* vertex = &vertices[num_vertices++];
//ufbx_vec3 pos = ufbx_get_vertex_vec3(&mesh->vertex_position, index);
//vertex->pos.x = static_cast<float>(pos.x);
//vertex->pos.y = static_cast<float>(pos.y);
//vertex->pos.z = static_cast<float>(pos.z);
// Fix the target_unit_meters opt not working
ufbx_vec3 transformedPos = ufbx_transform_position(&meshMatrix, ufbx_get_vertex_vec3(&mesh->vertex_position, index));
vertex->pos.x = static_cast<float>(transformedPos.x);
vertex->pos.y = static_cast<float>(transformedPos.y);
@@ -131,10 +138,11 @@ bool loadFBXMesh(ufbx_node* node)
case CM_MATERIAL_COLOUR:
{
float factor = static_cast<float>(mesh->materials.data[i]->fbx.diffuse_factor.value_real);
vertex->color[0] = static_cast<float>(mesh->materials.data[i]->fbx.diffuse_color.value_vec3.x * factor);
vertex->color[1] = static_cast<float>(mesh->materials.data[i]->fbx.diffuse_color.value_vec3.y * factor);
vertex->color[2] = static_cast<float>(mesh->materials.data[i]->fbx.diffuse_color.value_vec3.z * factor);
vertex->color[3] = static_cast<float>(mesh->materials.data[i]->fbx.diffuse_color.value_vec4.w * factor);
ufbx_vec4 diffuse = mesh->materials.data[i]->fbx.diffuse_color.value_vec4;
vertex->color[0] = static_cast<float>(diffuse.x * factor);
vertex->color[1] = static_cast<float>(diffuse.y * factor);
vertex->color[2] = static_cast<float>(diffuse.z * factor);
vertex->color[3] = static_cast<float>(diffuse.w * factor);
break;
}
@@ -144,20 +152,23 @@ bool loadFBXMesh(ufbx_node* node)
// 1.0f - uv.v:
// 1.0f - uv.y reason:
// https://gamedev.stackexchange.com/questions/92886/fbx-uv-coordinates-is-strange
vertex->texCoord[0] = (float)(ufbx_get_vertex_vec2(&mesh->vertex_uv, index).x);
vertex->texCoord[1] = (float)(1.0f - ufbx_get_vertex_vec2(&mesh->vertex_uv, index).y);
ufbx_vec2 uv = ufbx_get_vertex_vec2(&mesh->vertex_uv, index);
vertex->texCoord[0] = (float)(uv.x);
vertex->texCoord[1] = (float)(1.0f - uv.y);
vertex->normal.x = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_normal, index).x);
vertex->normal.y = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_normal, index).y);
vertex->normal.z = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_normal, index).z);
ufbx_vec3 normal = ufbx_get_vertex_vec3(&mesh->vertex_normal, index);
vertex->normal.x = static_cast<float>(normal.x);
vertex->normal.y = static_cast<float>(normal.y);
vertex->normal.z = static_cast<float>(normal.z);
if (mesh->vertex_tangent.exists)
{
vertex->tangent.x = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_tangent, index).x);
vertex->tangent.y = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_tangent, index).y);
vertex->tangent.z = static_cast<float>(ufbx_get_vertex_vec3(&mesh->vertex_tangent, index).z);
ufbx_vec3 tangent = ufbx_get_vertex_vec3(&mesh->vertex_tangent, index);
vertex->tangent.x = static_cast<float>(tangent.x);
vertex->tangent.y = static_cast<float>(tangent.y);
vertex->tangent.z = static_cast<float>(tangent.z);
}
else
{
@@ -208,22 +219,13 @@ bool loadFBXModel(ufbx_node* node)
model.name = node->name.data;
ufbx_transform origTransform = node->local_transform;
origTransform.translation.x /= 100.0f;
origTransform.translation.y /= 100.0f;
origTransform.translation.z /= 100.0f;
origTransform.scale.x /= 100.0f;
origTransform.scale.y /= 100.0f;
origTransform.scale.z /= 100.0f;
ufbx_matrix meshMatrix = ufbx_transform_to_matrix(&origTransform);
model.origin.x = static_cast<float>(node->local_transform.translation.x) / 100.0f;
model.origin.y = static_cast<float>(node->local_transform.translation.y) / 100.0f;
model.origin.z = static_cast<float>(node->local_transform.translation.z) / 100.0f;
model.origin.x = static_cast<float>(node->local_transform.translation.x);
model.origin.y = static_cast<float>(node->local_transform.translation.y);
model.origin.z = static_cast<float>(node->local_transform.translation.z);
model.rotation.x = static_cast<float>(node->euler_rotation.x);
model.rotation.y = static_cast<float>(node->euler_rotation.y);
model.rotation.z = static_cast<float>(node->euler_rotation.z);
model.scale = static_cast<float>(node->local_transform.scale.x) / 100.0f;
model.scale = static_cast<float>(node->local_transform.scale.x);
if (model.scale == 0.0f)
{
@@ -324,7 +326,7 @@ void parseCollisionData(ufbx_scene* scene, customMapInfo* projInfo)
printf("warning: one or more meshes have no tangent space. Be sure to select the tangent space box when exporting the FBX from blender.\n");
}
customMapInfo* CustomMapInfo::createCustomMapInfo(std::string& projectName, ISearchPath& searchPath)
customMapInfo* ProjectCreator::createCustomMapInfo(std::string& projectName, ISearchPath& searchPath)
{
ufbx_scene* gfxScene;
ufbx_scene* colScene;
@@ -342,10 +344,13 @@ customMapInfo* CustomMapInfo::createCustomMapInfo(std::string& projectName, ISea
gfxFile.m_stream->read(gfxMapData, gfxFile.m_length);
ufbx_error error;
ufbx_load_opts opts;
ufbx_load_opts opts; // IDK why but opts don't seem to be working correctly, target_unit_meters isn't being used
memset(&opts, 0, sizeof(ufbx_load_opts));
opts.target_axes = ufbx_axes_right_handed_y_up;
opts.generate_missing_normals = true;
opts.allow_missing_vertex_position = false;
opts.target_unit_meters = 100.0f;
//gfxScene = ufbx_load_memory(gfxMapData, static_cast<size_t>(gfxFile.m_length), &opts, &error);
gfxScene = ufbx_load_memory(gfxMapData, static_cast<size_t>(gfxFile.m_length), NULL, &error);
if (!gfxScene)
{
@@ -366,12 +371,7 @@ customMapInfo* CustomMapInfo::createCustomMapInfo(std::string& projectName, ISea
colFile.m_stream->seekg(0);
colFile.m_stream->read(colMapData, colFile.m_length);
ufbx_error error;
ufbx_load_opts opts;
opts.target_axes = ufbx_axes_right_handed_y_up;
opts.generate_missing_normals = true;
opts.allow_missing_vertex_position = false;
colScene = ufbx_load_memory(colMapData, static_cast<size_t>(colFile.m_length), NULL, &error);
colScene = ufbx_load_memory(colMapData, static_cast<size_t>(colFile.m_length), &opts, &error);
if (!colScene)
{
fprintf(stderr, "Failed to load map collision fbx file: %s\n", error.description.data);

View File

@@ -5,7 +5,7 @@
#include "Game/T6/T6.h"
using namespace T6;
class CustomMapInfo
class ProjectCreator
{
public:
static customMapInfo* createCustomMapInfo(std::string& projectName, ISearchPath& searchPath);

View File

@@ -1,99 +0,0 @@
#pragma once
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include "Util.h"
#include "CustomMapConsts.h"
using TriangleGroup = std::vector<unsigned int>; // Group of triangle indices
using AdjacencyList = std::vector<TriangleGroup>; // Triangle-to-triangle connections
class TriangleSort
{
private:
static void computeTriCentroid(vec3_t& result, std::vector<vec3_t>& vertices, uint16_t i0, uint16_t i1, uint16_t i2)
{
result.x = 0.0f;
result.y = 0.0f;
result.z = 0.0f;
result.x += vertices[i0].x;
result.y += vertices[i0].y;
result.z += vertices[i0].z;
result.x += vertices[i1].x;
result.y += vertices[i1].y;
result.z += vertices[i1].z;
result.x += vertices[i2].x;
result.y += vertices[i2].y;
result.z += vertices[i2].z;
result.x /= 3.0f;
result.y /= 3.0f;
result.z /= 3.0f;
}
static AdjacencyList buildAdjacencyList(const std::vector<uint16_t>& indices, std::vector<vec3_t>& vertices, int maxTrianglesPerGroup)
{
size_t numTriangles = indices.size() / 3;
AdjacencyList adjList(numTriangles);
std::vector<vec3_t> centroids;
for (size_t i = 0; i < numTriangles; i++)
{
vec3_t center;
computeTriCentroid(center, vertices, indices[i * 3], indices[i * 3 + 1], indices[i * 3 + 2]);
centroids.push_back(center);
}
std::vector<bool> visited(numTriangles, false);
for (size_t i = 0; i < numTriangles; ++i)
{
_ASSERT(i < MAX_COL_VERTS);
if (visited[i] == true)
continue;
visited[i] = true;
// Store distances to other triangles
std::vector<std::pair<float, unsigned int>> distances;
for (size_t j = 0; j < numTriangles; ++j)
{
if (visited[j] == true)
continue;
if (i != j)
{
float dist = CMUtil::distBetweenPoints(centroids[i], centroids[j]);
distances.emplace_back(dist, j);
}
}
// Sort by distance and take the closest maxNeighbors
std::sort(distances.begin(), distances.end());
for (size_t k = 0; k < std::min((size_t)maxTrianglesPerGroup, distances.size()); ++k)
{
unsigned int neighbour = distances[k].second;
adjList[i].push_back(neighbour);
visited[neighbour] = true;
}
}
for (size_t i = 0; i < visited.size(); i++)
{
if (visited[i] == false)
printf("WARN: missing triangle: %i\n", i);
}
return adjList;
}
public:
static AdjacencyList groupTriangles(std::vector<uint16_t>& indices, std::vector<vec3_t>& vertices, int maxTrianglesPerGroup)
{
return buildAdjacencyList(indices, vertices, maxTrianglesPerGroup);
}
};

View File

@@ -336,7 +336,6 @@ namespace T6
collection.AddDefaultAssetCreator(std::make_unique<DefaultAssetCreator<AssetFootstepTable>>(memory));
collection.AddDefaultAssetCreator(std::make_unique<DefaultAssetCreator<AssetFootstepFxTable>>(memory));
collection.AddDefaultAssetCreator(std::make_unique<DefaultAssetCreator<AssetZBarrier>>(memory));
// custom maps have no default
}
void ConfigureGlobalAssetPoolsLoaders(AssetCreatorCollection& collection, Zone& zone)
@@ -389,7 +388,6 @@ namespace T6
collection.AddAssetCreator(std::make_unique<GlobalAssetPoolsLoader<AssetFootstepTable>>(zone));
collection.AddAssetCreator(std::make_unique<GlobalAssetPoolsLoader<AssetFootstepFxTable>>(zone));
collection.AddAssetCreator(std::make_unique<GlobalAssetPoolsLoader<AssetZBarrier>>(zone));
//collection.AddAssetCreator(std::make_unique<GlobalAssetPoolsLoader<AssetCustomMap>>(zone));
}
void ConfigureLoaders(AssetCreatorCollection& collection, Zone& zone, ISearchPath& searchPath, IGdtQueryable& gdt)
@@ -451,6 +449,7 @@ namespace T6
collection.AddAssetCreator(CreateRawZBarrierLoader(memory, searchPath, zone));
collection.AddAssetCreator(CreateGdtZBarrierLoader(memory, searchPath, gdt, zone));
// The Custom Map Loader uses gfxworld as the starting asset
collection.AddAssetCreator(CreateCustomMapLoader(memory, searchPath, zone));
}
} // namespace

View File

@@ -44,10 +44,10 @@ namespace
};
} // namespace
namespace script
namespace T6
{
std::unique_ptr<AssetCreator<AssetScript>> CreateLoaderT6(MemoryManager& memory, ISearchPath& searchPath)
std::unique_ptr<AssetCreator<AssetScript>> CreateScriptLoader(MemoryManager& memory, ISearchPath& searchPath)
{
return std::make_unique<ScriptLoader>(memory, searchPath);
}
} // namespace script
} // namespace T6

View File

@@ -7,7 +7,7 @@
#include <memory>
namespace script
namespace T6
{
std::unique_ptr<AssetCreator<T6::AssetScript>> CreateLoaderT6(MemoryManager& memory, ISearchPath& searchPath);
} // namespace script
std::unique_ptr<AssetCreator<AssetScript>> CreateScriptLoader(MemoryManager& memory, ISearchPath& searchPath);
} // namespace T6