lwjglgamedev / lwjglbook-leg

Source code of the chapters of the book 3D Game Development with LWJGL 3
https://ahbejarano.gitbook.io/lwjglgamedev/
Apache License 2.0
560 stars 202 forks source link

Assimp importing problem #46

Closed mudlee closed 6 years ago

mudlee commented 6 years ago

I try to import obj files from MagicaVoxel and from Blender as well. Both have materials. You can check those here: https://drive.google.com/open?id=1E6g-rqI3QPTFfMnMi90CPjKzcxx9tyYp

What I see when importing from blender:

What I see when importing from MagicaVoxel:

However, both have one material and not that diffuse color. For fun, I tried to export the monkey to FBX, where the diffuse color looks OK.

What can be the problem? Code is the same you wrote.

lwjglgamedev commented 6 years ago

Sure, I'll try to help.

lwjglgamedev commented 6 years ago

Just one question, when you import it from blender it also says that diffuse is 0.6 ?

mudlee commented 6 years ago

Only if I use obj when exporting from blender or use MagicaVoxel. So three cases I tried:

All of them work in Unity.

Here is the code and the output for the blender-obj: https://gist.github.com/mudlee/25e734d77c1cb2542ce772e77eb3e18e

lwjglgamedev commented 6 years ago

Ok, here are my findings:

`mtllib

tower.mtl usemtl palette

g tower `

If I have more time I will check the wavefront material spec, but the code is ok.

mudlee commented 6 years ago

OK, it looks like OK, as I moved the mtl declaration before the model group. Also, I use the mMaterialIndex and works like the charm. I recommend to update with these infos with the book :) Oh, and one more. If you package your game into a jar, you will not be able to use aiImportFile as its not available in the jar. According to Spaasi from the lwjgl forum, you have to options:

I choose the second one, here is the code (not well polished):

` URL res = StaticMeshLoader.class.getResource(resourcePath);

    if (res.toString().startsWith("jar:")) {
        objPath = unpackModelsFromJar(resourcePath);
    }

WHERE

    private static String unpackModelsFromJar(String resourcePath) {
    String origName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1).replace(".obj", "");

    try {
        String tempDir = System.getProperty("java.io.tmpdir");
        LOGGER.debug("> Model found in a jar file, extracting files to a temp directory: {}", tempDir);

        // OBJ
        File objFile = new File(tempDir + origName + ".obj");
        objFile.deleteOnExit();
        objFile.createNewFile();

        InputStream objInput = StaticMeshLoader.class.getResourceAsStream(resourcePath);
        Files.write(objFile.toPath(), objInput.readAllBytes());
        LOGGER.debug("> File {} extracted", tempDir + origName + ".obj");

        // MTL
        File mtlFile = new File(tempDir + origName + ".mtl");
        mtlFile.deleteOnExit();
        mtlFile.createNewFile();

        InputStream mtlInput = StaticMeshLoader.class.getResourceAsStream(resourcePath.replace(".obj", ".mtl"));
        Files.write(mtlFile.toPath(), mtlInput.readAllBytes());
        LOGGER.debug("> File {} extracted", tempDir + origName + ".mtl");

        // MTL
        File textureFile = new File(tempDir + origName + ".png");
        textureFile.deleteOnExit();
        textureFile.createNewFile();

        InputStream textureInput = StaticMeshLoader.class.getResourceAsStream(resourcePath.replace(".obj", ".png"));
        Files.write(textureFile.toPath(), textureInput.readAllBytes());
        LOGGER.debug("> File {} extracted", tempDir + origName + ".png");

        return objFile.getPath();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

`