public RocketObject(float s, Model m, float f = 5000) : base(false, Vector3.One, new CircleCollider()) { _model = Attach(m, new Material { Color = new Vector4(1, 1, 0, 1), Diffusion = 5f, Ambient = 0.1f }); Scale = s * Vector3.One; MaxFuel = f; Fuel = f; }
public void SetModelInstance(SceneLayer layer, ModelHandle modelHandle, Material material) { lock (InstanceMutationLock) { Assure.False(isDisposed); if (modelInstance != null) { modelInstance.Value.Dispose(); } modelInstance = layer.CreateModelInstance(modelHandle, material, transform); } }
/// <summary> /// This method gets all missing models from database, instantiates all models, and returns thier orientation data /// </summary> /// <returns></returns> public List <ModelHandle> GetModels() { // Get and parse request into model fragments (thing still only have id, roation, and location) JSONObject jobject = MakeWebRequest(getModelsApiRequest); List <ModelFrag> frags = new List <ModelFrag>(); // loop through each models returned for (int i = 0; i < jobject.list[0].list.Count; i++) { JSONObject jo = jobject.list[0].list[i]; ModelFrag frag = new ModelFrag(); // loop through each field of the model, saving everything into a fragment object (in a list) for (int j = 0; j < jo.list.Count; j++) { switch (j) { case 0: frag.model_id = Int32.Parse(jo[j].ToString()); break; case 1: frag.model_location = jo[j].ToString(); break; case 2: frag.model_rotation = jo[j].ToString(); break; default: break; } } frags.Add(frag); } // create model directory if it doesn't exist if (!Directory.Exists("Models")) { Directory.CreateDirectory("Models"); } // Get all missing model data foreach (ModelFrag frag in frags) { if (!Directory.Exists("Models\\" + frag.model_id)) { // found a model that we don't already have // make web request to get the rest of the data string[] lines = new string[4]; Directory.CreateDirectory("Models\\" + frag.model_id); JSONObject jsonData = MakeWebRequest(getModelsApiRequest + frag.model_id); jsonData = jsonData.list[0].list[0]; // loop through the data returned for the model for (int i = 0; i < jsonData.keys.Count; i++) { switch (i) { case 0: // we found the model zip data // write zip file to disk List <byte> bitey = new List <byte>(); string imgString = jsonData[i].ToString(); imgString = imgString.StripToHex(); for (int j = 0; j < imgString.Length; j += 2) { char[] charArr = { imgString[j], imgString[j + 1] }; bitey.Add(Convert.ToByte(new string(charArr), 16)); } if (File.Exists("Models\\temp.zip")) { // delete any previous zip folder File.Delete("Models\\temp.zip"); } File.WriteAllBytes("Models\\temp.zip", bitey.ToArray()); // unzip to the Models\\frag.model_id folder DecompressToDirectory("Models\\temp.zip", "Models\\" + frag.model_id); // create meta file lines[0] = frag.model_rotation; lines[1] = frag.model_location; break; case 1: // it's the offset lines[2] = jsonData[i].ToString(); break; case 2: // it's the scaling quat lines[3] = jsonData[i].ToString(); break; default: break; } } File.WriteAllLines("Models\\" + frag.model_id + "\\meta.txt", lines); } } // end of getting missing model data string[] folders = Directory.GetDirectories("Models"); List <string> fragIDS = new List <string>(); foreach (ModelFrag frag in frags) { fragIDS.Add(frag.model_id.ToString()); } List <string> folderID = new List <string>(); foreach (string folder in folders) { folderID.Add(folder.Split('\\')[1]); } List <string> PathToDelete = folderID.Except(fragIDS).ToList(); foreach (string path in PathToDelete) { EmptyFolder(path); //Directory.Delete(path); } foreach (string f in folders) { string[] files = Directory.GetFiles(f); if (files.Length == 0) { Directory.Delete(f); } } // ToDo // Use frags (List<frag>) to check if any folder inside "Models\\" exist that don't exist in frags list // FOreach folder that exists that isn't in the list of frags, use EmptyFolder method (in objectLoader.cs in Model tool) // then delete that directory using Directory.Delete("Models\\" + id); // time to load all models // get all models into a list to return List <ModelHandle> models = new List <ModelHandle>(); string[] modelFolders = Directory.GetDirectories("Models"); // loop through all files in each directory to find the obj and metadata files foreach (string fold in modelFolders) { ModelHandle newModel = new ModelHandle(); string[] files = Directory.GetFiles(fold); foreach (string fileStr in files) { if (fileStr.EndsWith(".obj")) { // found the obj file, make the object //newModel.GameObj = new OBJLoader().Load(fileStr); newModel.FilePath = fileStr; } if (fileStr.EndsWith("meta.txt")) { // found the metadata file // parse it and save to object string[] metaLines = File.ReadAllLines(fileStr); newModel.Rotation = JsonUtility.FromJson <Quaternion>(Destringify(metaLines[0])); newModel.Position = ParsePointStr(Destringify(metaLines[1])); newModel.Offset = float.Parse(Destringify(metaLines[2])); newModel.Scale = JsonUtility.FromJson <Vector3>(Destringify(metaLines[3])); } } models.Add(newModel); } // return the list of all model handles (they still need to have their orientation data applied to them) return(models); }