- Home /
Setting a mesh's triangles and vertices from file causes distortion.
Hi, I am trying to load a mesh from a text file that contains the vertices and triangles, but when applying the values to the mesh filter, the mesh becomes blocky. The object is saved after the changed and then can be loaded. The first image below is the original object before it is loaded and the second is after it is loaded. I also have included the save and load C# script.
Thanks
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
public class save : MonoBehaviour {
public GameObject planet;
void Start()
{
}
public void Save()
{
System.IO.StreamWriter file = new System.IO.StreamWriter(Application.dataPath + @"\pl128.txt");
for(int i = 0; i < planet.GetComponent<MeshFilter>().mesh.vertices.Length; i++)
{
file.WriteLine(planet.GetComponent<MeshFilter>().mesh.vertices[i]);
}
for(int i = 0; i < planet.GetComponent<MeshFilter>().mesh.uv.Length; i++)
{
file.WriteLine(planet.GetComponent<MeshFilter>().mesh.uv[i]);
}
for(int i = 0; i < planet.GetComponent<MeshFilter>().mesh.triangles.Length; i++)
{
file.WriteLine(planet.GetComponent<MeshFilter>().mesh.triangles[i]);
}
file.Close();
}
}
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; public class loadPlanet : MonoBehaviour { public GameObject planet;
public List<int> triangles = new List<int>();
public List<Vector3> vert = new List<Vector3>();
public List<Vector2> uv = new List<Vector2>();
public int counter = 0;
void Start()
{
}
public void Load()
{
string curline;
if(File.Exists(Application.dataPath + @"\pl128.txt"))
{
System.IO.StreamReader file = new System.IO.StreamReader(Application.dataPath + @"\pl128.txt");
while((curline = file.ReadLine()) != null)
{
counter = 0;
char[] temp;
for(int i = 0; i < curline.Length; i++)
{
temp = curline.ToCharArray();
if(temp[i] == ',' || temp[i] == '(' || temp[i] == ')')
{
counter++;
}
}
if(counter == 4)
{
ReadVert(curline);
}
if(counter == 3)
{
ReadUV(curline);
}
if(counter == 0)
{
ReadTri(curline);
}
}
file.Close();
planet.GetComponent<MeshFilter>().mesh.vertices = vert.ToArray();
planet.GetComponent<MeshFilter>().mesh.uv = uv.ToArray();
planet.GetComponent<MeshFilter>().mesh.triangles = triangles.ToArray();
planet.GetComponent<MeshFilter>().mesh.RecalculateNormals();
planet.GetComponent<MeshFilter>().mesh.RecalculateBounds();
planet.GetComponent<MeshCollider>().sharedMesh = null;
planet.GetComponent<MeshCollider>().sharedMesh = planet.GetComponent<MeshFilter>().mesh;
}
else
{
}
}
public void ReadVert(string s)
{
float tempx;
float tempy;
float tempz;
var charsToRemove = new string[] { "@", ",", ";", "'", "(", ")"};
foreach (var c in charsToRemove)
{
s = s.Replace(c, string.Empty);
}
var commands = s.Split (' ');
tempx = float.Parse(commands[0]);
tempy = float.Parse(commands[1]);
tempz = float.Parse(commands[2]);
vert.Add (new Vector3(tempx,tempy,tempz));
}
public void ReadUV(string s)
{
float tempx;
float tempy;
var charsToRemove = new string[] { "@", ",", ";", "'", "(", ")"};
foreach (var c in charsToRemove)
{
s = s.Replace(c, string.Empty);
}
var commands = s.Split (' ');
tempx = float.Parse(commands[0]);
tempy = float.Parse(commands[1]);
uv.Add (new Vector2(tempx,tempy));
}
public void ReadTri(string s)
{
triangles.Add(int.Parse(s));
}
}
Another thing I just noticed was the fact that every time it loaded, the poly count increased yet it still remained distorted.
I just tested it with a simple cube and it worked fine, but with anything else it made it blocky.
Answer by unimechanic · Dec 09, 2014 at 03:31 PM
Check whether you are parsing the floats correctly from the file, maybe the decimals are being removed.
Thanks for your input, but unfortunately that isn't the problem. I have tried running Debug.Log on a few of the vertices,uv, etc..., and they do have the correct decimals.
I decided to create a file that has the data directly from the new mesh, and the triangles had a lot of discrepancies from the original save file which is odd, since nothing else had any.
Answer by Gaidzin · Jul 27, 2015 at 04:46 AM
The problem with this:
file.WriteLine(planet.GetComponent<MeshFilter>().mesh.vertices[i]);
Use it:
file.WriteLine(vertices[i].x+","+vertices[i].y+","+vertices[i].z);