Skip to content

Commit 39ed8a2

Browse files
committed
Obj import refactor and support ngons
(single material/mesh per file only at the moment)
1 parent 6585aa8 commit 39ed8a2

File tree

5 files changed

+219
-91
lines changed

5 files changed

+219
-91
lines changed

Assets/Scripts/api_clients/objectstore_client/ObjectStoreClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ internal CreateMeshWork(Dictionary<string, Material> materials, string obj,
4848

4949
public void BackgroundWork()
5050
{
51-
successfullyReadMesh = ObjImporter.ImportMeshes(objString, materials, out meshes);
51+
// TODO AB: We need to create mmeshes not MeshVerticesAndTriangles
52+
// successfullyReadMesh = ObjImporter.ImportMeshes(objString, materials, out meshes);
5253
}
5354

5455
public void PostWork()

Assets/Scripts/desktop_app/DebugConsole.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Reflection;
1920
using System.Text;
2021
using UnityEngine;
@@ -43,6 +44,7 @@ public class DebugConsole : MonoBehaviour
4344
"flag\n lists/sets feature flags\n" +
4445
"fuse\n fuses all selected meshes into a single mesh.\n" +
4546
"help\n shows this help text\n" +
47+
"import\n insert 3d model (obj, block\n" +
4648
"insert\n insert primitives\n" +
4749
"insertduration <duration>\n sets the mesh insert effect duration (e.g. 0.6).\n" +
4850
"loadfile <path>\n loads a model from the given file (use full path).\n" +
@@ -63,6 +65,7 @@ public class DebugConsole : MonoBehaviour
6365
public GameObject consoleObject;
6466
public Text consoleOutput;
6567
public InputField consoleInput;
68+
public ObjImportController objImportController;
6669

6770
private string lastCommand = "";
6871

@@ -73,6 +76,7 @@ public class DebugConsole : MonoBehaviour
7376

7477
public void Start()
7578
{
79+
objImportController = gameObject.GetComponent<ObjImportController>();
7680
consoleOutput.text = "DEBUG CONSOLE\n" +
7781
"Blocks version: " + Config.Instance.version + "\n" +
7882
"For a list of available commands, type 'help'." +
@@ -190,13 +194,22 @@ private void RunCommand(string command)
190194
case "tut":
191195
CommandTut(parts);
192196
break;
197+
case "import":
198+
CommandImport(parts);
199+
break;
193200
default:
194201
PrintLn("Unrecognized command: " + command);
195202
PrintLn("Type 'help' for a list of commands.");
196203
break;
197204
}
198205
}
199206

207+
private void CommandImport(string[] parts)
208+
{
209+
string userPath = PeltzerMain.Instance.userPath;
210+
objImportController.Import(parts.Skip(1).Select(p => Path.Combine(userPath, p)).ToArray());
211+
}
212+
200213
private void PrintLn(string message)
201214
{
202215
consoleOutput.text += message + "\n";

Assets/Scripts/desktop_app/ObjImportController.cs

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using UnityEngine;
1616
using System.IO;
17+
using System.Linq;
1718
using System.Text;
1819
// using System.Windows.Forms;
1920

@@ -25,7 +26,7 @@
2526
namespace com.google.apps.peltzer.client.desktop_app
2627
{
2728
/// <summary>
28-
/// Responsible for handling the button-click to import obj files from the desktop app, and
29+
/// Responsible for handling the button-click to import obj files from the desktop app, and
2930
/// loading them into the model.
3031
/// </summary>
3132
public class ObjImportController : MonoBehaviour
@@ -37,14 +38,15 @@ public class ObjImportController : MonoBehaviour
3738
private const float MIN_IMPORTED_OBJ_DISTANCE_FROM_USER = 2.0f;
3839

3940
/// <summary>
40-
/// Handles the button-click to import an obj. Opens up a dialog and in the background, waits for the
41+
/// Either handles the button-click to import an obj. Opens up a dialog and in the background, waits for the
4142
/// user to hit 'ok' with two files selected.
43+
/// or imports the obj files passed in as arguments.
4244
/// </summary>
43-
public void SelectObjToImport()
45+
public void Import(string[] filenames = null)
4446
{
4547
Model model = PeltzerMain.Instance.GetModel();
46-
BackgroundWork openDialog = new OpenFileDialogAndLoadObj(model);
47-
PeltzerMain.Instance.DoPolyMenuBackgroundWork(openDialog);
48+
BackgroundWork work = new LoadObj(model, filenames);
49+
PeltzerMain.Instance.DoPolyMenuBackgroundWork(work);
4850
}
4951

5052
/// <summary>
@@ -69,51 +71,91 @@ private static string FileToString(string filename)
6971
}
7072
}
7173

72-
class OpenFileDialogAndLoadObj : BackgroundWork
74+
public class LoadObj : BackgroundWork
7375
{
7476
// A reference to the model.
7577
private readonly Model model;
7678
// File contents to be passed from a background thread to a foreground thread.
7779
string mtlFileContents;
7880
string objFileContents;
81+
private string[] filenames;
7982
PeltzerFile peltzerFile;
8083

81-
public OpenFileDialogAndLoadObj(Model model)
84+
public LoadObj(Model model, string[] filenames = null)
8285
{
8386
this.model = model;
87+
this.filenames = filenames;
8488
}
8589

8690
// In the background we perform all the File I/O to get file contents. There are no graceful failures here,
8791
// and there is no feedback to the user in case of failure.
8892
public void BackgroundWork()
8993
{
90-
// OpenFileDialog dialog = new OpenFileDialog();
91-
// dialog.Multiselect = true;
92-
// Expect that the user selected two files, one .obj and one .mtl
93-
// if (dialog.ShowDialog() == DialogResult.OK) {
94-
// if (dialog.FileNames.Length == 1) {
95-
// if (dialog.FileNames[0].EndsWith(".peltzer") || dialog.FileNames[0].EndsWith(".poly")
96-
// || dialog.FileNames[0].EndsWith(".blocks")) {
97-
// byte[] peltzerFileBytes = File.ReadAllBytes(dialog.FileNames[0]);
98-
// PeltzerFileHandler.PeltzerFileFromBytes(peltzerFileBytes, out peltzerFile);
99-
// } else if (dialog.FileNames[0].EndsWith(".obj")) {
100-
// objFileContents = FileToString(dialog.FileNames[0]);
101-
// } else {
102-
// Debug.Log("When selecting only one file for OBJ import, it must have a .obj extension");
103-
// }
104-
// } else if (dialog.FileNames.Length == 2) {
105-
// string objFile = dialog.FileNames[0].EndsWith(".obj") ? dialog.FileNames[0] : dialog.FileNames[1];
106-
// string mtlFile = dialog.FileNames[0].EndsWith(".mtl") ? dialog.FileNames[0] : dialog.FileNames[1];
107-
// if (!objFile.EndsWith(".obj") || !mtlFile.EndsWith(".mtl")) {
108-
// Debug.Log("When selecting two files for OBJ import, one must be .obj and the other .mtl");
109-
// }
94+
if (filenames != null)
95+
{
96+
DoImport(filenames);
97+
}
98+
else
99+
{
100+
// OpenFileDialog dialog = new OpenFileDialog();
101+
// dialog.Multiselect = true;
102+
// // Expect that the user selected two files, one .obj and one .mtl
103+
// if (dialog.ShowDialog() == DialogResult.OK)
104+
// {
105+
// DoImport(dialog.FileNames);
106+
// }
107+
108+
}
109+
}
110+
111+
public void DoImport(string[] filenames)
112+
{
113+
string objFile = null;
114+
string mtlFile = null;
115+
116+
// Should we retire some of these file extensions?
117+
// Does anything other than blocks exist in the wild?
118+
if (filenames.Length == 1 &&
119+
(filenames[0].EndsWith(".peltzer")
120+
|| filenames[0].EndsWith(".poly")
121+
|| filenames[0].EndsWith(".blocks")))
122+
{
123+
byte[] peltzerFileBytes = File.ReadAllBytes(filenames[0]);
124+
PeltzerFileHandler.PeltzerFileFromBytes(peltzerFileBytes, out peltzerFile);
125+
return;
126+
}
127+
128+
if (filenames.Length == 1 && filenames[0].EndsWith(".obj"))
129+
{
130+
objFile = filenames[0];
131+
mtlFile = filenames[0].Replace(".obj", ".mtl");
132+
if (!File.Exists(mtlFile))
133+
{
134+
mtlFile = null;
135+
}
136+
}
137+
else if (filenames.Length == 2)
138+
{
139+
objFile = filenames.FirstOrDefault(f => f.EndsWith(".obj"));
140+
mtlFile = filenames.FirstOrDefault(f => f.EndsWith(".mtl"));
141+
}
110142

111-
// objFileContents = FileToString(objFile);
112-
// mtlFileContents = FileToString(mtlFile);
113-
// } else {
114-
// Debug.Log("Exactly one .obj file or a pair of .obj and .mtl files must be selected for OBJ import");
115-
// }
116-
// }
143+
if (filenames.Length == 2 && mtlFile == null)
144+
{
145+
Debug.Log("When selecting two files for OBJ import, one must be .obj and the other .mtl");
146+
}
147+
else if (objFile == null)
148+
{
149+
Debug.Log("Exactly one .obj file or a pair of .obj and .mtl files must be selected for OBJ import");
150+
}
151+
else
152+
{
153+
objFileContents = FileToString(objFile);
154+
if (mtlFile != null)
155+
{
156+
mtlFileContents = FileToString(mtlFile);
157+
}
158+
}
117159
}
118160

119161
// In the foreground we add the mesh to the model.

0 commit comments

Comments
 (0)