Skip to content

Commit c671225

Browse files
authored
Merge pull request #566 from Drigax/drigax/refactorGLTFExport
Refactor Babylon->GLTF2 exporter to use shared project
2 parents f97c29c + c341072 commit c671225

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6518
-6149
lines changed

3ds Max/Max2Babylon.sln

Lines changed: 236 additions & 222 deletions
Large diffs are not rendered by default.

3ds Max/Max2Babylon/2015/Max2Babylon2015.csproj

Lines changed: 347 additions & 387 deletions
Large diffs are not rendered by default.

3ds Max/Max2Babylon/2017/Max2Babylon2017.csproj

Lines changed: 347 additions & 387 deletions
Large diffs are not rendered by default.

3ds Max/Max2Babylon/2018/Max2Babylon2018.csproj

Lines changed: 348 additions & 388 deletions
Large diffs are not rendered by default.

3ds Max/Max2Babylon/2019/Max2Babylon2019.csproj

Lines changed: 348 additions & 388 deletions
Large diffs are not rendered by default.

3ds Max/Max2Babylon/Exporter/AnimationGroup.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Autodesk.Max;
99
using Autodesk.Max.Plugins;
1010
using Newtonsoft.Json;
11+
using Utilities;
1112

1213
namespace Max2Babylon
1314
{
@@ -61,7 +62,7 @@ public string Name
6162
}
6263
public int FrameStart
6364
{
64-
get { return Tools.RoundToInt(ticksStart / (float)Loader.Global.TicksPerFrame); }
65+
get { return MathUtilities.RoundToInt(ticksStart / (float)Loader.Global.TicksPerFrame); }
6566
set
6667
{
6768
if (value.Equals(FrameStart)) // property getter
@@ -72,7 +73,7 @@ public int FrameStart
7273
}
7374
public int FrameEnd
7475
{
75-
get { return Tools.RoundToInt(TicksEnd / (float)Loader.Global.TicksPerFrame); }
76+
get { return MathUtilities.RoundToInt(TicksEnd / (float)Loader.Global.TicksPerFrame); }
7677
set
7778
{
7879
if (value.Equals(FrameEnd)) // property getter
@@ -274,6 +275,59 @@ public void LoadFromData()
274275
info.LoadFromData(propertyNameStr);
275276
Add(info);
276277
}
278+
}
279+
280+
public static AnimationGroupList InitAnimationGroups(ILoggingProvider logger)
281+
{
282+
AnimationGroupList animationList = new AnimationGroupList();
283+
animationList.LoadFromData();
284+
285+
if (animationList.Count > 0)
286+
{
287+
int timelineStart = Loader.Core.AnimRange.Start / Loader.Global.TicksPerFrame;
288+
int timelineEnd = Loader.Core.AnimRange.End / Loader.Global.TicksPerFrame;
289+
290+
foreach (AnimationGroup animGroup in animationList)
291+
{
292+
// ensure min <= start <= end <= max
293+
List<string> warnings = new List<string>();
294+
if (animGroup.FrameStart < timelineStart || animGroup.FrameStart > timelineEnd)
295+
{
296+
warnings.Add("Start frame '" + animGroup.FrameStart + "' outside of timeline range [" + timelineStart + ", " + timelineEnd + "]. Set to timeline start time '" + timelineStart + "'");
297+
animGroup.FrameStart = timelineStart;
298+
}
299+
if (animGroup.FrameEnd < timelineStart || animGroup.FrameEnd > timelineEnd)
300+
{
301+
warnings.Add("End frame '" + animGroup.FrameEnd + "' outside of timeline range [" + timelineStart + ", " + timelineEnd + "]. Set to timeline end time '" + timelineEnd + "'");
302+
animGroup.FrameEnd = timelineEnd;
303+
}
304+
if (animGroup.FrameEnd <= animGroup.FrameStart)
305+
{
306+
if (animGroup.FrameEnd < animGroup.FrameStart)
307+
// Strict
308+
warnings.Add("End frame '" + animGroup.FrameEnd + "' lower than Start frame '" + animGroup.FrameStart + "'. Start frame set to timeline start time '" + timelineStart + "'. End frame set to timeline end time '" + timelineEnd + "'.");
309+
else
310+
// Equal
311+
warnings.Add("End frame '" + animGroup.FrameEnd + "' equal to Start frame '" + animGroup.FrameStart + "'. Single frame animation are not allowed. Start frame set to timeline start time '" + timelineStart + "'. End frame set to timeline end time '" + timelineEnd + "'.");
312+
313+
animGroup.FrameStart = timelineStart;
314+
animGroup.FrameEnd = timelineEnd;
315+
}
316+
317+
// Print animation group warnings if any
318+
// Nothing printed otherwise
319+
if (warnings.Count > 0)
320+
{
321+
logger.RaiseWarning(animGroup.Name, 1);
322+
foreach (string warning in warnings)
323+
{
324+
logger.RaiseWarning(warning, 2);
325+
}
326+
}
327+
}
328+
}
329+
330+
return animationList;
277331
}
278332

279333
public void SaveToData()

3ds Max/Max2Babylon/Exporter/BabylonExporter.Animation.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private IList<BabylonAnimationGroup> ExportAnimationGroups(BabylonScene babylonS
1313
IList<BabylonAnimationGroup> animationGroups = new List<BabylonAnimationGroup>();
1414

1515
// Retrieve and parse animation group data
16-
AnimationGroupList animationList = InitAnimationGroups();
16+
AnimationGroupList animationList = AnimationGroupList.InitAnimationGroups(this);
1717

1818
foreach (AnimationGroup animGroup in animationList)
1919
{
@@ -571,10 +571,11 @@ private BabylonAnimation ExportAnimation(string property, Func<int, float[]> ext
571571
}
572572
var keysFull = new List<BabylonAnimationKey>(keys);
573573

574+
// Optimization process always keeps first and last frames
574575
if (optimizeAnimations)
575576
{
576577
OptimizeAnimations(keys, removeLinearAnimationKeys);
577-
}
578+
}
578579

579580
if (IsAnimationKeysRelevant(keys))
580581
{

3ds Max/Max2Babylon/Exporter/BabylonExporter.ExportItem.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
6+
using Utilities;
87

98
namespace Max2Babylon
109
{
@@ -145,12 +144,12 @@ public void SetExportFilePath(string filePath)
145144
else if (Path.IsPathRooted(filePath)) // absolute path
146145
{
147146
absolutePath = Path.GetFullPath(filePath);
148-
relativePath = Tools.GetRelativePath(dirName, filePath);
147+
relativePath = PathUtilities.GetRelativePath(dirName, filePath);
149148
}
150149
else // relative path
151150
{
152151
absolutePath = Path.GetFullPath(Path.Combine(dirName, filePath));
153-
relativePath = Tools.GetRelativePath(dirName, absolutePath);
152+
relativePath = PathUtilities.GetRelativePath(dirName, absolutePath);
154153

155154
exportPathRelative = relativePath;
156155
}

3ds Max/Max2Babylon/Exporter/BabylonExporter.IMaterialExporter.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Drawing;
34
using Autodesk.Max;
5+
using Babylon2GLTF;
46
using BabylonExport.Entities;
5-
using GLTFExport.Entities;
6-
7+
using GLTFExport.Entities;
8+
using Utilities;
9+
710
namespace Max2Babylon
811
{
9-
10-
11-
public interface IMaterialExporter
12+
public interface IMaxMaterialExporter
1213
{
1314
ClassIDWrapper MaterialClassID { get; }
1415
}
1516

16-
public interface IBabylonMaterialExporter : IMaterialExporter
17+
public interface IMaxBabylonMaterialExporter : IMaxMaterialExporter
1718
{
1819
BabylonMaterial ExportBabylonMaterial(IIGameMaterial material);
1920
}
2021

2122
delegate string TryWriteImageCallback(string sourceTexturePath);
2223

23-
internal interface IGLTFMaterialExporter : IMaterialExporter
24+
internal interface IMaxGLTFMaterialExporter : IMaxMaterialExporter
2425
{
2526
/// <summary>
2627
/// Creates a GLTF material using the given GameMaterial.
2728
/// </summary>
28-
/// <param name="exporter">The current exporter, for export parameters like CopyTexturesToOuput.</param>
29+
/// <param name="exporter">The current exporter parameters, like CopyTexturesToOuput.</param>
2930
/// <param name="gltf">The GLTF output structure, for adding instances of classes such as GLTFSampler, GLTFImage and GLTFTexture.</param>
3031
/// <param name="material">The input material matching the MaterialClassID defined by the exporter. </param>
3132
/// <param name="tryWriteImageFunc">Callback function to verify images and to write images to the output folder.
@@ -35,7 +36,7 @@ internal interface IGLTFMaterialExporter : IMaterialExporter
3536
/// <param name="raiseWarningAction">Callback function to raise warnings.</param>
3637
/// <param name="raiseErrorAction">Callback function to raise errors.</param>
3738
/// <returns>The exported GLTF material.</returns>
38-
GLTFMaterial ExportGLTFMaterial(BabylonExporter exporter, GLTF gltf, IIGameMaterial material, Func<string, string, string> tryWriteImageFunc,
39+
GLTFMaterial ExportGLTFMaterial(ExportParameters exportParameters, GLTF gltf, IIGameMaterial material, Func<string, string, string> tryWriteImageFunc,
3940
Action<string, Color> raiseMessageAction, Action<string> raiseWarningAction, Action<string> raiseErrorAction);
4041
}
4142

3ds Max/Max2Babylon/Exporter/BabylonExporter.Material.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Linq;
33
using System.Collections.Generic;
44
using Autodesk.Max;
5-
using BabylonExport.Entities;
6-
using System.Diagnostics;
5+
using Utilities;
6+
using BabylonExport.Entities;
77

88
namespace BabylonExport.Entities
99
{
@@ -18,7 +18,7 @@ namespace Max2Babylon
1818
partial class BabylonExporter
1919
{
2020
readonly List<IIGameMaterial> referencedMaterials = new List<IIGameMaterial>();
21-
Dictionary<ClassIDWrapper, IMaterialExporter> materialExporters;
21+
Dictionary<ClassIDWrapper, IMaxMaterialExporter> materialExporters;
2222

2323
private void ExportMaterial(IIGameMaterial materialNode, BabylonScene babylonScene)
2424
{
@@ -137,15 +137,15 @@ private void ExportMaterial(IIGameMaterial materialNode, BabylonScene babylonSce
137137
bool isUnlit = unlitProperty != null ? unlitProperty.GetBoolValue() : false;
138138

139139
// check custom exporters first, to allow custom exporters of supported material classes
140-
IMaterialExporter materialExporter;
140+
IMaxMaterialExporter materialExporter;
141141
materialExporters.TryGetValue(new ClassIDWrapper(materialNode.MaxMaterial.ClassID), out materialExporter);
142142

143143

144144
var stdMat = materialNode.MaxMaterial.NumParamBlocks > 0 ? materialNode.MaxMaterial.GetParamBlock(0).Owner as IStdMat2 : null;
145145

146-
if (isBabylonExported && materialExporter != null && materialExporter is IBabylonMaterialExporter)
146+
if (isBabylonExported && materialExporter != null && materialExporter is IMaxBabylonMaterialExporter)
147147
{
148-
IBabylonMaterialExporter babylonMaterialExporter = materialExporter as IBabylonMaterialExporter;
148+
IMaxBabylonMaterialExporter babylonMaterialExporter = materialExporter as IMaxBabylonMaterialExporter;
149149
BabylonMaterial babylonMaterial = babylonMaterialExporter.ExportBabylonMaterial(materialNode);
150150
if (babylonMaterial == null)
151151
{
@@ -155,7 +155,7 @@ private void ExportMaterial(IIGameMaterial materialNode, BabylonScene babylonSce
155155
}
156156
else babylonScene.MaterialsList.Add(babylonMaterial);
157157
}
158-
else if (isGltfExported && materialExporter != null && materialExporter is IGLTFMaterialExporter)
158+
else if (isGltfExported && materialExporter != null && materialExporter is IMaxGLTFMaterialExporter)
159159
{
160160
// add a basic babylon material to the list to forward the max material reference
161161
var babylonMaterial = new BabylonMaterial(id)
@@ -731,12 +731,12 @@ public IIGameMaterial isMaterialSupported(IIGameMaterial materialNode)
731731
}
732732

733733
// Custom material exporters
734-
IMaterialExporter materialExporter;
734+
IMaxMaterialExporter materialExporter;
735735
if (materialExporters.TryGetValue(new ClassIDWrapper(materialNode.MaxMaterial.ClassID), out materialExporter))
736736
{
737-
if (isGltfExported && materialExporter is IGLTFMaterialExporter)
737+
if (isGltfExported && materialExporter is IMaxGLTFMaterialExporter)
738738
return null;
739-
else if (isBabylonExported && materialExporter is IBabylonMaterialExporter)
739+
else if (isBabylonExported && materialExporter is IMaxBabylonMaterialExporter)
740740
return null;
741741
}
742742

0 commit comments

Comments
 (0)