render: unified MultiMeshPartPool with MechPaint, PomDecalPool, and per-weapon VFX
This commit is contained in:
parent
3e499c31ee
commit
2529ba62d2
9 changed files with 2593 additions and 871 deletions
|
|
@ -41,6 +41,13 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
NodePath RelativePath,
|
||||
bool HasProtectedDescendant);
|
||||
|
||||
public sealed record RigidPartTemplate(
|
||||
NodePath PivotPath,
|
||||
ArrayMesh Mesh,
|
||||
int CellX,
|
||||
int CellY,
|
||||
string Kind);
|
||||
|
||||
public sealed class VehicleTemplate
|
||||
{
|
||||
public required VehiclePackage Package { get; init; }
|
||||
|
|
@ -48,12 +55,18 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
public required Node3D SceneTemplate { get; init; }
|
||||
public required ArrayMesh BakedStaticMesh { get; init; }
|
||||
public required ArrayMesh BakedLodFarMesh { get; init; }
|
||||
public required ArrayMesh SingleSurfaceStaticMesh { get; init; }
|
||||
public required ArrayMesh SingleSurfaceLodFarMesh { get; init; }
|
||||
public required Vector3[] FarRayTris { get; init; }
|
||||
public required Aabb FarRayAabb { get; init; }
|
||||
public required float MinY { get; init; }
|
||||
public required float MaxY { get; init; }
|
||||
public required IReadOnlyDictionary<string, GlbAnchorInfo> Anchors { get; init; }
|
||||
public required IReadOnlyDictionary<string, GlbJointInfo> Joints { get; init; }
|
||||
public required IReadOnlyDictionary<string, NodePath> NodesByOriginalName { get; init; }
|
||||
public required IReadOnlyList<GlbPartNodeInfo> DetachableParts { get; init; }
|
||||
public required IReadOnlyList<RigidPartTemplate> StaticRigidParts { get; init; }
|
||||
public required IReadOnlyList<RigidPartTemplate> DetachableRigidParts { get; init; }
|
||||
public required IReadOnlyList<NodePath> MobilityNodePaths { get; init; }
|
||||
}
|
||||
|
||||
|
|
@ -63,9 +76,11 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
public required string GlbPath { get; init; }
|
||||
public required Node3D SceneTemplate { get; init; }
|
||||
public required ArrayMesh BakedDropMesh { get; init; }
|
||||
public required ArrayMesh SingleSurfaceMesh { get; init; }
|
||||
public required Vector3 Size { get; init; }
|
||||
public required float MinY { get; init; }
|
||||
public required Vector3 LocalModelForward { get; init; }
|
||||
public required IReadOnlyDictionary<string, Vector3> PartCentroidsLocal { get; init; }
|
||||
}
|
||||
|
||||
public static readonly string[] ExpectedGearIds =
|
||||
|
|
@ -75,6 +90,7 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
|
||||
private readonly Dictionary<string, VehicleTemplate> _vehicles = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, GearTemplate> _gear = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, ArrayMesh> _gearVariantMeshes = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly string _vehiclesDir;
|
||||
private readonly string _gearDir;
|
||||
private bool _disposed;
|
||||
|
|
@ -153,6 +169,26 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
return template;
|
||||
}
|
||||
|
||||
public ArrayMesh GetOrBuildGearVariantMesh(
|
||||
string moduleId,
|
||||
string gearGlbId,
|
||||
Color? paintTint,
|
||||
Color? accentTint)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
if (_gearVariantMeshes.TryGetValue(moduleId, out ArrayMesh? cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
GearTemplate tpl = GetOrLoadGear(gearGlbId);
|
||||
ArrayMesh mesh = !paintTint.HasValue && !accentTint.HasValue
|
||||
? tpl.SingleSurfaceMesh
|
||||
: CollapseToSingleSurfaceMechPaintMesh(tpl.BakedDropMesh, paintTint, accentTint);
|
||||
_gearVariantMeshes[moduleId] = mesh;
|
||||
return mesh;
|
||||
}
|
||||
|
||||
private static VehicleTemplate LoadVehicleTemplate(VehiclePackage pkg, string glbPath)
|
||||
{
|
||||
byte[] jsonBytes = ReadGlbJsonChunk(glbPath);
|
||||
|
|
@ -443,10 +479,24 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
}
|
||||
|
||||
// C: Build single-node BakedStaticMesh + decimated BakedLodFarMesh (>130m) per template
|
||||
ArrayMesh bakedStaticMesh = BuildBakedStaticMesh(sceneRoot, coarseMaterials: false);
|
||||
ArrayMesh bakedCoarseStatic = BuildBakedStaticMesh(sceneRoot, coarseMaterials: true);
|
||||
ArrayMesh bakedStaticMesh = BuildBakedStaticMesh(sceneRoot, coarseMaterials: false, bakeVertexColor: false);
|
||||
ArrayMesh bakedCoarseStatic = BuildBakedStaticMesh(sceneRoot, coarseMaterials: true, bakeVertexColor: false);
|
||||
ArrayMesh bakedLodFarMesh = DecimateMeshGrid(bakedCoarseStatic, cellSize: 0.45f, targetMinY: minY);
|
||||
|
||||
ArrayMesh singleSurfaceStatic = CollapseToSingleSurfaceMechPaintMesh(bakedStaticMesh);
|
||||
ArrayMesh singleSurfaceLodFar = CollapseToSingleSurfaceMechPaintMesh(bakedLodFarMesh);
|
||||
Vector3[] farRayTris = ExtractFlatTriangles(singleSurfaceLodFar);
|
||||
Aabb farRayAabb = singleSurfaceLodFar.GetAabb().Grow(0.20f);
|
||||
|
||||
// D: Extract single-surface MechPaint rigid parts (static limbs + detachable armor chunks)
|
||||
// and strip all MeshInstance3D nodes from SceneTemplate so live rigs have zero MeshInstance3D children!
|
||||
ExtractRigidPartsAndStripMeshes(
|
||||
sceneRoot,
|
||||
protectedNodes,
|
||||
mergedDetachableInfos,
|
||||
out var staticRigidParts,
|
||||
out var detachableRigidParts);
|
||||
|
||||
return new VehicleTemplate
|
||||
{
|
||||
Package = pkg,
|
||||
|
|
@ -454,12 +504,18 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
SceneTemplate = sceneRoot,
|
||||
BakedStaticMesh = bakedStaticMesh,
|
||||
BakedLodFarMesh = bakedLodFarMesh,
|
||||
SingleSurfaceStaticMesh = singleSurfaceStatic,
|
||||
SingleSurfaceLodFarMesh = singleSurfaceLodFar,
|
||||
FarRayTris = farRayTris,
|
||||
FarRayAabb = farRayAabb,
|
||||
MinY = minY,
|
||||
MaxY = maxY,
|
||||
Anchors = anchors,
|
||||
Joints = joints,
|
||||
NodesByOriginalName = survivingNames,
|
||||
DetachableParts = mergedDetachableInfos,
|
||||
StaticRigidParts = staticRigidParts,
|
||||
DetachableRigidParts = detachableRigidParts,
|
||||
MobilityNodePaths = mobilityPaths
|
||||
};
|
||||
}
|
||||
|
|
@ -489,8 +545,10 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
ClearOwnerRecursive(sceneRoot);
|
||||
|
||||
ComputeSceneMeshBounds(sceneRoot, out Vector3 bMin, out Vector3 bMax);
|
||||
var partCentroids = CollectGearPartCentroids(sceneRoot);
|
||||
MergeGearSubMeshes(sceneRoot, gearId);
|
||||
ArrayMesh bakedDropMesh = BuildBakedStaticMesh(sceneRoot, coarseMaterials: false, bakeVertexColor: false);
|
||||
ArrayMesh singleSurfaceMesh = CollapseToSingleSurfaceMechPaintMesh(bakedDropMesh);
|
||||
Vector3 localFwd = new Vector3(0f, 0f, 1f);
|
||||
|
||||
return new GearTemplate
|
||||
|
|
@ -499,9 +557,11 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
GlbPath = glbPath,
|
||||
SceneTemplate = sceneRoot,
|
||||
BakedDropMesh = bakedDropMesh,
|
||||
SingleSurfaceMesh = singleSurfaceMesh,
|
||||
Size = bMax - bMin,
|
||||
MinY = bMin.Y,
|
||||
LocalModelForward = localFwd
|
||||
LocalModelForward = localFwd,
|
||||
PartCentroidsLocal = partCentroids
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -887,6 +947,453 @@ public sealed class VehicleAssetCache : IDisposable
|
|||
return BuildMergedArrayMesh(items, coarseMaterials, bakeVertexColor) ?? new ArrayMesh();
|
||||
}
|
||||
|
||||
private static Dictionary<string, Vector3> CollectGearPartCentroids(Node3D sceneRoot)
|
||||
{
|
||||
var result = new Dictionary<string, Vector3>(StringComparer.OrdinalIgnoreCase);
|
||||
var allMeshes = new List<MeshInstance3D>();
|
||||
CollectAllMeshInstances(sceneRoot, allMeshes);
|
||||
|
||||
for (int i = 0; i < allMeshes.Count; i++)
|
||||
{
|
||||
MeshInstance3D mi = allMeshes[i];
|
||||
if (!PreservedGearPartNames.Contains(mi.Name) || mi.Mesh is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Transform3D toRoot = ComputeRelativeTransform(sceneRoot, mi);
|
||||
Vector3 sum = Vector3.Zero;
|
||||
int count = 0;
|
||||
int surfCount = mi.Mesh.GetSurfaceCount();
|
||||
for (int s = 0; s < surfCount; s++)
|
||||
{
|
||||
var arr = mi.Mesh.SurfaceGetArrays(s);
|
||||
if (arr.Count > (int)Mesh.ArrayType.Vertex &&
|
||||
arr[(int)Mesh.ArrayType.Vertex].VariantType == Variant.Type.PackedVector3Array)
|
||||
{
|
||||
Vector3[] verts = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
|
||||
for (int v = 0; v < verts.Length; v++)
|
||||
{
|
||||
sum += toRoot * verts[v];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count > 0)
|
||||
{
|
||||
result[mi.Name] = sum / count;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void ExtractRigidPartsAndStripMeshes(
|
||||
Node3D sceneRoot,
|
||||
HashSet<Node> protectedNodes,
|
||||
List<GlbPartNodeInfo> detachableInfos,
|
||||
out List<RigidPartTemplate> staticPartsOut,
|
||||
out List<RigidPartTemplate> detachablePartsOut)
|
||||
{
|
||||
staticPartsOut = new List<RigidPartTemplate>();
|
||||
detachablePartsOut = new List<RigidPartTemplate>();
|
||||
|
||||
var detachableSet = new Dictionary<MeshInstance3D, GlbPartNodeInfo>();
|
||||
for (int i = 0; i < detachableInfos.Count; i++)
|
||||
{
|
||||
var dp = detachableInfos[i];
|
||||
if (sceneRoot.GetNodeOrNull(dp.RelativePath) is MeshInstance3D dmi)
|
||||
{
|
||||
detachableSet[dmi] = dp;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Extract detachable per-cell armor chunks in their rigidParent's local space
|
||||
foreach (var (mi, dp) in detachableSet)
|
||||
{
|
||||
if (!CanMergeMeshInstance(mi) || mi.Mesh is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Node3D rigidParent = protectedNodes.Contains(mi)
|
||||
? mi
|
||||
: FindNearestProtectedParent(mi, sceneRoot, protectedNodes);
|
||||
Transform3D relXf = ReferenceEquals(mi, rigidParent)
|
||||
? Transform3D.Identity
|
||||
: ComputeRelativeTransform(rigidParent, mi);
|
||||
var single = new List<(Mesh Mesh, Transform3D RelXf)>(1) { (mi.Mesh, relXf) };
|
||||
ArrayMesh? collapsed = CollapseToSingleSurfaceMechPaintMesh(single);
|
||||
if (collapsed is not null)
|
||||
{
|
||||
detachablePartsOut.Add(new RigidPartTemplate(
|
||||
BuildRelativeNodePath(sceneRoot, rigidParent),
|
||||
collapsed,
|
||||
dp.CellX,
|
||||
dp.CellY,
|
||||
dp.Kind));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Group remaining non-detachable rigid meshes by rigidParent and collapse each to 1 surface
|
||||
var allMeshes = new List<MeshInstance3D>();
|
||||
CollectAllMeshInstances(sceneRoot, allMeshes);
|
||||
var staticByPivot = new Dictionary<Node3D, List<(Mesh Mesh, Transform3D RelXf)>>();
|
||||
for (int i = 0; i < allMeshes.Count; i++)
|
||||
{
|
||||
MeshInstance3D mi = allMeshes[i];
|
||||
if (detachableSet.ContainsKey(mi) || !CanMergeMeshInstance(mi) || mi.Mesh is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Node3D rigidParent = protectedNodes.Contains(mi)
|
||||
? mi
|
||||
: FindNearestProtectedParent(mi, sceneRoot, protectedNodes);
|
||||
Transform3D relXf = ReferenceEquals(mi, rigidParent)
|
||||
? Transform3D.Identity
|
||||
: ComputeRelativeTransform(rigidParent, mi);
|
||||
if (!staticByPivot.TryGetValue(rigidParent, out var list))
|
||||
{
|
||||
list = new List<(Mesh, Transform3D)>();
|
||||
staticByPivot[rigidParent] = list;
|
||||
}
|
||||
list.Add((mi.Mesh, relXf));
|
||||
}
|
||||
|
||||
foreach (var (rigidParent, items) in staticByPivot)
|
||||
{
|
||||
ArrayMesh? collapsed = CollapseToSingleSurfaceMechPaintMesh(items);
|
||||
if (collapsed is not null)
|
||||
{
|
||||
staticPartsOut.Add(new RigidPartTemplate(
|
||||
BuildRelativeNodePath(sceneRoot, rigidParent),
|
||||
collapsed,
|
||||
-1,
|
||||
-1,
|
||||
"static"));
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Strip all MeshInstance3D nodes from sceneRoot:
|
||||
// - Protected MeshInstance3Ds become plain Node3D pivots preserving Name, Transform, and children.
|
||||
// - Non-protected MeshInstance3Ds are removed cleanly.
|
||||
for (int i = 0; i < allMeshes.Count; i++)
|
||||
{
|
||||
MeshInstance3D mi = allMeshes[i];
|
||||
if (!GodotObject.IsInstanceValid(mi) || ReferenceEquals(mi, sceneRoot))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (protectedNodes.Contains(mi))
|
||||
{
|
||||
ReplaceMeshInstanceWithPivot(mi);
|
||||
}
|
||||
else
|
||||
{
|
||||
ReparentChildrenAndRemove(mi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReplaceMeshInstanceWithPivot(MeshInstance3D mi)
|
||||
{
|
||||
Node? parent = mi.GetParent();
|
||||
if (parent is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int idx = mi.GetIndex();
|
||||
var pivot = new Node3D
|
||||
{
|
||||
Name = mi.Name,
|
||||
Transform = mi.Transform
|
||||
};
|
||||
parent.RemoveChild(mi);
|
||||
parent.AddChild(pivot);
|
||||
parent.MoveChild(pivot, idx);
|
||||
while (mi.GetChildCount() > 0)
|
||||
{
|
||||
Node ch = mi.GetChild(0);
|
||||
mi.RemoveChild(ch);
|
||||
pivot.AddChild(ch);
|
||||
}
|
||||
mi.QueueFree();
|
||||
}
|
||||
|
||||
private static Vector3[] ExtractFlatTriangles(ArrayMesh mesh)
|
||||
{
|
||||
var tris = new List<Vector3>();
|
||||
int surfCount = mesh.GetSurfaceCount();
|
||||
for (int s = 0; s < surfCount; s++)
|
||||
{
|
||||
var arr = mesh.SurfaceGetArrays(s);
|
||||
if (arr.Count <= (int)Mesh.ArrayType.Vertex ||
|
||||
arr[(int)Mesh.ArrayType.Vertex].VariantType != Variant.Type.PackedVector3Array)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Vector3[] verts = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
|
||||
int[]? indices = arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array
|
||||
? arr[(int)Mesh.ArrayType.Index].AsInt32Array()
|
||||
: null;
|
||||
int triCount = (indices is not null ? indices.Length : verts.Length) / 3;
|
||||
for (int t = 0; t < triCount; t++)
|
||||
{
|
||||
int i0 = indices is not null ? indices[t * 3] : t * 3;
|
||||
int i1 = indices is not null ? indices[t * 3 + 1] : t * 3 + 1;
|
||||
int i2 = indices is not null ? indices[t * 3 + 2] : t * 3 + 2;
|
||||
tris.Add(verts[i0]);
|
||||
tris.Add(verts[i1]);
|
||||
tris.Add(verts[i2]);
|
||||
}
|
||||
}
|
||||
return tris.ToArray();
|
||||
}
|
||||
|
||||
public static ArrayMesh CollapseToSingleSurfaceMechPaintMesh(
|
||||
ArrayMesh src,
|
||||
Color? gearPaintTint = null,
|
||||
Color? gearAccentTint = null)
|
||||
{
|
||||
var list = new List<(Mesh Mesh, Transform3D RelXf)>(1) { (src, Transform3D.Identity) };
|
||||
return CollapseToSingleSurfaceMechPaintMesh(list, gearPaintTint, gearAccentTint) ?? new ArrayMesh();
|
||||
}
|
||||
|
||||
private static ArrayMesh? CollapseToSingleSurfaceMechPaintMesh(
|
||||
List<(Mesh Mesh, Transform3D RelXf)> sources,
|
||||
Color? gearPaintTint = null,
|
||||
Color? gearAccentTint = null)
|
||||
{
|
||||
int totalVerts = 0;
|
||||
int totalIndices = 0;
|
||||
var chunks = new List<(Godot.Collections.Array Arrays, Transform3D RelXf, Vector3 LinearCol, int Band, float Rough, int VertCount, int IdxCount)>();
|
||||
|
||||
for (int i = 0; i < sources.Count; i++)
|
||||
{
|
||||
var (mesh, relXf) = sources[i];
|
||||
int surfCount = mesh.GetSurfaceCount();
|
||||
var am = mesh as ArrayMesh;
|
||||
for (int s = 0; s < surfCount; s++)
|
||||
{
|
||||
if (am is not null && am.SurfaceGetPrimitiveType(s) != Mesh.PrimitiveType.Triangles)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var arr = mesh.SurfaceGetArrays(s);
|
||||
if (arr.Count <= (int)Mesh.ArrayType.Vertex ||
|
||||
arr[(int)Mesh.ArrayType.Vertex].VariantType != Variant.Type.PackedVector3Array)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
Vector3[] verts = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
|
||||
if (verts.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int idxCount = verts.Length;
|
||||
if (arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array)
|
||||
{
|
||||
idxCount = arr[(int)Mesh.ArrayType.Index].AsInt32Array().Length;
|
||||
}
|
||||
if (idxCount < 3)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ClassifyMaterialForMechPaint(mesh.SurfaceGetMaterial(s), gearPaintTint, gearAccentTint, out Vector3 linCol, out int band, out float rough);
|
||||
chunks.Add((arr, relXf, linCol, band, rough, verts.Length, idxCount));
|
||||
totalVerts += verts.Length;
|
||||
totalIndices += idxCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalVerts == 0 || totalIndices == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var outVerts = new Vector3[totalVerts];
|
||||
var outNorms = new Vector3[totalVerts];
|
||||
var outColors = new Color[totalVerts];
|
||||
var outUvs = new Vector2[totalVerts];
|
||||
var outUv2s = new Vector2[totalVerts];
|
||||
var outIndices = new int[totalIndices];
|
||||
|
||||
int vOff = 0;
|
||||
int iOff = 0;
|
||||
for (int c = 0; c < chunks.Count; c++)
|
||||
{
|
||||
var (arr, relXf, linCol, band, rough, vCount, idxCount) = chunks[c];
|
||||
Vector3[] srcV = arr[(int)Mesh.ArrayType.Vertex].AsVector3Array();
|
||||
bool flipWinding = relXf.Basis.Determinant() < 0f;
|
||||
Basis normBasis = relXf.Basis.Inverse().Transposed();
|
||||
|
||||
for (int v = 0; v < vCount; v++)
|
||||
{
|
||||
outVerts[vOff + v] = relXf * srcV[v];
|
||||
}
|
||||
|
||||
if (arr.Count > (int)Mesh.ArrayType.Normal && arr[(int)Mesh.ArrayType.Normal].VariantType == Variant.Type.PackedVector3Array)
|
||||
{
|
||||
Vector3[] srcN = arr[(int)Mesh.ArrayType.Normal].AsVector3Array();
|
||||
for (int v = 0; v < vCount; v++)
|
||||
{
|
||||
Vector3 n = normBasis * srcN[v];
|
||||
outNorms[vOff + v] = n.LengthSquared() > 1e-10f ? n.Normalized() : Vector3.Up;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int v = 0; v < vCount; v++) outNorms[vOff + v] = Vector3.Up;
|
||||
}
|
||||
|
||||
if (arr.Count > (int)Mesh.ArrayType.Color && arr[(int)Mesh.ArrayType.Color].VariantType == Variant.Type.PackedColorArray)
|
||||
{
|
||||
Array.Copy(arr[(int)Mesh.ArrayType.Color].AsColorArray(), 0, outColors, vOff, vCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Gear or unbaked submesh: neutral cavity AO, crisp edge highlight, steady rest height above mud line
|
||||
var defaultCol = new Color(0.86f, 0.36f, 0.50f, 0.65f);
|
||||
for (int v = 0; v < vCount; v++) outColors[vOff + v] = defaultCol;
|
||||
}
|
||||
|
||||
var uv0 = new Vector2(linCol.X, linCol.Y);
|
||||
var uv1 = new Vector2(linCol.Z, band * 2.0f + rough);
|
||||
for (int v = 0; v < vCount; v++)
|
||||
{
|
||||
outUvs[vOff + v] = uv0;
|
||||
outUv2s[vOff + v] = uv1;
|
||||
}
|
||||
|
||||
bool hasIdx = arr.Count > (int)Mesh.ArrayType.Index && arr[(int)Mesh.ArrayType.Index].VariantType == Variant.Type.PackedInt32Array;
|
||||
int[]? srcIdx = hasIdx ? arr[(int)Mesh.ArrayType.Index].AsInt32Array() : null;
|
||||
if (!flipWinding)
|
||||
{
|
||||
if (srcIdx is not null)
|
||||
{
|
||||
for (int k = 0; k < idxCount; k++) outIndices[iOff + k] = srcIdx[k] + vOff;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int k = 0; k < idxCount; k++) outIndices[iOff + k] = k + vOff;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int k = 0; k + 2 < idxCount; k += 3)
|
||||
{
|
||||
int a = srcIdx is not null ? srcIdx[k] : k;
|
||||
int bIdx = srcIdx is not null ? srcIdx[k + 1] : k + 1;
|
||||
int cIdx = srcIdx is not null ? srcIdx[k + 2] : k + 2;
|
||||
outIndices[iOff + k] = a + vOff;
|
||||
outIndices[iOff + k + 1] = cIdx + vOff;
|
||||
outIndices[iOff + k + 2] = bIdx + vOff;
|
||||
}
|
||||
}
|
||||
|
||||
vOff += vCount;
|
||||
iOff += idxCount;
|
||||
}
|
||||
|
||||
var surfArrays = new Godot.Collections.Array();
|
||||
surfArrays.Resize((int)Mesh.ArrayType.Max);
|
||||
surfArrays[(int)Mesh.ArrayType.Vertex] = outVerts;
|
||||
surfArrays[(int)Mesh.ArrayType.Normal] = outNorms;
|
||||
surfArrays[(int)Mesh.ArrayType.Color] = outColors;
|
||||
surfArrays[(int)Mesh.ArrayType.TexUV] = outUvs;
|
||||
surfArrays[(int)Mesh.ArrayType.TexUV2] = outUv2s;
|
||||
surfArrays[(int)Mesh.ArrayType.Index] = outIndices;
|
||||
|
||||
var outMesh = new ArrayMesh();
|
||||
outMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfArrays);
|
||||
return outMesh;
|
||||
}
|
||||
|
||||
private static void ClassifyMaterialForMechPaint(
|
||||
Material? mat,
|
||||
Color? gearPaintTint,
|
||||
Color? gearAccentTint,
|
||||
out Vector3 linearColor,
|
||||
out int band,
|
||||
out float roughness)
|
||||
{
|
||||
if (mat is not StandardMaterial3D sm)
|
||||
{
|
||||
linearColor = new Vector3(0.08f, 0.09f, 0.06f);
|
||||
band = 3;
|
||||
roughness = 0.58f;
|
||||
return;
|
||||
}
|
||||
|
||||
string mName = (sm.ResourceName ?? string.Empty).ToLowerInvariant();
|
||||
roughness = Math.Clamp(sm.Roughness, 0.08f, 0.90f);
|
||||
|
||||
if (mName.Contains("lamp") || mName.Contains("glow") || mName.Contains("cyan") ||
|
||||
mName.Contains("amber") || mName.Contains("optic_red"))
|
||||
{
|
||||
Color eCol = (sm.EmissionEnabled ? sm.Emission : sm.AlbedoColor).SrgbToLinear();
|
||||
linearColor = new Vector3(eCol.R, eCol.G, eCol.B);
|
||||
band = 5;
|
||||
roughness = 0.22f;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mName.Contains("olive") || mName.Contains("khaki") || mName.Contains("armour") || mName.Contains("armor"))
|
||||
{
|
||||
if (gearPaintTint.HasValue)
|
||||
{
|
||||
Color c = gearPaintTint.Value.SrgbToLinear();
|
||||
linearColor = new Vector3(c.R, c.G, c.B);
|
||||
band = 6;
|
||||
roughness = 0.56f;
|
||||
return;
|
||||
}
|
||||
Color b = sm.AlbedoColor.SrgbToLinear();
|
||||
linearColor = new Vector3(b.R, b.G, b.B);
|
||||
band = 3;
|
||||
roughness = 0.58f;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mName.Contains("ivory") && gearPaintTint.HasValue)
|
||||
{
|
||||
Color c = gearPaintTint.Value.SrgbToLinear();
|
||||
linearColor = new Vector3(c.R, c.G, c.B);
|
||||
band = 6;
|
||||
roughness = 0.42f;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mName.Contains("signal") || mName.Contains("orange") || mName.Contains("hazard"))
|
||||
{
|
||||
if (gearAccentTint.HasValue)
|
||||
{
|
||||
Color c = gearAccentTint.Value.SrgbToLinear();
|
||||
linearColor = new Vector3(c.R, c.G, c.B);
|
||||
band = 7;
|
||||
roughness = 0.42f;
|
||||
return;
|
||||
}
|
||||
Color b = sm.AlbedoColor.SrgbToLinear();
|
||||
linearColor = new Vector3(b.R, b.G, b.B);
|
||||
band = 4;
|
||||
roughness = 0.44f;
|
||||
return;
|
||||
}
|
||||
|
||||
if (gearAccentTint.HasValue && (mName.Contains("copper") || mName.Contains("gold")))
|
||||
{
|
||||
Color c = gearAccentTint.Value.SrgbToLinear();
|
||||
linearColor = new Vector3(c.R, c.G, c.B);
|
||||
band = 7;
|
||||
roughness = 0.36f;
|
||||
return;
|
||||
}
|
||||
|
||||
Color lin = sm.AlbedoColor.SrgbToLinear();
|
||||
linearColor = new Vector3(lin.R, lin.G, lin.B);
|
||||
band = sm.Metallic >= 0.70f ? 2 : (sm.Metallic >= 0.35f ? 1 : 0);
|
||||
}
|
||||
|
||||
private static void PruneEmptyNonProtectedNodes(Node cur, Node sceneRoot, HashSet<Node> protectedNodes)
|
||||
{
|
||||
for (int i = cur.GetChildCount() - 1; i >= 0; i--)
|
||||
|
|
|
|||
Loading…
Reference in a new issue