Browse Source

Max2Babylon:
Fixing specular exportation
Adding cancel button
Working on animations

David Catuhe 11 years ago
parent
commit
aa5bf5b7f4

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


+ 1 - 0
Exporters/3ds Max/Max2Babylon/BabylonExportActionItem.cs

@@ -15,6 +15,7 @@ namespace Max2Babylon
             if (form == null)
                 form = new ExporterForm(this);
             form.Show();
+            form.BringToFront();
 
             return true;
         }

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

@@ -58,8 +58,8 @@ namespace Max2Babylon
 
             babylonMaterial.ambient = materialNode.GetAmbient(0).ToArray();
             babylonMaterial.diffuse = materialNode.GetDiffuse(0).ToArray();
-            babylonMaterial.specular = materialNode.GetSpecular(0).Scale(materialNode.GetShininess(0));
-            babylonMaterial.specularPower = materialNode.GetShinyStrength(0);
+            babylonMaterial.specular = materialNode.GetSpecular(0).Scale(materialNode.GetShinyStrength(0));
+            babylonMaterial.specularPower = materialNode.GetShininess(0) * 256;
 
             babylonMaterial.emissive = materialNode.SelfIlluminationColorOn ? materialNode.GetSelfIllumColor(0).ToArray() : materialNode.GetDiffuse(0).Scale(materialNode.GetSelfIllumination(0));
             babylonMaterial.alpha = 1.0f - materialNode.GetTransparency(0);

+ 4 - 1
Exporters/3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.InteropServices;
+using System.Threading;
 using Autodesk.Max;
 using BabylonExport.Entities;
 using MaxSharp;
@@ -10,7 +11,7 @@ namespace Max2Babylon
 {
     partial class BabylonExporter
     {
-        private BabylonMesh ExportMesh(Node meshNode, BabylonScene babylonScene)
+        private BabylonMesh ExportMesh(Node meshNode, BabylonScene babylonScene, CancellationToken token)
         {
             var babylonMesh = new BabylonMesh();
             int vx1, vx2, vx3;
@@ -139,6 +140,7 @@ namespace Max2Babylon
                     indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx2, vertices, hasUV, hasUV2, noOptimize));
                     indices.Add(CreateGlobalVertex(mesh, computedMesh, face, vx3, vertices, hasUV, hasUV2, noOptimize));
                     matIDs.Add(mesh.Faces[face].MatID % multiMatsCount);
+                    if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
                 }
 
                 if (vertices.Count >= 65536)
@@ -227,6 +229,7 @@ namespace Max2Babylon
 
                         subMeshes.Add(subMesh);
                     }
+                    if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
                 }
                 babylonMesh.subMeshes = subMeshes.ToArray();
 

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

@@ -5,6 +5,7 @@ using System.IO;
 using System.Runtime.InteropServices;
 using System.Runtime.Serialization.Json;
 using System.Text;
+using System.Threading;
 using Autodesk.Max;
 using BabylonExport.Entities;
 using MaxSharp;
@@ -54,7 +55,7 @@ namespace Max2Babylon
             }
         }
 
-        public void Export(string outputFile)
+        public void Export(string outputFile, CancellationToken token)
         {
             RaiseMessage("Exportation started");
             ReportProgressChanged(25);
@@ -124,7 +125,8 @@ namespace Max2Babylon
             RaiseMessage("Exporting meshes");
             foreach (var meshNode in maxScene.NodesListBySuperClass(SuperClassID.GeometricObject))
             {
-                ExportMesh(meshNode, babylonScene);
+                ExportMesh(meshNode, babylonScene, token);
+                if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
             }
 
             // Materials
@@ -133,6 +135,7 @@ namespace Max2Babylon
             foreach (var mat in matsToExport)
             {
                 ExportMaterial(mat, babylonScene);
+                if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
             }
 
             // Lights
@@ -140,6 +143,7 @@ namespace Max2Babylon
             foreach (var lightNode in maxScene.NodesListBySuperClass(SuperClassID.Light))
             {
                 ExportLight(lightNode, babylonScene);
+                if (token.IsCancellationRequested) token.ThrowIfCancellationRequested();
             }
 
             if (babylonScene.LightsList.Count == 0)

+ 17 - 1
Exporters/3ds Max/Max2Babylon/Forms/ExporterForm.Designer.cs

@@ -35,6 +35,7 @@
             this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
             this.progressBar = new System.Windows.Forms.ProgressBar();
             this.treeView = new System.Windows.Forms.TreeView();
+            this.butCancel = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // butExport
@@ -93,7 +94,7 @@
             | System.Windows.Forms.AnchorStyles.Right)));
             this.progressBar.Location = new System.Drawing.Point(12, 486);
             this.progressBar.Name = "progressBar";
-            this.progressBar.Size = new System.Drawing.Size(699, 23);
+            this.progressBar.Size = new System.Drawing.Size(613, 23);
             this.progressBar.TabIndex = 5;
             // 
             // treeView
@@ -106,11 +107,25 @@
             this.treeView.Size = new System.Drawing.Size(699, 401);
             this.treeView.TabIndex = 6;
             // 
+            // butCancel
+            // 
+            this.butCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
+            this.butCancel.Enabled = false;
+            this.butCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
+            this.butCancel.Location = new System.Drawing.Point(631, 486);
+            this.butCancel.Name = "butCancel";
+            this.butCancel.Size = new System.Drawing.Size(80, 23);
+            this.butCancel.TabIndex = 7;
+            this.butCancel.Text = "Cancel";
+            this.butCancel.UseVisualStyleBackColor = true;
+            this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
+            // 
             // ExporterForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(723, 525);
+            this.Controls.Add(this.butCancel);
             this.Controls.Add(this.treeView);
             this.Controls.Add(this.progressBar);
             this.Controls.Add(this.butExport);
@@ -139,5 +154,6 @@
         private System.Windows.Forms.SaveFileDialog saveFileDialog;
         private System.Windows.Forms.ProgressBar progressBar;
         private System.Windows.Forms.TreeView treeView;
+        private System.Windows.Forms.Button butCancel;
     }
 }

+ 29 - 1
Exporters/3ds Max/Max2Babylon/Forms/ExporterForm.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using MaxCustomControls;
@@ -10,6 +11,7 @@ namespace Max2Babylon
     public partial class ExporterForm : MaxForm
     {
         private readonly BabylonExportActionItem babylonExportAction;
+        private CancellationTokenSource cancellationToken;
 
         public ExporterForm(BabylonExportActionItem babylonExportAction)
         {
@@ -83,7 +85,28 @@ namespace Max2Babylon
                 }
             }));
 
-            await Task.Run(() => exporter.Export(txtFilename.Text));
+            butExport.Enabled = false;
+            butCancel.Enabled = true;
+
+            cancellationToken = new CancellationTokenSource();
+            var token = cancellationToken.Token;
+
+            try
+            {
+                await Task.Run(() => exporter.Export(txtFilename.Text, token), token);
+            }
+            catch
+            {
+                previousNode = new TreeNode("Exportation cancelled") { ForeColor = Color.Red };
+
+                currentNode = CreateTreeNode(false, currentNode, previousNode);
+
+                previousNode.EnsureVisible();
+                progressBar.Value = 0;
+            }
+
+            butCancel.Enabled = false;
+            butExport.Enabled = true;
         }
 
         private TreeNode CreateTreeNode(bool asChild, TreeNode currentNode, TreeNode treeNode)
@@ -113,5 +136,10 @@ namespace Max2Babylon
         {
             butExport.Enabled = !string.IsNullOrEmpty(txtFilename.Text.Trim());
         }
+
+        private void butCancel_Click(object sender, EventArgs e)
+        {
+            cancellationToken.Cancel();
+        }
     }
 }