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

查看:30
本文介绍了如何在 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,然后添加到 StageActor 渲染模型批次和 3D 实例在它的 draw() 方法中.

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.

简单游戏类

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天全站免登陆