Weird Artifacts in Mesh generated by Script
I would like to understand what I am doing wrong. I am parsing a file that is pulling the Normals from 3D program. The triangles everything seems to be in order. The mesh is produced perfectly but when the mesh is finish the shadows cast on it are very odd. I assume it has something to go with the normals?
Has anybody seen these on meshes before?
Things that I have tired: RecalculateNormals(); Still same result. I read something about spliting vertices but not quite sure how to do that. Each Triangle has its own vertices and maybe that is the problem.
here is the sample code below:
public class BuildModel : MonoBehaviour {
public TextAsset modelInfo;
public List<int> Triangles = new List<int> ();
public List<Vector3> Verts = new List<Vector3>();
public List<Vector3> Norms=new List<Vector3>();
// Use this for initialization
void Start () {
string infoText = modelInfo.text;
string[] TriangleInfo=Regex.Split(infoText, "r\n|\r|\n" );
// Triangles.Clear ();
// Verts.Clear ();
int index = 0;
foreach (string line in TriangleInfo) {
string[] info = line.Split (':');
if (info.Length >= 6) {
string ID = info [0];
string v1 = info [1];
string v2 = info [2];
string v3 = info [3];
string Name = info [4];
string matName = info [5];
string normal = info [6];
Vector3 point1 = stringtoVector (v1,4);
Vector3 point2 = stringtoVector (v2,4);
Vector3 point3 = stringtoVector (v3,4);
Vector3 normalV=stringtoVector(normal,7);
Verts.Add (point1);
Norms.Add (normalV);
Verts.Add (point2);
Norms.Add (normalV);
Verts.Add (point3);
Norms.Add (normalV);
int newT = index;
int newT2 = index + 1;
int newT3=index+2;
index = index + 3;
Triangles.Add (newT);
Triangles.Add (newT2);
Triangles.Add (newT3);
}
}
makeMesh ();
}
void makeMesh(){
GameObject newMesh = new GameObject ();
newMesh.AddComponent<MeshFilter> ();
newMesh.AddComponent<MeshRenderer> ();
Material mm=new Material(Shader.Find("Standard"));
newMesh.GetComponent<MeshRenderer> ().material = mm;
Mesh newM = newMesh.GetComponent<MeshFilter> ().mesh;
newM.SetVertices (Verts);
newM.SetTriangles (Triangles.ToArray(), 0);
newM.SetNormals (Norms);
// newM.RecalculateNormals ();
newMesh.transform.rotation = Quaternion.Euler(new Vector3(-90f,0f,0f));
}
Vector3 stringtoVector(string v_String,int amount){
v_String=v_String.Remove (0, amount);
v_String = v_String.Remove (v_String.Length - 1, 1);
string[] splitV = v_String.Split (',');
float x = float.Parse (splitV [0]);
float y = float.Parse (splitV[1]);
float z = float.Parse (splitV [2]);
Vector3 parse_Point = new Vector3 (x, y, z);
return parse_Point;
}
Answer by alfaro190 · Apr 11, 2017 at 01:16 AM
Not sure if this is the solution but, I went into my project settings and changed my shadow Projection to Stable Fit. Seem to fix the problem. Still skeptical about the solution. So wont mark it as answered