Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Text.RegularExpressions;
- [CreateAssetMenu(fileName = "SamplingPointList", menuName = "FlowMapCity/Sampling Point List", order = 1)]
- public class SamplingPointsList : ScriptableObject {
- //these are the .txt files generated via Houdini
- public TextAsset PositionFile;
- public TextAsset NormalFile;
- //temporary list that will store all the float after each other
- private List<float> floatValPOS = new List<float>();
- private List<float> floatValNM = new List<float>();
- //the actual data that i will be using in game code
- [HideInInspector]
- public List<Vector3> Positions = new List<Vector3>();
- [HideInInspector]
- public List<Vector3> Normals = new List<Vector3>();
- public void GetPosFromFile() {
- Debug.Log("Parse Text...");
- ParsePos(PositionFile.text); //send the text from the TextFile as one big string
- ParseNM(NormalFile.text);
- }
- void ParsePos(string s) {
- string text = s;
- //clear all the list to be clean
- floatValPOS.Clear();
- Positions.Clear();
- //those are the special character for splitting the text
- char[] separators = {'[',']'};
- char[] VectorSeparator = { ',' };
- //First step is to separate stuff between '[]' i.e : "[0.1,0.5,5.4]" => "0.1, 0.5, 5.4"
- //We now have a big Array like this {"0.1, 0.5, 5.4", "0.4, 2.55, 5.8",...}
- string[] strVectors = text.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
- //we iterate through each of the array elements
- foreach (string str in strVectors) {
- //Now with split everything between the ','
- //We now have an Array like this {"0.1","0.5","5.4","0.4",...}
- string[] strVals = str.Split(VectorSeparator, System.StringSplitOptions.RemoveEmptyEntries);
- //we iterate through each of the array elements (each float is a string)
- foreach (string axis in strVals) {
- float val;
- float.TryParse(axis, out val); //conver the string to a float i.e : "0.4" => 0.4f (numeric value, not a string anymore)
- floatValPOS.Add(val); //save it in our list
- }
- }
- //we now have a huge array with all the float value one after each other
- //We need to take each set of 3 following float, and put them in the XYZ of a vector
- int count = 0;
- int vectorCount = 0;
- //fill arrray with null vectors
- for (int i = 0; i < floatValPOS.Count/3; i++) {
- Positions.Add(Vector3.zero);
- }
- //we iterate through each float value
- foreach (float f in floatValPOS) {
- //this count 0 1 2 / 0 1 2 / 0 1 2....
- switch (count % 3) {
- case 0:
- Positions[vectorCount] = new Vector3(f, Positions[vectorCount].y, Positions[vectorCount].z);
- break;
- case 1:
- Positions[vectorCount] = new Vector3(Positions[vectorCount].x, f, Positions[vectorCount].z);
- break;
- case 2:
- Positions[vectorCount] = new Vector3(Positions[vectorCount].x, Positions[vectorCount].y, f);
- vectorCount++; //when we count to "2" we now we need to work on the following vector, so we increment vectorCount
- break;
- default:
- break;
- }
- count++;
- }
- }
- void ParseNM(string s) { //same code but for normals
- string text = s;
- floatValNM.Clear();
- Normals.Clear();
- char[] separators = { '(', ')' }; //houdini store Normal in parenthesis instead of brackets
- char[] VectorSeparator = { ',' };
- string[] strVectors = text.Split(separators, System.StringSplitOptions.RemoveEmptyEntries);
- foreach (string str in strVectors) {
- string[] strVals = str.Split(VectorSeparator, System.StringSplitOptions.RemoveEmptyEntries);
- foreach (string axis in strVals) {
- float val;
- float.TryParse(axis, out val);
- floatValNM.Add(val);
- }
- }
- int count = 0;
- int vectorCount = 0;
- for (int i = 0; i < floatValNM.Count / 3; i++) {
- Normals.Add(Vector3.zero);
- }
- foreach (float f in floatValNM) {
- switch (count % 3) {
- case 0:
- Normals[vectorCount] = new Vector3(f, Normals[vectorCount].y, Normals[vectorCount].z);
- break;
- case 1:
- Normals[vectorCount] = new Vector3(Normals[vectorCount].x, f, Normals[vectorCount].z);
- break;
- case 2:
- Normals[vectorCount] = new Vector3(Normals[vectorCount].x, Normals[vectorCount].y, f);
- vectorCount++;
- break;
- default:
- break;
- }
- count++;
- }
- }
- }
Add Comment
Please, Sign In to add comment