using System.Text; using CUE4Parse.FileProvider; using CUE4Parse.UE4.Assets.Exports; using CUE4Parse.UE4.Assets.Exports.Component.StaticMesh; using CUE4Parse.UE4.Assets.Exports.StaticMesh; using CUE4Parse.UE4.Assets.Exports.Material; using CUE4Parse.UE4.Objects.Core.Math; using CUE4Parse.UE4.Objects.UObject; namespace GvrMapExtract; /// 영구 레벨 이름 (Ascent, HURM_Alley …) /// 경기 기록의 맵 경로 (/Game/Maps/Ascent/Ascent) public sealed record MapInfo(string Code, string MapUrl, List Levels); /// 맵 레벨들의 StaticMesh 배치를 모아 월드 좌표(UE cm) 삼각형 하나로 굽는다. /// 재질마다 텍스처(디퓨즈, 작게 줄임) · 색을 붙인다. public sealed class MapScene { public int Placements, Meshes, Failed, Skipped; private readonly List _levels = new(); private string _code = ""; private string _mapUrl = ""; /// 맵 = 하위 레벨을 불러오는 영구 레벨 (경기 기록의 맵 경로 "/Game/Maps/Ascent/Ascent" 와 같다). /// 연습장 · 튜토리얼 · 메뉴처럼 경기가 없는 곳은 뺀다 public static List FindMaps(IFileProvider provider, Action? log = null) { var umaps = provider.Files.Keys.Where(k => k.StartsWith("ShooterGame/Content/Maps/", StringComparison.OrdinalIgnoreCase) && k.EndsWith(".umap", StringComparison.OrdinalIgnoreCase)).ToList(); var byDir = umaps.GroupBy(k => k[..k.LastIndexOf('/')]).ToDictionary(g => g.Key, g => g.Select(x => Path.GetFileNameWithoutExtension(x)).ToList()); var res = new List(); foreach (var k in umaps.OrderBy(x => x)) { var dir = k[..k.LastIndexOf('/')]; var name = Path.GetFileNameWithoutExtension(k); if (NotPlayable.Any(x => k.Contains(x, StringComparison.OrdinalIgnoreCase))) continue; if (!byDir[dir].Any(o => o.StartsWith(name + "_", StringComparison.OrdinalIgnoreCase))) continue; List<(string level, string cls, string props)> subs; try { subs = StreamingLevels(provider, k); } catch (Exception e) { log?.Invoke($"{k}: 레벨 목록 실패 {e.Message}"); continue; } var levels = subs.Where(x => x.cls == "LevelStreamingAlwaysLoaded" && IsVisualLevel(x.level)) .Select(x => "ShooterGame/Content" + x.level["/Game".Length..] + ".umap") .Where(provider.Files.ContainsKey).ToList(); if (levels.Count == 0) continue; levels.Insert(0, k); res.Add(new MapInfo(name, "/Game" + k["ShooterGame/Content".Length..^".umap".Length], levels)); } return res; } private static readonly string[] NotPlayable = ["/Poveglia", "/NPE", "/Menu/", "/Init/", "/Pregame", "_Copy/", "/SharedSubLevels/"]; // 보이는 모양이 아닌 하위 레벨 (게임 규칙 · 빛 · 소리 · 부피 · 길찾기 · 카메라 · 효과 · 먼 배경 · 이벤트 장식) private static readonly string[] NotVisual = ["Gameplay", "Lighting", "Volume", "NavMesh", "Audio", "Sound", "_BV", "Callout", "VFX", "_FX", "Vista", "Background", "Decal", "Activation", "Birthday", "Snow", "LiveStory", "Sequence", "Camera", "Probe", "Profiling", "Greybox", "Blockout", "Design", "Layout", "Lanes", "Mode_", "Spawns_Proto", "SpawnPoints", "Skybox", "AuxiliaryZones", "KillVol", "Barrier", "Collider"]; /// 영구 레벨이 불러오는 하위 레벨들 (패키지 경로 · 형 · b 로 시작하는 속성) public static List<(string level, string cls, string props)> StreamingLevels(IFileProvider provider, string umap) { var res = new List<(string, string, string)>(); var pkg = provider.LoadPackage(umap); var world = pkg.GetExports().OfType().FirstOrDefault(); if (world?.StreamingLevels == null) return res; foreach (var r in world.StreamingLevels) { var ls = r.Load(); if (ls?.WorldAsset == null) continue; var pkgName = ls.WorldAsset.Value.AssetPathName.Text.Split('.')[0]; var props = string.Join(' ', ls.Properties.Where(p => p.Name.Text.StartsWith('b')).Select(p => $"{p.Name.Text}={p.Tag?.GenericValue}")); res.Add((pkgName, ls.ExportType, props)); } return res; } public static bool IsVisualLevel(string level) { var name = level[(level.LastIndexOf('/') + 1)..]; return !NotVisual.Any(x => name.Contains(x, StringComparison.OrdinalIgnoreCase)); } // 부피 · 하늘 · 먼 배경 · 블록아웃 메시는 뺀다 private static readonly string[] SkipMesh = ["Box_For_Volumes", "BasicShapes/Plane", "Blockout", "/Sky", "Vista", "EditorMeshes", "Cubemap"]; private sealed record Sec(int First, int Tris, int Mat); /// 메시 한 LOD: 위치 · 법선 · UV · 정점 색 · 삼각형 · 재질 구간 private sealed record Geo(FVector[] V, FVector[] N, float[] Uv, FColor[]? Col, uint[] Ib, Sec[] Secs, int Lod); /// 재질 하나 = glb primitive 하나. 같은 재질의 배치를 모두 모은다 private sealed class Bucket { public readonly List Pos = new(), Nrm = new(), Uv = new(); public readonly List Col = new(); public readonly List Idx = new(); } /// 사이트에 쓸 재질 요약. Blend = 정점 색(R)으로 두 층(A · B) 섞기 private sealed record Mat(string Key, string Name, string? TexA, string? TexB, float[] TintA, float[] TintB, bool Masked, bool Translucent, bool Blend); private readonly Dictionary _mats = new(); private readonly Dictionary _buckets = new(); private long _verts, _tris; private Mat? MaterialOf(UMaterialInterface? m) { var key = m?.GetPathName() ?? ""; if (_mats.TryGetValue(key, out var r)) return r; if (m == null) return _mats[key] = new Mat("", "(없음)", null, null, [0.6f, 0.6f, 0.6f], [1, 1, 1], false, false, false); var p = MatParams.Resolve(m); if (p.BlendMode is "BLEND_Additive" or "BLEND_Modulate" || p.Base.Contains("/VFX/Materials/")) return _mats[key] = null; string? Tex(params string[] names) => names.Select(n => p.Textures.GetValueOrDefault(n)).FirstOrDefault(x => !string.IsNullOrEmpty(x)) ?? (names.Length > 0 && names[0] == "Diffuse" ? p.Textures.FirstOrDefault(kv => kv.Key.Contains("Diffuse", StringComparison.OrdinalIgnoreCase) && !kv.Key.Contains("Low") && !kv.Key.EndsWith(" B")).Value : null); float[] Tint(params string[] names) { foreach (var n in names) if (p.Vectors.TryGetValue(n, out var c)) return [c.R, c.G, c.B]; return [1, 1, 1]; } var blend = p.Base.Contains("Blend", StringComparison.OrdinalIgnoreCase) && p.Textures.ContainsKey("Diffuse B"); var texA = blend ? Tex("Diffuse A", "Diffuse") : Tex("Diffuse", "Diffuse A", "Albedo", "Base Color", "Texture"); var tintA = blend ? Tint("Layer A Tint") : Tint("DiffuseColor", "Diffuse Tint", "Layer A Tint", "Tree Base Color"); // 텍스처가 없으면 색만 (없으면 회색) if (texA == null && !p.Vectors.Keys.Any(k => k is "DiffuseColor" or "Diffuse Tint" or "Layer A Tint" or "Tree Base Color")) tintA = [0.6f, 0.6f, 0.6f]; return _mats[key] = new Mat(key, m.Name, texA, blend ? p.Textures["Diffuse B"] : null, tintA, Tint("Layer B Tint"), p.BlendMode == "BLEND_Masked", p.BlendMode == "BLEND_Translucent", blend); } public static MapScene Build(IFileProvider provider, MapInfo map) { var s = new MapScene { _code = map.Code, _mapUrl = map.MapUrl }; var meshCache = new Dictionary(); foreach (var level in map.Levels) { s._levels.Add(Path.GetFileNameWithoutExtension(level)); var pkg = provider.LoadPackage(level); foreach (var lazy in pkg.ExportsLazy) { UObject o; try { o = lazy.Value; } catch { s.Failed++; continue; } if (o is not UStaticMeshComponent c) continue; if (c.GetOrDefault("bHiddenInGame") || !c.GetOrDefault("bVisible", true)) { s.Skipped++; continue; } UStaticMesh? mesh; try { mesh = c.GetStaticMesh()?.Load(); } catch { s.Failed++; continue; } if (mesh == null) continue; var path = mesh.GetPathName(); if (SkipMesh.Any(x => path.Contains(x, StringComparison.OrdinalIgnoreCase))) { s.Skipped++; continue; } if (!meshCache.TryGetValue(path, out var geo)) { try { geo = Geometry(mesh); } catch { geo = null; s.Failed++; } meshCache[path] = geo; if (geo != null) s.Meshes++; } if (geo == null) continue; FTransform w; try { w = c.GetAbsoluteTransform(); } catch { s.Failed++; continue; } // 재질: 컴포넌트가 바꾼 것 먼저, 없으면 메시 기본 var over = c.GetOrDefault("OverrideMaterials", []); var mats = new Mat?[geo.Secs.Length]; for (var k = 0; k < geo.Secs.Length; k++) { var mi = geo.Secs[k].Mat; var idx = mi < over.Length && over[mi] is { IsNull: false } ov ? ov : mi < mesh.StaticMaterials.Length ? mesh.StaticMaterials[mi].MaterialInterface : null; UMaterialInterface? mat = null; try { mat = idx?.Load(); } catch { } mats[k] = s.MaterialOf(mat); } // 칠한 정점 색 (두 층 섞기에 쓴다): 컴포넌트 값이 정점 수와 맞을 때만 var col = geo.Col; if (c is { LODData: { } lodData } && geo.Lod < lodData.Length && lodData[geo.Lod].OverrideVertexColors is { Data: { } oc } && oc.Length == geo.V.Length) col = oc; if (c is UInstancedStaticMeshComponent ism && ism.PerInstanceSMData is { Length: > 0 } data) foreach (var d in data) s.Add(geo, mats, col, d.TransformData * w); else s.Add(geo, mats, col, w); } } return s; } /// 가장 낮은 LOD (사이트에서 쓰기에 충분하고 가볍다) private static Geo? Geometry(UStaticMesh mesh) { var lods = mesh.RenderData?.LODs; if (lods == null) return null; for (var l = lods.Length - 1; l >= 0; l--) { var lod = lods[l]; if (lod.SkipLod || lod.PositionVertexBuffer?.Verts is not { Length: > 0 } v || lod.IndexBuffer?.Buffer is not { Length: > 0 } ib) continue; var uvItems = lod.VertexBuffer?.UV; var n = new FVector[v.Length]; var uv = new float[v.Length * 2]; if (uvItems != null && uvItems.Length == v.Length) for (var i = 0; i < v.Length; i++) { var it = uvItems[i]; if (it.Normal is { Length: >= 3 }) n[i] = (FVector)it.Normal[2]; if (it.UV is { Length: > 0 }) { uv[i * 2] = it.UV[0].U; uv[i * 2 + 1] = it.UV[0].V; } } var col = lod.ColorVertexBuffer?.Data is { } cd && cd.Length == v.Length ? cd : null; var secs = lod.Sections.Select(x => new Sec(x.FirstIndex, x.NumTriangles, x.MaterialIndex)).ToArray(); return new Geo(v, n, uv, col, ib.Select(x => (uint)x).ToArray(), secs, l); } return null; } private void Add(Geo g, Mat?[] mats, FColor[]? col, FTransform t) { Placements++; // UE 는 왼손 좌표계라 glTF(오른손, 반시계 앞면) 로 그대로 옮기면 앞뒤가 바뀐다 → 기본으로 뒤집는다. // 크기가 음수(거울)면 한 번 더 뒤집힌다 var flip = t.Scale3D.X * t.Scale3D.Y * t.Scale3D.Z >= 0; var inv = new FVector(1 / NonZero(t.Scale3D.X), 1 / NonZero(t.Scale3D.Y), 1 / NonZero(t.Scale3D.Z)); for (var k = 0; k < g.Secs.Length; k++) { var m = mats[k]; if (m == null) continue; var sec = g.Secs[k]; if (!_buckets.TryGetValue(m.Key, out var b)) _buckets[m.Key] = b = new Bucket(); // 이 구간이 쓰는 정점만 옮긴다 var map = new Dictionary(); uint Vert(uint i) { if (map.TryGetValue(i, out var o)) return o; o = (uint)(b.Pos.Count / 3); var p = t.TransformPosition(g.V[i]); b.Pos.Add(p.X); b.Pos.Add(p.Y); b.Pos.Add(p.Z); var nn = t.Rotation.RotateVector(g.N[i] * inv); var len = nn.Size(); if (len > 1e-6f) nn /= len; b.Nrm.Add(nn.X); b.Nrm.Add(nn.Y); b.Nrm.Add(nn.Z); b.Uv.Add(g.Uv[i * 2]); b.Uv.Add(g.Uv[i * 2 + 1]); if (m.Blend) { var c = col != null ? col[i] : new FColor(255, 255, 255, 255); b.Col.Add(c.R); b.Col.Add(c.G); b.Col.Add(c.B); b.Col.Add(c.A); } map[i] = o; _verts++; return o; } for (var j = 0; j < sec.Tris; j++) { var i0 = g.Ib[sec.First + j * 3]; var i1 = g.Ib[sec.First + j * 3 + 1]; var i2 = g.Ib[sec.First + j * 3 + 2]; b.Idx.Add(Vert(i0)); b.Idx.Add(Vert(flip ? i2 : i1)); b.Idx.Add(Vert(flip ? i1 : i2)); _tris++; } } } private static float NonZero(float v) => Math.Abs(v) < 1e-6f ? 1e-6f : v; public long Triangles => _tris; public int Materials => _buckets.Count; public int TextureCount, TextureFailed; public long TextureBytes; /// glb: 재질마다 primitive 하나 (위치는 UE 월드 cm 그대로, 법선 · UV · (두 층 재질만) 정점 색). /// 재질은 baseColor 텍스처 + 색(tint). 두 층 재질은 extras.blend = {texture, tint} 로 B 층을 넘긴다 (사이트가 정점 색 R 로 섞는다) public void WriteGlb(IFileProvider provider, string file, int maxTex) { var glb = new Glb(); var tex = new TexStore(provider, glb, maxTex); var prims = new System.Text.Json.Nodes.JsonArray(); var byKey = _mats.Values.Where(x => x != null).ToDictionary(x => x!.Key, x => x!); foreach (var (key, b) in _buckets.OrderBy(x => x.Key, StringComparer.Ordinal)) { if (b.Idx.Count == 0) continue; var m = byKey[key]; var a = tex.Get(m.TexA); var bt = m.Blend ? tex.Get(m.TexB) : null; var pbr = new System.Text.Json.Nodes.JsonObject { ["baseColorFactor"] = new System.Text.Json.Nodes.JsonArray(Clamp(m.TintA[0]), Clamp(m.TintA[1]), Clamp(m.TintA[2]), m.Translucent ? 0.45f : 1f), ["metallicFactor"] = 0, ["roughnessFactor"] = 0.9, }; if (a is { } at) pbr["baseColorTexture"] = new System.Text.Json.Nodes.JsonObject { ["index"] = at.tex }; var mat = new System.Text.Json.Nodes.JsonObject { ["name"] = m.Name, ["pbrMetallicRoughness"] = pbr }; if (m.Translucent) mat["alphaMode"] = "BLEND"; else if (m.Masked && a is { alpha: true }) { mat["alphaMode"] = "MASK"; mat["alphaCutoff"] = 0.333; mat["doubleSided"] = true; } if (m.Blend) { var ex = new System.Text.Json.Nodes.JsonObject { ["tint"] = new System.Text.Json.Nodes.JsonArray(Clamp(m.TintB[0]), Clamp(m.TintB[1]), Clamp(m.TintB[2])) }; if (bt is { } btt) ex["texture"] = btt.tex; mat["extras"] = new System.Text.Json.Nodes.JsonObject { ["blend"] = ex }; } glb.Materials.Add(mat); var attrs = new System.Text.Json.Nodes.JsonObject { ["POSITION"] = glb.Floats(b.Pos.ToArray(), 3, bounds: true), ["NORMAL"] = glb.Floats(b.Nrm.ToArray(), 3), ["TEXCOORD_0"] = glb.Floats(b.Uv.ToArray(), 2), }; if (m.Blend) attrs["COLOR_0"] = glb.Bytes(b.Col.ToArray(), 4); prims.Add(new System.Text.Json.Nodes.JsonObject { ["attributes"] = attrs, ["indices"] = glb.Indices(b.Idx.ToArray()), ["material"] = glb.Materials.Count - 1, ["mode"] = 4 }); } glb.Meshes.Add(new System.Text.Json.Nodes.JsonObject { ["name"] = _code, ["primitives"] = prims }); glb.Nodes.Add(new System.Text.Json.Nodes.JsonObject { ["mesh"] = 0, ["name"] = _code }); glb.Write(file); TextureCount = tex.Count; TextureFailed = tex.Failed; TextureBytes = tex.Bytes; } private static float Clamp(float v) => Math.Clamp(v, 0f, 1f); public void WriteInfo(string file, string manifest, string? usmap) { var info = new { code = _code, mapUrl = _mapUrl, levels = _levels, placements = Placements, meshes = Meshes, triangles = Triangles, vertices = _verts, materials = Materials, textures = TextureCount, texturesFailed = TextureFailed, textureBytes = TextureBytes, skipped = Skipped, failed = Failed, manifest, usmap = usmap == null ? null : Path.GetFileName(usmap), extractedAt = DateTimeOffset.UtcNow.ToString("o"), }; File.WriteAllText(file, System.Text.Json.JsonSerializer.Serialize(info, new System.Text.Json.JsonSerializerOptions { WriteIndented = true })); } }