Browse Source

Fixing DDS exportation issues

David Catuhe 11 years ago
parent
commit
9aac781a22

+ 8 - 1
Exporters/3ds Max/BabylonExport.Entities/BabylonAnimation.cs

@@ -15,7 +15,7 @@ namespace BabylonExport.Entities
         public DataType dataType { get; set; }
 
         [DataMember]
-        public int loopBehavior { get; set; }
+        public LoopBehavior loopBehavior { get; set; }
 
         [DataMember]
         public int framePerSecond { get; set; }
@@ -30,5 +30,12 @@ namespace BabylonExport.Entities
             Quaternion = 2,
             Matrix = 3
         }
+
+        public enum LoopBehavior
+        {
+            Relative = 0,
+            Cycle = 1,
+            Constant = 2
+        }
     }
 }

+ 6 - 0
Exporters/3ds Max/BabylonExport.Entities/BabylonMesh.cs

@@ -101,6 +101,12 @@ namespace BabylonExport.Entities
         [DataMember]
         public BabylonAnimation[] animations { get; set; }
 
+        [DataMember]
+        public bool showBoundingBox { get; set; }
+
+        [DataMember]
+        public bool showSubMeshesBoundingBox { get; set; }
+
         public BabylonMesh()
         {
             isEnabled = true;

BIN
Exporters/3ds Max/Max2Babylon-0.4.3.zip


+ 48 - 7
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

@@ -27,9 +27,11 @@ namespace Max2Babylon
             babylonMesh.isVisible = meshNode._Node.Renderable == 1;
             babylonMesh.pickable = meshNode._Node.GetBoolProperty("babylonjs_checkpickable");
             babylonMesh.receiveShadows = meshNode._Node.RcvShadows == 1;
+            babylonMesh.showBoundingBox = meshNode._Node.GetBoolProperty("babylonjs_showboundingbox");
+            babylonMesh.showSubMeshesBoundingBox = meshNode._Node.GetBoolProperty("babylonjs_showsubmeshesboundingbox");
 
             // Collisions
-            babylonMesh.checkCollisions = meshNode._Node.GetBoolProperty("babylonjs_checkcollisions");            
+            babylonMesh.checkCollisions = meshNode._Node.GetBoolProperty("babylonjs_checkcollisions");
 
             // Position / rotation / scaling
             var wm = meshNode.GetWorldMatrix(0, meshNode.HasParent());
@@ -130,7 +132,7 @@ namespace Max2Babylon
                 var hasUV2 = mesh.GetNumMapVerts(2) > 0;
 
                 var noOptimize = meshNode._Node.GetBoolProperty("babylonjs_nooptimize");
-                
+
                 for (var face = 0; face < mesh.NumFaces; face++)
                 {
                     indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx1, vertices, hasUV, hasUV2, noOptimize));
@@ -169,14 +171,14 @@ namespace Max2Babylon
 
                     subMesh.indexStart = indexStart;
                     subMesh.materialIndex = index;
-                    
+
                     for (var face = 0; face < matIDs.Count; face++)
                     {
                         if (matIDs[face] == index)
                         {
-                            var a = indices[3*face];
-                            var b = indices[3*face + 1];
-                            var c = indices[3*face + 2];
+                            var a = indices[3 * face];
+                            var b = indices[3 * face + 1];
+                            var c = indices[3 * face + 2];
 
                             sortedIndices.Add(a);
                             sortedIndices.Add(b);
@@ -187,7 +189,7 @@ namespace Max2Babylon
                             {
                                 minVertexIndex = a;
                             }
-                            
+
                             if (b < minVertexIndex)
                             {
                                 minVertexIndex = b;
@@ -233,6 +235,45 @@ namespace Max2Babylon
                 babylonMesh.indices = sortedIndices.ToArray();
             }
 
+            // Animations - Position
+            const int ticks = 160;
+            var start = Loader.Core.AnimRange.Start;
+            var end = Loader.Core.AnimRange.End;
+
+            IPoint3 previousPosition = null;
+            var keys = new List<BabylonAnimationKey>();
+            for (var key = start; key <= end; key += ticks)
+            {
+                var worldMatrix = meshNode.GetWorldMatrix(key, meshNode.HasParent());
+                var currentPosition = worldMatrix.Trans;
+
+                if (key == start || key == end || (previousPosition.Equals(currentPosition, Tools.Epsilon) != 0))
+                {
+                    keys.Add(new BabylonAnimationKey()
+                    {
+                        frame = key,
+                        values = currentPosition.ToArraySwitched()
+                    });
+                }
+
+                previousPosition = currentPosition;
+            }
+
+            if (keys.Count > 0)
+            {
+                var babylonAnimation = new BabylonAnimation
+                {
+                    dataType = BabylonAnimation.DataType.Vector3,
+                    name = "Position animation",
+                    keys = keys.ToArray(),
+                    framePerSecond = Loader.Global.FrameRate,
+                    loopBehavior = BabylonAnimation.LoopBehavior.Relative,
+                    property = "position"
+                };
+
+                babylonMesh.animations = new[] {babylonAnimation};
+            }
+
             babylonScene.MeshesList.Add(babylonMesh);
 
             return babylonMesh;

+ 5 - 0
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Texture.cs

@@ -56,6 +56,11 @@ namespace Max2Babylon
             babylonTexture.uScale = uvGen.GetUScl(0);
             babylonTexture.vScale = uvGen.GetVScl(0);
 
+            if (Path.GetExtension(texture.MapName).ToLower() == ".dds")
+            {
+                babylonTexture.vScale *= -1; // Need to invert Y-axis for DDS texture
+            }
+
             babylonTexture.uAng = uvGen.GetUAng(0);
             babylonTexture.vAng = uvGen.GetVAng(0);
             babylonTexture.wAng = uvGen.GetWAng(0);

+ 5 - 0
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.cs

@@ -142,6 +142,11 @@ namespace Max2Babylon
                 ExportLight(lightNode, babylonScene);
             }
 
+            if (babylonScene.LightsList.Count == 0)
+            {
+                RaiseWarning("No light defined", true);
+            }
+
             // Output
             babylonScene.Prepare(false);
             var jsonSerializer = JsonSerializer.Create();

+ 4 - 5
Exporters/3ds Max/Max2Babylon/Exporter/GlobalVertex.cs

@@ -4,7 +4,6 @@ namespace Max2Babylon
 {
     public struct GlobalVertex
     {
-        const float epsilon = 0.001f;
         public IPoint3 Position { get; set; }
         public IPoint3 Normal { get; set; }
         public IPoint2 UV { get; set; }
@@ -24,22 +23,22 @@ namespace Max2Babylon
 
             var other = (GlobalVertex)obj;
 
-            if (!other.Position.IsAlmostEqualTo(Position, epsilon))
+            if (!other.Position.IsAlmostEqualTo(Position, Tools.Epsilon))
             {
                 return false;
             }
 
-            if (!other.Normal.IsAlmostEqualTo(Normal, epsilon))
+            if (!other.Normal.IsAlmostEqualTo(Normal, Tools.Epsilon))
             {
                 return false;
             }
 
-            if (UV != null && !other.UV.IsAlmostEqualTo(UV, epsilon))
+            if (UV != null && !other.UV.IsAlmostEqualTo(UV, Tools.Epsilon))
             {
                 return false;
             }
 
-            if (UV2 != null && !other.UV2.IsAlmostEqualTo(UV2, epsilon))
+            if (UV2 != null && !other.UV2.IsAlmostEqualTo(UV2, Tools.Epsilon))
             {
                 return false;
             }

+ 39 - 11
Exporters/3ds Max/Max2Babylon/Forms/ObjectPropertiesForm.Designer.cs

@@ -33,8 +33,10 @@
             this.butCancel = new System.Windows.Forms.Button();
             this.butOK = new System.Windows.Forms.Button();
             this.groupBox2 = new System.Windows.Forms.GroupBox();
-            this.chkPickable = new System.Windows.Forms.CheckBox();
             this.chkNoOptimize = new System.Windows.Forms.CheckBox();
+            this.chkPickable = new System.Windows.Forms.CheckBox();
+            this.chkShowBoundingBox = new System.Windows.Forms.CheckBox();
+            this.chkShowSubMeshesBoundingBox = new System.Windows.Forms.CheckBox();
             this.groupBox1.SuspendLayout();
             this.groupBox2.SuspendLayout();
             this.SuspendLayout();
@@ -88,16 +90,29 @@
             // 
             // groupBox2
             // 
+            this.groupBox2.Controls.Add(this.chkShowSubMeshesBoundingBox);
+            this.groupBox2.Controls.Add(this.chkShowBoundingBox);
             this.groupBox2.Controls.Add(this.chkNoOptimize);
             this.groupBox2.Controls.Add(this.chkPickable);
             this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
             this.groupBox2.Location = new System.Drawing.Point(12, 77);
             this.groupBox2.Name = "groupBox2";
-            this.groupBox2.Size = new System.Drawing.Size(319, 113);
+            this.groupBox2.Size = new System.Drawing.Size(319, 143);
             this.groupBox2.TabIndex = 2;
             this.groupBox2.TabStop = false;
             this.groupBox2.Text = "Misc.";
             // 
+            // chkNoOptimize
+            // 
+            this.chkNoOptimize.AutoSize = true;
+            this.chkNoOptimize.Location = new System.Drawing.Point(21, 51);
+            this.chkNoOptimize.Name = "chkNoOptimize";
+            this.chkNoOptimize.Size = new System.Drawing.Size(165, 17);
+            this.chkNoOptimize.TabIndex = 1;
+            this.chkNoOptimize.Text = "Do not try to optimize vertices";
+            this.chkNoOptimize.ThreeState = true;
+            this.chkNoOptimize.UseVisualStyleBackColor = true;
+            // 
             // chkPickable
             // 
             this.chkPickable.AutoSize = true;
@@ -109,16 +124,27 @@
             this.chkPickable.ThreeState = true;
             this.chkPickable.UseVisualStyleBackColor = true;
             // 
-            // chkNoOptimize
+            // chkShowBoundingBox
             // 
-            this.chkNoOptimize.AutoSize = true;
-            this.chkNoOptimize.Location = new System.Drawing.Point(21, 51);
-            this.chkNoOptimize.Name = "chkNoOptimize";
-            this.chkNoOptimize.Size = new System.Drawing.Size(165, 17);
-            this.chkNoOptimize.TabIndex = 1;
-            this.chkNoOptimize.Text = "Do not try to optimize vertices";
-            this.chkNoOptimize.ThreeState = true;
-            this.chkNoOptimize.UseVisualStyleBackColor = true;
+            this.chkShowBoundingBox.AutoSize = true;
+            this.chkShowBoundingBox.Location = new System.Drawing.Point(21, 74);
+            this.chkShowBoundingBox.Name = "chkShowBoundingBox";
+            this.chkShowBoundingBox.Size = new System.Drawing.Size(120, 17);
+            this.chkShowBoundingBox.TabIndex = 2;
+            this.chkShowBoundingBox.Text = "Show bounding box";
+            this.chkShowBoundingBox.ThreeState = true;
+            this.chkShowBoundingBox.UseVisualStyleBackColor = true;
+            // 
+            // chkShowSubMeshesBoundingBox
+            // 
+            this.chkShowSubMeshesBoundingBox.AutoSize = true;
+            this.chkShowSubMeshesBoundingBox.Location = new System.Drawing.Point(21, 97);
+            this.chkShowSubMeshesBoundingBox.Name = "chkShowSubMeshesBoundingBox";
+            this.chkShowSubMeshesBoundingBox.Size = new System.Drawing.Size(187, 17);
+            this.chkShowSubMeshesBoundingBox.TabIndex = 3;
+            this.chkShowSubMeshesBoundingBox.Text = "Show submeshes bounding boxes";
+            this.chkShowSubMeshesBoundingBox.ThreeState = true;
+            this.chkShowSubMeshesBoundingBox.UseVisualStyleBackColor = true;
             // 
             // ObjectPropertiesForm
             // 
@@ -153,5 +179,7 @@
         private System.Windows.Forms.GroupBox groupBox2;
         private System.Windows.Forms.CheckBox chkPickable;
         private System.Windows.Forms.CheckBox chkNoOptimize;
+        private System.Windows.Forms.CheckBox chkShowSubMeshesBoundingBox;
+        private System.Windows.Forms.CheckBox chkShowBoundingBox;
     }
 }

+ 4 - 0
Exporters/3ds Max/Max2Babylon/Forms/ObjectPropertiesForm.cs

@@ -19,6 +19,8 @@ namespace Max2Babylon
             Tools.UpdateCheckBox(chkCollisions, objects, "babylonjs_checkcollisions");
             Tools.UpdateCheckBox(chkPickable, objects, "babylonjs_checkpickable");   
             Tools.UpdateCheckBox(chkNoOptimize, objects, "babylonjs_nooptimize");
+            Tools.UpdateCheckBox(chkShowBoundingBox, objects, "babylonjs_showboundingbox");
+            Tools.UpdateCheckBox(chkShowSubMeshesBoundingBox, objects, "babylonjs_showsubmeshesboundingbox");
         }
 
         private void ObjectPropertiesForm_Load(object sender, EventArgs e)
@@ -36,6 +38,8 @@ namespace Max2Babylon
             Tools.PrepareCheckBox(chkCollisions, objects, "babylonjs_checkcollisions");
             Tools.PrepareCheckBox(chkPickable, objects, "babylonjs_checkpickable");
             Tools.PrepareCheckBox(chkNoOptimize, objects, "babylonjs_nooptimize");
+            Tools.PrepareCheckBox(chkShowBoundingBox, objects, "babylonjs_showboundingbox");
+            Tools.PrepareCheckBox(chkShowSubMeshesBoundingBox, objects, "babylonjs_showsubmeshesboundingbox");
         }
     }
 }

+ 21 - 13
Exporters/3ds Max/Max2Babylon/GlobalUtility.cs

@@ -1,4 +1,5 @@
-using Autodesk.Max;
+using System;
+using Autodesk.Max;
 using Autodesk.Max.IQuadMenuContext;
 using Autodesk.Max.Plugins;
 using MaxSharp;
@@ -16,21 +17,28 @@ namespace Max2Babylon
 
         public override void Stop()
         {
-            if (actionTable != null)
+            try
             {
-                Loader.Global.COREInterface.ActionManager.DeactivateActionTable(actionCallback, idActionTable);
-            }
+                if (actionTable != null)
+                {
+                    Loader.Global.COREInterface.ActionManager.DeactivateActionTable(actionCallback, idActionTable);
+                }
 
-            // Clean up menu
-            if (menu != null)
-            {
-                Loader.Global.COREInterface.MenuManager.UnRegisterMenu(menu);
-                Loader.Global.ReleaseIMenu(menu);
-                Loader.Global.ReleaseIMenuItem(menuItemBabylon);
-                Loader.Global.ReleaseIMenuItem(menuItem);
+                // Clean up menu
+                if (menu != null)
+                {
+                    Loader.Global.COREInterface.MenuManager.UnRegisterMenu(menu);
+                    Loader.Global.ReleaseIMenu(menu);
+                    Loader.Global.ReleaseIMenuItem(menuItemBabylon);
+                    Loader.Global.ReleaseIMenuItem(menuItem);
 
-                menu = null;
-                menuItem = null;
+                    menu = null;
+                    menuItem = null;
+                }
+            }
+            catch
+            {
+                // Fails silently
             }
         }
 

+ 6 - 0
Exporters/3ds Max/Max2Babylon/Tools.cs

@@ -12,6 +12,7 @@ namespace Max2Babylon
 {
     public static class Tools
     {
+        public const float Epsilon = 0.001f;
         public static float[] ToArray(this IMatrix3 value)
         {
             var row0 = value.GetRow(0).ToArraySwitched();
@@ -56,6 +57,11 @@ namespace Max2Babylon
             return new[] { value.X, value.Y };
         }
 
+        public static float[] ToArraySwitched(this IPoint2 value)
+        {
+            return new[] { value.X, 1.0f - value.Y };
+        }
+
         public static float[] ToArraySwitched(this IPoint3 value)
         {
             return new[] { value.X, value.Z, value.Y };

+ 4 - 0
Exporters/3ds Max/Max2Babylon/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
+</packages>