Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kowalski042 · Feb 02, 2015 at 08:23 AM · meshmeshrenderermeshfilter

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kowalski042 · Feb 02, 2015 at 04:37 AM 0
Share

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)

avatar image Kowalski042 · Feb 02, 2015 at 04:38 AM 0
Share

Also the OBJ file I provided (the thing after the scripts) is just an example

avatar image Kowalski042 · Mar 01, 2015 at 05:52 AM 0
Share

And not solved

avatar image RyanAchtenSoma · Apr 07, 2015 at 01:15 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bounds of dynamic mesh not updating 1 Answer

Procedurally generated mesh not Rendering all triangles 1 Answer

Unity5 Procedural meshing slower than in Unity4? 1 Answer

Mesh between lines - Mesh.SetIndices 1 Answer

Mesh coloring isnt working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges