jMonkeyEngine / jmonkeyengine

A complete 3-D game development suite written in Java.
http://jmonkeyengine.org
BSD 3-Clause "New" or "Revised" License
3.74k stars 1.12k forks source link

GLTF 2.0 loading #2245

Closed jseinturier closed 2 months ago

jseinturier commented 2 months ago

Hello,

I'm trying to load and display a model that is stored as GLB file within JMonkey.

Here is my code:

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.input.ChaseCamera;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.SpotLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

public class GLBAnimatedModel extends SimpleApplication {

    @Override
    public void simpleInitApp() {
        DirectionalLight sun = new DirectionalLight();
            sun.setDirection(new Vector3f(0,0,0).normalizeLocal());
            sun.setColor(ColorRGBA.White);
            rootNode.addLight(sun);

            AmbientLight al = new AmbientLight();
            al.setColor(ColorRGBA.White.mult(1.3f));
            rootNode.addLight(al);

            SpotLight sl = new SpotLight(new Vector3f(0.0f, 0.0f, 0.0f), Vector3f.UNIT_Y);
        sl.setColor(ColorRGBA.White.mult(1.3f));
        rootNode.addLight(sl);

        // Load the GLB model
        assetManager.registerLocator("data", FileLocator.class);
        Spatial scene = assetManager.loadModel("HoverRocket.glb");

        rootNode.attachChild(scene);

        flyCam.setEnabled(false);
        ChaseCamera chaseCam = new ChaseCamera(cam, scene, inputManager);

        this.getViewPort().setBackgroundColor(ColorRGBA.LightGray);
    }

    /**
     * The main method.
     * @param args the main method arguments
     */
    public static void main(String[] args) {
        GLBAnimatedModel app = new GLBAnimatedModel();
        app.start();
    }
}

The GLB file (HoverRocket.glb) is accessible by downloading the GLB export from SketchFab here: https://sketchfab.com/3d-models/hover-bike-the-rocket-8b2e5bfca78e41c791b4e5b5e8c04512.

Options that i've used for download are: .glb 4MB Texture size: 1k

When i run the model, only some textures are visible, other ones are just black.

I think that there is a problem within the GLTF loader (or maybe a miss use from myself.

The GLB fine is fine when imported within Blender (all animations and all textures availables).

I also made a validation on the GLB file, using Kronos Tool and the result shows error within animation but not for models or textures:

 {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/0/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/1/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/2/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/3/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/4/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/5/output"
            },
            {
                "code": "ANIMATION_SAMPLER_ACCESSOR_WITH_BYTESTRIDE",
                "message": "bufferView.byteStride must not be defined for buffer views used by animation sampler accessors.",
                "severity": 0,
                "pointer": "/animations/0/samplers/6/output"
            },
            {
                "code": "NODE_EMPTY",
                "message": "Empty node encountered.",
                "severity": 2,
                "pointer": "/nodes/5"
            },
            {
                "code": "UNUSED_OBJECT",
                "message": "This object may be unused.",
                "severity": 2,
                "pointer": "/skins/0"
            }
stephengold commented 2 months ago

@jseinturier In order for the model's PBR materials to render property, the scene needs a suitable LightProbe.

jseinturier commented 2 months ago

Thanks a lot for the quick Answer. I have not been able to find clear examples or documentation regarding the use of LightProbe within JMonkey. I will search a little more.

pspeed42 commented 2 months ago

For tech support questions, you are likely to get deeper/more thorough help on the forum. Also numerous examples of light probe use, both generated and pre-calculated.

jseinturier commented 2 months ago

I've found informations about LightProbe within the jme3-examples package. Following code:

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.FileLocator;
import com.jme3.environment.EnvironmentCamera;
import com.jme3.environment.LightProbeFactory;
import com.jme3.input.ChaseCamera;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.LightProbe;
import com.jme3.light.SpotLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

public class GLBAnimatedModel extends SimpleApplication {

    private int frame = 0;

    @Override
    public void simpleInitApp() {
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(0,0,0).normalizeLocal());
        sun.setColor(ColorRGBA.White);
        rootNode.addLight(sun);

        AmbientLight al = new AmbientLight();
        al.setColor(ColorRGBA.White.mult(1.3f));
        rootNode.addLight(al);

        SpotLight sl = new SpotLight(new Vector3f(0.0f, 0.0f, 0.0f), Vector3f.UNIT_Y);
        sl.setColor(ColorRGBA.White.mult(1.3f));
        rootNode.addLight(sl);

        // Load the GLB model
        assetManager.registerLocator("data", FileLocator.class);
        Spatial scene = assetManager.loadModel("HoverRocket.glb");

        rootNode.attachChild(scene);

        // PBR - Environment camera initialization
        final EnvironmentCamera envCam = new EnvironmentCamera(256, new Vector3f(0.0f, 0.0f, 0.0f));
        stateManager.attach(envCam);

        flyCam.setEnabled(false);
        ChaseCamera chaseCam = new ChaseCamera(cam, scene, inputManager);

        this.getViewPort().setBackgroundColor(ColorRGBA.LightGray);
    }

    @Override
    public void simpleUpdate(float tpf) {

        frame++;

        // PBR - LightProbe initialization
        // Rendering need to be effective before adding LightProbe
        if (frame == 2) {
            final LightProbe probe = LightProbeFactory.makeProbe(stateManager.getState(EnvironmentCamera.class), rootNode);

            probe.getArea().setRadius(100);
            rootNode.addLight(probe);
        }

    }

    /**
     * The main method.
     * @param args the main method arguments
     */
    public static void main(String[] args) {
        GLBAnimatedModel app = new GLBAnimatedModel();
        app.start();
    }
}

works fine and display the model with all its textures.

Thanks to all of you..