Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 sergiobd · Jul 09, 2015 at 12:08 PM · meshserializeserializedobject

Serialize a mesh

Hi,

I am currently capturing a user mesh from a Kinect (I am making a mesh by subsampling the user depth map). I would like to save the frames to a file in order to reproduce them in another context. My first approach is serialization. Apparently, all objects in my graph should be serializable, so I am asked to serialize the Mesh class, the Vector3 class, and so on. Is there another way to do this?

My current approach is having a Frame class which simply contains a timestamp and a mesh. I construct a List of Frame objects and I attempt to save it when the application is closed.

[A note to moderators: I would appreciate some feedback if this question gets rejected...]

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Runtime.Serialization;
 
 
 [RequireComponent (typeof(MeshFilter), (typeof(MeshRenderer)))]
 public class meshRecorder : MonoBehaviour {
 
     Mesh mesh;
     public String fileName;
 
     List <Frame> frames;
     public int expectedTime = 60;
     public int expectedFrameRate = 30;
 
 
 
     void Start () {
 
         // Estimated number of frames:
         int numFrames = expectedTime * expectedFrameRate;
         print ("Expected number of frames: " + numFrames);
 
         frames = new List<Frame> (Mathf.RoundToInt (1.5f * numFrames)); // Overestimated initial capacity.
         mesh = GetComponent <MeshFilter> ().mesh;
 
         // FileName
         if (String.IsNullOrEmpty( fileName)) {
             fileName = DateTime.Now.ToString("yyyy_mm_dd_hh_mm_ss");
 
             print ("will save to " + fileName);
         }
 
     }
     
     // Update is called once per frame
     void Update () {
 
         //Frame f = new Frame (Time.time, mesh);
         Frame f = new Frame (Time.time, mesh.triangles, mesh.vertices);
         frames.Add (f);
     }
 
     void OnApplicationQuit(){
 
         // Open stream for writing.
         Stream stream = File.Open (fileName + ".mst", FileMode.Create);
         BinaryFormatter bf = new BinaryFormatter ();
 
         bf.Serialize (stream, frames);
     }
 }
 
 [Serializable()]
 public class Frame {
 
     float time; // En segundos.
     int [] triangles;
     Vector3 [] vertices;
     //Mesh mesh;
 
     /*public Frame( float t, Mesh m){
         time = t;
         mesh = m;
     }*/
 
     public Frame (float t, int [] _triangles, Vector3 [] _vertices){
         time = t;
         triangles = _triangles;
         vertices = _vertices;
 
     }
 }
 

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sergiobd · Jul 17, 2015 at 03:43 PM

Answering to my own question here:

As vertices are vectors, I decided to save them as a 2-dimensional array of floats (of size [3, numVertices]).

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

2 People are following this question.

avatar image avatar image

Related Questions

3d mesh serialize 1 Answer

How to deserialize class and load script/class onto GameObject? 2 Answers

Fastest Way To Save/Load a SkinnedMeshRenderer at Runtime 0 Answers

Serialized data show information after load 0 Answers

Do mesh vertices have the same index as their corresponding normal? 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