Skip to content

Commit

Permalink
Improved handling of per-model flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbyte committed May 16, 2019
1 parent 10a460d commit dea2b41
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 37 deletions.
28 changes: 24 additions & 4 deletions ConverterApp/GR2Pane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,30 @@ private void UpdateInputState()
buildDummySkeleton.Checked = true;
}

var modelFlags = DivinityHelpers.DetermineModelFlags(_root);
meshRigid.Checked = modelFlags.IsRigid();
meshCloth.Checked = modelFlags.IsCloth();
meshProxy.Checked = modelFlags.IsMeshProxy();
bool hasUndeterminedModelTypes = false;
DivinityModelFlag accumulatedModelFlags = 0;
if (_root.Meshes != null)
{
foreach (var mesh in _root.Meshes)
{
if (!mesh.HasDefiniteModelType)
{
hasUndeterminedModelTypes = true;
}

accumulatedModelFlags |= mesh.ModelType;
}
}

// If the type of all models are known, either via LSMv1 ExtendedData
// or via Collada <extra> properties, there is nothing to override.
meshRigid.Enabled = hasUndeterminedModelTypes;
meshCloth.Enabled = hasUndeterminedModelTypes;
meshProxy.Enabled = hasUndeterminedModelTypes;

meshRigid.Checked = accumulatedModelFlags.IsRigid();
meshCloth.Checked = accumulatedModelFlags.IsCloth();
meshProxy.Checked = accumulatedModelFlags.IsMeshProxy();

UpdateExportableObjects();
UpdateResourceFormats();
Expand Down
57 changes: 46 additions & 11 deletions LSLib/Granny/Model/ColladaImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,57 @@ private ExporterInfo ImportExporterInfo(COLLADA collada)
return exporterInfo;
}

private void UpdateUserDefinedProperties(Root root)
private DivinityModelFlag DetermineSkeletonModelFlagsFromModels(Root root, Skeleton skeleton, DivinityModelFlag modelFlagOverrides)
{
if (Options.ModelInfoFormat == DivinityModelInfoFormat.None)
DivinityModelFlag accumulatedFlags = 0;
if (root.Meshes != null)
{
return;
foreach (var model in root.Models)
{
if (model.Skeleton == skeleton)
{
if (model.MeshBindings != null)
{
foreach (var meshBinding in model.MeshBindings)
{
var mesh = meshBinding.Mesh;
if (mesh.HasDefiniteModelType)
{
accumulatedFlags |= mesh.ModelType;
}
else
{
accumulatedFlags |= modelFlagOverrides;
}
}
}
}
}
}

var modelFlags = Options.ModelType;
if (modelFlags == 0)
return accumulatedFlags;
}

private void UpdateUserDefinedProperties(Root root)
{
if (Options.ModelInfoFormat == DivinityModelInfoFormat.None)
{
modelFlags = DivinityHelpers.DetermineModelFlags(root);
return;
}

var userDefinedProperties = "";
var modelFlagOverrides = Options.ModelType;

if (root.Meshes != null)
{
userDefinedProperties = DivinityHelpers.ModelFlagsToUserDefinedProperties(modelFlags);

foreach (var mesh in root.Meshes)
{
DivinityModelFlag modelFlags = modelFlagOverrides;
if (mesh.HasDefiniteModelType)
{
modelFlags = mesh.ModelType;
}

var userDefinedProperties = DivinityHelpers.ModelFlagsToUserDefinedProperties(modelFlags);
mesh.ExtendedData = DivinityHelpers.MakeMeshExtendedData(mesh, Options.ModelInfoFormat, Options.ModelType);
}
}
Expand All @@ -215,15 +245,18 @@ private void UpdateUserDefinedProperties(Root root)
{
if (skeleton.Bones != null)
{
var accumulatedFlags = DetermineSkeletonModelFlagsFromModels(root, skeleton, modelFlagOverrides);

foreach (var bone in skeleton.Bones)
{
if (bone.ExtendedData == null)
{
bone.ExtendedData = new DivinityBoneExtendedData();
}


var userDefinedProperties = DivinityHelpers.ModelFlagsToUserDefinedProperties(accumulatedFlags);
bone.ExtendedData.UserDefinedProperties = userDefinedProperties;
bone.ExtendedData.IsRigid = (modelFlags.IsRigid()) ? 1 : 0;
bone.ExtendedData.IsRigid = (accumulatedFlags.IsRigid()) ? 1 : 0;
}
}
}
Expand Down Expand Up @@ -279,6 +312,7 @@ private technique FindExporterExtraData(extra[] extras)
private DivinityModelFlag FindDivModelType(mesh mesh)
{
DivinityModelFlag flags = 0;

var technique = FindExporterExtraData(mesh.extra);
if (technique != null)
{
Expand Down Expand Up @@ -350,6 +384,7 @@ private Mesh ImportMesh(geometry geom, mesh mesh, VertexDescriptor vertexFormat)
if (divModelType != 0)
{
m.ModelType = divModelType;
m.HasDefiniteModelType = true;
}

Utils.Info(String.Format("Imported {0} mesh ({1} tri groups, {2} tris)",
Expand Down
31 changes: 11 additions & 20 deletions LSLib/Granny/Model/DivinityMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,27 @@ public static DivinityModelFlag UserDefinedPropertiesToModelType(string userDefi
return flags;
}

public static DivinityModelFlag DetermineModelFlags(Mesh mesh)
public static DivinityModelFlag DetermineModelFlags(Mesh mesh, out bool hasDefiniteModelType)
{
DivinityModelFlag flags = 0;

if (mesh.ModelType != 0)
if (mesh.HasDefiniteModelType)
{
flags = mesh.ModelType;
hasDefiniteModelType = true;
}
else if (mesh.ExtendedData != null
&& mesh.ExtendedData.LSMVersion >= 1
&& mesh.ExtendedData.UserMeshProperties != null)
{
flags = mesh.ExtendedData.UserMeshProperties.MeshFlags;
hasDefiniteModelType = true;
}
else if (mesh.ExtendedData != null
&& mesh.ExtendedData.UserDefinedProperties != null)
{
flags = UserDefinedPropertiesToModelType(mesh.ExtendedData.UserDefinedProperties);
hasDefiniteModelType = true;
}
else
{
Expand All @@ -304,34 +307,22 @@ public static DivinityModelFlag DetermineModelFlags(Mesh mesh)
{
flags |= DivinityModelFlag.Rigid;
}
}

return flags;
}

public static DivinityModelFlag DetermineModelFlags(Root root)
{
DivinityModelFlag flags = 0;

if (root.Meshes != null)
{
foreach (var mesh in root.Meshes)
{
flags |= DetermineModelFlags(mesh);
}
hasDefiniteModelType = false;
}

return flags;
}

public static DivinityMeshExtendedData MakeMeshExtendedData(Mesh mesh, DivinityModelInfoFormat format,
DivinityModelFlag modelFlags)
DivinityModelFlag modelFlagOverrides)
{
var extendedData = DivinityMeshExtendedData.Make();

if (modelFlags == 0)
DivinityModelFlag modelFlags = modelFlagOverrides;

if (mesh.HasDefiniteModelType)
{
modelFlags = DetermineModelFlags(mesh);
modelFlags = mesh.ModelType;
}

if (mesh.VertexFormat.HasBoneWeights)
Expand Down
10 changes: 10 additions & 0 deletions LSLib/Granny/Model/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,22 @@ public class Mesh
[Serialization(Kind = SerializationKind.None)]
public DivinityModelFlag ModelType = 0;

// Is the model type inferred, or was the exact model type flag imported from the resource file?
// (Determines how the exporter reacts to model type override flags)
[Serialization(Kind = SerializationKind.None)]
public bool HasDefiniteModelType = false;

public void PostLoad()
{
if (PrimaryVertexData.Vertices.Count > 0)
{
VertexFormat = PrimaryVertexData.Vertices[0].Format;
}

if (HasDefiniteModelType == false)
{
ModelType = DivinityHelpers.DetermineModelFlags(this, out HasDefiniteModelType);
}
}

public List<string> VertexComponentNames()
Expand Down
4 changes: 2 additions & 2 deletions LSLib/LS/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace LSLib.LS
public static class Common
{
public const int MajorVersion = 1;
public const int MinorVersion = 12;
public const int PatchVersion = 4;
public const int MinorVersion = 13;
public const int PatchVersion = 0;

/// <summary>
/// Returns the version number of the LSLib library
Expand Down

0 comments on commit dea2b41

Please sign in to comment.