Added ExportMesh support -- (#31)

utilizing a modified ManagedFbx
This commit is contained in:
ordigdug 2017-03-06 11:20:21 -05:00 committed by Perfare
parent 45b9b781b1
commit cfbcdecfe6
5 changed files with 916 additions and 818 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@ using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using System.Web.Script.Serialization;
using ManagedFbx;
namespace Unity_Studio
@ -280,7 +281,6 @@ namespace Unity_Studio
assetsFile.TransformList.Add(asset.m_PathID, m_Rect.m_Transform);
break;
}
//case 21: //Material
case 28: //Texture2D
{
Texture2D m_Texture2D = new Texture2D(asset, false);
@ -324,8 +324,13 @@ namespace Unity_Studio
productName = plSet.productName;
break;
}
case 21: //Material
case 43: //Mesh
{
Mesh m_Mesh = new Mesh(asset, false);
exportable = true;
break;
}
case 21: //Material
case 74: //AnimationClip
case 90: //Avatar
case 91: //AnimatorController
@ -920,7 +925,7 @@ namespace Unity_Studio
//StatusStripUpdate("Writing Geometry");
foreach (var MeshPD in Meshes)
{
Mesh m_Mesh = new Mesh(MeshPD);
Mesh m_Mesh = new Mesh(MeshPD, true);
MeshFBX(m_Mesh, MeshPD.uniqueID, ob);
//write data 8MB at a time
@ -951,7 +956,7 @@ namespace Unity_Studio
//find a way to test if a mesh instance was loaded previously and if it uses the same skeleton, then create instance or copy
var keepID = MeshPD.uniqueID;
MeshPD.uniqueID = SkinnedMeshPD.uniqueID;
Mesh m_Mesh = new Mesh(MeshPD);
Mesh m_Mesh = new Mesh(MeshPD, true);
MeshFBX(m_Mesh, MeshPD.uniqueID, ob);
//write data 8MB at a time
@ -1734,6 +1739,111 @@ namespace Unity_Studio
}
}
public static void ExportMesh(Mesh m_Mesh, string exportPath)
{
Scene m_scene = Scene.CreateScene("Scene");
SceneNode root = m_scene.RootNode;
SceneNode meshnode = Scene.CreateNode(m_scene, m_Mesh.m_Name);
SceneNode.AddChild(root, meshnode);
ManagedFbx.Mesh mesh = Scene.CreateMesh(m_scene, meshnode, "Mesh");
if (m_Mesh.m_VertexCount > 0)
{
#region Vertices
int count = 3;
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4)
{
count = 4;
}
var vertices = new ManagedFbx.Vector3[m_Mesh.m_VertexCount];
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
vertices[v] = new ManagedFbx.Vector3(
m_Mesh.m_Vertices[v * count],
m_Mesh.m_Vertices[v * count + 1],
m_Mesh.m_Vertices[v * count + 2]);
}
mesh.Vertices = vertices;
#endregion
#region Indicies
List<int> indices = new List<int>();
for (int i = 0; i < m_Mesh.m_Indices.Count; i = i + 3)
{
indices.Add((int)m_Mesh.m_Indices[i]);
indices.Add((int)m_Mesh.m_Indices[i + 1]);
indices.Add((int)m_Mesh.m_Indices[i + 2]);
}
mesh.AddPolygons(indices, 0);
#endregion
#region Normals
if (m_Mesh.m_Normals != null && m_Mesh.m_Normals.Length > 0)
{
if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3)
{
count = 3;
}
else if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 4)
{
count = 4;
}
var normals = new ManagedFbx.Vector3[m_Mesh.m_VertexCount];
for (int n = 0; n < m_Mesh.m_VertexCount; n++)
{
normals[n] = new ManagedFbx.Vector3(
m_Mesh.m_Normals[n * count],
m_Mesh.m_Normals[n * count + 1],
m_Mesh.m_Normals[n * count + 2]);
}
mesh.Normals = normals;
}
#endregion
#region Colors
if (m_Mesh.m_Colors == null)
{
var colors = new ManagedFbx.Colour[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colors[c] = new ManagedFbx.Colour(
0.5f, 0.5f, 0.5f, 1.0f);
}
mesh.VertexColours = colors;
}
else if (m_Mesh.m_Colors.Length == m_Mesh.m_VertexCount * 3)
{
var colors = new ManagedFbx.Colour[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colors[c] = new ManagedFbx.Colour(
m_Mesh.m_Colors[c * 4],
m_Mesh.m_Colors[c * 4 + 1],
m_Mesh.m_Colors[c * 4 + 2],
1.0f);
}
mesh.VertexColours = colors;
}
else
{
var colors = new ManagedFbx.Colour[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colors[c] = new ManagedFbx.Colour(
m_Mesh.m_Colors[c * 4],
m_Mesh.m_Colors[c * 4 + 1],
m_Mesh.m_Colors[c * 4 + 2],
m_Mesh.m_Colors[c * 4 + 3]);
}
mesh.VertexColours = colors;
}
#endregion
}
SceneNode.AddMesh(meshnode, mesh);
m_scene.Save(exportPath); //default is .fbx
//m_scene.Save(exportPath + ".fbx");
m_scene.Save(exportPath + ".obj");
m_scene.Save(exportPath + ".dae");
}
public static bool ExportFileExists(string filename)
{
if (File.Exists(filename))

View File

@ -73,6 +73,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="ManagedFbx">
<HintPath>library\ManagedFbx.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>library\OpenTK.dll</HintPath>

View File

@ -13,9 +13,9 @@ using System.Diagnostics;
using System.Drawing.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using ManagedFbx;
using static Unity_Studio.UnityStudio;
namespace Unity_Studio
{
partial class UnityStudioForm : Form
@ -50,65 +50,11 @@ namespace Unity_Studio
int vboColors;
int vboViewMatrix;
int eboElements;
Vector3[] vertexData =
{
new Vector3(-0.5f, -0.5f, -0.5f),
new Vector3(0.5f, -0.5f, -0.5f),
new Vector3(0.5f, 0.5f, -0.5f),
new Vector3(-0.5f, 0.5f, -0.5f),
new Vector3(-0.5f, -0.5f, 0.5f),
new Vector3(0.5f, -0.5f, 0.5f),
new Vector3(0.5f, 0.5f, 0.5f),
new Vector3(-0.5f, 0.5f, 0.5f)
};
Vector3[] normalData =
{
//left
new Vector3(-1.0f, 0.0f, 0.0f)
//back
//new Vector3(0.0f, 0.0f, -1.0f),
//right
//new Vector3(1.0f, 0.0f, 0.0f),
//top
//new Vector3(0.0f, 1.0f, 0.0f),
//front
//new Vector3(0.0f, 0.0f, 1.0f),
//bottom
//new Vector3(0.0f, -1.0f, 0.0f)
};
Vector4[] colorData =
{
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f)
};
Matrix4[] viewMatrixData = { Matrix4.Identity };
int[] indiceData =
{
//left
0,2,1,
0,3,2,
//back
1,2,6,
6,5,1,
//right
4,5,6,
6,7,4,
//top
2,3,6,
6,3,7,
//front
0,7,3,
0,4,7,
//bottom
0,1,5,
0,5,4
};
OpenTK.Vector3[] vertexData;
OpenTK.Vector3[] normalData;
OpenTK.Vector4[] colorData;
Matrix4[] viewMatrixData;
int[] indiceData;
bool wireFrameView;
#endregion
@ -1021,24 +967,25 @@ namespace Unity_Studio
Matrix4.Identity
* Matrix4.CreateTranslation( 0.0f, -1.0f, 0.0f )
* Matrix4.CreateRotationY(-90.0f)};
var m_Mesh = new Mesh(asset);
var m_Mesh = new Mesh(asset, true);
if (m_Mesh.m_VertexCount > 0)
{
int count = 3;//vertex components
//skip last component in vector4
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4) { count = 4; }
vertexData = new Vector3[m_Mesh.m_VertexCount];
#region Vertices
int count = 3;
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4)
{
count = 4;
}
vertexData = new OpenTK.Vector3[m_Mesh.m_VertexCount];
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
vertexData[v] = new Vector3(
vertexData[v] = new OpenTK.Vector3(
m_Mesh.m_Vertices[v * count],
m_Mesh.m_Vertices[v * count + 1],
m_Mesh.m_Vertices[v * count + 2]);
}
#endregion
#region Indicies
indiceData = new int[m_Mesh.m_Indices.Count];
for (int i = 0; i < m_Mesh.m_Indices.Count; i = i + 3)
{
@ -1046,6 +993,8 @@ namespace Unity_Studio
indiceData[i + 1] = (int)m_Mesh.m_Indices[i + 1];
indiceData[i + 2] = (int)m_Mesh.m_Indices[i + 2];
}
#endregion
#region Normals
if (m_Mesh.m_Normals != null && m_Mesh.m_Normals.Length > 0)
{
if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3)
@ -1057,40 +1006,53 @@ namespace Unity_Studio
count = 4;
}
normalData = new Vector3[m_Mesh.m_VertexCount];
normalData = new OpenTK.Vector3[m_Mesh.m_VertexCount];
for (int n = 0; n < m_Mesh.m_VertexCount; n++)
{
normalData[n] = new Vector3(
normalData[n] = new OpenTK.Vector3(
m_Mesh.m_Normals[n * count],
m_Mesh.m_Normals[n * count + 1],
m_Mesh.m_Normals[n * count + 2]);
}
}
if (m_Mesh.m_Colors == null || m_Mesh.m_Colors.Length == m_Mesh.m_VertexCount * 3)
#endregion
#region Colors
if (m_Mesh.m_Colors == null)
{
colorData = new Vector4[m_Mesh.m_VertexCount];
colorData = new OpenTK.Vector4[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colorData[c] = new Vector4(
colorData[c] = new OpenTK.Vector4(
0.5f, 0.5f, 0.5f, 1.0f);
}
}
else if (m_Mesh.m_Colors.Length == m_Mesh.m_VertexCount * 3)
{
colorData = new OpenTK.Vector4[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colorData[c] = new OpenTK.Vector4(
m_Mesh.m_Colors[c * 4],
m_Mesh.m_Colors[c * 4 + 1],
m_Mesh.m_Colors[c * 4 + 2],
1.0f);
}
}
else
{
colorData = new Vector4[m_Mesh.m_VertexCount];
colorData = new OpenTK.Vector4[m_Mesh.m_VertexCount];
for (int c = 0; c < m_Mesh.m_VertexCount; c++)
{
colorData[c] = new Vector4(
colorData[c] = new OpenTK.Vector4(
m_Mesh.m_Colors[c * 4],
m_Mesh.m_Colors[c * 4 + 1],
m_Mesh.m_Colors[c * 4 + 2],
m_Mesh.m_Colors[c * 4 + 3]);
}
}
#endregion
}
createVAO();
StatusStripUpdate("Using OpenGL Version: " + GL.GetString(StringName.Version)
+ " | 'T'=Start/Stop Rotation | 'WASD'=Manual Rotate | "
+ "'Shift WASD'=Move | 'Q/E'=Zoom | 'Ctl W' =Wireframe");
@ -1574,9 +1536,10 @@ namespace Unity_Studio
}
break;
case 48:
Shader m_Shader = new Shader(asset, true);
if (!ExportFileExists(exportpath + asset.Text + asset.extension))
{
ExportShader(new Shader(asset, true), exportpath + asset.Text + ".txt");
ExportShader(m_Shader, exportpath + asset.Text + ".txt");
exportedCount++;
}
break;
@ -1603,6 +1566,15 @@ namespace Unity_Studio
ExportFont(m_Font, exportpath + asset.Text + asset.extension);
exportedCount++;
}
break;
case 43: //Mesh
Mesh m_Mesh = new Mesh(asset, true);
if (!ExportFileExists(exportpath + asset.Text + asset.extension))
{
ExportMesh(m_Mesh, exportpath + asset.Text);
exportedCount++;
}
break;
default:
if (!ExportFileExists(exportpath + asset.Text + asset.extension))
@ -1755,24 +1727,24 @@ namespace Unity_Studio
GL.DeleteShader(address);
}
private void createVBO(int vboAddress, Vector3[] data, int address)
private void createVBO(int vboAddress, OpenTK.Vector3[] data, int address)
{
GL.GenBuffers(1, out vboAddress);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboAddress);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
(IntPtr)(data.Length * Vector3.SizeInBytes),
GL.BufferData<OpenTK.Vector3>(BufferTarget.ArrayBuffer,
(IntPtr)(data.Length * OpenTK.Vector3.SizeInBytes),
data,
BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(address, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableVertexAttribArray(address);
}
private void createVBO(int vboAddress, Vector4[] data, int address)
private void createVBO(int vboAddress, OpenTK.Vector4[] data, int address)
{
GL.GenBuffers(1, out vboAddress);
GL.BindBuffer(BufferTarget.ArrayBuffer, vboAddress);
GL.BufferData<Vector4>(BufferTarget.ArrayBuffer,
(IntPtr)(data.Length * Vector4.SizeInBytes),
GL.BufferData<OpenTK.Vector4>(BufferTarget.ArrayBuffer,
(IntPtr)(data.Length * OpenTK.Vector4.SizeInBytes),
data,
BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(address, 4, VertexAttribPointerType.Float, false, 0, 0);
@ -1802,10 +1774,10 @@ namespace Unity_Studio
GL.GenVertexArrays(1, out vao);
GL.BindVertexArray(vao);
createVBO(vboPositions, vertexData, attributeVertexPosition);
createVBO(vboNormals, normalData, attributeNormalDirection);
createVBO(vboColors, colorData, attributeVertexColor);
createVBO(vboViewMatrix, viewMatrixData, uniformViewMatrix);
createEBO(eboElements, indiceData);
createVBO(vboNormals, normalData, attributeNormalDirection);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
}

Binary file not shown.