Search in sources :

Example 1 with AiMesh

use of assimp.AiMesh in project chunkstories by Hugobros3.

the class AssimpMeshLoader method load.

public Mesh load(Asset mainAsset) throws MeshLoadException {
    Importer im = new Importer();
    assimp.SettingsKt.setASSIMP_LOAD_TEXTURES(false);
    im.setIoHandler(new AssetIOSystem(store.parent()));
    AiScene scene = im.readFile(mainAsset.getName(), im.getIoHandler(), 0);
    /*System.out.println(scene);
		
		for(AiMesh mesh : scene.getMeshes()) {
			System.out.println(mesh.getName());
			System.out.println(mesh.getFaces().size());
			
			AiMaterial material = scene.getMaterials().get(mesh.getMaterialIndex());
			System.out.println("mat: "+material.getName());
			System.out.println(material.getTextures());
			
			System.out.println("bones: "+mesh.getNumBones());
			for(AiBone bone : mesh.getBones()) {
				System.out.println(bone.getName().substring(bone.getName().lastIndexOf('_') + 1));
				System.out.println(bone.getNumWeights());
				System.out.println(tomat4(bone.getOffsetMatrix()));
			}
		}*/
    if (scene == null) {
        logger.error("Could not load meshes from asset: " + mainAsset);
        return null;
    }
    if (scene.getMeshes() == null || scene.getMeshes().size() == 0) {
        logger.error("Loaded mesh did not contain any mesh data.");
        return null;
    }
    FloatArrayList vertices = new FloatArrayList();
    FloatArrayList normals = new FloatArrayList();
    FloatArrayList texcoords = new FloatArrayList();
    Map<String, Integer> boneNames = new HashMap<>();
    ByteArrayList boneIds = new ByteArrayList();
    ByteArrayList boneWeights = new ByteArrayList();
    boolean has_bones = scene.getMeshes().get(0).getHasBones();
    Map<Integer, VertexBoneWeights> boneWeightsForeachVertex = null;
    if (has_bones)
        boneWeightsForeachVertex = new HashMap<>();
    for (AiMesh mesh : scene.getMeshes()) {
        int existing_vertices = vertices.size() / 3;
        if (has_bones) {
            for (int i = 0; i < mesh.getNumVertices(); i++) {
                boneWeightsForeachVertex.put(i + existing_vertices, new VertexBoneWeights());
            }
            for (AiBone bone : mesh.getBones()) {
                String boneName = bone.getName().substring(bone.getName().lastIndexOf('_') + 1);
                int boneId = boneNames.getOrDefault(boneName, -1);
                if (boneId == -1) {
                    boneId = boneNames.size();
                    boneNames.put(boneName, boneId);
                }
                for (AiVertexWeight weight : bone.getWeights()) {
                    int vid = existing_vertices + weight.getVertexId();
                    VertexBoneWeights vw = boneWeightsForeachVertex.get(vid);
                    vw.bones[vw.slot] = boneId;
                    vw.weights[vw.slot] = weight.getWeight();
                    vw.slot++;
                    vw.totalWeight += weight.getWeight();
                    if (vw.totalWeight > 1.0f) {
                        logger.warn("Total weight > 1 for vertex #" + vid);
                    }
                    if (vw.slot >= 4) {
                        logger.error("More than 4 bones weighted against vertex #" + vid);
                        return null;
                    }
                }
            }
        }
        for (List<Integer> face : mesh.getFaces()) {
            if (face.size() == 3) {
                for (int i = 0; i < 3; i++) {
                    Vec3 vertex = mesh.getVertices().get(face.get(i));
                    Vec3 normal = mesh.getNormals().get(face.get(i));
                    float[] texcoord = mesh.getTextureCoords().get(0).get(face.get(i));
                    vertices.add(vertex.x, vertex.y, vertex.z);
                    normals.add(normal.x, normal.y, normal.z);
                    texcoords.add(texcoord[0], 1.0f - texcoord[1]);
                    if (has_bones) {
                        VertexBoneWeights boned = boneWeightsForeachVertex.get(existing_vertices + face.get(i));
                        boneIds.add((byte) boned.bones[0]);
                        boneIds.add((byte) boned.bones[1]);
                        boneIds.add((byte) boned.bones[2]);
                        boneIds.add((byte) boned.bones[3]);
                        boneWeights.add((byte) (boned.weights[0] * 255));
                        boneWeights.add((byte) (boned.weights[1] * 255));
                        boneWeights.add((byte) (boned.weights[2] * 255));
                        boneWeights.add((byte) (boned.weights[3] * 255));
                    }
                }
            } else
                logger.warn("Should triangulate! (face=" + face.size() + ")");
        }
    }
    FloatBuffer verticesBuffer = toFloatBuffer(vertices);
    FloatBuffer normalsBuffer = toFloatBuffer(normals);
    FloatBuffer texcoordsBuffer = toFloatBuffer(texcoords);
    ByteBuffer boneIdsBuffer = toByteBuffer(boneIds);
    ByteBuffer boneWeightsBuffer = toByteBuffer(boneWeights);
    String[] boneNamesArray = new String[boneNames.size()];
    for (Entry<String, Integer> e : boneNames.entrySet()) {
        boneNamesArray[e.getValue()] = e.getKey();
    }
    if (has_bones)
        return new AnimatableMesh(verticesBuffer, texcoordsBuffer, normalsBuffer, boneNamesArray, boneIdsBuffer, boneWeightsBuffer);
    else
        return new Mesh(verticesBuffer, texcoordsBuffer, normalsBuffer);
}
Also used : AiVertexWeight(assimp.AiVertexWeight) AiMesh(assimp.AiMesh) HashMap(java.util.HashMap) AiScene(assimp.AiScene) FloatBuffer(java.nio.FloatBuffer) ByteArrayList(com.carrotsearch.hppc.ByteArrayList) Vec3(glm_.vec3.Vec3) FloatArrayList(com.carrotsearch.hppc.FloatArrayList) Importer(assimp.Importer) AnimatableMesh(io.xol.chunkstories.api.mesh.AnimatableMesh) AnimatableMesh(io.xol.chunkstories.api.mesh.AnimatableMesh) Mesh(io.xol.chunkstories.api.mesh.Mesh) AiMesh(assimp.AiMesh) ByteBuffer(java.nio.ByteBuffer) AiBone(assimp.AiBone)

Aggregations

AiBone (assimp.AiBone)1 AiMesh (assimp.AiMesh)1 AiScene (assimp.AiScene)1 AiVertexWeight (assimp.AiVertexWeight)1 Importer (assimp.Importer)1 ByteArrayList (com.carrotsearch.hppc.ByteArrayList)1 FloatArrayList (com.carrotsearch.hppc.FloatArrayList)1 Vec3 (glm_.vec3.Vec3)1 AnimatableMesh (io.xol.chunkstories.api.mesh.AnimatableMesh)1 Mesh (io.xol.chunkstories.api.mesh.Mesh)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 HashMap (java.util.HashMap)1