M2.5: ShaderMaterial + texture resolution + fog; multi-skeleton verified
- m2_material: Metin2-style ShaderMaterial. blend_mix shader (opaque /
alpha-blend / alpha-test-cutout via `mode` uniform) + blend_add shader
(additive glow). modulate(tex, COORD) * COLOR, diffuse_lambert.
- Metin2Model: material_mode prop ("metin2" ShaderMaterial | "standard");
_resolve_texture does filename-convention + case-insensitive dir scan of
*.dds (face/hair keyword slots, stem match, fallback); set_surface_texture
override; guess_blend heuristic (hair->cutout, cape->alpha, effect->add).
Texture cache. Fixed opaque path to NOT alpha-discard (was hiding bodies).
- harness: AABB-based camera framing, engine depth fog (MTGODOT_FOG=1),
MTGODOT_ANIM_T fixed-pose capture (needs explicit anim reload first),
MTGODOT_AUTOSHOT batch screenshot.
Verified: warrior 5/5 surfaces textured under the shader (t=9 dance pose,
lit, fogged). assassin 17/17 textured via dir scan. shaman_lord (gr2 v7)
renders. Screenshots test/golden/m25-*.png, m2c-t*.png.
Gap (M2.5 gate not closed): libgr2 POD API exposes no material/texture-name/
blend data, so multi-slot -> texture/blend mapping is heuristic only. Needs
a libgr2 material API (gr2_material.cpp walking the type tree) — same gap as
xrender-poc M1 T5. Documented in docs/GODOT-POC-PLAN.md §M2.5 T2.5.6.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WaHYEY9rwLWt21PULiYjeJ
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
#include "m2_material.h"
|
||||
|
||||
#include <godot_cpp/classes/shader.hpp>
|
||||
#include <godot_cpp/classes/shader_material.hpp>
|
||||
#include <godot_cpp/classes/texture2d.hpp>
|
||||
#include <godot_cpp/variant/color.hpp>
|
||||
|
||||
using namespace godot;
|
||||
|
||||
namespace mtgodot {
|
||||
namespace {
|
||||
|
||||
// blend_mix shader: covers opaque (mode 0, alpha-scissor) and alpha (mode 1).
|
||||
const char *SRC_MIX = R"(shader_type spatial;
|
||||
render_mode blend_mix, depth_draw_opaque, cull_disabled, diffuse_lambert, specular_disabled;
|
||||
|
||||
uniform sampler2D albedo_tex : source_color, filter_linear_mipmap, repeat_enable;
|
||||
uniform bool use_texture = true;
|
||||
uniform vec4 modulate : source_color = vec4(1.0);
|
||||
uniform float alpha_scissor : hint_range(0.0, 1.0) = 0.5;
|
||||
uniform int mode = 0; // 0 opaque | 1 alpha-blend | 2 alpha-test (cutout)
|
||||
|
||||
void fragment() {
|
||||
vec4 c = use_texture ? texture(albedo_tex, UV) : vec4(1.0);
|
||||
c *= modulate * COLOR;
|
||||
if (mode == 2 && c.a < alpha_scissor) {
|
||||
discard;
|
||||
}
|
||||
ALBEDO = c.rgb;
|
||||
ALPHA = (mode == 1) ? c.a : 1.0;
|
||||
}
|
||||
)";
|
||||
|
||||
// blend_add shader: additive glow (effects, enchant overlays).
|
||||
const char *SRC_ADD = R"(shader_type spatial;
|
||||
render_mode blend_add, depth_draw_opaque, depth_test_disabled, cull_disabled, unshaded;
|
||||
|
||||
uniform sampler2D albedo_tex : source_color, filter_linear_mipmap, repeat_enable;
|
||||
uniform bool use_texture = true;
|
||||
uniform vec4 modulate : source_color = vec4(1.0);
|
||||
|
||||
void fragment() {
|
||||
vec4 c = use_texture ? texture(albedo_tex, UV) : vec4(1.0);
|
||||
c *= modulate * COLOR;
|
||||
ALBEDO = c.rgb * c.a;
|
||||
ALPHA = 1.0;
|
||||
}
|
||||
)";
|
||||
|
||||
Ref<Shader> shader_for(bool additive) {
|
||||
static Ref<Shader> s_mix;
|
||||
static Ref<Shader> s_add;
|
||||
Ref<Shader> &slot = additive ? s_add : s_mix;
|
||||
if (slot.is_null()) {
|
||||
slot.instantiate();
|
||||
slot->set_code(additive ? SRC_ADD : SRC_MIX);
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Ref<ShaderMaterial> make_material(const MaterialDesc &d) {
|
||||
Ref<ShaderMaterial> m;
|
||||
m.instantiate();
|
||||
|
||||
const bool additive = (d.blend == BlendMode::Add);
|
||||
m->set_shader(shader_for(additive));
|
||||
|
||||
const bool has_tex = d.albedo.is_valid();
|
||||
m->set_shader_parameter("use_texture", has_tex);
|
||||
if (has_tex) {
|
||||
m->set_shader_parameter("albedo_tex", d.albedo);
|
||||
}
|
||||
m->set_shader_parameter("modulate", has_tex ? Color(1, 1, 1, 1) : Color(0.8, 0.8, 0.82, 1));
|
||||
|
||||
if (!additive) {
|
||||
m->set_shader_parameter("alpha_scissor", d.alpha_scissor);
|
||||
int mode = 0;
|
||||
if (d.blend == BlendMode::Alpha) {
|
||||
mode = 1;
|
||||
} else if (d.blend == BlendMode::AlphaTest) {
|
||||
mode = 2;
|
||||
}
|
||||
m->set_shader_parameter("mode", mode);
|
||||
}
|
||||
if (d.blend == BlendMode::Alpha) {
|
||||
m->set_render_priority(1);
|
||||
} else if (additive) {
|
||||
m->set_render_priority(2);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
} // namespace mtgodot
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
// m2_material — Metin2-style forward material as a Godot ShaderMaterial.
|
||||
//
|
||||
// M2.5 infrastructure. The gr2 fixed-function texture-stage / blend data is not
|
||||
// yet exposed by libgr2's POD API (see docs/GODOT-POC-PLAN.md §M2.5 gap list),
|
||||
// so callers pick the blend mode heuristically for now. The shader itself
|
||||
// already covers the common Metin2 cases: modulate(tex, vertex_color) + light,
|
||||
// alpha-test, alpha-blend, additive.
|
||||
|
||||
#include <godot_cpp/classes/ref.hpp>
|
||||
|
||||
namespace godot {
|
||||
class ShaderMaterial;
|
||||
class Texture2D;
|
||||
} // namespace godot
|
||||
|
||||
namespace mtgodot {
|
||||
|
||||
enum class BlendMode {
|
||||
Opaque, // blend_mix, ALPHA=1, no discard
|
||||
Alpha, // blend_mix, sorted, uses texture alpha
|
||||
AlphaTest, // blend_mix, discard below alpha_scissor (cutout)
|
||||
Add, // blend_add (glow / effects)
|
||||
};
|
||||
|
||||
struct MaterialDesc {
|
||||
godot::Ref<godot::Texture2D> albedo; // null -> flat white
|
||||
BlendMode blend = BlendMode::Opaque;
|
||||
float alpha_scissor = 0.5f;
|
||||
bool two_sided = true; // winding not yet verified per-model
|
||||
};
|
||||
|
||||
godot::Ref<godot::ShaderMaterial> make_material(const MaterialDesc &d);
|
||||
|
||||
} // namespace mtgodot
|
||||
+172
-37
@@ -2,13 +2,16 @@
|
||||
|
||||
#include "dxt.h"
|
||||
#include "gr2_bridge.h"
|
||||
#include "m2_material.h"
|
||||
|
||||
#include <godot_cpp/classes/array_mesh.hpp>
|
||||
#include <godot_cpp/classes/base_material3d.hpp>
|
||||
#include <godot_cpp/classes/dir_access.hpp>
|
||||
#include <godot_cpp/classes/file_access.hpp>
|
||||
#include <godot_cpp/classes/image.hpp>
|
||||
#include <godot_cpp/classes/image_texture.hpp>
|
||||
#include <godot_cpp/classes/mesh_instance3d.hpp>
|
||||
#include <godot_cpp/classes/shader_material.hpp>
|
||||
#include <godot_cpp/classes/skeleton3d.hpp>
|
||||
#include <godot_cpp/classes/skin.hpp>
|
||||
#include <godot_cpp/classes/standard_material3d.hpp>
|
||||
@@ -38,6 +41,9 @@ void Metin2Model::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("get_flip_z"), &Metin2Model::get_flip_z);
|
||||
ClassDB::bind_method(D_METHOD("set_flip_winding", "v"), &Metin2Model::set_flip_winding);
|
||||
ClassDB::bind_method(D_METHOD("get_flip_winding"), &Metin2Model::get_flip_winding);
|
||||
ClassDB::bind_method(D_METHOD("set_material_mode", "m"), &Metin2Model::set_material_mode);
|
||||
ClassDB::bind_method(D_METHOD("get_material_mode"), &Metin2Model::get_material_mode);
|
||||
ClassDB::bind_method(D_METHOD("set_surface_texture", "surface", "path"), &Metin2Model::set_surface_texture);
|
||||
ClassDB::bind_method(D_METHOD("reload"), &Metin2Model::reload);
|
||||
ClassDB::bind_method(D_METHOD("get_info"), &Metin2Model::get_info);
|
||||
ClassDB::bind_method(D_METHOD("probe_gr2", "path"), &Metin2Model::probe_gr2);
|
||||
@@ -50,6 +56,28 @@ void Metin2Model::_bind_methods() {
|
||||
"set_unit_scale", "get_unit_scale");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_z"), "set_flip_z", "get_flip_z");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_winding"), "set_flip_winding", "get_flip_winding");
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "material_mode", PROPERTY_HINT_ENUM, "metin2,standard"),
|
||||
"set_material_mode", "get_material_mode");
|
||||
}
|
||||
|
||||
void Metin2Model::set_material_mode(const String &m) {
|
||||
material_mode = m;
|
||||
if (is_inside_tree()) {
|
||||
reload();
|
||||
}
|
||||
}
|
||||
|
||||
void Metin2Model::set_surface_texture(int surface, const String &path) {
|
||||
if (surface < 0) {
|
||||
return;
|
||||
}
|
||||
while (surface_tex_override.size() <= surface) {
|
||||
surface_tex_override.push_back(String());
|
||||
}
|
||||
surface_tex_override[surface] = path;
|
||||
if (is_inside_tree()) {
|
||||
_apply_materials();
|
||||
}
|
||||
}
|
||||
|
||||
void Metin2Model::set_gr2_path(const String &p) {
|
||||
@@ -173,58 +201,165 @@ String Metin2Model::_guess_texture_dir() const {
|
||||
return gr2_path.get_base_dir();
|
||||
}
|
||||
|
||||
Ref<godot::ImageTexture> Metin2Model::_load_dds(const String &path) {
|
||||
Ref<ImageTexture> t;
|
||||
if (tex_cache.has(path)) {
|
||||
return tex_cache[path];
|
||||
}
|
||||
if (FileAccess::file_exists(path)) {
|
||||
mtgodot::Image dds = mtgodot::load_dds_path(path.utf8().get_data());
|
||||
if (dds.ok()) {
|
||||
PackedByteArray bytes;
|
||||
bytes.resize((int)dds.rgba.size());
|
||||
memcpy(bytes.ptrw(), dds.rgba.data(), dds.rgba.size());
|
||||
Ref<godot::Image> img = godot::Image::create_from_data(
|
||||
dds.w, dds.h, false, godot::Image::FORMAT_RGBA8, bytes);
|
||||
if (img.is_valid()) {
|
||||
img->generate_mipmaps();
|
||||
t = ImageTexture::create_from_image(img);
|
||||
}
|
||||
}
|
||||
}
|
||||
tex_cache[path] = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
// Best-effort texture pick for a surface. libgr2's POD API gives no material or
|
||||
// texture names yet (see GODOT-POC-PLAN.md §M2.5 gap list), so this is filename
|
||||
// convention + a case-insensitive directory scan.
|
||||
Ref<godot::ImageTexture> Metin2Model::_resolve_texture(const String &dir, const String &stem,
|
||||
const String &surface_name, const PackedStringArray &dds_files) {
|
||||
String sn = surface_name.to_lower();
|
||||
|
||||
auto scan = [&](const String &needle) -> Ref<ImageTexture> {
|
||||
for (int i = 0; i < dds_files.size(); ++i) {
|
||||
if (dds_files[i].to_lower().find(needle) != -1) {
|
||||
Ref<ImageTexture> t = _load_dds(dir.path_join(dds_files[i]));
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ref<ImageTexture>();
|
||||
};
|
||||
|
||||
// 1. surface-name keyword slots
|
||||
if (sn.find("face") != -1) {
|
||||
Ref<ImageTexture> t = _load_dds(dir.path_join(stem + String("_face.dds")));
|
||||
if (t.is_null()) {
|
||||
t = scan("face");
|
||||
}
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
if (sn.find("hair") != -1) {
|
||||
Ref<ImageTexture> t = _load_dds(dir.path_join(stem + String("_hair.dds")));
|
||||
if (t.is_null()) {
|
||||
t = scan("hair");
|
||||
}
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
// 2. surface name used verbatim as a texture basename
|
||||
if (!sn.is_empty()) {
|
||||
Ref<ImageTexture> t = _load_dds(dir.path_join(surface_name + String(".dds")));
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
// 3. exact stem
|
||||
Ref<ImageTexture> t = _load_dds(dir.path_join(stem + String(".dds")));
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
// 4. any dds whose name contains the stem, then any non-face/hair dds
|
||||
t = scan(stem);
|
||||
if (t.is_valid()) {
|
||||
return t;
|
||||
}
|
||||
for (int i = 0; i < dds_files.size(); ++i) {
|
||||
String f = dds_files[i].to_lower();
|
||||
if (f.find("face") == -1 && f.find("hair") == -1 && f.find("_lod") == -1) {
|
||||
Ref<ImageTexture> any = _load_dds(dir.path_join(dds_files[i]));
|
||||
if (any.is_valid()) {
|
||||
return any;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ref<ImageTexture>();
|
||||
}
|
||||
|
||||
static mtgodot::BlendMode guess_blend(const String &surface_name) {
|
||||
String sn = surface_name.to_lower();
|
||||
if (sn.find("effect") != -1 || sn.find("glow") != -1 || sn.find("aura") != -1) {
|
||||
return mtgodot::BlendMode::Add;
|
||||
}
|
||||
if (sn.find("hair") != -1) {
|
||||
return mtgodot::BlendMode::AlphaTest; // Metin2 hair = alpha-test cutout
|
||||
}
|
||||
if (sn.find("cloak") != -1 || sn.find("cape") != -1 ||
|
||||
sn.find("skirt") != -1 || sn.find("ribbon") != -1) {
|
||||
return mtgodot::BlendMode::Alpha;
|
||||
}
|
||||
return mtgodot::BlendMode::Opaque;
|
||||
}
|
||||
|
||||
void Metin2Model::_apply_materials() {
|
||||
if (!mi || !file || !*file) {
|
||||
return;
|
||||
}
|
||||
const gr2::FileInfo &fi = (*file)->file_info();
|
||||
const String dir = _guess_texture_dir();
|
||||
const String stem = gr2_path.get_file().get_basename();
|
||||
|
||||
// Cache: texture path -> ImageTexture.
|
||||
auto load_tex = [&](const String &path) -> Ref<ImageTexture> {
|
||||
Ref<ImageTexture> t;
|
||||
if (!FileAccess::file_exists(path)) {
|
||||
return t;
|
||||
PackedStringArray dds_files;
|
||||
{
|
||||
Ref<DirAccess> da = DirAccess::open(dir);
|
||||
if (da.is_valid()) {
|
||||
da->list_dir_begin();
|
||||
for (String f = da->get_next(); !f.is_empty(); f = da->get_next()) {
|
||||
if (!da->current_is_dir() && f.to_lower().ends_with(".dds")) {
|
||||
dds_files.push_back(f);
|
||||
}
|
||||
}
|
||||
da->list_dir_end();
|
||||
}
|
||||
mtgodot::Image dds = mtgodot::load_dds_path(path.utf8().get_data());
|
||||
if (!dds.ok()) {
|
||||
return t;
|
||||
}
|
||||
PackedByteArray bytes;
|
||||
bytes.resize((int)dds.rgba.size());
|
||||
memcpy(bytes.ptrw(), dds.rgba.data(), dds.rgba.size());
|
||||
Ref<godot::Image> img = godot::Image::create_from_data(
|
||||
dds.w, dds.h, false, godot::Image::FORMAT_RGBA8, bytes);
|
||||
if (img.is_null()) {
|
||||
return t;
|
||||
}
|
||||
img->generate_mipmaps();
|
||||
t = ImageTexture::create_from_image(img);
|
||||
return t;
|
||||
};
|
||||
|
||||
Ref<ImageTexture> tex_main = load_tex(dir.path_join(stem + String(".dds")));
|
||||
Ref<ImageTexture> tex_face = load_tex(dir.path_join(stem + String("_face.dds")));
|
||||
if (tex_face.is_null()) {
|
||||
tex_face = load_tex(dir.path_join(String("warrior_face.dds")));
|
||||
}
|
||||
|
||||
Ref<ArrayMesh> mesh = mi->get_mesh();
|
||||
int textured = 0;
|
||||
for (int s = 0; s < mesh->get_surface_count(); ++s) {
|
||||
String sname = mesh->surface_get_name(s).to_lower();
|
||||
Ref<ImageTexture> tex = (sname.find("face") != -1 && tex_face.is_valid()) ? tex_face : tex_main;
|
||||
|
||||
Ref<StandardMaterial3D> mat;
|
||||
mat.instantiate();
|
||||
mat->set_cull_mode(BaseMaterial3D::CULL_DISABLED); // M1: winding unverified
|
||||
if (tex.is_valid()) {
|
||||
mat->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, tex);
|
||||
const String sname = mesh->surface_get_name(s);
|
||||
Ref<ImageTexture> tex;
|
||||
if (s < surface_tex_override.size() && !String(surface_tex_override[s]).is_empty()) {
|
||||
tex = _load_dds(surface_tex_override[s]);
|
||||
} else {
|
||||
mat->set_albedo(Color(0.8, 0.8, 0.82));
|
||||
tex = _resolve_texture(dir, stem, sname, dds_files);
|
||||
}
|
||||
if (tex.is_valid()) {
|
||||
++textured;
|
||||
}
|
||||
|
||||
if (material_mode == String("standard")) {
|
||||
Ref<StandardMaterial3D> mat;
|
||||
mat.instantiate();
|
||||
mat->set_cull_mode(BaseMaterial3D::CULL_DISABLED);
|
||||
if (tex.is_valid()) {
|
||||
mat->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, tex);
|
||||
} else {
|
||||
mat->set_albedo(Color(0.8, 0.8, 0.82));
|
||||
}
|
||||
mi->set_surface_override_material(s, mat);
|
||||
} else {
|
||||
mtgodot::MaterialDesc md;
|
||||
md.albedo = tex;
|
||||
md.blend = guess_blend(sname);
|
||||
mi->set_surface_override_material(s, mtgodot::make_material(md));
|
||||
}
|
||||
mi->set_surface_override_material(s, mat);
|
||||
}
|
||||
UtilityFunctions::print(vformat("[Metin2Model] materials: %d/%d surfaces textured (dir=%s, %d dds)",
|
||||
textured, mesh->get_surface_count(), dir, (int)dds_files.size()));
|
||||
}
|
||||
|
||||
const gr2::Skeleton *Metin2Model::gr2_skeleton() const {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <godot_cpp/classes/image_texture.hpp>
|
||||
#include <godot_cpp/classes/node3d.hpp>
|
||||
#include <godot_cpp/classes/ref.hpp>
|
||||
#include <godot_cpp/templates/hash_map.hpp>
|
||||
#include <godot_cpp/variant/packed_string_array.hpp>
|
||||
#include <godot_cpp/variant/string.hpp>
|
||||
|
||||
#include <gr2/gr2.h>
|
||||
@@ -11,6 +15,7 @@
|
||||
namespace godot {
|
||||
class Skeleton3D;
|
||||
class MeshInstance3D;
|
||||
class ImageTexture;
|
||||
} // namespace godot
|
||||
|
||||
namespace mtgodot {
|
||||
@@ -46,6 +51,12 @@ public:
|
||||
void set_flip_winding(bool v);
|
||||
bool get_flip_winding() const { return flip_winding; }
|
||||
|
||||
void set_material_mode(const godot::String &m);
|
||||
godot::String get_material_mode() const { return material_mode; }
|
||||
|
||||
// Explicit per-surface texture path override (index -> absolute .dds path).
|
||||
void set_surface_texture(int surface, const godot::String &path);
|
||||
|
||||
// Rebuild the subtree from the current properties.
|
||||
void reload();
|
||||
|
||||
@@ -69,14 +80,22 @@ private:
|
||||
bool flip_z = false;
|
||||
bool flip_winding = false;
|
||||
|
||||
godot::String material_mode = "metin2"; // "metin2" (ShaderMaterial) | "standard"
|
||||
godot::PackedStringArray surface_tex_override;
|
||||
|
||||
std::shared_ptr<std::optional<gr2::File>> file; // shared_ptr so accessor stays valid
|
||||
godot::Skeleton3D *skel = nullptr;
|
||||
godot::MeshInstance3D *mi = nullptr;
|
||||
godot::String last_info;
|
||||
godot::HashMap<godot::String, godot::Ref<godot::ImageTexture>> tex_cache;
|
||||
|
||||
void _clear_children();
|
||||
void _apply_materials();
|
||||
godot::String _guess_texture_dir() const;
|
||||
godot::Ref<godot::ImageTexture> _load_dds(const godot::String &path);
|
||||
godot::Ref<godot::ImageTexture> _resolve_texture(const godot::String &dir,
|
||||
const godot::String &stem, const godot::String &surface_name,
|
||||
const godot::PackedStringArray &dds_files);
|
||||
};
|
||||
|
||||
} // namespace mtgodot
|
||||
|
||||
Reference in New Issue
Block a user