- Home /
Im reading a .obj file and converting it to a mesh (doesnt work)
Im reading a .obj file and converting it to a mesh. I have a Mesh Renderer and Mesh Filter but these scripts(3) are only giving me a blank mesh.
Code: Script (1):
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class MeshFromObj : MonoBehaviour {
Mesh mesh = new Mesh();
List<Vector3> verticies = new List<Vector3>();
List<int> triangles = new List<int>();
void InitVerts() {
if (mesh.vertices.Length == 1) {
return;
} else {
foreach (Vector3 verticy in mesh.vertices){
verticies.Add(verticy);
}
}
if (mesh.triangles.Length == 1) {
return;
} else {
foreach (int triangle in mesh.triangles){
triangles.Add(triangle);
}
}
}
void AssignToVerts() {
int i = 0;
mesh.vertices = new Vector3[verticies.Count];
foreach (Vector3 point in verticies) {
mesh.vertices[i] = point;
i++;
}
int b = 0;
mesh.triangles = new int[triangles.Count];
foreach (int num in triangles) {
mesh.triangles[b] = num;
b++;
}
}
public Mesh CreateMesh(string file){
InitVerts ();
foreach (string line in file.Split(Environment.NewLine.ToCharArray())) {
try {
if (line.Contains("v")){
string[] lna = line.Split(' ');
float x = float.Parse(lna[1]);
float y = float.Parse(lna[2]);
float z = float.Parse(lna[3]);
verticies.Add(new Vector3(x,y,z));
}
}
catch (Exception) {
}
}
foreach (string line in file.Split(Environment.NewLine.ToCharArray())) {
try {
if (line.Contains("f")){
string[] lna = line.Split(' ');
int a = int.Parse(lna[1]);
int b = int.Parse(lna[2]);
int c = int.Parse(lna[3]);
triangles.Add(a);
triangles.Add(b);
triangles.Add(c);
}
}
catch (Exception) {
}
}
AssignToVerts ();
mesh.Optimize ();
return mesh;
}
}
Script 2:
using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
public class OBJ {
Mesh me;
public Mesh GetMesh () {
return me;
}
public int[] GetTriangles() {
return me.triangles;
}
public Vector3[] GetVerticies() {
return me.vertices;
}
public OBJ LoadFromUrl(string url) {
me = new GameObject ().AddComponent<MeshFromObj> ().CreateMesh (new WebClient().DownloadString (url));
return this;
}
public OBJ LoadFromFile(string filePos) {
me = new GameObject ().AddComponent<MeshFromObj> ().CreateMesh (File.ReadAllText(filePos));
return this;
}
}
Script 3:
using UnityEngine;
using System.Collections;
public class LoadOBJ : MonoBehaviour {
public string Path = @"C:\anything.obj";
OBJ obj = new OBJ();
void Start () {
(GetComponent<MeshFilter> ().mesh = obj.LoadFromFile (Path).GetMesh()).name = "OpenedMesh";
}
}
Script 3 is the one attached to the gameobject with the MeshFilter (and MeshRenderer).
If its too little info tell me to add more.
By a triangle do you mean a 3d mesh or 2d? Also Im not doing anything for a material, just a shape... I just noticed that the material might be the problem... Lets see what the mesh collider says ($$anonymous$$esh collider outline)
Also the OBJ file I provided (the thing after the scripts) is just an example
Hey $$anonymous$$owalski did you ever get this working?
By 'reading' an .obj file do you mean parsing the source code that makes up an .obj file (in a similar manner to when opened in a text editor) and then transposing this information into mesh data? I'm working on a similar project and would be interested to hear how this turned out.
Cheers, Ryan