Advertisement
Lusien_Lashans

резерв

Aug 14th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4.  
  5. public class PointCloudManager : MonoBehaviour {
  6.  
  7.     // File
  8.     public string dataPath;
  9.     private string filename;
  10.     public Material matVertex;
  11.  
  12.     // GUI
  13.     private float progress = 0;
  14.     private string guiText;
  15.     private bool loaded = false;
  16.  
  17.     // PointCloud
  18.     private GameObject pointCloud;
  19.  
  20.     public float scale = 1;
  21.     public bool invertYZ = false;
  22.     public bool forceReload = false;
  23.  
  24.     public int numPoints;
  25.     public int numPointGroups;
  26.     private int limitPoints = 65000;
  27.  
  28.     private Vector3[] points;
  29.     private Color[] colors;
  30.     private Vector3 minValue;
  31.  
  32.    
  33.     void Start () {
  34.         // Create Resources folder
  35.         createFolders();
  36.         Parser.Start();
  37.         // Get Filename
  38.         filename = Path.GetFileName(dataPath);
  39.         loadPointCloud();
  40.         //loadStoredMeshes();
  41.         //loadScene();
  42.     }
  43.  
  44.  
  45.  
  46.     void loadScene(){
  47.         // Check if the PointCloud was loaded previously
  48.         if(!Directory.Exists (Application.dataPath + "/Resources/PointCloudMeshes/" + filename)){
  49.             UnityEditor.AssetDatabase.CreateFolder ("Assets/Resources/PointCloudMeshes", filename);
  50.             loadPointCloud ();
  51.         } else if (forceReload){
  52.             UnityEditor.FileUtil.DeleteFileOrDirectory(Application.dataPath + "/Resources/PointCloudMeshes/" + filename);
  53.             UnityEditor.AssetDatabase.Refresh();
  54.             UnityEditor.AssetDatabase.CreateFolder ("Assets/Resources/PointCloudMeshes", filename);
  55.             loadPointCloud ();
  56.         } else
  57.             // Load stored PointCloud
  58.             loadStoredMeshes();
  59.     }
  60.    
  61.    
  62.     void loadPointCloud(){
  63.         //Check what file exists
  64.         //if (File.Exists(Application.dataPath + dataPath + ".off"))
  65.         //    // load off
  66.         //    StartCoroutine("loadOFF", dataPath + ".off");
  67.         //else
  68.         //    Debug.Log("File '" + dataPath + "' could not be found");
  69.         StartCoroutine("loadOFF");
  70.  
  71.     }
  72.    
  73.     // Load stored PointCloud
  74.     void loadStoredMeshes(){
  75.  
  76.         Debug.Log ("Using previously loaded PointCloud: " + filename);
  77.  
  78.         GameObject pointGroup = Instantiate(Resources.Load ("PointCloudMeshes/" + filename)) as GameObject;
  79.  
  80.         loaded = true;
  81.     }
  82.    
  83.     // Start Coroutine of reading the points from the OFF file and creating the meshes
  84.     IEnumerator loadOFF(/*string dPath*/){
  85.  
  86.         // Read file
  87.         //StreamReader sr = new StreamReader (Application.dataPath + dPath);
  88.         //sr.ReadLine (); // OFF
  89.         //string[] buffer = sr.ReadLine ().Split(); // nPoints, nFaces
  90.         string[] buffer;
  91.         //numPoints = int.Parse (buffer[0]);
  92.         var arrayOfPoints = Parser.listOfPoints.ToArray();
  93.         numPoints = arrayOfPoints.Length;
  94.         points = new Vector3[numPoints];
  95.         colors = new Color[numPoints];
  96.         minValue = new Vector3();
  97.         var output = "";
  98.         for (int i = 0; i< numPoints; i++){
  99.             //buffer = sr.ReadLine ().Split ();
  100.             var str = arrayOfPoints[i].X + " " + arrayOfPoints[i].Y + " " + arrayOfPoints[i].Z;
  101.             buffer = str.Split();
  102.             points[i] = new Vector3(
  103.                 float.Parse(arrayOfPoints[i].X.ToString()) * scale,
  104.                 float.Parse(arrayOfPoints[i].Y.ToString()) * scale,
  105.                 float.Parse(arrayOfPoints[i].Z.ToString()) * scale
  106.                 );
  107.             //if (!invertYZ)
  108.             //  points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[1])*scale,float.Parse (buffer[2])*scale) ;
  109.             //else
  110.             //  points[i] = new Vector3 (float.Parse (buffer[0])*scale, float.Parse (buffer[2])*scale,float.Parse (buffer[1])*scale) ;
  111.  
  112.             //if (buffer.Length >= 5)
  113.             //  colors[i] = new Color (int.Parse (buffer[3])/255.0f,int.Parse (buffer[4])/255.0f,int.Parse (buffer[5])/255.0f);
  114.             //else
  115.             //  colors[i] = Color.cyan;
  116.  
  117.             // Relocate Points near the origin
  118.             //calculateMin(points[i]);
  119.  
  120.             // GUI
  121.             //progress = i *1.0f/(numPoints-1)*1.0f;
  122.             //if (i%Mathf.FloorToInt(numPoints/20) == 0){
  123.             //  guiText=i.ToString() + " out of " + numPoints.ToString() + " loaded";
  124.             //  yield return null;
  125.             //}
  126.         }
  127.         // Instantiate Point Groups
  128.         numPointGroups = Mathf.CeilToInt (numPoints*1.0f / limitPoints*1.0f);
  129.  
  130.         pointCloud = new GameObject (filename);
  131.  
  132.         for (int i = 0; i < numPointGroups-1; i ++) {
  133.             InstantiateMesh(i, limitPoints);
  134.             if (i % 10 == 0)
  135.             {
  136.                 guiText = i.ToString() + " out of " + numPointGroups.ToString() + " PointGroups loaded";
  137.                 yield return null;
  138.             }
  139.         }
  140.         InstantiateMesh (numPointGroups-1, numPoints- (numPointGroups-1) * limitPoints);
  141.  
  142.         //Store PointCloud
  143.         //UnityEditor.PrefabUtility.CreatePrefab("Assets/Resources/PointCloudMeshes/" + filename + ".prefab", pointCloud);
  144.  
  145.         loaded = true;
  146.     }
  147.  
  148.    
  149.     void InstantiateMesh(int meshInd, int nPoints){
  150.         // Create Mesh
  151.         GameObject pointGroup = new GameObject (filename + meshInd);
  152.         pointGroup.AddComponent<MeshFilter> ();
  153.         pointGroup.AddComponent<MeshRenderer> ();
  154.         pointGroup.GetComponent<Renderer>().material = matVertex;
  155.  
  156.         pointGroup.GetComponent<MeshFilter> ().mesh = CreateMesh (meshInd, nPoints, limitPoints);
  157.         pointGroup.transform.parent = pointCloud.transform;
  158.  
  159.         // Store Mesh
  160.         //UnityEditor.AssetDatabase.CreateAsset(pointGroup.GetComponent<MeshFilter>().mesh, "Assets/Resources/PointCloudMeshes/" + filename + @"/" + filename + meshInd + ".asset");
  161.         //UnityEditor.AssetDatabase.SaveAssets();
  162.         //UnityEditor.AssetDatabase.Refresh();
  163.     }
  164.  
  165.     Mesh CreateMesh(int id, int nPoints, int limitPoints){
  166.        
  167.         Mesh mesh = new Mesh ();
  168.        
  169.         Vector3[] myPoints = new Vector3[nPoints];
  170.         int[] indecies = new int[nPoints];
  171.         Color[] myColors = new Color[nPoints];
  172.  
  173.         for(int i=0;i<nPoints;++i) {
  174.             myPoints[i] = points[id*limitPoints + i] - minValue;
  175.             indecies[i] = i;
  176.             myColors[i] = colors[id*limitPoints + i];
  177.         }
  178.  
  179.         mesh.vertices = myPoints;
  180.         mesh.colors = myColors;
  181.         mesh.SetIndices(indecies, MeshTopology.Points,0);
  182.         mesh.uv = new Vector2[nPoints];
  183.         mesh.normals = new Vector3[nPoints];
  184.  
  185.  
  186.         return mesh;
  187.     }
  188.  
  189.     void calculateMin(Vector3 point){
  190.         if (minValue.magnitude == 0)
  191.             minValue = point;
  192.  
  193.  
  194.         if (point.x < minValue.x)
  195.             minValue.x = point.x;
  196.         if (point.y < minValue.y)
  197.             minValue.y = point.y;
  198.         if (point.z < minValue.z)
  199.             minValue.z = point.z;
  200.     }
  201.  
  202.     void createFolders(){
  203.         if(!Directory.Exists (Application.dataPath + "/Resources/"))
  204.             UnityEditor.AssetDatabase.CreateFolder ("Assets", "Resources");
  205.  
  206.         if (!Directory.Exists (Application.dataPath + "/Resources/PointCloudMeshes/"))
  207.             UnityEditor.AssetDatabase.CreateFolder ("Assets/Resources", "PointCloudMeshes");
  208.     }
  209.     void OnGUI(){
  210.  
  211.  
  212.         if (!loaded){
  213.             GUI.BeginGroup (new Rect(Screen.width/2-100, Screen.height/2, 400.0f, 20));
  214.             GUI.Box (new Rect (0, 0, 200.0f, 20.0f), guiText);
  215.             GUI.Box (new Rect (0, 0, progress*200.0f, 20), "");
  216.             GUI.EndGroup ();
  217.         }
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement