- 桥梁与静态物体高度采样修复: - 严格对齐 40250 CMapOutdoor::GetHeight 与 CAttributeInstance::GetHeight - 解析 .mdatr 中的 AttributeHeight 网格,使用 is_in_triangle_2d 准确计算桥面多边形平面方程 - sample_height 查询邻近区块并返回 fMAX(fObjectHeight, fTerrainHeight),彻底解决走上桥面穿透掉入水底/河床的问题 - 新增 test_bridge_height_parity.gd 自动化对拍测试 - 40250 怪物击杀经验动效: - 1:1 实现 FLY_EXP(0) / FLY_HP / FLY_SP 粒子轨迹与爆炸吸附 - 40250 客户端全系统功能对齐(Batches 1-31): - 包含公会、交易、骑乘、变身、钓鱼、采矿、商城、信件、结婚、地牢等 134 套对拍系统与自动化回归测试 - 文档沉淀: - 新增 docs/CLIENT-PARITY-AUDIT-AND-FIX-GUIDE.md 客户端对拍缺陷发现与修复工程指南
164 lines
5.4 KiB
C++
164 lines
5.4 KiB
C++
#include "static_object.h"
|
||
|
||
#include "asset_io.h"
|
||
#include "dxt.h"
|
||
#include "gr2_bridge.h"
|
||
#include "texture_util.h"
|
||
|
||
#include <godot_cpp/classes/image.hpp>
|
||
#include <godot_cpp/classes/image_texture.hpp>
|
||
#include <godot_cpp/classes/standard_material3d.hpp>
|
||
#include <godot_cpp/variant/packed_byte_array.hpp>
|
||
#include <godot_cpp/variant/utility_functions.hpp>
|
||
|
||
#include <asset_resolver.h>
|
||
#include <gr2/gr2.h>
|
||
|
||
#include <unordered_map>
|
||
|
||
using namespace godot;
|
||
|
||
namespace mtgodot {
|
||
|
||
namespace {
|
||
|
||
std::unordered_map<std::string, Ref<ImageTexture>> g_dds_cache;
|
||
|
||
// Building albedo (sRGB colour) -> mobile-ASTC-eligible via make_color_texture
|
||
// (no-op on desktop). mipmaps=false keeps the desktop result byte-identical to
|
||
// the old path (buildings had no mip chain). §F4.
|
||
Ref<ImageTexture> decode_dds_cached(const std::string &real_path) {
|
||
auto &cache = g_dds_cache;
|
||
auto it = cache.find(real_path);
|
||
if (it != cache.end())
|
||
return it->second;
|
||
Ref<ImageTexture> tex;
|
||
mtgodot::Image d = mtgodot::dds_from_file(godot::String(real_path.c_str()));
|
||
if (d.ok()) {
|
||
tex = mtgodot::make_color_texture(d.w, d.h, d.rgba.data(), d.rgba.size(),
|
||
/*mipmaps=*/false);
|
||
}
|
||
cache.emplace(real_path, tex);
|
||
return tex;
|
||
}
|
||
|
||
Ref<StandardMaterial3D> material_for(const std::string &tex_name, bool alpha_blend, bool two_sided,
|
||
const fmt::AssetResolver &res) {
|
||
Ref<StandardMaterial3D> mat;
|
||
mat.instantiate();
|
||
mat->set_albedo(Color(0.7f, 0.7f, 0.7f));
|
||
mat->set_roughness(1.0f);
|
||
mat->set_texture_filter(StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC);
|
||
mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
|
||
mat->set_diffuse_mode(StandardMaterial3D::DIFFUSE_LAMBERT_WRAP);
|
||
mat->set_specular_mode(StandardMaterial3D::SPECULAR_DISABLED);
|
||
mat->set_feature(StandardMaterial3D::FEATURE_BACKLIGHT, true);
|
||
mat->set_backlight(Color(0.2f, 0.2f, 0.2f));
|
||
|
||
String t = String(tex_name.c_str()).to_lower();
|
||
bool kw_alpha = t.find("leaf") != -1 || t.find("grass") != -1 || t.find("fence") != -1 ||
|
||
t.find("net") != -1 || t.find("ivy") != -1 || t.find("tree") != -1 ||
|
||
t.find("branch") != -1;
|
||
if (alpha_blend) {
|
||
// EterGrnLib TYPE_BLEND_PNT:真 alpha 混合(第 2 map 作 opacity)
|
||
mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
|
||
mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
|
||
} else if (kw_alpha) {
|
||
mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA_SCISSOR);
|
||
mat->set_alpha_scissor_threshold(0.5f);
|
||
mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
|
||
}
|
||
if (two_sided)
|
||
mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
|
||
|
||
if (tex_name.empty())
|
||
return mat;
|
||
std::string rp = res.resolve(tex_name, nullptr);
|
||
if (rp.empty()) {
|
||
// gr2 里常是 .dds;有时资产用别的大小写 / 扩展。先只试原名。
|
||
return mat;
|
||
}
|
||
Ref<ImageTexture> tex = decode_dds_cached(rp);
|
||
if (tex.is_valid()) {
|
||
mat->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, tex);
|
||
mat->set_albedo(Color(1, 1, 1));
|
||
mat->set_feature(StandardMaterial3D::FEATURE_EMISSION, true);
|
||
mat->set_texture(StandardMaterial3D::TEXTURE_EMISSION, tex);
|
||
mat->set_emission(Color(1.0f, 1.0f, 1.0f));
|
||
mat->set_emission_energy_multiplier(0.18f);
|
||
}
|
||
return mat;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
Ref<godot::ArrayMesh> get_static_mesh(const std::string &real_gr2_path,
|
||
const fmt::AssetResolver &res, StaticMeshCache &cache) {
|
||
auto it = cache.by_path.find(real_gr2_path);
|
||
if (it != cache.by_path.end())
|
||
return it->second;
|
||
|
||
Ref<godot::ArrayMesh> result; // invalid until success
|
||
|
||
gr2::LoadError err;
|
||
auto loaded = mtgodot::gr2_from_file(godot::String(real_gr2_path.c_str()), &err);
|
||
if (!loaded) {
|
||
++cache.failed;
|
||
UtilityFunctions::push_warning(String("[static] gr2 load failed: ") +
|
||
real_gr2_path.c_str() + " (" + err.message.c_str() + ")");
|
||
cache.by_path.emplace(real_gr2_path, result);
|
||
return result;
|
||
}
|
||
const gr2::FileInfo &fi = loaded->file_info();
|
||
|
||
// 贴图名 -> 渲染态(来自 libgr2 dump_materials 的名字/map 推断)
|
||
std::vector<gr2::MaterialInfo> mats = gr2::dump_materials(*loaded);
|
||
std::map<std::string, std::pair<bool, bool>> tex_state; // lower(tex) -> {alpha, two_sided}
|
||
for (const auto &m : mats) {
|
||
std::string k;
|
||
for (char c : m.diffuse_texture)
|
||
k += (char)std::tolower((unsigned char)c);
|
||
if (!k.empty())
|
||
tex_state[k] = {m.alpha_blend, m.two_sided};
|
||
}
|
||
|
||
std::vector<mtgodot::RenderPart> parts = mtgodot::build_parts(fi);
|
||
AABB bounds;
|
||
Ref<godot::ArrayMesh> mesh = mtgodot::build_mesh(fi, parts, /*flip_winding=*/false, bounds);
|
||
if (mesh.is_valid() && mesh->get_surface_count() > 0) {
|
||
for (int s = 0; s < mesh->get_surface_count() && s < (int)parts.size(); ++s) {
|
||
const mtgodot::RenderPart &rp = parts[s];
|
||
std::string tex;
|
||
if (rp.mesh >= 0 && rp.mesh < (int)fi.meshes.size()) {
|
||
const auto &mt = fi.meshes[rp.mesh].material_textures;
|
||
if (rp.mat_index >= 0 && rp.mat_index < (int)mt.size())
|
||
tex = mt[rp.mat_index];
|
||
else if (!mt.empty())
|
||
tex = mt[0];
|
||
}
|
||
bool alpha = false, two = false;
|
||
{
|
||
std::string k;
|
||
for (char c : tex)
|
||
k += (char)std::tolower((unsigned char)c);
|
||
auto f = tex_state.find(k);
|
||
if (f != tex_state.end()) {
|
||
alpha = f->second.first;
|
||
two = f->second.second;
|
||
}
|
||
}
|
||
mesh->surface_set_material(s, material_for(tex, alpha, two, res));
|
||
}
|
||
result = mesh;
|
||
++cache.loaded;
|
||
} else {
|
||
++cache.failed;
|
||
}
|
||
cache.by_path.emplace(real_gr2_path, result);
|
||
return result;
|
||
}
|
||
|
||
void cleanup_static_object_cache() { g_dds_cache.clear(); }
|
||
|
||
} // namespace mtgodot
|