/** * 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; }
public static Mesh[] load(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = 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 Map<String, Animation> processAnimations(AIScene aiScene, List<Bone> boneList, Node rootNode, Matrix4f rootTransformation) { Map<String, Animation> animations = new HashMap<>(); // Process all animations int numAnimations = aiScene.mNumAnimations(); PointerBuffer aiAnimations = aiScene.mAnimations(); for (int i = 0; i < numAnimations; i++) { AIAnimation aiAnimation = AIAnimation.create(aiAnimations.get(i)); // Calculate transformation matrices for each node int numChanels = aiAnimation.mNumChannels(); PointerBuffer aiChannels = aiAnimation.mChannels(); for (int j = 0; j < numChanels; j++) { AINodeAnim aiNodeAnim = AINodeAnim.create(aiChannels.get(j)); String nodeName = aiNodeAnim.mNodeName().dataString(); Node node = rootNode.findByName(nodeName); buildTransFormationMatrices(aiNodeAnim, node); } List<AnimatedFrame> frames = buildAnimationFrames(boneList, rootNode, rootTransformation); Animation animation = new Animation(aiAnimation.mName().dataString(), frames, aiAnimation.mDuration()); animations.put(animation.getName(), animation); } return animations; }
/** * Loads a model file and a texture to a {@link TexturedModel}. This method requires flags that will determine how * how the mesh is handled. * * @param modelPath Path to the model * @param texturePath Path to the texture. If the model contains a material library, * the material library should point to the texture's folder. * @param flags Flags separated by the Binary OR operator, "|" * @return Array of {@link TexturedModel}s that were contained by the * model file. */ public static TexturedModel[] load(String modelPath, String texturePath, int flags) { AIScene aiScene = aiImportFile(modelPath, flags); if (aiScene == null) throw new RuntimeException("Could not load model: " + modelPath + "\nWith flags " + flags); int numMaterials = aiScene.mNumMaterials(); PointerBuffer aiMaterials = aiScene.mMaterials(); List<Material> materials = new ArrayList<Material>(); for (int i = 0; i < numMaterials; i++) { AIMaterial aiMaterial = AIMaterial.create(aiMaterials.get(i)); processMat(aiMaterial, materials, texturePath); } int numMeshes = aiScene.mNumMeshes(); PointerBuffer aiMeshes = aiScene.mMeshes(); TexturedModel[] meshes = new TexturedModel[numMeshes]; for (int i = 0; i < numMeshes; i++) { AIMesh aiMesh = AIMesh.create(aiMeshes.get(i)); TexturedModel mesh = processStaticMesh(aiMesh, materials); String name = modelPath.split("/")[modelPath.split("/").length - 1]; String tex = texturePath.split("/")[texturePath.split("/").length - 1]; DBObject obj = new DBObject(name, DBObjectType.MODEL); obj.addArray(DBArray.createFloatArray("vertices", mesh.getModelData().getVertices())); obj.addArray(DBArray.createFloatArray("textureCoords", mesh.getModelData().getTextureCoords())); obj.addArray(DBArray.createFloatArray("normals", mesh.getModelData().getNormals())); obj.addArray(DBArray.createIntegerArray("indices", mesh.getModelData().getIndices())); obj.addString(DBString.create("texture", tex)); AssetCache.db.addObject(obj); meshes[i] = mesh; } AssetCache.db.serialize("assets.lum"); return meshes; }
public static AnimGameItem loadAnimGameItem(String resourcePath, String texturesDir, int flags) throws Exception { AIScene aiScene = 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); } List<Bone> boneList = new ArrayList<>(); 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, boneList); meshes[i] = mesh; } AINode aiRootNode = aiScene.mRootNode(); Matrix4f rootTransfromation = AnimMeshesLoader.toMatrix(aiRootNode.mTransformation()); Node rootNode = processNodesHierarchy(aiRootNode, null); Map<String, Animation> animations = processAnimations(aiScene, boneList, rootNode, rootTransfromation); AnimGameItem item = new AnimGameItem(meshes, animations); return item; }
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; }