Move material creation back to Mission

This commit is contained in:
Jarrod Doyle 2024-08-11 15:43:44 +01:00
parent 59edc0744c
commit d1d6b310ab
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 17 additions and 20 deletions

View File

@ -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<Vector3> _vertices = new();
private readonly List<Vector3> _normals = new();
private readonly List<int> _indices = new();
private readonly List<Vector2> _textureUvs = new();
private readonly List<Vector2> _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<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial;
material.SetShaderParameter("texture_albedo", _texture);
material.SetShaderParameter("lightmap_albedo", lightmapTexture);
return material;
}
}

View File

@ -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<ShaderMaterial>(MATERIAL_PATH).Duplicate() as ShaderMaterial;
material.SetShaderParameter("texture_albedo", albedoTexture);
material.SetShaderParameter("lightmap_albedo", lightmapTexture);
return material;
}
}