Ogre Mesh and Skeleton XML Loader (Unity Engine)
2 posters
Page 1 of 1
Ogre Mesh and Skeleton XML Loader (Unity Engine)
if i loaded the xml directly into unity it would be slow, so i will be using a mid binary format to handle things. thought i share my work so others can help improve it. code written in visual studio express C#
//tested and works. doesn't load bone weights yet
my failed skeleton loading script. works but not completely.
//tested and works. doesn't load bone weights yet
- Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public class OgreMesh {
XmlDocument meshdoc = new XmlDocument();
public void loadFile(string FileName)
{
meshdoc.Load(FileName);
}
public Mesh constructMesh()
{
//Debug.Log ("works");
Mesh mesh = new Mesh();
//indices
XmlNode sharedgeometry = meshdoc.DocumentElement.SelectSingleNode("/mesh/sharedgeometry");
XmlNode vertexbuffer = sharedgeometry.FirstChild;
int Count;
int texCoords;
bool norms;
bool positions;
int.TryParse(sharedgeometry.Attributes["vertexcount"].InnerText,out Count);
bool.TryParse(vertexbuffer.Attributes["positions"].InnerText, out positions);
int.TryParse(vertexbuffer.Attributes["texture_coords"].InnerText, out texCoords);
bool.TryParse(vertexbuffer.Attributes["normals"].InnerText, out norms);
XmlNodeList vertexList = vertexbuffer.ChildNodes;
List<Vector3> Vertices = new List<Vector3> ();
List<Vector3> Normals = new List<Vector3> ();
List<Vector2> UVs = new List<Vector2> ();
for (int i = 0; i < Count; i++)
{
XmlNodeList vertex = vertexList.Item(i).ChildNodes;
if (positions)
{
XmlNode pNode = vertex.Item(findChild(vertex, "position", 0));
Vertices.Add(new Vector3(float.Parse(pNode.Attributes["x"].InnerText),
float.Parse(pNode.Attributes["y"].InnerText),
float.Parse(pNode.Attributes["z"].InnerText)));
}
if (norms)
{
XmlNode nNode = vertex.Item(findChild(vertex, "normal", 1));
Normals.Add(new Vector3(float.Parse(nNode.Attributes["x"].InnerText),
float.Parse(nNode.Attributes["y"].InnerText),
float.Parse(nNode.Attributes["z"].InnerText)));
}
if (texCoords > 0)
{
XmlNode tNode = vertex.Item(findChild(vertex, "texcoord", 1));
UVs.Add(new Vector2(float.Parse(tNode.Attributes["u"].InnerText),
float.Parse(tNode.Attributes["v"].InnerText)));
}
}
if (positions)
mesh.vertices = Vertices.ToArray();
if (norms)
mesh.normals = Normals.ToArray();
if (texCoords > 0)
mesh.uv = UVs.ToArray();
//triangles/faces
XmlNode faces = meshdoc.DocumentElement.SelectSingleNode("/mesh/submeshes/submesh/faces");
int.TryParse(faces.Attributes["count"].InnerText, out Count);
List<int> Triangles = new List<int> ();
for (int i = 0; i < Count; i++)
{
Triangles.Add(int.Parse(faces.ChildNodes.Item(i).Attributes["v1"].InnerText));
Triangles.Add(int.Parse(faces.ChildNodes.Item(i).Attributes["v2"].InnerText));
Triangles.Add(int.Parse(faces.ChildNodes.Item(i).Attributes["v3"].InnerText));
}
mesh.triangles = Triangles.ToArray ();
return mesh;
}
int findChild(XmlNodeList List, string Name, int firstCheck)
{
if (List.Item(firstCheck).Name == Name)
return firstCheck;
return findChild(List, Name);
}
int findChild(XmlNodeList List, string Name)
{
for(int k = 0; k < List.Count; k++)
{
if (List.Item(k).Name == Name)
return k;
}
return -1;
}
public BoneWeight[] constructSkinning()
{
List<BoneWeight> weights = new List<BoneWeight> ();
return weights.ToArray ();
}
}
my failed skeleton loading script. works but not completely.
- Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
public struct BoneData
{
public string Name;
public string Parent;
public Vector3 Position;
public Quaternion Rotation;
}
public class OgreSkeleton{
XmlDocument meshdoc = new XmlDocument();
public void loadFile(string FileName)
{
meshdoc.Load(FileName);
}
public BoneData[] constructSkeleton()
{
List<BoneData> BD = new List<BoneData> ();
BoneData sBD;
XmlNode Bones = meshdoc.DocumentElement.SelectSingleNode ("/skeleton/bones");
for (int i = 0; i < Bones.ChildNodes.Count; i ++) {
XmlNode Bone = Bones.ChildNodes[i];
XmlNode bonePosition = Bone.ChildNodes[0];
XmlNode boneRotation = Bone.ChildNodes[1];
sBD = new BoneData();
sBD.Name = Bone.Attributes["name"].InnerText;
sBD.Position = new Vector3(float.Parse(bonePosition.Attributes["x"].InnerText),
float.Parse(bonePosition.Attributes["y"].InnerText),
float.Parse(bonePosition.Attributes["z"].InnerText));
sBD.Rotation = new Quaternion(float.Parse(boneRotation.FirstChild.Attributes["x"].InnerText),
float.Parse(boneRotation.FirstChild.Attributes["y"].InnerText),
float.Parse(boneRotation.FirstChild.Attributes["z"].InnerText),
float.Parse(boneRotation.Attributes["angle"].InnerText));
BD.Add(sBD);
}
BoneData[] BoneDataArray = BD.ToArray();
XmlNode bonehierarchy = meshdoc.DocumentElement.SelectSingleNode ("/skeleton/bonehierarchy");
for (int i = 0; i < bonehierarchy.ChildNodes.Count; i ++) {
string bpBone = bonehierarchy.ChildNodes[i].Attributes["bone"].InnerText;
string bpParent = bonehierarchy.ChildNodes[i].Attributes["parent"].InnerText;
//find bone a set parent
for (int x = 0; x < BoneDataArray.Length; x++)
{
if (BoneDataArray[x].Name == bpBone)
{
BoneDataArray[x].Parent = bpParent; break;
}
}
}
return BoneDataArray;
}
public void constructAnimations()
{
}
}
InfinitasImpetum- (0%)-Lv8
- Posts : 2162
Join date : 2012-05-30
Age : 34
Re: Ogre Mesh and Skeleton XML Loader (Unity Engine)
who understands programming here other than u?
00f- (90%)-Lv4
- Posts : 505
Join date : 2014-11-06
Currently : Japan
Re: Ogre Mesh and Skeleton XML Loader (Unity Engine)
so it here for those who are learning or interested. maxilos is studying programming he might want to see my scripts.
InfinitasImpetum- (0%)-Lv8
- Posts : 2162
Join date : 2012-05-30
Age : 34
Similar topics
» RGB Color Swap and Grayscale Shader (Unity Engine)
» Unity Experiments
» Unity Voxel Experiments
» my Engine tools
» Winz2DS Engine DEV thread
» Unity Experiments
» Unity Voxel Experiments
» my Engine tools
» Winz2DS Engine DEV thread
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum