/// <summary> Parse a DataBlock into a MaterialProps. Any missing variables will be set to their default value. </summary> /// <param name="dataBlock"></param> /// <returns></returns> public static MaterialProps Parse(List <string> dataBlock) { MaterialProps res = MaterialProps.Default(); if (dataBlock.Count > 1) { float parsedValue; if (dataBlock.Count > (int)MatProp.maxForce && TryGetFloat(dataBlock[(int)MatProp.maxForce], out parsedValue)) { res.maxForce = (int)parsedValue; } if (dataBlock.Count > (int)MatProp.maxForceDist && TryGetFloat(dataBlock[(int)MatProp.maxForceDist], out parsedValue)) { res.maxForceDist = parsedValue; } if (dataBlock.Count > (int)MatProp.yieldDist && TryGetFloat(dataBlock[(int)MatProp.yieldDist], out parsedValue)) { if (float.IsNaN(parsedValue)) { parsedValue = float.MaxValue; } res.yieldDist = parsedValue; } if (dataBlock.Count > (int)MatProp.hapticMagn && TryGetFloat(dataBlock[(int)MatProp.hapticMagn], out parsedValue)) { res.hapticForce = (int)parsedValue; } if (dataBlock.Count > (int)MatProp.hapticDur && TryGetFloat(dataBlock[(int)MatProp.hapticDur], out parsedValue)) { res.hapticDur = (int)parsedValue; } } return(res); }
/// <summary> Add a material with specified properties tot he internal memory </summary> /// <param name="matName"></param> /// <param name="props"></param> private static void AddMaterial(string matName, MaterialProps props) { MaterialProps exist; if (!materials.TryGetValue(matName, out exist)) { materials.Add(matName, props); } }
/// <summary> Retrieve a 'default' material. </summary> /// <returns></returns> public static MaterialProps Default() { MaterialProps res = new MaterialProps(); res.maxForce = 100; res.maxForceDist = 0; res.yieldDist = float.MaxValue; res.hapticDur = 100; res.hapticForce = 100; return(res); }
/// <summary> Retrieve the Material properties from the chosen Library. </summary> /// <param name="libName"></param> /// <param name="matName"></param> /// <returns></returns> public static MaterialProps GetMaterial(string matName) { matName = matName.ToLowerInvariant(); //always convert to lower case to prevent typos on Dev/User side; MaterialProps res; if (materials.TryGetValue(matName, out res)) { return(res); } else { SenseGlove_Debugger.Log("Error loading " + matName + ": No such material loaded."); } return(MaterialProps.Default()); }
/// <summary> Load a materials library from a TextAsset </summary> /// <param name="databaseFile"></param> public static void LoadLibrary(string dir, string file) { if (!IsLoaded(file)) { string[] fLines; if (Util.FileIO.ReadTxtFile(dir + file, out fLines)) { //Splitup file int line = 0; List <string> dataBlock = new List <string>(); string lastName = ""; while (line <= fLines.Length) { if (line >= fLines.Length || (fLines[line].Length > 0 && fLines[line][0] == '#')) //its a new Material { if (dataBlock.Count > 0 && lastName.Length > 0) //parse & add previous Material if it has a good name { AddMaterial(lastName, MaterialProps.Parse(dataBlock)); } dataBlock.Clear(); if (line < fLines.Length) { try //extract name of new material { lastName = fLines[line].Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)[1].ToLowerInvariant(); //condition name } catch (System.Exception Ex) { SenseGlove_Debugger.LogWarning(Ex.Message); lastName = ""; } } } if (line < fLines.Length) { dataBlock.Add(fLines[line]); } line++; } } libraryNames.Add(file); //prevents us from continuously trying to open files } }