Move material creation back to Mission
This commit is contained in:
parent
59edc0744c
commit
d1d6b310ab
|
@ -7,20 +7,16 @@ namespace KeepersCompound.TMV;
|
||||||
|
|
||||||
public class MeshSurfaceData
|
public class MeshSurfaceData
|
||||||
{
|
{
|
||||||
const string MATERIAL_PATH = "res://project/materials/base.tres";
|
|
||||||
|
|
||||||
public bool Empty { get; private set; }
|
public bool Empty { get; private set; }
|
||||||
|
|
||||||
private readonly Texture _texture;
|
|
||||||
private readonly List<Vector3> _vertices = new();
|
private readonly List<Vector3> _vertices = new();
|
||||||
private readonly List<Vector3> _normals = new();
|
private readonly List<Vector3> _normals = new();
|
||||||
private readonly List<int> _indices = new();
|
private readonly List<int> _indices = new();
|
||||||
private readonly List<Vector2> _textureUvs = new();
|
private readonly List<Vector2> _textureUvs = new();
|
||||||
private readonly List<Vector2> _lightmapUvs = new();
|
private readonly List<Vector2> _lightmapUvs = new();
|
||||||
|
|
||||||
public MeshSurfaceData(Texture texture)
|
public MeshSurfaceData()
|
||||||
{
|
{
|
||||||
_texture = texture;
|
|
||||||
Empty = true;
|
Empty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,12 +74,4 @@ public class MeshSurfaceData
|
||||||
|
|
||||||
return array;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -139,19 +139,19 @@ public partial class Mission : Node3D
|
||||||
}
|
}
|
||||||
|
|
||||||
var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap);
|
var lightmapTexture = BuildLightmapTexture(cells, packingRects.ToArray(), rectDataMap, surfaceDataMap);
|
||||||
foreach (var surface in surfaceDataMap.Values)
|
foreach (var (textureId, surface) in surfaceDataMap)
|
||||||
{
|
{
|
||||||
if (surface.Empty)
|
if (surface.Empty)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var array = surface.BuildSurfaceArray();
|
var albedoTexture = _textureLoader.Get(textureId);
|
||||||
var material = surface.BuildMaterial(lightmapTexture);
|
|
||||||
var mesh = new ArrayMesh();
|
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 };
|
var meshInstance = new MeshInstance3D { Mesh = mesh };
|
||||||
AddChild(meshInstance);
|
AddChild(meshInstance);
|
||||||
}
|
}
|
||||||
|
@ -178,7 +178,7 @@ public partial class Mission : Node3D
|
||||||
|
|
||||||
if (!surfaceDataMap.ContainsKey(textureId))
|
if (!surfaceDataMap.ContainsKey(textureId))
|
||||||
{
|
{
|
||||||
surfaceDataMap.Add(textureId, new MeshSurfaceData(_textureLoader.Get(textureId)));
|
surfaceDataMap.Add(textureId, new MeshSurfaceData());
|
||||||
}
|
}
|
||||||
var surfaceData = surfaceDataMap[textureId];
|
var surfaceData = surfaceDataMap[textureId];
|
||||||
var (start, end) = surfaceData.AddPolygon(vertices, normal, textureUvs, lightmapUvs);
|
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}");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue