AStrkl

Unity - Parse Position in text file from Houdini

May 19th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Text.RegularExpressions;
  6.  
  7. [CreateAssetMenu(fileName = "SamplingPointList", menuName = "FlowMapCity/Sampling Point List", order = 1)]
  8. public class SamplingPointsList : ScriptableObject {
  9.  
  10.     //these are the .txt files generated via Houdini
  11.     public TextAsset PositionFile;
  12.     public TextAsset NormalFile;
  13.  
  14.     //temporary list that will store all the float after each other
  15.     private List<float> floatValPOS = new List<float>();
  16.     private List<float> floatValNM = new List<float>();
  17.  
  18.     //the actual data that i will be using in game code
  19.     [HideInInspector]
  20.     public List<Vector3> Positions = new List<Vector3>();
  21.     [HideInInspector]
  22.     public List<Vector3> Normals = new List<Vector3>();
  23.  
  24.  
  25.     public void GetPosFromFile() {
  26.  
  27.         Debug.Log("Parse Text...");
  28.         ParsePos(PositionFile.text); //send the text from the TextFile as one big string
  29.         ParseNM(NormalFile.text);
  30.     }
  31.  
  32.  
  33.    
  34.    
  35.  
  36.     void ParsePos(string s) {
  37.  
  38.         string text = s;
  39.  
  40.         //clear all the list to be clean
  41.         floatValPOS.Clear();
  42.         Positions.Clear();
  43.  
  44.         //those are the special character for splitting the text
  45.         char[] separators = {'[',']'};
  46.         char[] VectorSeparator = { ',' };
  47.  
  48.         //First step is to separate stuff between '[]' i.e :  "[0.1,0.5,5.4]" => "0.1, 0.5, 5.4"
  49.         //We now have a big Array like this {"0.1, 0.5, 5.4", "0.4, 2.55, 5.8",...}
  50.         string[] strVectors = text.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
  51.        
  52.         //we iterate through each of the array elements
  53.         foreach (string str in strVectors) {
  54.             //Now with split everything between the ','
  55.             //We now have an Array like this {"0.1","0.5","5.4","0.4",...}
  56.             string[] strVals = str.Split(VectorSeparator, System.StringSplitOptions.RemoveEmptyEntries);
  57.  
  58.             //we iterate through each of the array elements (each float is a string)
  59.             foreach (string axis in strVals) {
  60.                 float val;
  61.                 float.TryParse(axis, out val); //conver the string to a float i.e :  "0.4" => 0.4f (numeric value, not a string anymore)
  62.                 floatValPOS.Add(val); //save it in our list
  63.             }
  64.         }
  65.  
  66.         //we now have a huge array with all the float value one after each other
  67.         //We need to take each set of 3 following float, and put them in the XYZ of a vector
  68.  
  69.         int count = 0;
  70.         int vectorCount = 0;
  71.  
  72.         //fill arrray with null vectors
  73.         for (int i = 0; i < floatValPOS.Count/3; i++) {
  74.             Positions.Add(Vector3.zero);
  75.         }
  76.  
  77.         //we iterate through each float value
  78.         foreach (float f in floatValPOS) {
  79.  
  80.             //this count 0 1 2 / 0 1 2 / 0 1 2....
  81.             switch (count % 3) {
  82.  
  83.                 case 0:
  84.                     Positions[vectorCount] = new Vector3(f, Positions[vectorCount].y, Positions[vectorCount].z);
  85.                     break;
  86.                 case 1:
  87.                     Positions[vectorCount] = new Vector3(Positions[vectorCount].x, f, Positions[vectorCount].z);
  88.                     break;
  89.                 case 2:
  90.                     Positions[vectorCount] = new Vector3(Positions[vectorCount].x, Positions[vectorCount].y, f);
  91.                     vectorCount++; //when we count to "2" we now we need to work on the following vector, so we increment vectorCount
  92.                     break;
  93.                 default:
  94.                     break;
  95.             }
  96.             count++;
  97.         }
  98.  
  99.     }
  100.  
  101.     void ParseNM(string s) { //same code but for normals
  102.  
  103.         string text = s;
  104.         floatValNM.Clear();
  105.         Normals.Clear();
  106.         char[] separators = { '(', ')' }; //houdini store Normal in parenthesis instead of brackets
  107.         char[] VectorSeparator = { ',' };
  108.  
  109.         string[] strVectors = text.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
  110.  
  111.         foreach (string str in strVectors) {
  112.             string[] strVals = str.Split(VectorSeparator, System.StringSplitOptions.RemoveEmptyEntries);
  113.  
  114.             foreach (string axis in strVals) {
  115.                 float val;
  116.                 float.TryParse(axis, out val);
  117.                 floatValNM.Add(val);
  118.             }
  119.         }
  120.  
  121.         int count = 0;
  122.         int vectorCount = 0;
  123.  
  124.         for (int i = 0; i < floatValNM.Count / 3; i++) {
  125.             Normals.Add(Vector3.zero);
  126.         }
  127.  
  128.         foreach (float f in floatValNM) {
  129.  
  130.             switch (count % 3) {
  131.  
  132.                 case 0:
  133.                     Normals[vectorCount] = new Vector3(f, Normals[vectorCount].y, Normals[vectorCount].z);
  134.                     break;
  135.                 case 1:
  136.                     Normals[vectorCount] = new Vector3(Normals[vectorCount].x, f, Normals[vectorCount].z);
  137.                     break;
  138.                 case 2:
  139.                     Normals[vectorCount] = new Vector3(Normals[vectorCount].x, Normals[vectorCount].y, f);
  140.                     vectorCount++;
  141.                     break;
  142.                 default:
  143.                     break;
  144.             }
  145.             count++;
  146.         }
  147.  
  148.     }
  149.  
  150. }
Add Comment
Please, Sign In to add comment