diff --git a/project/code/TMV/MeshSurfaceData.cs b/project/code/TMV/MeshSurfaceData.cs index 702ae29..f738a23 100644 --- a/project/code/TMV/MeshSurfaceData.cs +++ b/project/code/TMV/MeshSurfaceData.cs @@ -7,20 +7,16 @@ namespace KeepersCompound.TMV; public class MeshSurfaceData { - const string MATERIAL_PATH = "res://project/materials/base.tres"; - public bool Empty { get; private set; } - private readonly Texture _texture; private readonly List _vertices = new(); private readonly List _normals = new(); private readonly List _indices = new(); private readonly List _textureUvs = new(); private readonly List _lightmapUvs = new(); - public MeshSurfaceData(Texture texture) + public MeshSurfaceData() { - _texture = texture; Empty = true; } @@ -78,12 +74,4 @@ public class MeshSurfaceData return array; } - - public Material BuildMaterial(Texture lightmapTexture) - { - var material = ResourceLoader.Load(MATERIAL_PATH).Duplicate() as ShaderMaterial; - material.SetShaderParameter("texture_albedo", _texture); - material.SetShaderParameter("lightmap_albedo", lightmapTexture); - return material; - } } \ No newline at end of file diff --git a/project/code/TMV/Mission.cs b/project/code/TMV/Mission.cs index 16bf15e..f7a9561 100644 --- a/project/code/TMV/Mission.cs +++ b/project/code/TMV/Mission.cs @@ -139,19 +139,19 @@ public partial class Mission : Node3D } var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap); - foreach (var surface in surfaceDataMap.Values) + foreach (var (textureId, surface) in surfaceDataMap) { if (surface.Empty) { continue; } - var array = surface.BuildSurfaceArray(); - var material = surface.BuildMaterial(lightmapTexture); - var mesh = new ArrayMesh(); + var albedoTexture = _textureLoader.Get(textureId); + + var mesh = new ArrayMesh(); + mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surface.BuildSurfaceArray()); + mesh.SurfaceSetMaterial(0, BuildMaterial(albedoTexture, lightmapTexture)); - mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array); - mesh.SurfaceSetMaterial(0, material); var meshInstance = new MeshInstance3D { Mesh = mesh }; AddChild(meshInstance); } @@ -178,7 +178,7 @@ public partial class Mission : Node3D if (!surfaceDataMap.ContainsKey(textureId)) { - surfaceDataMap.Add(textureId, new MeshSurfaceData(_textureLoader.Get(textureId))); + surfaceDataMap.Add(textureId, new MeshSurfaceData()); } var surfaceData = surfaceDataMap[textureId]; var (start, end) = surfaceData.AddPolygon(vertices, normal, textureUvs, lightmapUvs); @@ -359,4 +359,13 @@ public partial class Mission : Node3D GD.Print($" {i}:\n Tokens: [{item.Tokens[0]}, {item.Tokens[1]}, {item.Tokens[2]}, {item.Tokens[3]}]\n Name: {item.Name}"); } } + + const string MATERIAL_PATH = "res://project/materials/base.tres"; + private static Material BuildMaterial(Texture albedoTexture, Texture lightmapTexture) + { + var material = ResourceLoader.Load(MATERIAL_PATH).Duplicate() as ShaderMaterial; + material.SetShaderParameter("texture_albedo", albedoTexture); + material.SetShaderParameter("lightmap_albedo", lightmapTexture); + return material; + } } \ No newline at end of file