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
2
Question by XerocakeGAME · Dec 07, 2014 at 06:54 PM · meshsavefiles

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

alt text

alt text

 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));
     }
 }
 








after.jpg (55.8 kB)
before.jpg (53.8 kB)
Comment
Add comment · Show 2
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 XerocakeGAME · Dec 07, 2014 at 06:31 PM 0
Share

Another thing I just noticed was the fact that every time it loaded, the poly count increased yet it still remained distorted.

avatar image XerocakeGAME · Dec 07, 2014 at 07:19 PM 0
Share

I just tested it with a simple cube and it worked fine, but with anything else it made it blocky.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
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 XerocakeGAME · Dec 09, 2014 at 06:54 PM 0
Share

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.

avatar image XerocakeGAME · Dec 09, 2014 at 07:21 PM 0
Share

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.

avatar image
0

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);
Comment
Add comment · Share
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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Procedural mesh, half the faces have wrong normals 1 Answer

transform.active question - Model Mesh Scripting 2 Answers

Ignore/Destroy then re-enable Mesh Colliders 3 Answers

Script attached to mesh affects all meshes? 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