Skip to content

Commit

Permalink
Merge pull request donkeyProgramming#202 from mr-phazer/ImportExport-…
Browse files Browse the repository at this point in the history
…Phazer-03

Updates and Fixes
  • Loading branch information
donkeyProgramming authored Nov 28, 2024
2 parents ee4944f + 5b2d9e8 commit 680166a
Show file tree
Hide file tree
Showing 19 changed files with 405 additions and 232 deletions.
1 change: 1 addition & 0 deletions AssetEditor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Global
{8494EA8F-DBD8-4E4C-880C-7401CE2FB240} = {CED3D0A1-90C6-4F8F-9BD4-2274D79331B4}
{D7225D7F-87E4-4FAD-A3BE-9165DFDC315E} = {07AC615B-A8FC-4E1A-BDD5-BC11452429A0}
{D29AB45B-3CFD-4994-BD54-CE301704D6CA} = {CED3D0A1-90C6-4F8F-9BD4-2274D79331B4}
{7070A990-1A62-46A9-ABDF-BE030E3A4336} = {CED3D0A1-90C6-4F8F-9BD4-2274D79331B4}
{D0F5B9E7-B545-41BF-AA8C-7E1923D35132} = {7070A990-1A62-46A9-ABDF-BE030E3A4336}
{E5F640D6-A970-446E-9DCD-71698B95C3AF} = {7070A990-1A62-46A9-ABDF-BE030E3A4336}
{D1F5CA6C-DD76-487F-AF31-22B1A71B294C} = {7070A990-1A62-46A9-ABDF-BE030E3A4336}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Numerics;

namespace Editors.ImportExport.Common
{
class VecConv
{
public static Vector4 NormalizeTangentVector4(Vector4 tangent)
{
// normalize only the xyz components of the tangent, the w component is the handedness (1 or -1) in sharpGLTF
var tempTangent = Vector3.Normalize(new Vector3(tangent.X, tangent.Y, tangent.Z));
return new Vector4(tempTangent.X, tempTangent.Y, tempTangent.Z, tangent.W);
}

public static Quaternion GetSys(Microsoft.Xna.Framework.Quaternion q) => new Quaternion(q.X, q.Y, q.Z, q.W);
public static Vector4 GetSys(Microsoft.Xna.Framework.Vector4 v) => new Vector4(v.X, v.Y, v.Z, v.W);
public static Vector3 GetSys(Microsoft.Xna.Framework.Vector3 v) => new Vector3(v.X, v.Y, v.Z);
public static Microsoft.Xna.Framework.Vector4 GetXna(Vector4 v) => new Microsoft.Xna.Framework.Vector4(v.X, v.Y, v.Z, v.W);
public static Microsoft.Xna.Framework.Vector3 GetXna(Vector3 v) => new Microsoft.Xna.Framework.Vector3(v.X, v.Y, v.Z);
public static Microsoft.Xna.Framework.Vector2 GetXna(Vector2 v) => new Microsoft.Xna.Framework.Vector2(v.X, v.Y);
public static Microsoft.Xna.Framework.Vector4 GetXnaVector4(Vector3 v) => new Microsoft.Xna.Framework.Vector4(v.X, v.Y, v.Z, 0);
public static Microsoft.Xna.Framework.Vector3 GetXnaVector3(Vector4 v) => new Microsoft.Xna.Framework.Vector3(v.X, v.Y, v.Z);
public static Matrix4x4 GetSys(Microsoft.Xna.Framework.Matrix invMatrices) =>
new(invMatrices.M11, invMatrices.M12, invMatrices.M13, invMatrices.M14,
invMatrices.M21, invMatrices.M22, invMatrices.M23, invMatrices.M24,
invMatrices.M31, invMatrices.M32, invMatrices.M33, invMatrices.M34,
invMatrices.M41, invMatrices.M42, invMatrices.M43, invMatrices.M44);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public override void Register(IServiceCollection services)
services.AddTransient<GltfMeshBuilder>();
services.AddTransient<IGltfTextureHandler, GltfTextureHandler>();
services.AddTransient<IGltfSceneSaver, GltfSceneSaver>();
services.AddTransient<GltfAnimationCreator>();

services.AddTransient<GltfSkeletonBuilder>();
services.AddTransient<GltfAnimationBuilder>();

RegisterAllAsInterface<IDeveloperConfiguration>(services, ServiceLifetime.Transient);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<ItemGroup>
<ProjectReference Include="..\..\..\GameWorld\View3D\GameWorld.Core.csproj" />
<ProjectReference Include="..\..\..\Shared\SharedCore\Shared.Core.csproj" />
<ProjectReference Include="..\..\..\Shared\SharedUI\Shared.Ui.csproj" />
<ProjectReference Include="..\..\..\Shared\GameFiles\Shared.GameFormats.csproj" />
<ProjectReference Include="..\..\..\Shared\SharedUI\Shared.Ui.csproj" />
<ProjectReference Include="..\..\Shared\Editors.Shared.Core\Editors.Shared.Core.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
using Editors.ImportExport.Common;
using GameWorld.Core.Animation;
using Shared.Core.PackFiles;
using Shared.Core.PackFiles.Models;
using Shared.GameFormats.Animation;
using SharpGLTF.Schema2;
using SysNum = System.Numerics;

namespace Editors.ImportExport.Exporting.Exporters.RmvToGltf.Helpers
{
public class GltfAnimationBuilder
{
private readonly IPackFileService _packFileService;


public GltfAnimationBuilder(IPackFileService packFileServoce)
{
_packFileService = packFileServoce;
}

public void Build(AnimationFile animSkeleton, RmvToGltfExporterSettings settings, ProcessedGltfSkeleton gltfSkeleton, ModelRoot outputScene)
{
foreach (var animationPackFile in settings.InputAnimationFiles)
{
var animationToExport = AnimationFile.Create(animationPackFile);
CreateFromTWAnim(animationPackFile.Name, gltfSkeleton, animSkeleton, animationToExport, outputScene, settings);
}
}

private void CreateFromTWAnim(string animationName, ProcessedGltfSkeleton gltfSkeleton, AnimationFile skeletonAnimFile, AnimationFile animationToExport, ModelRoot modelRoot, RmvToGltfExporterSettings settings)
{
var doMirror = settings.MirrorMesh;
var gameSkeleton = new GameSkeleton(skeletonAnimFile, null);
var animationClip = new AnimationClip(animationToExport, gameSkeleton);

var secondsPerFrame = animationClip.PlayTimeInSec / animationClip.DynamicFrames.Count;

var gltfAnimation = modelRoot.CreateAnimation(animationName);

for (var boneIndex = 0; boneIndex < animationClip.AnimationBoneCount; boneIndex++)
{
var translationKeyFrames = new Dictionary<float, SysNum.Vector3>();
var rotationKeyFrames = new Dictionary<float, SysNum.Quaternion>();
var scaleKeyFrames = new Dictionary<float, SysNum.Vector3>();

// populate the bone track containers with the key frames from the .ANIM animation file
for (var frameIndex = 0; frameIndex < animationClip.DynamicFrames.Count; frameIndex++)
{
translationKeyFrames.Add(secondsPerFrame * (float)frameIndex, VecConv.GetSys(GlobalSceneTransforms.FlipVector(animationClip.DynamicFrames[frameIndex].Position[boneIndex], doMirror)));
rotationKeyFrames.Add(secondsPerFrame * (float)frameIndex, VecConv.GetSys(GlobalSceneTransforms.FlipQuaternion(animationClip.DynamicFrames[frameIndex].Rotation[boneIndex], doMirror)));
scaleKeyFrames.Add(secondsPerFrame * (float)frameIndex, new SysNum.Vector3(1, 1, 1));
}

// add the transformations
var boneNode = gltfSkeleton.Data[boneIndex].Item1;
gltfAnimation.CreateRotationChannel(boneNode, rotationKeyFrames);
gltfAnimation.CreateTranslationChannel(boneNode, translationKeyFrames);
gltfAnimation.CreateScaleChannel(boneNode, scaleKeyFrames);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public List<IMeshBuilder<MaterialBuilder>> Build(RmvFile rmv2, List<TextureResul
{
var rmvMesh = lodLevel[i];
var meshTextures = textures.Where(x=>x.MeshIndex == i).ToList();
var gltfMaterial = Create(settings, rmvMesh.Material.ModelName + "_Material", textures);
var gltfMaterial = Create(settings, rmvMesh.Material.ModelName + "_Material", meshTextures);
var gltfMesh = GenerateMesh(rmvMesh.Mesh, rmvMesh.Material.ModelName, gltfMaterial, hasSkeleton, settings.MirrorMesh);
meshes.Add(gltfMesh);
}
Expand All @@ -46,8 +46,9 @@ MeshBuilder<VertexPositionNormalTangent, VertexTexture1, VertexJoints4> Generate
glTfvertex.Material.TexCoord = new Vector2(vertex.Uv.X, vertex.Uv.Y);

glTfvertex.Geometry.Position = VecConv.GetSys(GlobalSceneTransforms.FlipVector(VecConv.GetXna(glTfvertex.Geometry.Position), doMirror));
glTfvertex.Geometry.Normal = VecConv.GetSys(GlobalSceneTransforms.FlipVector(VecConv.GetXna(glTfvertex.Geometry.Normal), doMirror));
glTfvertex.Geometry.Tangent = VecConv.GetSys(GlobalSceneTransforms.FlipVector(VecConv.GetXna(glTfvertex.Geometry.Tangent), doMirror));

glTfvertex.Geometry.Normal = Vector3.Normalize(VecConv.GetSys(GlobalSceneTransforms.FlipVector(VecConv.GetXna(glTfvertex.Geometry.Normal), doMirror)));
glTfvertex.Geometry.Tangent = VecConv.NormalizeTangentVector4(VecConv.GetSys(GlobalSceneTransforms.FlipVector(VecConv.GetXna(glTfvertex.Geometry.Tangent), doMirror)));

if (hasSkeleton)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SharpGLTF.Schema2;
using System.Windows;
using Shared.Core.ErrorHandling.Exceptions;
using SharpGLTF.Schema2;

namespace Editors.ImportExport.Exporting.Exporters.RmvToGltf.Helpers
{
Expand All @@ -10,9 +12,23 @@ public interface IGltfSceneSaver

public class GltfSceneSaver : IGltfSceneSaver
{
public void Save(ModelRoot modelRoot, string fullSystemPath)
private readonly IExceptionService _exceptionService;

public GltfSceneSaver(IExceptionService exceptionService)
{
modelRoot.SaveGLTF(fullSystemPath);
_exceptionService = exceptionService;
}

public void Save(ModelRoot modelRoot, string fullSystemPath)
{
try
{
modelRoot.SaveGLTF(fullSystemPath);
}
catch (Exception ex)
{
_exceptionService.CreateDialog(ex);
}
}
}
}
Loading

0 comments on commit 680166a

Please sign in to comment.