如何在Libgdx Actor中渲染3D对象? [英] How to render 3D object within a Libgdx Actor?

查看:97
本文介绍了如何在Libgdx Actor中渲染3D对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的大多数Libgdx教程都展示了如何在3D世界中添加2D元素,但是我想知道相反的方法,即在2D舞台中添加3D元素.

Most of the Libgdx tutorials I found show how to add 2D elements in a 3D world, but I would like to know how to the the opposite, adding 3D elements in a 2D Stage.

我尝试在Stage中添加背景图像,然后在Stage中添加Actor,以在其draw()方法中呈现模型批处理和3D实例.

I tried adding a background image to the Stage, then adding to the Stage an Actor that renders the model batch and the 3D instances in its draw() method.

但是,相反,不会绘制图像并且3D对象的一部分被隐藏了.

But instead, the image isn't drawn and part of the 3D object is hidden.

SimpleGame类

SimpleGame class

public class SimpleGame extends ApplicationAdapter {

    Stage stage;

    @Override
    public void create () {
        stage = new Stage();

        InputMultiplexer im = new InputMultiplexer(stage);
        Gdx.input.setInputProcessor( im );

        Image background = new Image(new Texture("badlogic.jpg"));
        background.setSize(stage.getWidth(), stage.getHeight());
        stage.addActor(background);

        setup();
    }

    private void setup() {
        SimpleActor3D group = new SimpleActor3D();
        group.setSize(stage.getWidth(), stage.getHeight());
        group.setPosition(0, 0);
        stage.addActor(group);
    }

    @Override
    public void render () {
        stage.act();

        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );

        stage.draw();
    }
}

SimpleActor3D类

SimpleActor3D class

public class SimpleActor3D extends Actor {

    public Environment environment;
    public PerspectiveCamera camera;

    public ModelBatch modelBatch;
    public ModelInstance boxInstance;

    public SimpleActor3D() {
        environment = SimpleUtils.createEnvironment();
        camera = SimpleUtils.createCamera();
        boxInstance = SimpleUtils.createModelInstance(Color.GREEN);

        modelBatch = new ModelBatch();
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Gdx.gl.glViewport((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());

        modelBatch.begin(camera);
        modelBatch.render( boxInstance, environment );
        modelBatch.end();

        super.draw(batch, parentAlpha);
    }
}

SimpleUtils类

SimpleUtils class

public class SimpleUtils {

    public static Environment createEnvironment() {
        Environment environment = new Environment();
        environment.set( new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f) );

        DirectionalLight dLight = new DirectionalLight();
        Color lightColor = new Color(0.75f, 0.75f, 0.75f, 1);
        Vector3 lightVector = new Vector3(-1.0f, -0.75f, -0.25f);
        dLight.set( lightColor, lightVector );
        environment.add( dLight ) ;

        return environment;
    }

    public static PerspectiveCamera createCamera() {
        PerspectiveCamera camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.set(10f, 10f, 10f);
        camera.lookAt(0,0,0);
        camera.near = 1f;
        camera.far  = 300f;
        camera.update();

        return camera;
    }

    public static ModelInstance createModelInstance(Color color) {
        ModelBuilder modelBuilder = new ModelBuilder();

        Material boxMaterial = new Material();
        boxMaterial.set( ColorAttribute.createDiffuse(color) );

        int usageCode = VertexAttributes.Usage.Position + VertexAttributes.Usage.ColorPacked + VertexAttributes.Usage.Normal;

        Model boxModel = modelBuilder.createBox( 5f, 5f, 5f, boxMaterial, usageCode );

        return new ModelInstance(boxModel);
    }
}

我想要什么:

我所拥有的是:

我尝试直接在ApplicationAdapter render()方法中渲染模型批处理,并且效果很好,因此问题一定在Stage的某个地方,但是我找不到方法.

I have tried rendering the model batch directly in the ApplicationAdapter render() method and it works perfectly, so the problems must lie somewhere with the Stage but I can't find how.

推荐答案

我遇到了同样的问题,但是我只需要渲染一次3d对象,所以我想到了将3d模型渲染为Sprite的想法.为此,我通过modelBatch将模型渲染到帧缓冲区对象而不是默认的屏幕缓冲区,然后从FBO颜色缓冲区创建了一个精灵.

I had the same problem but I needed to render 3d object only once so I came with an idea to render 3d model as a Sprite. In order to do that I rendered my model via modelBatch to frame buffer object instead of default screen buffer and then created a sprite from FBO color buffer.

下面的示例代码:

FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);

Sprite renderModel(ModelInstance modelInstance) {
    frameBuffer.begin(); //Capture rendering to frame buffer.

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling ? GL20.GL_COVERAGE_BUFFER_BIT_NV : 0))
    modelBatch.begin(camera);
    modelBatch.render(modelInstance);
    modelBatch.end();

    frameBuffer.end();

    return new Sprite(frameBuffer.getColorBufferTexture());
}

您始终可以使用sprite.setTexture();方法在渲染循环中更新精灵纹理.您还可以从纹理-> new Image(frameBuffer.getColorBufferTexture());创建图像,然后在Scene2d中使用它.

You can always update your sprite texture in a render loop with use of sprite.setTexture(); method. You can also create an Image from a texture -> new Image(frameBuffer.getColorBufferTexture()); and use it in Scene2d.

这篇关于如何在Libgdx Actor中渲染3D对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆