Skip to content

Commit

Permalink
reclean of DCLGLTFMesh.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia committed Feb 26, 2024
1 parent b90816f commit fda4e4d
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions DCL_PiXYZ/SceneRepositioner/SceneBuilder/Entities/DCLGLTFMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,40 @@ public DCLGLTFMesh(string src)
{
this.src = src;
}

public override PXZModel InstantiateMesh(PiXYZAPI pxz, string entityID, uint parent, uint material, Dictionary<string, string> contentTable, SceneConversionPathHandler pathHandler)
{
this.pathHandler = pathHandler;

if (!contentTable.TryGetValue(src.ToLower(), out string modelPath))
{
LogError($"ERROR: GLTF {src} file not found in sceneContent");
return PXYZConstants.EMPTY_MODEL;
}

bool isModelProcessingSuccessful = TryProcessModel(modelPath);

if (!TryImportGLTF(pxz, parent, modelPath, isModelProcessingSuccessful, out var pxzModel))
bool modelRecreatedSuccessfully = true;
try
{
Console.WriteLine($"ERROR: Importing GLTF {entityID} failed");
return PXYZConstants.EMPTY_MODEL;
ModifyModelMaterials(modelPath);
ExportModifiedModel(modelPath);
}
catch (Exception e)
{
LogError($"ERROR pre-processing GLTF with GLTFSharp: {e}");
modelRecreatedSuccessfully = false;
}

return pxzModel;
}

private bool TryProcessModel(string modelPath)
{
try
{
ModifyModelMaterials(modelPath);
ExportModifiedModel(modelPath);
return true;
uint importedFileOccurrence = pxz.IO.ImportScene(modelRecreatedSuccessfully ? modelPath + "_EDITED.glb" : modelPath);
pxz.Scene.SetParent(importedFileOccurrence, parent);
return new PXZModel(true, importedFileOccurrence);
}
catch (Exception e)
{
LogError($"MODEL PROCESSING FAILED {modelPath} {e}");
return false;
Console.WriteLine($"ERROR: Importing GLTF {src} failed with error {e}");
return PXYZConstants.EMPTY_MODEL;
}
}

Expand All @@ -75,33 +75,15 @@ private void ModifyModelMaterials(string modelPath)

SaveModel(model, modelPath);
}

private void SaveModel(ModelRoot model, string modelPath)
{
if (Path.GetExtension(modelPath).Contains("glb", StringComparison.OrdinalIgnoreCase))
model.SaveGLB(modelPath);
else
model.SaveGLTF(modelPath);
}

private bool TryImportGLTF(PiXYZAPI pxz, uint parent, string modelPath, bool isModelProcessed, out PXZModel pxzModel)
{
try
{
string finalModelPath = isModelProcessed ? $"{modelPath}_EDITED.glb" : modelPath;
uint importedFileOccurrence = pxz.IO.ImportScene(finalModelPath);
pxz.Scene.SetParent(importedFileOccurrence, parent);
pxzModel = new PXZModel(true, importedFileOccurrence);
return true;
}
catch (Exception e)
{
LogError($"ERROR importing GLTF: {e}");
pxzModel = PXYZConstants.EMPTY_MODEL;
return false;
}
}


private void ExportModifiedModel(string modelPath)
{
// Determine the output file path based on the original model path
Expand All @@ -116,7 +98,6 @@ private void ExportModifiedModel(string modelPath)

var readSettings = new ReadSettings(ValidationMode.TryFix);
var model = ModelRoot.Load(modelPath, readSettings);

var modelRoot = ModelRoot.CreateModel();
var sceneModel = modelRoot.UseScene("Default");
foreach (var modelLogicalNode in model.LogicalNodes)
Expand All @@ -126,22 +107,20 @@ private void ExportModifiedModel(string modelPath)
foreach (var meshPrimitive in modelLogicalNode.Mesh.Primitives)
{
var meshToExport = modelRoot.CreateMesh(modelLogicalNode.Name);

BuildMesh(meshToExport.CreatePrimitive(), meshPrimitive.GetVertexAccessor("POSITION"),
meshPrimitive.GetVertexAccessor("NORMAL"),
meshPrimitive.GetVertexAccessor("TEXCOORD_0"),
(int[])(object)meshPrimitive.GetIndexAccessor().AsIndicesArray().ToArray(),
modelRoot.CreateMaterial(meshPrimitive.Material.ToMaterialBuilder()));

var node = sceneModel.CreateNode(modelLogicalNode.Name).WithMesh(meshToExport);
node.WorldMatrix = modelLogicalNode.WorldMatrix;
}
}
}

SaveModel(model, modelPath + "_EDITED.glb");
modelRoot.SaveGLB(modelPath + "_EDITED.glb");
}

private void BuildMesh(MeshPrimitive meshToExport, Accessor positions, Accessor normals, Accessor texcoord, int[] indices, Material material)
{
if (positions != null)
Expand All @@ -156,11 +135,14 @@ private void BuildMesh(MeshPrimitive meshToExport, Accessor positions, Accessor
meshToExport.WithIndicesAccessor(PrimitiveType.TRIANGLES, indices);
meshToExport.WithMaterial(material);
}



private void LogError(string message)
{
FileWriter.WriteToFile(message, pathHandler.FailGLBImporterFile);
}


}
}


}

0 comments on commit fda4e4d

Please sign in to comment.