chelo_c chelo_c - 1 month ago 16
Java Question

libGDX 3D Shadows

Im using libGDX and this is a Desktop project.

I have 2 models, one is the character and the other is the map, as you can see in the image below:

enter image description here

My question is:

How do I project the shadow of the character on the floor?

As you can see the character doesn't have a shadow, thus the Ambience Light. What do I have to use or how do I achieve this? Should I fake a shadow or is there a real way to project shadows?

Any comments or sugestions are welcome.

Answer Source

You can use following code:

Environment environment;
DirectionalShadowLight shadowLight;

@Override
public void show() {
    modelBatch = new ModelBatch(); 
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1f, .6f, 1f));
    environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 60f, 60f, .1f, 50f))                  
                .set(1f, 1f, 1f, 40.0f, -35f, -35f));   
    environment.shadowMap = shadowLight; 

    shadowBatch = new ModelBatch(new DepthShaderProvider());
}

@Override
public void render(float delta) {
        //create shadow texture
        shadowLight.begin(Vector3.Zero, camera.direction);
        shadowBatch.begin(shadowLight.getCamera());

        shadowBatch.render(instances);

        shadowBatch.end();
        shadowLight.end();

        //render scene
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);


        modelBatch.begin(cam);      
        modelBatch.render(instances, environment);  //environment has shadowMap!
        modelBatch.end();
}