Explorar o código

Max2Babylon: More warnings

David Catuhe %!s(int64=11) %!d(string=hai) anos
pai
achega
74e323f62e

BIN=BIN
Exporters/3ds Max/Max2Babylon-0.6.0.zip


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

@@ -55,6 +55,8 @@ namespace Max2Babylon
             babylonMaterial.name = name;
             babylonMaterial.id = id;
 
+            
+
             babylonMaterial.ambient = materialNode.GetAmbient(0, false).ToArray();
             babylonMaterial.diffuse = materialNode.GetDiffuse(0, false).ToArray();
             babylonMaterial.specular = materialNode.GetSpecular(0, false).Scale(materialNode.GetShinStr(0, false));
@@ -67,6 +69,9 @@ namespace Max2Babylon
 
             if (stdMat != null)
             {
+                babylonMaterial.backFaceCulling = !stdMat.TwoSided;
+                babylonMaterial.wireframe = stdMat.Wire;
+
                 // Textures
                 babylonMaterial.ambientTexture = ExportTexture(stdMat, 0, babylonScene);    // Ambient
                 babylonMaterial.diffuseTexture = ExportTexture(stdMat, 1, babylonScene);    // Diffuse

+ 47 - 2
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Texture.cs

@@ -8,6 +8,51 @@ namespace Max2Babylon
 {
     partial class BabylonExporter
     {
+        bool IsTextureCube(string filepath)
+        {
+            try
+            {
+                if (Path.GetExtension(filepath).ToLower() != ".dds")
+                {
+                    return false;
+                }
+
+                var data = File.ReadAllBytes(filepath);
+                var intArray = new int[data.Length / 4];
+
+                Buffer.BlockCopy(data, 0, intArray, 0, data.Length);
+
+
+                int width = intArray[4];
+                int height = intArray[3];
+                int mipmapsCount = intArray[7];
+
+                if ((width >> (mipmapsCount -1)) > 1)
+                {
+                    var expected = 1;
+                    var currentWidth = width;
+
+                    while (currentWidth > 1)
+                    {
+                        currentWidth = currentWidth >> 1;
+                        expected++;
+                    }
+
+                    RaiseWarning(string.Format("Mipmaps chain is not complete: {0} maps instead of {1} (based on texture width: {2})", mipmapsCount, expected, width), 2);
+                    RaiseWarning(string.Format("You must generate a complete mipmaps chain for .dds)"), 2);
+                    RaiseWarning(string.Format("Mipmaps will be disabled for this texture. If you want automatic texture generation you cannot use a .dds)"), 2);
+                }
+
+                bool isCube = (intArray[28] & 0x200) == 0x200;
+
+                return isCube;
+            }
+            catch
+            {
+                return false;
+            }
+        }
+
         private BabylonTexture ExportTexture(IStdMat2 stdMat, int index, BabylonScene babylonScene, Boolean allowCube = false)
         {
             if (!stdMat.MapEnabled(index))
@@ -107,7 +152,7 @@ namespace Max2Babylon
                     {
                         File.Copy(texture.MapName, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
                     }
-                    babylonTexture.isCube = Tools.IsTextureCube(texture.MapName);
+                    babylonTexture.isCube = IsTextureCube(texture.MapName);
                 }
                 else
                 {
@@ -118,7 +163,7 @@ namespace Max2Babylon
                         {
                             File.Copy(texturepath, Path.Combine(babylonScene.OutputPath, babylonTexture.name), true);
                         }
-                        babylonTexture.isCube = Tools.IsTextureCube(texturepath);
+                        babylonTexture.isCube = IsTextureCube(texturepath);
                     }
                     else
                     {

+ 32 - 18
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.cs

@@ -1,9 +1,11 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Text;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 using Autodesk.Max;
 using BabylonExport.Entities;
@@ -74,10 +76,10 @@ namespace Max2Babylon
             }
         }
 
-        public void Export(string outputFile, bool generateManifest, Form callerForm)
+        public async Task ExportAsync(string outputFile, bool generateManifest, Form callerForm)
         {
             IsCancelled = false;
-            RaiseMessage("Exportation started");
+            RaiseMessage("Exportation started", Color.Blue);
             ReportProgressChanged(0);
             var babylonScene = new BabylonScene(Path.GetDirectoryName(outputFile));
             var maxScene = Loader.Core.RootNode;
@@ -90,6 +92,9 @@ namespace Max2Babylon
                 return;
             }
 
+            var watch = new Stopwatch();
+            watch.Start();
+
             // Save scene
             RaiseMessage("Saving 3ds max file");
             var forceSave = Loader.Core.FileSave;
@@ -105,7 +110,7 @@ namespace Max2Babylon
             babylonScene.ambientColor = Loader.Core.GetAmbient(0, Tools.Forever).ToArray();
 
             babylonScene.gravity = maxScene.GetVector3Property("babylonjs_gravity");
-            exportQuaternionsInsteadOfEulers = maxScene.GetBoolProperty("babylonjs_exportquaternions");
+            exportQuaternionsInsteadOfEulers = maxScene.GetBoolProperty("babylonjs_exportquaternions", 1);
 
             // Cameras
             BabylonCamera mainCamera = null;
@@ -205,33 +210,42 @@ namespace Max2Babylon
             }
 
             // Skeletons
-            RaiseMessage("Exporting skeletons");
-            foreach (var skin in skins)
+            if (skins.Count > 0)
             {
-                ExportSkin(skin, babylonScene);
-                skin.Dispose();
+                RaiseMessage("Exporting skeletons");
+                foreach (var skin in skins)
+                {
+                    ExportSkin(skin, babylonScene);
+                    skin.Dispose();
+                }
             }
 
             // Output
+            RaiseMessage("Saving to output file");
             babylonScene.Prepare(false);
             var jsonSerializer = JsonSerializer.Create();
             var sb = new StringBuilder();
             var sw = new StringWriter(sb, CultureInfo.InvariantCulture);
-            using (var jsonWriter = new JsonTextWriterOptimized(sw))
-            {
-                jsonWriter.Formatting = Formatting.None;
-                jsonSerializer.Serialize(jsonWriter, babylonScene);
-            }
-            File.WriteAllText(outputFile, sb.ToString());
 
-            if (generateManifest)
+            await Task.Run(() =>
             {
-                File.WriteAllText(outputFile + ".manifest", "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
-            }
+                using (var jsonWriter = new JsonTextWriterOptimized(sw))
+                {
+                    jsonWriter.Formatting = Formatting.None;
+                    jsonSerializer.Serialize(jsonWriter, babylonScene);
+                }
+                File.WriteAllText(outputFile, sb.ToString());
 
-            ReportProgressChanged(100);
+                if (generateManifest)
+                {
+                    File.WriteAllText(outputFile + ".manifest",
+                        "{\r\n\"version\" : 1,\r\n\"enableSceneOffline\" : true,\r\n\"enableTexturesOffline\" : true\r\n}");
+                }
+            });
 
-            RaiseMessage("Exportation done");
+            ReportProgressChanged(100);
+            watch.Stop();
+            RaiseMessage(string.Format("Exportation done in {0:0.00}s", watch.ElapsedMilliseconds / 1000.0), Color.Blue);
         }
     }
 }

+ 2 - 19
Exporters/3ds Max/Max2Babylon/Forms/ExporterForm.Designer.cs

@@ -39,7 +39,6 @@
             this.butCancel = new System.Windows.Forms.Button();
             this.pictureBox2 = new System.Windows.Forms.PictureBox();
             this.chkManifest = new System.Windows.Forms.CheckBox();
-            this.chkQuaternions = new System.Windows.Forms.CheckBox();
             this.label2 = new System.Windows.Forms.Label();
             this.chkCopyTextures = new System.Windows.Forms.CheckBox();
             this.groupBox1 = new System.Windows.Forms.GroupBox();
@@ -152,17 +151,6 @@
             this.chkManifest.Text = "Generate .manifest";
             this.chkManifest.UseVisualStyleBackColor = true;
             // 
-            // chkQuaternions
-            // 
-            this.chkQuaternions.AutoSize = true;
-            this.chkQuaternions.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
-            this.chkQuaternions.Location = new System.Drawing.Point(18, 81);
-            this.chkQuaternions.Name = "chkQuaternions";
-            this.chkQuaternions.Size = new System.Drawing.Size(221, 17);
-            this.chkQuaternions.TabIndex = 1;
-            this.chkQuaternions.Text = "Export quaternions instead of Euler angles";
-            this.chkQuaternions.UseVisualStyleBackColor = true;
-            // 
             // label2
             // 
             this.label2.AutoSize = true;
@@ -174,10 +162,9 @@
             // 
             // chkCopyTextures
             // 
-            this.chkCopyTextures.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
             this.chkCopyTextures.AutoSize = true;
             this.chkCopyTextures.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
-            this.chkCopyTextures.Location = new System.Drawing.Point(338, 81);
+            this.chkCopyTextures.Location = new System.Drawing.Point(18, 81);
             this.chkCopyTextures.Name = "chkCopyTextures";
             this.chkCopyTextures.Size = new System.Drawing.Size(132, 17);
             this.chkCopyTextures.TabIndex = 12;
@@ -195,7 +182,6 @@
             this.groupBox1.Controls.Add(this.chkManifest);
             this.groupBox1.Controls.Add(this.butBrowse);
             this.groupBox1.Controls.Add(this.label2);
-            this.groupBox1.Controls.Add(this.chkQuaternions);
             this.groupBox1.Location = new System.Drawing.Point(12, 6);
             this.groupBox1.Name = "groupBox1";
             this.groupBox1.Size = new System.Drawing.Size(493, 136);
@@ -204,15 +190,13 @@
             // 
             // chkHidden
             // 
-            this.chkHidden.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
             this.chkHidden.AutoSize = true;
             this.chkHidden.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
-            this.chkHidden.Location = new System.Drawing.Point(338, 104);
+            this.chkHidden.Location = new System.Drawing.Point(196, 81);
             this.chkHidden.Name = "chkHidden";
             this.chkHidden.Size = new System.Drawing.Size(125, 17);
             this.chkHidden.TabIndex = 13;
             this.chkHidden.Text = "Export hidden objects";
-            this.chkHidden.ThreeState = true;
             this.chkHidden.UseVisualStyleBackColor = true;
             // 
             // ExporterForm
@@ -252,7 +236,6 @@
         private System.Windows.Forms.TreeView treeView;
         private System.Windows.Forms.Button butCancel;
         private System.Windows.Forms.PictureBox pictureBox2;
-        private System.Windows.Forms.CheckBox chkQuaternions;
         private System.Windows.Forms.CheckBox chkManifest;
         private System.Windows.Forms.Label label2;
         private System.Windows.Forms.CheckBox chkCopyTextures;

+ 25 - 21
Exporters/3ds Max/Max2Babylon/Forms/ExporterForm.cs

@@ -23,7 +23,6 @@ namespace Max2Babylon
         private void ExporterForm_Load(object sender, EventArgs e)
         {
             txtFilename.Text = Loader.Core.RootNode.GetLocalData();
-            Tools.PrepareCheckBox(chkQuaternions, Loader.Core.RootNode, "babylonjs_exportquaternions");
             Tools.PrepareCheckBox(chkManifest, Loader.Core.RootNode, "babylonjs_generatemanifest");
             Tools.PrepareCheckBox(chkCopyTextures, Loader.Core.RootNode, "babylonjs_copytextures", 1);
             Tools.PrepareCheckBox(chkHidden, Loader.Core.RootNode, "babylonjs_exporthidden");
@@ -37,9 +36,8 @@ namespace Max2Babylon
             }
         }
 
-        private void butExport_Click(object sender, EventArgs e)
+        private async void butExport_Click(object sender, EventArgs e)
         {
-            Tools.UpdateCheckBox(chkQuaternions, Loader.Core.RootNode, "babylonjs_exportquaternions");
             Tools.UpdateCheckBox(chkManifest, Loader.Core.RootNode, "babylonjs_generatemanifest");
             Tools.UpdateCheckBox(chkCopyTextures, Loader.Core.RootNode, "babylonjs_copytextures");
             Tools.UpdateCheckBox(chkHidden, Loader.Core.RootNode, "babylonjs_exporthidden");
@@ -105,7 +103,7 @@ namespace Max2Babylon
             {
                 exporter.ExportHiddenObjects = chkHidden.Checked;
                 exporter.CopyTexturesToOutput = chkCopyTextures.Checked;
-                exporter.Export(txtFilename.Text, chkManifest.Checked, this);
+                await exporter.ExportAsync(txtFilename.Text, chkManifest.Checked, this);
             }
             catch (OperationCanceledException)
             {
@@ -127,27 +125,33 @@ namespace Max2Babylon
 
         private TreeNode CreateTreeNode(int rank, string text, Color color)
         {
-            var newNode = new TreeNode(text) { ForeColor = color };
-            if (rank == 0)
-            {
-                treeView.Nodes.Add(newNode);
-            }
-            else if (rank == currentRank + 1)
-            {
-                currentNode.Nodes.Add(newNode);
-            }
-            else
+            TreeNode newNode = null;
+
+            Invoke(new Action(() =>
             {
-                var parentNode = currentNode;
-                while (currentRank != rank - 1)
+                newNode = new TreeNode(text) {ForeColor = color};
+                if (rank == 0)
                 {
-                    parentNode = parentNode.Parent;
-                    currentRank--;
+                    treeView.Nodes.Add(newNode);
                 }
-                parentNode.Nodes.Add(newNode);
-            }
+                else if (rank == currentRank + 1)
+                {
+                    currentNode.Nodes.Add(newNode);
+                }
+                else
+                {
+                    var parentNode = currentNode;
+                    while (currentRank != rank - 1)
+                    {
+                        parentNode = parentNode.Parent;
+                        currentRank--;
+                    }
+                    parentNode.Nodes.Add(newNode);
+                }
+
+                currentRank = rank;
+            }));
 
-            currentRank = rank;
             return newNode;
         }
 

+ 30 - 0
Exporters/3ds Max/Max2Babylon/Forms/ScenePropertiesForm.Designer.cs

@@ -33,7 +33,10 @@
             this.label3 = new System.Windows.Forms.Label();
             this.butCancel = new System.Windows.Forms.Button();
             this.butOK = new System.Windows.Forms.Button();
+            this.groupBox2 = new System.Windows.Forms.GroupBox();
+            this.chkQuaternions = new System.Windows.Forms.CheckBox();
             this.groupBox1.SuspendLayout();
+            this.groupBox2.SuspendLayout();
             this.SuspendLayout();
             // 
             // groupBox1
@@ -92,6 +95,28 @@
             this.butOK.UseVisualStyleBackColor = true;
             this.butOK.Click += new System.EventHandler(this.butOK_Click);
             // 
+            // groupBox2
+            // 
+            this.groupBox2.Controls.Add(this.chkQuaternions);
+            this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
+            this.groupBox2.Location = new System.Drawing.Point(12, 105);
+            this.groupBox2.Name = "groupBox2";
+            this.groupBox2.Size = new System.Drawing.Size(319, 87);
+            this.groupBox2.TabIndex = 5;
+            this.groupBox2.TabStop = false;
+            this.groupBox2.Text = "Advanced";
+            // 
+            // chkQuaternions
+            // 
+            this.chkQuaternions.AutoSize = true;
+            this.chkQuaternions.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
+            this.chkQuaternions.Location = new System.Drawing.Point(21, 28);
+            this.chkQuaternions.Name = "chkQuaternions";
+            this.chkQuaternions.Size = new System.Drawing.Size(221, 17);
+            this.chkQuaternions.TabIndex = 2;
+            this.chkQuaternions.Text = "Export quaternions instead of Euler angles";
+            this.chkQuaternions.UseVisualStyleBackColor = true;
+            // 
             // ScenePropertiesForm
             // 
             this.AcceptButton = this.butOK;
@@ -99,6 +124,7 @@
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.CancelButton = this.butCancel;
             this.ClientSize = new System.Drawing.Size(343, 390);
+            this.Controls.Add(this.groupBox2);
             this.Controls.Add(this.butCancel);
             this.Controls.Add(this.butOK);
             this.Controls.Add(this.groupBox1);
@@ -109,6 +135,8 @@
             this.Load += new System.EventHandler(this.ScenePropertiesForm_Load);
             this.groupBox1.ResumeLayout(false);
             this.groupBox1.PerformLayout();
+            this.groupBox2.ResumeLayout(false);
+            this.groupBox2.PerformLayout();
             this.ResumeLayout(false);
 
         }
@@ -120,5 +148,7 @@
         private System.Windows.Forms.Label label3;
         private System.Windows.Forms.Button butCancel;
         private System.Windows.Forms.Button butOK;
+        private System.Windows.Forms.GroupBox groupBox2;
+        private System.Windows.Forms.CheckBox chkQuaternions;
     }
 }

+ 2 - 0
Exporters/3ds Max/Max2Babylon/Forms/ScenePropertiesForm.cs

@@ -13,11 +13,13 @@ namespace Max2Babylon
         private void butOK_Click(object sender, EventArgs e)
         {
             Tools.UpdateVector3Control(gravityControl, Loader.Core.RootNode, "babylonjs_gravity");
+            Tools.UpdateCheckBox(chkQuaternions, Loader.Core.RootNode, "babylonjs_exportquaternions");
         }
 
         private void ScenePropertiesForm_Load(object sender, EventArgs e)
         {
             Tools.PrepareVector3Control(gravityControl, Loader.Core.RootNode, "babylonjs_gravity", 0, -0.9f, 0);
+            Tools.PrepareCheckBox(chkQuaternions, Loader.Core.RootNode, "babylonjs_exportquaternions", 1);
         }
     }
 }

+ 60 - 21
Exporters/3ds Max/Max2Babylon/Tools/Tools.cs

@@ -24,27 +24,6 @@ namespace Max2Babylon
 
         public static IMatrix3 Identity { get { return Loader.Global.Matrix3.Create(XAxis, YAxis, ZAxis, Origin); } }
 
-        public static bool IsTextureCube(string filepath)
-        {
-            try
-            {
-                if (Path.GetExtension(filepath).ToLower() != ".dds")
-                {
-                    return false;
-                }
-
-                var data = File.ReadAllBytes(filepath);
-                var intArray = new int[data.Length / 4];
-
-                Buffer.BlockCopy(data, 0, intArray, 0, data.Length);
-
-                return (intArray[28] & 0x200) == 0x200;
-            }
-            catch
-            {
-                return false;
-            }
-        }
 
         public static Vector3 ToEulerAngles(this IQuat q)
         {
@@ -330,6 +309,25 @@ namespace Max2Babylon
             return tm.Multiply(ptm);
         }
 
+        public static IMatrix3 GetWorldMatrixComplete(this IINode node, int t, bool parent)
+        {
+            var tm = node.GetObjTMAfterWSM(t, Forever);
+            var ptm = node.ParentNode.GetObjTMAfterWSM(t, Forever);
+
+            if (!parent)
+                return tm;
+
+            if (node.ParentNode.SuperClassID == SClass_ID.Camera)
+            {
+                var r = ptm.GetRow(3);
+                ptm.IdentityMatrix();
+                ptm.SetRow(3, r);
+            }
+
+            ptm.Invert();
+            return tm.Multiply(ptm);
+        }
+
         public static ITriObject GetMesh(this IObject obj)
         {
             var triObjectClassId = Loader.Global.Class_ID.Create(0x0009, 0);
@@ -403,7 +401,11 @@ namespace Max2Babylon
         public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
         {
             int state = defaultState;
+#if MAX2015
+            node.GetUserPropBool(propertyName, ref state);
+#else
             node.GetUserPropBool(ref propertyName, ref state);
+#endif
 
             return state == 1;
         }
@@ -411,7 +413,11 @@ namespace Max2Babylon
         public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
         {
             float state = defaultState;
+#if MAX2015
+            node.GetUserPropFloat(propertyName, ref state);
+#else
             node.GetUserPropFloat(ref propertyName, ref state);
+#endif
 
             return state;
         }
@@ -420,15 +426,28 @@ namespace Max2Babylon
         {
             float state0 = 0;
             string name = propertyName + "_x";
+#if MAX2015
+            node.GetUserPropFloat(name, ref state0);
+#else
             node.GetUserPropFloat(ref name, ref state0);
+#endif
+
 
             float state1 = 0;
             name = propertyName + "_y";
+#if MAX2015
+            node.GetUserPropFloat(name, ref state1);
+#else
             node.GetUserPropFloat(ref name, ref state1);
+#endif
 
             float state2 = 0;
             name = propertyName + "_z";
+#if MAX2015
+            node.GetUserPropFloat(name, ref state2);
+#else
             node.GetUserPropFloat(ref name, ref state2);
+#endif
 
             return new[] { state0, state1, state2 };
         }
@@ -470,7 +489,11 @@ namespace Max2Babylon
         {
             if (checkBox.CheckState != CheckState.Indeterminate)
             {
+#if MAX2015
+                node.SetUserPropBool(propertyName, checkBox.CheckState == CheckState.Checked);
+#else
                 node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
+#endif
             }
         }
 
@@ -491,7 +514,11 @@ namespace Max2Babylon
         {
             foreach (var node in nodes)
             {
+#if MAX2015
+                node.SetUserPropFloat(propertyName, (float)nup.Value);
+#else
                 node.SetUserPropFloat(ref propertyName, (float)nup.Value);
+#endif
             }
         }
 
@@ -505,13 +532,25 @@ namespace Max2Babylon
         public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
         {
             string name = propertyName + "_x";
+#if MAX2015
+            node.SetUserPropFloat(name, vector3Control.X);
+#else
             node.SetUserPropFloat(ref name, vector3Control.X);
+#endif
 
             name = propertyName + "_y";
+#if MAX2015
+            node.SetUserPropFloat(name, vector3Control.Y);
+#else
             node.SetUserPropFloat(ref name, vector3Control.Y);
+#endif
 
             name = propertyName + "_z";
+#if MAX2015
+            node.SetUserPropFloat(name, vector3Control.Z);
+#else
             node.SetUserPropFloat(ref name, vector3Control.Z);
+#endif
         }
 
         public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)