Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Selinux24 committed Apr 18, 2024
1 parent 812335f commit e01d33f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
5 changes: 3 additions & 2 deletions Engine/BuiltIn/Shadows/Default.Buffers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Engine.BuiltIn.Shadows
{
using Engine.Common;
using System;

/// <summary>
/// Per-shadow casting light data structure
Expand All @@ -23,8 +24,8 @@ struct PerCastingLight : IBufferData
/// <param name="context">Draw context</param>
public static PerCastingLight Build(DrawContextShadows context)
{
var viewProjection = context.ShadowMap?.LightSource.FromLightVP;
int length = viewProjection?.Length ?? 0;
var viewProjection = context.ShadowMap?.LightSource.FromLightVP ?? [];
int length = Math.Min(MaxCount, viewProjection.Length);

if (length > MaxCount)
{
Expand Down
16 changes: 13 additions & 3 deletions Engine/Common/DrawingData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected virtual void Dispose(bool disposing)

//Dispose mesh resources and clear the mesh collection
var bufferManager = Game.BufferManager;
meshes.Values.ToList().ForEach(mCollection => mCollection.DisposeResources(bufferManager));
meshes.Values.ToList().ForEach(mCollection => mCollection?.DisposeResources(bufferManager));
meshes.Clear();

//Clear the material list
Expand Down Expand Up @@ -263,9 +263,14 @@ public async Task Initialize(string name, BufferDescriptor instancingBuffer = nu
/// </summary>
public IEnumerable<(string MaterialName, IMeshMaterial MeshMaterial, string MeshName, Mesh Mesh)> IterateMaterials()
{
foreach (string meshName in meshes.Keys)
foreach ((var meshName, var meshValue) in meshes)
{
foreach ((string materialName, var mesh) in meshes[meshName])
if (meshValue == null)
{
continue;
}

foreach ((string materialName, var mesh) in meshValue)
{
if (!mesh.Ready)
{
Expand All @@ -286,6 +291,11 @@ public async Task Initialize(string name, BufferDescriptor instancingBuffer = nu
{
foreach (var meshMaterial in meshes.Values)
{
if (meshMaterial == null)
{
continue;
}

foreach (var mesh in meshMaterial)
{
yield return mesh;
Expand Down
37 changes: 21 additions & 16 deletions Engine/Content/ContentData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,9 @@ public async Task<Dictionary<string, MeshByMaterialCollection>> CreateGeometry(b

Dictionary<string, MeshByMaterialCollection> meshes = [];

foreach (var meshName in geometryContent.Keys)
var meshNames = geometryContent.Keys.ToArray();

foreach (var meshName in meshNames)
{
var mesh = await CreateGeometryMesh(meshName, loadAnimation, loadNormalMaps, constraint);

Expand Down Expand Up @@ -1120,25 +1122,24 @@ public void Optimize()
{
foreach (string material in materialContent.Keys)
{
OptimizeSkinnedMesh(geometryContent, skin, material);
OptimizeSkinnedMesh(skin, material);
}
}
}

foreach (string material in materialContent.Keys)
{
OptimizeStaticMesh(geometryContent, material);
OptimizeStaticMesh(material);
}
}
/// <summary>
/// Optimizes the skinned mesh
/// </summary>
/// <param name="geometry">Current geometry dictionary</param>
/// <param name="skin">Skin name</param>
/// <param name="material">Material name</param>
private void OptimizeSkinnedMesh(Dictionary<string, Dictionary<string, SubMeshContent>> geometry, string skin, string material)
private void OptimizeSkinnedMesh(string skin, string material)
{
var skinnedM = ComputeSubmeshContent(geometry, skin, skin, material);
var skinnedM = ComputeSubmeshContent(skin, skin, material);
if (skinnedM != null)
{
OptimizeSubmeshContent(skin, material, [skinnedM]);
Expand All @@ -1147,21 +1148,22 @@ private void OptimizeSkinnedMesh(Dictionary<string, Dictionary<string, SubMeshCo
/// <summary>
/// Optimizes the static mesh
/// </summary>
/// <param name="geometry">Current geometry dictionary</param>
/// <param name="material">Material name</param>
private void OptimizeStaticMesh(Dictionary<string, Dictionary<string, SubMeshContent>> geometry, string material)
private void OptimizeStaticMesh(string material)
{
var staticM = new List<SubMeshContent>();

foreach (string mesh in geometry.Keys)
var meshNames = geometryContent.Keys.ToArray();

foreach (string meshName in meshNames)
{
var skins = GetControllerSkins();
if (Array.Exists(skins, s => s == mesh))
if (Array.Exists(skins, s => s == meshName))
{
continue;
}

var submesh = ComputeSubmeshContent(geometry, mesh, StaticMesh, material);
var submesh = ComputeSubmeshContent(meshName, StaticMesh, material);
if (submesh != null)
{
staticM.Add(submesh);
Expand All @@ -1176,14 +1178,13 @@ private void OptimizeStaticMesh(Dictionary<string, Dictionary<string, SubMeshCon
/// <summary>
/// Computes the specified source mesh
/// </summary>
/// <param name="geometry">Current geometry dictionary</param>
/// <param name="sourceMesh">Source mesh name</param>
/// <param name="targetMesh">Target mesh name</param>
/// <param name="material">Material name</param>
/// <returns>Returns a submesh content if source mesh isn't a hull</returns>
private SubMeshContent ComputeSubmeshContent(Dictionary<string, Dictionary<string, SubMeshContent>> geometry, string sourceMesh, string targetMesh, string material)
private SubMeshContent ComputeSubmeshContent(string sourceMesh, string targetMesh, string material)
{
if (!geometry.TryGetValue(sourceMesh, out var dict))
if (!geometryContent.TryGetValue(sourceMesh, out var dict))
{
return null;
}
Expand Down Expand Up @@ -1360,7 +1361,9 @@ public bool FilterByArmature(string armatureName, out ContentData modelContent)
TryAddController(controller, ref modelContent);
}

foreach (var mesh in modelContent.geometryContent.Keys)
var meshNames = modelContent.geometryContent.Keys.ToArray();

foreach (var mesh in meshNames)
{
TryAddLights(mesh.Replace(MeshString, ""), ref modelContent);
}
Expand Down Expand Up @@ -1612,7 +1615,9 @@ public IEnumerable<Triangle> GetHullMeshes()
{
var meshes = new List<Triangle>();

foreach (var meshName in geometryContent.Keys)
var meshNames = geometryContent.Keys.ToArray();

foreach (var meshName in meshNames)
{
meshes.AddRange(GetHullMesh(meshName));
}
Expand Down
4 changes: 2 additions & 2 deletions Engine/Content/FileCubicImageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public FileCubicImageContent(string path, Rectangle[] faces = null)
this.path = p.First();
this.faces = faces ?? throw new ArgumentNullException(nameof(faces), "A cube face list must be specified.");

if (faces.Length != 6)
if (faces.Length != 0 && faces.Length != 6)
{
throw new ArgumentOutOfRangeException(nameof(faces), $"A list o 6 faces must be specified.");
}
Expand All @@ -82,7 +82,7 @@ public FileCubicImageContent(string contentFolder, string contentFileName, Recta
path = p.First();
this.faces = faces ?? throw new ArgumentNullException(nameof(faces), "A cube face list must be specified.");

if (faces.Length != 6)
if (faces.Length != 0 && faces.Length != 6)
{
throw new ArgumentOutOfRangeException(nameof(faces), $"A list o 6 faces must be specified.");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Content/MemoryCubicImageContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MemoryCubicImageContent(MemoryStream stream, Rectangle[] faces = null)
this.stream = stream ?? throw new ArgumentNullException(nameof(stream), "A stream must be specified.");
this.faces = faces ?? throw new ArgumentNullException(nameof(faces), "A cube face list must be specified.");

if (faces.Length != 6)
if (faces.Length != 0 && faces.Length != 6)
{
throw new ArgumentOutOfRangeException(nameof(faces), $"A list o 6 faces must be specified.");
}
Expand Down

0 comments on commit e01d33f

Please sign in to comment.