Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectXTexNet;
using Shared.Core.Settings;
using Shared.GameFormats.RigidModel.Types;

namespace Editors.ImportExport.Common.Interfaces
{
public interface IImageProcessor
{
ScratchImage Transform(ScratchImage scratchImage);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Editors.ImportExport.Importing.Importers.PngToDds.Helpers.ImageProcessor;
using Shared.Core.Settings;
using Shared.GameFormats.RigidModel.Types;

namespace Editors.ImportExport.Common.Interfaces
{
public static class ImageProcessorFactory
{
private static readonly Dictionary<TextureType, IImageProcessor> _textureAndGameTypeToTranformer = new Dictionary<TextureType, IImageProcessor>()
{
{TextureType.Diffuse, new DefaultImageProcessor() },
{TextureType.MaterialMap, new BlenderToWH3MaterialMapProcessor() },
{TextureType.BaseColour, new DefaultImageProcessor() },
{TextureType.Normal, new BlueToOrangeNormalMapProcessor() }
};

public static IImageProcessor CreateImageProcessor(TextureType textureType)
{
if (_textureAndGameTypeToTranformer.TryGetValue(textureType, out var imageProcessor))
{
return imageProcessor;
}

return new DefaultImageProcessor();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
using Editors.ImportExport.Importing;
using Editors.ImportExport.Importing.Importers.GltfToRmv;
using Editors.ImportExport.Importing.Importers.GltfToRmv.Helper;
using Editors.ImportExport.Importing.Presentation;
using Microsoft.Extensions.DependencyInjection;
using Shared.Core.DependencyInjection;
using Shared.Core.DevConfig;
using Editors.ImportImport.Importing.Presentation.RmvToGltf;
using Shared.Ui.BaseDialogs.PackFileTree.ContextMenu.External;

namespace Editors.ImportExport
Expand All @@ -38,8 +40,14 @@ public override void Register(IServiceCollection services)
services.AddTransient<IDdsToNormalPngExporter, DdsToNormalPngExporter>();
services.AddTransient<RmvToGltfExporter>();

// Importer ViewModels
RegisterWindow<ImportWindow>(services);
services.AddTransient<ImporterCoreViewModel>();
services.AddTransient<IImporterViewModel, RmvToGltfImporterViewModel>();

// Importers
services.AddTransient<GltfImporter>();
services.AddTransient<RmvMaterialBuilder>();

// Image Save Helper
services.AddScoped<IImageSaveHandler, SystemImageSaveHandler>();
Expand All @@ -57,7 +65,7 @@ public override void Register(IServiceCollection services)
services.AddTransient<IGltfSceneLoader, GltfSceneLoader>();
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 @@ -39,7 +39,7 @@ public void OpenFileOnLoad()

Directory.CreateDirectory(destPath);

var settings = new RmvToGltfExporterSettings(meshPackFile, new List<PackFile>() { animPackFile }, destPath + "myKarl.gltf", true, true, true, true);
var settings = new RmvToGltfExporterSettings(meshPackFile, new List<PackFile>() { animPackFile }, destPath + "myKarl.gltf", true, true, true, true, true);
_exporter.Export(settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DirectXTexNet" Version="1.0.7" />
<PackageReference Include="SharpGLTF.Core" Version="1.0.3" />
<PackageReference Include="SharpGLTF.Toolkit" Version="1.0.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Editors.ImportExport.Exporting.Exporters;
using Editors.ImportExport.Misc;
using Editors.ImportExport.Misc;
using Editors.ImportExport.Exporting.Exporters;
using Shared.Core.Events;
using Shared.Core.PackFiles.Models;
using Shared.Core.Settings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.IO;
using System.Numerics;
using Editors.ImportExport.Common;
using Shared.GameFormats.RigidModel;
using Shared.GameFormats.RigidModel.Vertex;
Expand Down Expand Up @@ -120,8 +121,17 @@ MaterialBuilder Create(RmvToGltfExporterSettings settings, string materialName,
.WithAlpha(AlphaMode.MASK);

foreach (var texture in texturesForModel)
{
material.WithChannelImage(texture.GlftTexureType, texture.SystemFilePath);

var channel = material.UseChannel(texture.GlftTexureType);
if (channel?.Texture?.PrimaryImage != null)
{
// Set SharpGLTF to re-resave textures with specified paths, default behavior is texturePath = "{folder}\meshName{counter}.png"
channel.Texture.PrimaryImage.AlternateWriteFileName = Path.GetFileName(texture.SystemFilePath);
}
}

return material;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Numerics;
using Editors.ImportExport.Common;
using Editors.ImportExport.Exporting.Exporters.GltfSkeleton;
using Editors.Shared.Core.Services;
using GameWorld.Core.Animation;
using GameWorld.Core.SceneNodes;
Expand All @@ -22,14 +21,14 @@ public class GltfSkeletonBuilder

public GltfSkeletonBuilder(IPackFileService packFileService)
{
_packFileService = packFileService;
_packFileService = packFileService;
}

public ProcessedGltfSkeleton CreateSkeleton(AnimationFile skeletonAnimFile, ModelRoot outputScene, RmvToGltfExporterSettings settings)
{
{
var gltfSkeleton = CreateSkeleton(outputScene, skeletonAnimFile, settings.MirrorMesh);
return gltfSkeleton;

return gltfSkeleton;
}

ProcessedGltfSkeleton CreateSkeleton(ModelRoot outputScene, AnimationFile animSkeletonFil, bool doMirror)
Expand All @@ -48,8 +47,8 @@ ProcessedGltfSkeleton CreateSkeleton(ModelRoot outputScene, AnimationFile animSk
var outputGltfBindings = new List<(Node node, Matrix4x4 invMatrix)>();

var scene = outputScene.UseScene("default");

scene.CreateNode($"//skeleton//{animSkeletonFil.Header.SkeletonName.ToLower()}");
AddSkeletonIdNodeToScene(animSkeletonFil, scene);

var parentIdToGltfNode = new Dictionary<int, Node>();
parentIdToGltfNode[-1] = scene.CreateNode(""); // bones with no parent will be children of the scene
Expand All @@ -58,7 +57,7 @@ ProcessedGltfSkeleton CreateSkeleton(ModelRoot outputScene, AnimationFile animSk
{
var parentNode = parentIdToGltfNode[animSkeletonFil.Bones[boneIndex].ParentId];
if (parentNode == null)
throw new Exception($"Parent Node not found for boneIndex={boneIndex}");
throw new Exception($"Parent Node cannot be null. boneIndex={boneIndex}");

parentIdToGltfNode[boneIndex] = parentNode.CreateNode(animSkeletonFil.Bones[boneIndex].Name);

Expand All @@ -74,7 +73,10 @@ ProcessedGltfSkeleton CreateSkeleton(ModelRoot outputScene, AnimationFile animSk

return new ProcessedGltfSkeleton() { Data = outputGltfBindings };
}



private static void AddSkeletonIdNodeToScene(AnimationFile animSkeletonFil, Scene scene)
{
scene.CreateNode($"//skeleton//{animSkeletonFil.Header.SkeletonName.ToLower()}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public List<TextureResult> HandleTextures(RmvFile rmvFile, RmvToGltfExporterSett
{
var output = new List<TextureResult>();

if (!settings.ExportMaterials)
return output;

var exportedTextures = new Dictionary<string, string>(); // To avoid exporting same texture multiple times

int lodICounnt = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Editors.ImportExport.Common;
using Microsoft.Xna.Framework;
using Shared.GameFormats.Animation;
using SharpGLTF.Animations;
using SharpGLTF.Schema2;

namespace Editors.ImportExport.Exporting.Exporters.GltfSkeleton
namespace Editors.ImportExport.Exporting.Exporters.RmvToGltf.Helpers
{
public class TransformData
{
Expand Down Expand Up @@ -32,7 +34,51 @@ public Matrix GlobalTransform
}
}
}
public class GltfAnimationTrackSampler
{
static public Vector3 SampleTranslation(ModelRoot model, string boneName, float time)
{
// Access the first animation
var animation = model.LogicalAnimations[0];

// Access the first node
var node = model.LogicalNodes.FirstOrDefault(n => n.Name.ToLower() == boneName.ToLower());

if (node == null)
{
throw new Exception("Error, Unexpected, Node not found");
}

// Get the translation sampler for the node
var translationSampler = animation.FindTranslationChannel(node).GetTranslationSampler().CreateCurveSampler();

var translationVector = translationSampler.GetPoint(time);

return GlobalSceneTransforms.FlipVector(translationVector, true);
}

static public Quaternion SampleQuaternion(ModelRoot model, string boneName, float time)
{
// Access the first animation
var animation = model.LogicalAnimations[0];

// Access the first nodef
var node = model.LogicalNodes.FirstOrDefault(n => n.Name.ToLower() == boneName.ToLower());

if (node == null)
{
throw new Exception("Error, Unexpected, Node not found");
}

// Get the translation sampler for the node
var quaternionSampler = animation.FindRotationChannel(node).GetRotationSampler().CreateCurveSampler();

var outQuaternion = quaternionSampler.GetPoint(time);

return GlobalSceneTransforms.FlipQuaternion(outQuaternion, true);
}

}
internal class FramePoseMatrixCalculator
{
private readonly AnimationFile _animationFile;
Expand Down Expand Up @@ -83,13 +129,13 @@ private static void ValidDateSkeletonFile(AnimationFile file)
throw new Exception($"No anim parts! Bone Count: {file.Bones.Length}");

if (file.AnimationParts[0].DynamicFrames.Count == 0)
throw new Exception($"No anim frames, in part 0! Bone Count: {file.Bones.Length}, Anim Parts Count: {file.AnimationParts.Count}");
throw new Exception($"No anim frames, in part 0! Bone Count: {file.Bones.Length}, Anim Parts Count: {file.AnimationParts.Count}");

if (file.AnimationParts[0].DynamicFrames[0].Quaternion.Count != file.Bones.Length)
throw new Exception($"Not a valid skeleton file, doesn't contain quaternion values for all bones! Quat count frame 0: {file.AnimationParts[0].DynamicFrames[0].Quaternion.Count}, Bone count {file.Bones.Length}");
throw new Exception($"Not a valid skeleton file, doesn't contain quaternion values for all bones! Quat count frame 0: {file.AnimationParts[0].DynamicFrames[0].Quaternion.Count}, Bone count {file.Bones.Length}");

if (file.AnimationParts[0].DynamicFrames[0].Transforms.Count != file.Bones.Length)
throw new Exception($"Not a valid skeleton file, doesn't contain translation values for all bones! Trans count frame 0: {file.AnimationParts[0].DynamicFrames[0].Transforms.Count}, Bone count {file.Bones.Length}");
throw new Exception($"Not a valid skeleton file, doesn't contain translation values for all bones! Trans count frame 0: {file.AnimationParts[0].DynamicFrames[0].Transforms.Count}, Bone count {file.Bones.Length}");
}

private void RebuildSkeletonGlobalMatrices(bool doMirror)
Expand All @@ -104,7 +150,7 @@ private void RebuildSkeletonGlobalMatrices(bool doMirror)
_worldTransform[boneIndex] = transform;
}

for (var i = 0; i < _worldTransform.Length; i++)
for (var i = 0; i < _worldTransform.Length; i++)
{
var parentIndex = _animationFile.Bones[i].ParentId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using Editors.ImportExport.Common;
using Editors.ImportExport.Exporting.Exporters.RmvToGltf.Helpers;
using Editors.ImportExport.Misc;
using GameWorld.Core.Services;
Expand Down Expand Up @@ -66,9 +67,10 @@ public void Export(RmvToGltfExporterSettings settings)
gltfSkeleton = _gltfSkeletonBuilder.CreateSkeleton(skeletonAnimFile, outputScene, settings);
_gltfAnimationBuilder.Build(skeletonAnimFile, settings, gltfSkeleton, outputScene);
}
}

var textures = _gltfTextureHandler.HandleTextures(rmv2, settings);
}

var textures = _gltfTextureHandler.HandleTextures(rmv2, settings);

var meshes = _gltfMeshBuilder.Build(rmv2, textures, settings);

_logger.Here().Information($"MeshCount={meshes.Count()} TextureCount={textures.Count()} Skeleton={gltfSkeleton?.Data.Count}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record RmvToGltfExporterSettings(
PackFile InputModelFile,
List<PackFile> InputAnimationFiles,
string OutputPath,
bool ExportMaterials,
bool ConvertMaterialTextureToBlender,
bool ConvertNormalTextureToBlue,
bool ExportAnimations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public RmvToGltfExporterViewModel(RmvToGltfExporter exporter)

public void Execute(PackFile exportSource, string outputPath, bool generateImporter)
{
var settings = new RmvToGltfExporterSettings(exportSource, [], outputPath, ConvertMaterialTextureToBlender, ConvertNormalTextureToBlue, ExportAnimations, true);
var settings = new RmvToGltfExporterSettings(exportSource, [], outputPath, ExportTextures, ConvertMaterialTextureToBlender, ConvertNormalTextureToBlue, ExportAnimations, true);
_exporter.Export(settings);
}
}
Expand Down
Loading
Loading