/** * Loads one or more meshes at the specified resource path with * the textures at the specified texture path with the specified flags. * * @param resourcePath - Path for the mesh resource to load. * @param texturesDir - Path for the textures to use for the mesh. * @param flags - Flags for the Assimp importer to use. * * @return Mesh array with the loaded meshes. * * @throws Exception */ public static Mesh[] load(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = Assimp.aiImportFile(resourcePath, flags); if (aiScene == null) { throw new Exception("Error loading model"); } int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMaterial(aiMaterial, materials, texturesDir); } int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); Mesh[] meshes = new Mesh[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); Mesh mesh = processMesh(aiMesh, materials); meshes[i] = mesh; } return meshes; }
private static Material processMaterial(AIMaterial aiMaterial, String texturesDir) { AIString path = AIString.calloc(); Assimp.aiGetMaterialTexture(aiMaterial, Assimp.aiTextureType_DIFFUSE, 0, path, (IntBuffer) null, null, null, null, null, null); String textPath = path.dataString(); Texture diffuseTexture = null; if (textPath != null && textPath.length() > 0) { diffuseTexture = new Texture(); diffuseTexture.setPath(texturesDir + "/" + textPath); } AIColor4D color = AIColor4D.create(); Vec3f diffuseColor = null; int result = Assimp.aiGetMaterialColor(aiMaterial, Assimp.AI_MATKEY_COLOR_AMBIENT, Assimp.aiTextureType_NONE, 0, color); if (result == 0) { diffuseColor = new Vec3f(color.r(), color.g(), color.b()); } Material material = new Material(); material.setDiffusemap(diffuseTexture); material.setColor(diffuseColor); return material; }
public static List<Model> loadModel(String path, String file) { List<Model> models = new ArrayList<>(); List<Material> materials = new ArrayList<>(); path = AssimpStaticModelLoader.class.getClassLoader().getResource(path).getPath().toString(); if (path.startsWith("/")) path = path.substring(1); AIScene aiScene = Assimp.aiImportFile(path + "/" + file, 0); if (aiScene.mMaterials() != null){ for (int i=0; i<aiScene.mNumMaterials(); i++){ AIMaterial aiMaterial = AIMaterial.create(aiScene.mMaterials().get(i)); Material material = processMaterial(aiMaterial, path); materials.add(material); } } for (int i=0; i<aiScene.mNumMeshes(); i++){ AIMesh aiMesh = AIMesh.create(aiScene.mMeshes().get(i)); Mesh mesh = processMesh(aiMesh); Model model = new Model(); model.setMesh(mesh); int materialIndex = aiMesh.mMaterialIndex(); model.setMaterial(materials.get(materialIndex)); models.add(model); } return models; }
/** * Loads one or more meshes at the specified resource path with * the textures at the specified texture path. * * @param resourcePath - Path for the mesh resource to load. * @param texturePath - Path for the textures to use for the mesh. * * @return Mesh array with the loaded meshes. * * @throws Exception */ public static Mesh[] load(String resourcePath, String texturePath) throws Exception { return load(resourcePath, texturePath, Assimp.aiProcess_JoinIdenticalVertices | Assimp.aiProcess_Triangulate | Assimp.aiProcess_FixInfacingNormals); }