public static List<Mesh> ReadVrml(string contents, ProgressDelegate progress, string debugNameObj = "???")
{
if (progress != null)
progress("Loading Vrml file", 0.0f);
List<Mesh> ret = new List<Mesh>();
MatchCollection matches = Regex.Matches(contents,
"Group {.*?"
+ "point\\s*\\[(?<verts>[^\\]]+).*?"
+ "coordIndex\\s*\\[(?<indices>[^\\]]+)"
+ "",
RegexOptions.Multiline | RegexOptions.Singleline);
int meshCounter = 0;
foreach(Match m in matches)
{
if (progress != null)
progress("Converting Vrml file to Unity meshes", 100f * (meshCounter + 0.5f) / matches.Count);
Mesh newMesh = new Mesh();
MatchCollection vertMatches = Regex.Matches(m.Groups["verts"].Value,
"^\\s*(?<x>-?\\d*\\.?\\d+)\\s+(?<y>-?\\d*\\.?\\d+)\\s+(?<z>-?\\d*\\.?\\d+),", RegexOptions.Multiline);
MatchCollection indexMatches = Regex.Matches(m.Groups["indices"].Value,
"^\\s*(?<x>\\d+),\\s+(?<y>\\d+),\\s+(?<z>\\d+),", RegexOptions.Multiline);
// Debug.LogFormat("Found match: v:{3}-{0},i:{4}-{1}, all:{2}", m.Groups["verts"].Value, m.Groups["indices"].Value, m.Value, vertMatches.Count, indexMatches.Count * 3);
Vector3[] verts = new Vector3[vertMatches.Count];
int[] indices = new int[indexMatches.Count * 3];
int itrCounter = 0;
// Have to reflect the x value for some reason
foreach(Match vm in vertMatches)
verts[itrCounter++] = new Vector3(-float.Parse(vm.Groups["x"].Value), float.Parse(vm.Groups["y"].Value), float.Parse(vm.Groups["z"].Value));
itrCounter = 0;
foreach(Match im in indexMatches)
{
indices[itrCounter++] = int.Parse(im.Groups["x"].Value);
indices[itrCounter++] = int.Parse(im.Groups["y"].Value);
indices[itrCounter++] = int.Parse(im.Groups["z"].Value);
}
newMesh.vertices = verts;
newMesh.triangles = indices;
// This checks the number of triangles in the each mesh and ignores
// the larger meshes. Probably for performance issues.
// newMesh.triangles.Length actually spits out three time the number of triangles!
if (newMesh.triangles.Length > 765)
Debug.LogWarningFormat("Too many triangles in vrml mesh for {0}! Found {1}", debugNameObj, newMesh.triangles.Length);
else if (newMesh.triangles.Length < 3)
Debug.LogWarningFormat ("Insufficient vertices to form a triangle in vrml mesh for {0}! Found {1}", debugNameObj, newMesh.triangles.Length);
else
ret.Add(newMesh);
meshCounter++;
}
return ret;
}