0
\$\begingroup\$

I'm new to 3D and trying to create grass simulation using decals in libGDX.

I'm following the logic outlined here.

Atm my grass decal is a billboard of grass stems with a transparent background. I've placed 3 decals together in the form of a triangle.

When I pan the camera around at times the decals blend together but most of the time you can see the outline of the decal and you can distinguish between the pngs.

Can someone please help me understand a way to blend my decals better?

The decal: http://www.reinerstilesets.de/3dtextures/billboardgrass0002.png

enter image description here

enter image description here

The code:

public class Grass implements Screen {
    private FirstPersonCameraController camera_control;
    private PerspectiveCamera camera;

    private Mesh floor_mesh;
    private Vector<Texture> grass;

    private Vector<Decal> decals;
    private DecalBatch decalBatch;
    private Vector<TextureRegion> grass_regions;

    private ShaderProgram shaderProgram;
    private Stage stage;

    @Override
    public void show() {
        stage = new Stage();
        decals = new Vector<Decal>();
        grass = new Vector<Texture>();
        grass_regions = new Vector<TextureRegion>();

        //camera
        camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.near = 0.01f;
        camera.far = 10f;
        camera.position.set(1, 2, 5);
        camera.update();
        camera_control = new FirstPersonCameraController(camera);
        camera_control.setVelocity(3);
        camera_control.setDegreesPerPixel(.25f);
        Gdx.input.setInputProcessor(camera_control);

        //grass
        grass.add(new Texture("billboardgrass0002.png"));

        for(Texture t : grass){
            grass_regions.add(new TextureRegion(t));
        }

        decalBatch = new DecalBatch(new CameraGroupStrategy(camera));
        for(int i = 0; i < 3; i++){
            decals.add(Decal.newDecal(0.75f, 1.25f, grass_regions.get(0), true));
        }

        //set grass
        decals.get(0).setPosition(1f, 0, 1f);
        decals.get(1).setPosition(1.1f, 0, 1.1f);
        decals.get(1).setRotationY(45);
        decals.get(2).setPosition(.8f, 0, 1.1f);
        decals.get(2).setRotationY(-45);
    }

    @Override
    public void render(float delta) {
        //gl
        Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        //Gdx.gl20.glClearColor(1, 1, 1, 1);
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
        Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
        Gdx.gl20.glEnable(GL20.GL_BLEND);
        Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl20.glCullFace(GL20.GL_NONE);

        //camera
        camera_control.update();

        //disable depth writing
        Gdx.gl20.glDepthMask(false);

        //draw grass decals
        for(Decal d : decals){
            decalBatch.add(d);
        }
        decalBatch.flush();

        //stage
        stage.getViewport().update(1280, 720, true);
        stage.act(delta);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {}

    @Override
    public void hide() {}

    @Override
    public void pause() {}

    @Override
    public void resume() {}

    @Override
    public void dispose() {
        shaderProgram.dispose();
        decalBatch.dispose();
        decals.clear();

        for(Texture t : grass){
            t.dispose();
        }

        grass.clear();
        floor_mesh.dispose();
    }
}

Edit: disabled depth buffer writing before drawing decals, now looks better:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

The transparent pixels can still hide objects behind them. You need to disable depth write when rendering anything transparent:

Gdx.gl20.glDepthMask(false);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.