public static HittableGeometryComponent ReadObj(FileInfo file) { var reader = new OBJReader(); var builder = new SimpleMeshBuilder(); using (var text = new StreamReader(File.OpenRead(file.FullName))) { var result = reader.Read(text, ReadOptions.Defaults, builder); } //var reader = new StandardMeshReader(); //var res = reader.Read(file.FullName, ReadOptions.Defaults); //StandardMeshReader.ReadMesh() var mesh = builder.Meshes[0]; return(new HittableGeometryComponent { Indices = mesh.GetTriangleArray().ToImmutableArray(), Normals = mesh.NormalsItr().Select(n => new Vector3(n.x, n.y, n.z)).ToImmutableArray(), Positions = mesh.VerticesItr().Select(n => new Vector3((float)n.x, (float)n.y, (float)n.z)).ToImmutableArray(), }); }
private async Task <SimpleMeshBuilder> loadObj(string filename) { using (StreamReader reader = File.OpenText(filename)) { OBJReader objReader = new OBJReader(); SimpleMeshBuilder meshBuilder = new SimpleMeshBuilder(); try { IOReadResult result = objReader.Read(reader, new ReadOptions(), meshBuilder); } catch (Exception e) when( e is UnauthorizedAccessException || e is DirectoryNotFoundException || e is FileNotFoundException || e is NotSupportedException ) { Debug.LogError("Failed to Load" + filename + " : " + e.ToString()); meshBuilder = new SimpleMeshBuilder(); } return(meshBuilder); } }
// TODO: handle missing files public static DataType Load <DataType>(string filePath, ResourceLifespan lifespan = ResourceLifespan.GLOBAL, bool absolutePath = false) { string normalizedPath = IOUtil.NormalizePath(absolutePath ? filePath : IOUtil.PathCombine(Application.streamingAssetsPath, filePath)); // if the cache contains the resource, return it if (_resources.ContainsKey(normalizedPath)) { return(((GenericResource <DataType>)_resources[normalizedPath]).Resource); } string fileExt = Path.GetExtension(normalizedPath); bool fileNotFound = false; switch (fileExt) { case ".png": { GenericResource <Texture2D> resource = new GenericResource <Texture2D>(); resource.Resource = IOUtil.LoadPNG(normalizedPath); if (resource.Resource == null) { fileNotFound = true; break; } resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".json": { GenericResource <JSONClass> resource = new GenericResource <JSONClass>(); resource.Resource = IOUtil.ReadJSONFromDisk(normalizedPath); if (resource.Resource == null) { fileNotFound = true; break; } resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".map": { GenericResource <GameObject> resource = new GenericResource <GameObject>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } resource.Resource = MapReader.LoadMap(normalizedPath, IOUtil.PathCombine(Application.streamingAssetsPath, "textures", "wad"), Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/DiffuseSetNoAffine" : "LSD/DiffuseSet"), Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/TransparentSetNoAffine" : "LSD/TransparentSet")); resource.Resource.transform.SetParent(_resourceManagerGameObject.transform); resource.Resource.SetActive(false); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".tmap": { GenericResource <TMAP> resource = new GenericResource <TMAP>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } resource.Resource = ToriiMapReader.ReadFromFile(normalizedPath); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".tobj": { GenericResource <GameObject> resource = new GenericResource <GameObject>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } TOBJ tobj = new TOBJ(); ToriiObjectReader.Read(normalizedPath, ref tobj); resource.Resource = OBJReader.ReadOBJString(tobj.ObjectFile); resource.Resource.AddComponent <ToriiObject>(); ToriiObject toriiObjScript = resource.Resource.GetComponent <ToriiObject>(); toriiObjScript.ToriiObj = tobj; resource.Resource.transform.SetParent(_resourceManagerGameObject.transform); resource.Resource.SetActive(false); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } default: { throw new ResourceLoadException("Did not recognize file type: " + normalizedPath); } } if (fileNotFound) { throw new ResourceLoadException("Could not find resource!"); } return(((GenericResource <DataType>)_resources[normalizedPath]).Resource); }