Skip to content

Commit

Permalink
- Fixed load models without vertex normal attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorgemagic committed May 19, 2024
1 parent 36a38ca commit 21e75c3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions DonatelloAI/Importers/GLB/GLBRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class GLBRuntime : ModelRuntime
private byte[] binaryChunk;
private BufferInfo[] bufferInfos;

private Func<Color, Texture, SamplerState, AlphaModeEnum, float, float, bool, Material> materialAssigner = null;
private Func<Color, Texture, SamplerState, AlphaModeEnum, float, float, string[], Material> materialAssigner = null;

// If the bufferView is compressed (Draco), we can cache the decoded data
// next time we find a primitive that references this bufferView, we can check if the buffer has been already decoded
Expand Down Expand Up @@ -536,7 +536,8 @@ private async Task<Mesh> ReadPrimitive(MeshPrimitive primitive)
if (primitive.Material.HasValue)
{
int materialId = primitive.Material.Value;
materialIndex = await this.ReadMaterial(materialId, vertexColorEnabled);
var vertexAttributes = primitive.Attributes.Keys.ToArray();
materialIndex = await this.ReadMaterial(materialId, vertexAttributes);
}

// Create Mesh
Expand Down Expand Up @@ -822,7 +823,7 @@ private int SizeInBytes(Accessor accessor)
}
}

private async Task<int> ReadMaterial(int materialId, bool vertexColorEnabled)
private async Task<int> ReadMaterial(int materialId, string[] vertexAttributes)
{
var glbMaterial = this.glbModel.Materials[materialId];
if (!this.materials.ContainsKey(materialId))
Expand Down Expand Up @@ -873,22 +874,22 @@ private async Task<int> ReadMaterial(int materialId, bool vertexColorEnabled)

if (this.materialAssigner == null)
{
material = this.CreateEngineMaterial(baseColor.ToColor(), baseColorTexture, baseColorSampler, glbMaterial.AlphaMode, baseColor.A, glbMaterial.AlphaCutoff, vertexColorEnabled);
material = this.CreateEngineMaterial(baseColor.ToColor(), baseColorTexture, baseColorSampler, glbMaterial.AlphaMode, baseColor.A, glbMaterial.AlphaCutoff, vertexAttributes);
}
else
{
material = this.materialAssigner(baseColor.ToColor(), baseColorTexture, baseColorSampler, glbMaterial.AlphaMode, baseColor.A, glbMaterial.AlphaCutoff, vertexColorEnabled);
material = this.materialAssigner(baseColor.ToColor(), baseColorTexture, baseColorSampler, glbMaterial.AlphaMode, baseColor.A, glbMaterial.AlphaCutoff, vertexAttributes);
}

this.materials.Add(materialId, (glbMaterial.Name, material));
this.materials.Add(materialId, (glbMaterial.Name ?? $"material{materialId}", material));

return this.materials.Count - 1;
}

return this.materials.Keys.ToList().IndexOf(materialId);
}

private Material CreateEngineMaterial(Color baseColor, Texture baseColorTexture, SamplerState baseColorSampler, AlphaModeEnum alphaMode, float alpha, float alphaCutOff, bool vertexColorEnabled)
private Material CreateEngineMaterial(Color baseColor, Texture baseColorTexture, SamplerState baseColorSampler, AlphaModeEnum alphaMode, float alpha, float alphaCutOff, string[] vertexAttributes)
{
RenderLayerDescription layer;
switch (alphaMode)
Expand All @@ -905,10 +906,12 @@ private Material CreateEngineMaterial(Color baseColor, Texture baseColorTexture,

var effect = this.assetsService.Load<Effect>(DefaultResourcesIDs.StandardEffectID);

var hasNormalAttribute = vertexAttributes.Contains("NORMAL");
var hasVertexColor = vertexAttributes.Contains("COLOR");
StandardMaterial material = new StandardMaterial(effect)
{
LightingEnabled = true,
IBLEnabled = true,
LightingEnabled = hasNormalAttribute,
IBLEnabled = hasNormalAttribute,
BaseColor = baseColor,
Alpha = alpha,
BaseColorTexture = baseColorTexture,
Expand All @@ -917,7 +920,7 @@ private Material CreateEngineMaterial(Color baseColor, Texture baseColorTexture,
AlphaCutout = alphaCutOff,
};

if (vertexColorEnabled)
if (hasVertexColor)
{
if (material.ActiveDirectivesNames.Contains("VCOLOR"))
{
Expand Down

0 comments on commit 21e75c3

Please sign in to comment.