// Load all object prefabs from an absolute path public void LoadNavObjPrefabs(List <NavObj> m_objList, string path) { int currObjInstanceNum = 0; // The instance number for current loaded object foreach (string objCat in Enum.GetNames(typeof(NavObj.ObjCategory))) { foreach (string filePath in Directory.GetFiles(path + objCat + "/", "*.prefab")) { string shortPath = filePath.Replace(Application.dataPath, "Assets"); Object objLoaded = AssetDatabase.LoadAssetAtPath(shortPath, typeof(GameObject)); // Add the loaded object prefab to the list NavObj navObj = new NavObj(objLoaded, Enum.GetNames(typeof(NavObj.ObjCategory)).ToList().IndexOf(objCat)); navObj.objInstanceNum = currObjInstanceNum++; m_objList.Add(navObj); } } // Debug.Log($"The number of object instances is {currObjInstanceNum}"); }
void Start() { Random rnd = new Random(); int[] anglesList = Enumerable.Range(0, 4).ToArray(); anglesList = anglesList.ToList().OrderBy(x => rnd.Next()).ToArray(); Array.Copy(anglesList, 0, trainAngles, 0, 2); Array.Copy(anglesList, 2, testAngles, 0, 2); foreach (string objCat in Enum.GetNames(typeof(NavObj.ObjCategory))) { foreach (string filePath in Directory.GetFiles(Application.dataPath + "/Resources/NavObj/" + objCat + "/", "*.prefab")) { string shortPath = filePath.Replace(Application.dataPath, "Assets"); Object objLoaded = AssetDatabase.LoadAssetAtPath(shortPath, typeof(GameObject)); // Add the loaded object prefab to the list NavObj navObj = new NavObj(objLoaded, Enum.GetNames(typeof(NavObj.ObjCategory)).ToList().IndexOf(objCat)); navObjects.Add(navObj); } } }
public void GenerateOneImage() { NavObj navObj = new NavObj(objPrefab, Enum.GetNames(typeof(NavObj.ObjCategory)).ToList().IndexOf(objCat.ToString())); CaptureNavObjImage(navObj); }
private void CaptureNavObjImage(NavObj navObj) { // Initiate the gameobject Transform locReceptable = GameObject.Find($"PosReceptacles/{navObj.objCat.ToString()}").transform; GameObject objInst = GameObject.Instantiate(navObj.objInstance, locReceptable) as GameObject; objInst.transform.localPosition = Vector3.zero; objInst.transform.localRotation = Quaternion.identity; // Initialized camera used for image capturing GameObject cameraObj = Instantiate(targetCamera, locReceptable) as GameObject; currentCamera = cameraObj.transform; // Select location for (int h = 0; h < locReceptable.childCount; h++) { objInst.transform.SetParent(locReceptable.GetChild(h), false); cameraObj.transform.SetParent(locReceptable.GetChild(h), false); objInst.transform.localRotation = Quaternion.Euler(0f, UnityEngine.Random.Range(-180f, 180f), 0f); // Get current object bounds Bounds targetObjBounds = new Bounds(Vector3.zero, Vector3.zero); // Debug.Log(targetObjBounds.extents); // Find target object Bounds foreach (Collider col in objInst.GetComponentsInChildren <Collider>()) { // Debug.Log(col.bounds.extents); targetObjBounds.Encapsulate(col.bounds); } // Debug.Log(targetObjBounds.extents); // Select room for (int k = 0; k < 7; k++) { for (int m = 0; m < 7; m++) { roomRootT.GetChild(m).gameObject.SetActive(m == k? true:false); } // Select height for (int i = 0; i < 1; i++) { currentCamera.position = targetObjBounds.center; // Select angle for (int j = 0; j < 4; j++) { currentCamera.localRotation = Quaternion.Euler(0f, -90f * (j + 1), 0f); float r = Mathf.Max(targetObjBounds.extents.x, targetObjBounds.extents.z); currentCamera.localPosition = new Vector3(5f * r * Mathf.Cos(90f * j * Mathf.Deg2Rad), (i + 1.25f) * targetObjBounds.extents.y, 5f * r * Mathf.Sin(90f * j * Mathf.Deg2Rad)); // currentCamera.Rotate(currentCamera.right, 10f*i); if (trainAngles.Contains(j)) { Directory.CreateDirectory($"{trainImageSavePath}{navObj.objType.ToString()}"); string path = $"{trainImageSavePath}{navObj.objType.ToString()}/{navObj.objName}_{k}_{h}_{i}_{j}.png"; // fiel save path CaptureCameraView(currentCamera.GetComponent <Camera>(), path); } else if (testAngles.Contains(j)) { Directory.CreateDirectory($"{testImageSavePath}{navObj.objType.ToString()}"); string path = $"{testImageSavePath}{navObj.objType.ToString()}/{navObj.objName}_{k}_{h}_{i}_{j}.png"; // fiel save path CaptureCameraView(currentCamera.GetComponent <Camera>(), path); } } } } } DestroyImmediate(objInst); DestroyImmediate(cameraObj); }