- 추출기: 레벨 BuiltData 의 HQ 라이트맵을 메시 칸마다 풀어 irr · sh 아틀라스 두 장으로 (src/Lightmaps.cs), 조명 있는 배치는 LOD0 + 라이트맵 UV(TEXCOORD_1), ISM 은 인스턴스마다 칸 - glb: 재질 occlusionTexture(irr) · emissiveTexture(sh, 세기 0) · extras.lightmap.k. pack.py 는 맵 UV 를 실수로(-vtf, gltfpack 이 두 UV 채널을 한 범위로 양자화해서 라이트맵이 어긋나던 것) - 사이트 map3d.ts: 색 = 텍스처 × irr × K × max(0, 1 + dot(sh, N.yzx)). 옛 glb 는 그대로 - install.sh: 꺼 둔 자동 갱신 타이머는 다시 켜지 않는다 - 조사 명령 lmprobe · 작업 노트 docs/dev/replay-3d.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
423 lines
24 KiB
C#
423 lines
24 KiB
C#
using System.Text;
|
||
using CUE4Parse.FileProvider;
|
||
using CUE4Parse.UE4.Assets.Exports;
|
||
using CUE4Parse.UE4.Assets.Exports.BuildData;
|
||
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;
|
||
|
||
/// <param name="Code">영구 레벨 이름 (Ascent, HURM_Alley …)</param>
|
||
/// <param name="MapUrl">경기 기록의 맵 경로 (/Game/Maps/Ascent/Ascent)</param>
|
||
public sealed record MapInfo(string Code, string MapUrl, List<string> Levels);
|
||
|
||
/// <summary>맵 레벨들의 StaticMesh 배치를 모아 월드 좌표(UE cm) 삼각형 하나로 굽는다.
|
||
/// 재질마다 텍스처(디퓨즈, 작게 줄임) · 색을 붙인다.</summary>
|
||
public sealed class MapScene
|
||
{
|
||
public int Placements, Meshes, Failed, Skipped;
|
||
private readonly List<string> _levels = new();
|
||
private string _code = "";
|
||
private string _mapUrl = "";
|
||
|
||
/// <summary>맵 = 하위 레벨을 불러오는 영구 레벨 (경기 기록의 맵 경로 "/Game/Maps/Ascent/Ascent" 와 같다).
|
||
/// 연습장 · 튜토리얼 · 메뉴처럼 경기가 없는 곳은 뺀다</summary>
|
||
public static List<MapInfo> FindMaps(IFileProvider provider, Action<string>? 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<MapInfo>();
|
||
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"];
|
||
|
||
/// <summary>영구 레벨이 불러오는 하위 레벨들 (패키지 경로 · 형 · b 로 시작하는 속성)</summary>
|
||
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<CUE4Parse.UE4.Objects.Engine.UWorld>().FirstOrDefault();
|
||
if (world?.StreamingLevels == null) return res;
|
||
foreach (var r in world.StreamingLevels)
|
||
{
|
||
var ls = r.Load<CUE4Parse.UE4.Objects.Engine.ULevelStreaming>();
|
||
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);
|
||
/// <summary>메시 한 LOD: 위치 · 법선 · UV · 라이트맵 UV(LOD0 만) · 정점 색 · 삼각형 · 재질 구간</summary>
|
||
private sealed record Geo(FVector[] V, FVector[] N, float[] Uv, float[]? Lm, FColor[]? Col, uint[] Ib, Sec[] Secs, int Lod);
|
||
|
||
/// <summary>배치 하나의 라이트맵 자리: 페이지 · 페이지 안 UV = 라이트맵 UV × Scale + Bias</summary>
|
||
private readonly record struct LmPlace(int Page, float Sx, float Sy, float Bx, float By);
|
||
private readonly Lightmaps _lm = new();
|
||
public int LitPlacements;
|
||
|
||
/// <summary>재질 하나 = glb primitive 하나. 같은 재질의 배치를 모두 모은다</summary>
|
||
private sealed class Bucket
|
||
{
|
||
public readonly List<float> Pos = new(), Nrm = new(), Uv = new(), Lm = new();
|
||
public readonly List<int> LmPage = new();
|
||
public readonly List<byte> Col = new();
|
||
public readonly List<uint> Idx = new();
|
||
}
|
||
|
||
/// <summary>사이트에 쓸 재질 요약. Blend = 정점 색(R)으로 두 층(A · B) 섞기</summary>
|
||
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<string, Mat?> _mats = new();
|
||
private readonly Dictionary<string, Bucket> _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<string, Geo?>();
|
||
foreach (var level in map.Levels)
|
||
{
|
||
s._levels.Add(Path.GetFileNameWithoutExtension(level));
|
||
var pkg = provider.LoadPackage(level);
|
||
// 이 레벨에 구워 둔 조명 (<레벨>_BuiltData)
|
||
UMapBuildDataRegistry? reg = null;
|
||
var bd = level[..^".umap".Length] + "_BuiltData.uasset";
|
||
if (provider.Files.ContainsKey(bd))
|
||
try { reg = provider.LoadPackage(bd).GetExports().OfType<UMapBuildDataRegistry>().FirstOrDefault(); }
|
||
catch (Exception e) { Console.Error.WriteLine($"{bd}: 조명 읽기 실패 {e.GetType().Name} {e.Message}"); }
|
||
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<bool>("bHiddenInGame") || !c.GetOrDefault("bVisible", true)) { s.Skipped++; continue; }
|
||
UStaticMesh? mesh;
|
||
try { mesh = c.GetStaticMesh()?.Load<UStaticMesh>(); } catch { s.Failed++; continue; }
|
||
if (mesh == null) continue;
|
||
var path = mesh.GetPathName();
|
||
if (SkipMesh.Any(x => path.Contains(x, StringComparison.OrdinalIgnoreCase))) { s.Skipped++; continue; }
|
||
// 구운 조명이 있으면 LOD0 (라이트맵 UV 는 LOD0 에만 있다), 없으면 가장 낮은 LOD
|
||
FLightMap2D? lm = null;
|
||
FMeshMapBuildData? bdata = null;
|
||
if (reg?.MeshBuildData != null && c.LODData is { Length: > 0 } ld0 && reg.MeshBuildData.TryGetValue(ld0[0].MapBuildDataId, out bdata)
|
||
&& bdata.LightMap is FLightMap2D l2 && l2.Textures?[0] is { IsNull: false } && l2.ScaleVectors != null && l2.AddVectors != null) lm = l2;
|
||
var lmIdx = lm != null ? mesh.GetOrDefault("LightMapCoordinateIndex", 0) : -1;
|
||
var key = lm != null ? $"{path}#lm{lmIdx}" : path;
|
||
if (!meshCache.TryGetValue(key, out var geo))
|
||
{
|
||
try { geo = Geometry(mesh, lmIdx); } catch { geo = null; s.Failed++; }
|
||
meshCache[key] = geo;
|
||
if (geo != null) s.Meshes++;
|
||
}
|
||
if (geo == null) continue;
|
||
var page = lm != null && geo.Lm != null ? s._lm.Page(lm) : -1;
|
||
FTransform w;
|
||
try { w = c.GetAbsoluteTransform(); } catch { s.Failed++; continue; }
|
||
// 재질: 컴포넌트가 바꾼 것 먼저, 없으면 메시 기본
|
||
var over = c.GetOrDefault<FPackageIndex[]>("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<UMaterialInterface>(); } 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;
|
||
var cs = lm?.CoordinateScale ?? new FVector2D(1, 1);
|
||
LmPlace? Place(FVector2D bias)
|
||
{
|
||
if (page < 0) return null;
|
||
s._lm.Allocate(page, lm!, bias);
|
||
return new LmPlace(page, cs.X, cs.Y, bias.X, bias.Y);
|
||
}
|
||
if (c is UInstancedStaticMeshComponent ism && ism.PerInstanceSMData is { Length: > 0 } data)
|
||
{
|
||
// 인스턴스마다 라이트맵 칸이 따로 (FPerInstanceLightmapData). HISM 은 순서가 바뀌어 있을 수 있다 (InstanceReorderTable)
|
||
var per = bdata?.PerInstanceLightmapData ?? [];
|
||
var reorder = c.GetOrDefault<int[]>("InstanceReorderTable", []);
|
||
for (var i = 0; i < data.Length; i++)
|
||
{
|
||
var ri = reorder.Length == data.Length ? reorder[i] : i;
|
||
var lp = ri >= 0 && ri < per.Length ? Place(per[ri].LightmapUVBias) : null;
|
||
s.Add(geo, mats, col, data[i].TransformData * w, lp);
|
||
}
|
||
}
|
||
else s.Add(geo, mats, col, w, lm != null ? Place(lm.CoordinateBias ?? new FVector2D(0, 0)) : null);
|
||
}
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/// <summary>가장 낮은 LOD (사이트에서 쓰기에 충분하고 가볍다). lmIdx ≥ 0 이면 LOD0 + 그 UV 채널(라이트맵)</summary>
|
||
private static Geo? Geometry(UStaticMesh mesh, int lmIdx = -1)
|
||
{
|
||
var lods = mesh.RenderData?.LODs;
|
||
if (lods == null) return null;
|
||
for (var l = lmIdx >= 0 ? 0 : lods.Length - 1; l >= 0 && l < lods.Length; l += lmIdx >= 0 ? 1 : -1)
|
||
{
|
||
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];
|
||
var lmUv = lmIdx >= 0 && uvItems != null && uvItems.Length == v.Length && uvItems.All(x => x.UV != null && x.UV.Length > lmIdx) ? new float[v.Length * 2] : null;
|
||
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;
|
||
}
|
||
if (lmUv != null)
|
||
{
|
||
lmUv[i * 2] = it.UV[lmIdx].U;
|
||
lmUv[i * 2 + 1] = it.UV[lmIdx].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, lmUv, col, ib.Select(x => (uint)x).ToArray(), secs, l);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
private void Add(Geo g, Mat?[] mats, FColor[]? col, FTransform t, LmPlace? lp)
|
||
{
|
||
Placements++;
|
||
if (lp != null) LitPlacements++;
|
||
// 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, uint>();
|
||
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 (lp is { } q && g.Lm != null)
|
||
{
|
||
b.Lm.Add(g.Lm[i * 2] * q.Sx + q.Bx); b.Lm.Add(g.Lm[i * 2 + 1] * q.Sy + q.By);
|
||
b.LmPage.Add(q.Page);
|
||
}
|
||
else
|
||
{
|
||
b.Lm.Add(0); b.Lm.Add(0);
|
||
b.LmPage.Add(-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, LightmapBytes;
|
||
public string LightmapSummary => _lm.Any ? $"조명 {LitPlacements}/{Placements} 배치 · 페이지 {_lm.Pages}(실패 {_lm.Failed}) · 칸 {_lm.Allocations} · 아틀라스 {_lm.AtlasW}x{_lm.AtlasH} (1/{_lm.Factor}) {LightmapBytes >> 10}KB · K {_lm.K:F2}" : "조명 없음";
|
||
|
||
/// <summary>glb: 재질마다 primitive 하나 (위치는 UE 월드 cm 그대로, 법선 · UV · (두 층 재질만) 정점 색).
|
||
/// 재질은 baseColor 텍스처 + 색(tint). 두 층 재질은 extras.blend = {texture, tint} 로 B 층을 넘긴다 (사이트가 정점 색 R 로 섞는다)</summary>
|
||
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();
|
||
// 구운 조명 아틀라스: 모든 재질에 occlusionTexture(irr) · emissiveTexture(sh, 세기 0) 로 TEXCOORD_1 에 붙인다.
|
||
// (gltfpack 이 쓰지 않는 UV 채널을 버리지 않게 텍스처가 그 채널을 가리켜야 한다. 사이트는 둘을 떼어 조명으로 쓴다)
|
||
int? irrTex = null, shTex = null;
|
||
var lmMax = int.Parse(Environment.GetEnvironmentVariable("GVR_LIGHTMAP") ?? "2048");
|
||
if (lmMax > 0 && _lm.Build(lmMax) is var (irrJpg, shJpg))
|
||
{
|
||
irrTex = glb.Texture(irrJpg, "image/jpeg", clamp: true);
|
||
shTex = glb.Texture(shJpg, "image/jpeg", clamp: true);
|
||
LightmapBytes = irrJpg.Length + shJpg.Length;
|
||
}
|
||
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 (irrTex is { } it && shTex is { } st)
|
||
{
|
||
mat["occlusionTexture"] = new System.Text.Json.Nodes.JsonObject { ["index"] = it, ["texCoord"] = 1, ["strength"] = 1 };
|
||
mat["emissiveTexture"] = new System.Text.Json.Nodes.JsonObject { ["index"] = st, ["texCoord"] = 1 };
|
||
mat["emissiveFactor"] = new System.Text.Json.Nodes.JsonArray(0, 0, 0);
|
||
}
|
||
if (m.Translucent) mat["alphaMode"] = "BLEND";
|
||
else if (m.Masked && a is { alpha: true })
|
||
{
|
||
mat["alphaMode"] = "MASK";
|
||
mat["alphaCutoff"] = 0.333;
|
||
mat["doubleSided"] = true;
|
||
}
|
||
var extras = new System.Text.Json.Nodes.JsonObject();
|
||
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;
|
||
extras["blend"] = ex;
|
||
}
|
||
// 구운 조명: irr 텍스처 값(sRGB) 1 = 밝기 K
|
||
if (irrTex != null) extras["lightmap"] = new System.Text.Json.Nodes.JsonObject { ["k"] = _lm.K, ["v"] = 1 };
|
||
if (extras.Count > 0) mat["extras"] = extras;
|
||
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);
|
||
if (irrTex != null)
|
||
{
|
||
var lmuv = new float[b.Lm.Count];
|
||
for (var i = 0; i < b.LmPage.Count; i++)
|
||
(lmuv[i * 2], lmuv[i * 2 + 1]) = _lm.ToAtlas(b.LmPage[i], b.Lm[i * 2], b.Lm[i * 2 + 1]);
|
||
attrs["TEXCOORD_1"] = glb.Floats(lmuv, 2);
|
||
}
|
||
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,
|
||
lightmap = _lm.Any ? new { placements = LitPlacements, pages = _lm.Pages, failed = _lm.Failed, allocations = _lm.Allocations, atlas = new[] { _lm.AtlasW, _lm.AtlasH }, factor = _lm.Factor, bytes = LightmapBytes, k = _lm.K } : null,
|
||
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 }));
|
||
}
|
||
}
|