2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
recast4j Copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using DotRecast.Recast.Geom;
|
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
namespace DotRecast.Recast
|
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
public static class ObjImporter
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-05-05 03:06:43 +03:00
|
|
|
public static IInputGeomProvider Load(byte[] chunck)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
var context = LoadContext(chunck);
|
2023-03-16 19:48:49 +03:00
|
|
|
return new SimpleInputGeomProvider(context.vertexPositions, context.meshFaces);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public static ObjImporterContext LoadContext(byte[] chunck)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
ObjImporterContext context = new ObjImporterContext();
|
|
|
|
try
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
using StreamReader reader = new StreamReader(new MemoryStream(chunck));
|
|
|
|
string line;
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
{
|
|
|
|
line = line.Trim();
|
2023-05-05 02:44:48 +03:00
|
|
|
ReadLine(line, context);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
throw new Exception(e.Message, e);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
return context;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public static void ReadLine(string line, ObjImporterContext context)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
if (line.StartsWith("v"))
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
ReadVertex(line, context);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
else if (line.StartsWith("f"))
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
ReadFace(line, context);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private static void ReadVertex(string line, ObjImporterContext context)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
if (line.StartsWith("v "))
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
float[] vert = ReadVector3f(line);
|
2023-03-16 19:48:49 +03:00
|
|
|
foreach (float vp in vert)
|
|
|
|
{
|
|
|
|
context.vertexPositions.Add(vp);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private static float[] ReadVector3f(string line)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
string[] v = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (v.Length < 4)
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid vector, expected 3 coordinates, found " + (v.Length - 1));
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
return new float[] { float.Parse(v[1]), float.Parse(v[2]), float.Parse(v[3]) };
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private static void ReadFace(string line, ObjImporterContext context)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
string[] v = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (v.Length < 4)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
throw new Exception("Invalid number of face vertices: 3 coordinates expected, found " + v.Length);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int j = 0; j < v.Length - 3; j++)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
context.meshFaces.Add(ReadFaceVertex(v[1], context));
|
2023-03-16 19:48:49 +03:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
context.meshFaces.Add(ReadFaceVertex(v[2 + j + i], context));
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private static int ReadFaceVertex(string face, ObjImporterContext context)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
string[] v = face.Split("/");
|
2023-05-05 02:44:48 +03:00
|
|
|
return GetIndex(int.Parse(v[0]), context.vertexPositions.Count);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-03-16 19:48:49 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
private static int GetIndex(int posi, int size)
|
2023-03-14 08:02:43 +03:00
|
|
|
{
|
2023-03-16 19:48:49 +03:00
|
|
|
if (posi > 0)
|
|
|
|
{
|
|
|
|
posi--;
|
|
|
|
}
|
|
|
|
else if (posi < 0)
|
|
|
|
{
|
|
|
|
posi = size + posi;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("0 vertex index");
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
return posi;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
2023-05-07 12:10:20 +03:00
|
|
|
}
|