Skip to content

Commit

Permalink
[Pipeline] Use AssimpNetter nuget for now (MonoGame#8607)
Browse files Browse the repository at this point in the history
For now, using the prebuilt binaries from the wrapper itself instead of
our prebuilt binaries, until we get the time to either make a wrapper or
setup the native mgcp project.
  • Loading branch information
harry-cpp authored Dec 21, 2024
1 parent d2edb55 commit f07129f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@


<ItemGroup>
<PackageReference Include="AssimpNet" Version="5.0.0-beta1" IncludeAssets="compile;runtime;build" ExcludeAssets="native" />
<PackageReference Include="MonoGame.Library.Assimp" Version="5.3.1.2" />
<PackageReference Include="AssimpNetter" Version="5.4.3" />
<PackageReference Include="BCnEncoder.Net" Version="2.1.0" />
<PackageReference Include="Microsoft.NETCore.App" Version="2.1.30" />
<PackageReference Include="MonoGame.Library.FreeImage" Version="3.18.0.3" />
Expand Down
134 changes: 34 additions & 100 deletions MonoGame.Framework.Content.Pipeline/OpenAssetImporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// MonoGame - Copyright (C) MonoGame Foundation, Inc
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -376,20 +377,20 @@ private void ImportXnaMaterials()
material.Textures.Add("Bump", ImportTextureContentRef(aiMaterial.TextureHeight));

if (aiMaterial.HasColorDiffuse)
material.DiffuseColor = ToXna(aiMaterial.ColorDiffuse);
material.DiffuseColor = new Vector3(aiMaterial.ColorDiffuse.X, aiMaterial.ColorDiffuse.Y, aiMaterial.ColorDiffuse.Z);

if (aiMaterial.HasColorEmissive)
material.EmissiveColor = ToXna(aiMaterial.ColorEmissive);
material.EmissiveColor = new Vector3(aiMaterial.ColorEmissive.X, aiMaterial.ColorEmissive.Y, aiMaterial.ColorEmissive.Z);

if (aiMaterial.HasOpacity)
material.Alpha = aiMaterial.Opacity;

if (aiMaterial.HasColorSpecular)
material.SpecularColor = ToXna(aiMaterial.ColorSpecular);
material.SpecularColor = new Vector3(aiMaterial.ColorSpecular.X, aiMaterial.ColorSpecular.Y, aiMaterial.ColorSpecular.Z);

if (aiMaterial.HasShininessStrength)
material.SpecularPower = aiMaterial.Shininess;

_materials.Add(material);
}
}
Expand Down Expand Up @@ -452,17 +453,17 @@ private void ImportMaterials()
if (aiMaterial.HasBumpScaling)
material.OpaqueData.Add("BumpScaling", aiMaterial.BumpScaling);
if (aiMaterial.HasColorAmbient)
material.OpaqueData.Add("AmbientColor", ToXna(aiMaterial.ColorAmbient));
material.OpaqueData.Add("AmbientColor", (Color)aiMaterial.ColorAmbient);
if (aiMaterial.HasColorDiffuse)
material.OpaqueData.Add("DiffuseColor", ToXna(aiMaterial.ColorDiffuse));
material.OpaqueData.Add("DiffuseColor", (Color)aiMaterial.ColorDiffuse);
if (aiMaterial.HasColorEmissive)
material.OpaqueData.Add("EmissiveColor", ToXna(aiMaterial.ColorEmissive));
material.OpaqueData.Add("EmissiveColor", (Color)aiMaterial.ColorEmissive);
if (aiMaterial.HasColorReflective)
material.OpaqueData.Add("ReflectiveColor", ToXna(aiMaterial.ColorReflective));
material.OpaqueData.Add("ReflectiveColor", (Color)aiMaterial.ColorReflective);
if (aiMaterial.HasColorSpecular)
material.OpaqueData.Add("SpecularColor", ToXna(aiMaterial.ColorSpecular));
material.OpaqueData.Add("SpecularColor", (Color)aiMaterial.ColorSpecular);
if (aiMaterial.HasColorTransparent)
material.OpaqueData.Add("TransparentColor", ToXna(aiMaterial.ColorTransparent));
material.OpaqueData.Add("TransparentColor", (Color)aiMaterial.ColorTransparent);
if (aiMaterial.HasOpacity)
material.OpaqueData.Add("Opacity", aiMaterial.Opacity);
if (aiMaterial.HasReflectivity)
Expand Down Expand Up @@ -513,7 +514,7 @@ private NodeContent ImportNodes(Node aiNode, Node aiParent, NodeContent parent)
{
Name = aiNode.Name,
Identity = _identity,
Transform = ToXna(GetRelativeTransform(aiNode, aiParent))
Transform = GetRelativeTransform(aiNode, aiParent)
};

foreach (var meshIndex in aiNode.MeshIndices)
Expand Down Expand Up @@ -544,7 +545,7 @@ private NodeContent ImportNodes(Node aiNode, Node aiParent, NodeContent parent)
_pivots.Add(originalName, pivot);
}

Matrix transform = ToXna(aiNode.Transform);
Matrix transform = aiNode.Transform;
if (aiNode.Name.EndsWith("_Translation"))
pivot.Translation = transform;
else if (aiNode.Name.EndsWith("_RotationOffset"))
Expand Down Expand Up @@ -582,7 +583,7 @@ private NodeContent ImportNodes(Node aiNode, Node aiParent, NodeContent parent)
{
Name = aiNode.Name,
Identity = _identity,
Transform = ToXna(GetRelativeTransform(aiNode, aiParent))
Transform = GetRelativeTransform(aiNode, aiParent)
};
}

Expand Down Expand Up @@ -625,7 +626,7 @@ private GeometryContent CreateGeometry(MeshContent mesh, Mesh aiMesh)
// Vertices
var baseVertex = mesh.Positions.Count;
foreach (var vert in aiMesh.Vertices)
mesh.Positions.Add(ToXna(vert));
mesh.Positions.Add(vert);
geom.Vertices.AddRange(Enumerable.Range(baseVertex, aiMesh.VertexCount));
geom.Indices.AddRange(aiMesh.GetIndices());

Expand Down Expand Up @@ -673,13 +674,13 @@ private GeometryContent CreateGeometry(MeshContent mesh, Mesh aiMesh)

// Individual channels go here
if (aiMesh.HasNormals)
geom.Vertices.Channels.Add(VertexChannelNames.Normal(), aiMesh.Normals.Select(ToXna));
geom.Vertices.Channels.Add(VertexChannelNames.Normal(), aiMesh.Normals.Select(s => (Vector3)s));

for (var i = 0; i < aiMesh.TextureCoordinateChannelCount; i++)
geom.Vertices.Channels.Add(VertexChannelNames.TextureCoordinate(i), aiMesh.TextureCoordinateChannels[i].Select(ToXnaTexCoord));
geom.Vertices.Channels.Add(VertexChannelNames.TextureCoordinate(i), aiMesh.TextureCoordinateChannels[i].Select(s => new Vector2(s.X, s.Y)));

for (var i = 0; i < aiMesh.VertexColorChannelCount; i++)
geom.Vertices.Channels.Add(VertexChannelNames.Color(i), aiMesh.VertexColorChannels[i].Select(ToXnaColor));
geom.Vertices.Channels.Add(VertexChannelNames.Color(i), aiMesh.VertexColorChannels[i].Select(s => (Vector4)s));

return geom;
}
Expand Down Expand Up @@ -726,7 +727,7 @@ private static Dictionary<string, Matrix> FindDeformationBones(Scene scene)
if (mesh.HasBones)
foreach (var bone in mesh.Bones)
if (!offsetMatrices.ContainsKey(bone.Name))
offsetMatrices[bone.Name] = ToXna(bone.OffsetMatrix);
offsetMatrices[bone.Name] = bone.OffsetMatrix;

return offsetMatrices;
}
Expand Down Expand Up @@ -808,7 +809,7 @@ private NodeContent ImportBones(Node aiNode, Node aiParent, NodeContent parent)
{
Name = aiNode.Name.Replace(mangling, string.Empty),
Identity = _identity,
Transform = ToXna(GetRelativeTransform(aiNode, aiParent))
Transform = GetRelativeTransform(aiNode, aiParent)
};
}
else if (_bones.Contains(aiNode))
Expand Down Expand Up @@ -876,7 +877,7 @@ private NodeContent ImportBones(Node aiNode, Node aiParent, NodeContent parent)
// Offset matrices are not provided by Assimp. :(
// Let's hope that the skeleton was exported in bind pose.
// (Otherwise we are just importing garbage.)
node.Transform = ToXna(GetRelativeTransform(aiNode, aiParent));
node.Transform = GetRelativeTransform(aiNode, aiParent);
}
}
}
Expand Down Expand Up @@ -951,24 +952,24 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
scaleKeys = aiChannel.ScalingKeys;

Debug.Assert(pivot.Scaling.HasValue);
Debug.Assert(!aiChannel.HasRotationKeys || (aiChannel.RotationKeyCount == 1 && (aiChannel.RotationKeys[0].Value == new Assimp.Quaternion(1, 0, 0, 0) || aiChannel.RotationKeys[0].Value == new Assimp.Quaternion(0, 0, 0, 0))));
Debug.Assert(!aiChannel.HasPositionKeys || (aiChannel.PositionKeyCount == 1 && aiChannel.PositionKeys[0].Value == new Vector3D(0, 0, 0)));
Debug.Assert(!aiChannel.HasRotationKeys || (aiChannel.RotationKeyCount == 1 && (aiChannel.RotationKeys[0].Value == new Quaternion(1, 0, 0, 0) || aiChannel.RotationKeys[0].Value == new Quaternion(0, 0, 0, 0))));
Debug.Assert(!aiChannel.HasPositionKeys || (aiChannel.PositionKeyCount == 1 && aiChannel.PositionKeys[0].Value == new Vector3(0, 0, 0)));
}
else if (aiChannel.NodeName.EndsWith("_$AssimpFbx$_Rotation"))
{
rotationKeys = aiChannel.RotationKeys;

Debug.Assert(pivot.Rotation.HasValue);
Debug.Assert(!aiChannel.HasScalingKeys || (aiChannel.ScalingKeyCount == 1 && aiChannel.ScalingKeys[0].Value == new Vector3D(1, 1, 1)));
Debug.Assert(!aiChannel.HasPositionKeys || (aiChannel.PositionKeyCount == 1 && aiChannel.PositionKeys[0].Value == new Vector3D(0, 0, 0)));
Debug.Assert(!aiChannel.HasScalingKeys || (aiChannel.ScalingKeyCount == 1 && aiChannel.ScalingKeys[0].Value == new Vector3(1, 1, 1)));
Debug.Assert(!aiChannel.HasPositionKeys || (aiChannel.PositionKeyCount == 1 && aiChannel.PositionKeys[0].Value == new Vector3(0, 0, 0)));
}
else if (aiChannel.NodeName.EndsWith("_$AssimpFbx$_Translation"))
{
translationKeys = aiChannel.PositionKeys;

Debug.Assert(pivot.Translation.HasValue);
Debug.Assert(!aiChannel.HasScalingKeys || (aiChannel.ScalingKeyCount == 1 && aiChannel.ScalingKeys[0].Value == new Vector3D(1, 1, 1)));
Debug.Assert(!aiChannel.HasRotationKeys || (aiChannel.RotationKeyCount == 1 && (aiChannel.RotationKeys[0].Value == new Assimp.Quaternion(1, 0, 0, 0) || aiChannel.RotationKeys[0].Value == new Assimp.Quaternion(0, 0, 0, 0))));
Debug.Assert(!aiChannel.HasScalingKeys || (aiChannel.ScalingKeyCount == 1 && aiChannel.ScalingKeys[0].Value == new Vector3(1, 1, 1)));
Debug.Assert(!aiChannel.HasRotationKeys || (aiChannel.RotationKeyCount == 1 && (aiChannel.RotationKeys[0].Value == new Quaternion(1, 0, 0, 0) || aiChannel.RotationKeys[0].Value == new Quaternion(0, 0, 0, 0))));
}
else
{
Expand Down Expand Up @@ -1006,7 +1007,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
if (scaleIndex != -1)
{
// Scaling key found.
scale = ToXna(scaleKeys[scaleIndex].Value);
scale = scaleKeys[scaleIndex].Value;
prevScaleIndex = scaleIndex;
prevScaleTime = time;
prevScale = scale;
Expand All @@ -1019,7 +1020,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
// Lerp between previous and next scaling key.
var nextScaleKey = scaleKeys[prevScaleIndex + 1];
var nextScaleTime = nextScaleKey.Time;
var nextScale = ToXna(nextScaleKey.Value);
var nextScale = nextScaleKey.Value;
var amount = (float)((time - prevScaleTime) / (nextScaleTime - prevScaleTime));
scale = Vector3.Lerp(prevScale.Value, nextScale, amount);
}
Expand All @@ -1036,7 +1037,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
if (rotationIndex != -1)
{
// Rotation key found.
rotation = ToXna(rotationKeys[rotationIndex].Value);
rotation = rotationKeys[rotationIndex].Value;
prevRotationIndex = rotationIndex;
prevRotationTime = time;
prevRotation = rotation;
Expand All @@ -1049,7 +1050,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
// Lerp between previous and next rotation key.
var nextRotationKey = rotationKeys[prevRotationIndex + 1];
var nextRotationTime = nextRotationKey.Time;
var nextRotation = ToXna(nextRotationKey.Value);
var nextRotation = nextRotationKey.Value;
var amount = (float)((time - prevRotationTime) / (nextRotationTime - prevRotationTime));
rotation = Quaternion.Slerp(prevRotation.Value, nextRotation, amount);
}
Expand All @@ -1066,7 +1067,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
if (translationIndex != -1)
{
// Translation key found.
translation = ToXna(translationKeys[translationIndex].Value);
translation = translationKeys[translationIndex].Value;
prevTranslationIndex = translationIndex;
prevTranslationTime = time;
prevTranslation = translation;
Expand All @@ -1079,7 +1080,7 @@ private AnimationContent ImportAnimation(Animation aiAnimation, string nodeName
// Lerp between previous and next translation key.
var nextTranslationKey = translationKeys[prevTranslationIndex + 1];
var nextTranslationTime = nextTranslationKey.Time;
var nextTranslation = ToXna(nextTranslationKey.Value);
var nextTranslation = nextTranslationKey.Value;
var amount = (float)((time - prevTranslationTime) / (nextTranslationTime - prevTranslationTime));
translation = Vector3.Lerp(prevTranslation.Value, nextTranslation, amount);
}
Expand Down Expand Up @@ -1166,72 +1167,5 @@ private static string GetNodeName(string name)
int index = name.IndexOf("_$AssimpFbx$", StringComparison.Ordinal);
return (index >= 0) ? name.Remove(index) : name;
}

#region Conversion Helpers

[DebuggerStepThrough]
public static Matrix ToXna(Matrix4x4 matrix)
{
var result = Matrix.Identity;

result.M11 = matrix.A1;
result.M12 = matrix.B1;
result.M13 = matrix.C1;
result.M14 = matrix.D1;

result.M21 = matrix.A2;
result.M22 = matrix.B2;
result.M23 = matrix.C2;
result.M24 = matrix.D2;

result.M31 = matrix.A3;
result.M32 = matrix.B3;
result.M33 = matrix.C3;
result.M34 = matrix.D3;

result.M41 = matrix.A4;
result.M42 = matrix.B4;
result.M43 = matrix.C4;
result.M44 = matrix.D4;

return result;
}

[DebuggerStepThrough]
public static Vector2 ToXna(Vector2D vector)
{
return new Vector2(vector.X, vector.Y);
}

[DebuggerStepThrough]
public static Vector3 ToXna(Vector3D vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}

[DebuggerStepThrough]
public static Quaternion ToXna(Assimp.Quaternion quaternion)
{
return new Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
}

[DebuggerStepThrough]
public static Vector3 ToXna(Color4D color)
{
return new Vector3(color.R, color.G, color.B);
}

[DebuggerStepThrough]
public static Vector2 ToXnaTexCoord(Vector3D vector)
{
return new Vector2(vector.X, vector.Y);
}

[DebuggerStepThrough]
public static Color ToXnaColor(Color4D color)
{
return new Color(color.R, color.G, color.B, color.A);
}
#endregion
}
}
14 changes: 12 additions & 2 deletions MonoGame.Framework/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,18 @@ public byte A
this._packedValue = (this._packedValue & 0x00ffffff) | ((uint)value << 24);
}
}

/// <summary>

/// <summary>
/// Converts a <see cref="System.Numerics.Vector4"/> to a <see cref="Vector4"/> by mapping XYZW -> RGBA.
/// </summary>
/// <param name="value">The converted value.</param>
/// <returns></returns>
public static implicit operator Color(System.Numerics.Vector4 value)
{
return new Color(value.X, value.Y, value.Z, value.W);
}

/// <summary>
/// Compares whether two <see cref="Color"/> instances are equal.
/// </summary>
/// <param name="a"><see cref="Color"/> instance on the left of the equal sign.</param>
Expand Down

0 comments on commit f07129f

Please sign in to comment.