【问题标题】:LibGDX Drawing a 2D sprite into a 3D environmentLibGDX 将 2D 精灵绘制到 3D 环境中
【发布时间】:2021-11-19 06:10:38
【问题描述】:

我正在尝试将 2d 精灵绘制到 3d 环境中,以尝试获得一个 delver 风格的游戏。因为这是一个项目,所以我只使用 LibGDX 而没有游戏引擎。如果有人知道任何好的指南或至少为我指出正确的方向,我们将不胜感激。

【问题讨论】:

  • 使用贴花。来自 LibGDX wiki “贴花基本上是可以在 3D 空间中操作和渲染的 Sprite。”在此处查看更多信息以及如何使用它们:github.com/libgdx/libgdx/wiki/Decals

标签: java 3d libgdx draw


【解决方案1】:

幸运的是,Delver 的源代码是公开的,供您查看,因此您可以了解 Delver 是如何做到的。我相信他们确实使用了贴花,可能会为他们扩展更多功能。贴花绝对是您想要的。

以下是 LibGDX 上 Decals wiki 中的一些示例

实例化: Decal decal = Decal.newDecal(textureRegion, true);

看着相机(所以它总是面对它) decal.lookAt(camera.position, camera.up);

DecalBatch 用于渲染贴花。

查看Decals 上的 LibGDX wiki 以获取完整细分。

来自 LibGDX tests 的完整示例:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g3d.decals.CameraGroupStrategy;
import com.badlogic.gdx.graphics.g3d.decals.Decal;
import com.badlogic.gdx.graphics.g3d.decals.DecalBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.utils.PerspectiveCamController;
import com.badlogic.gdx.utils.Array;

public class SimpleDecalTest extends GdxTest {
    private static final int NUM_DECALS = 3;
    DecalBatch batch;
    Array<Decal> decals = new Array<Decal>();
    PerspectiveCamera camera;
    PerspectiveCamController controller;
    FPSLogger logger = new FPSLogger();

    public void create () {
        float width = Gdx.graphics.getWidth();
        float height = Gdx.graphics.getHeight();

        camera = new PerspectiveCamera(45, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.near = 1;
        camera.far = 300;
        camera.position.set(0, 0, 5);
        controller = new PerspectiveCamController(camera);

        Gdx.input.setInputProcessor(controller);
        batch = new DecalBatch(new CameraGroupStrategy(camera));

        TextureRegion[] textures = {new TextureRegion(new Texture(Gdx.files.internal("data/egg.png"))),
            new TextureRegion(new Texture(Gdx.files.internal("data/wheel.png"))),
            new TextureRegion(new Texture(Gdx.files.internal("data/badlogic.jpg")))};

        Decal decal = Decal.newDecal(1, 1, textures[1]);
        decal.setPosition(0, 0, 0);
        decals.add(decal);

        decal = Decal.newDecal(1, 1, textures[0], true);
        decal.setPosition(0.5f, 0.5f, 1);
        decals.add(decal);

        decal = Decal.newDecal(1, 1, textures[0], true);
        decal.setPosition(1, 1, -1);
        decals.add(decal);

        decal = Decal.newDecal(1, 1, textures[2]);
        decal.setPosition(1.5f, 1.5f, -2);
        decals.add(decal);

        decal = Decal.newDecal(1, 1, textures[1]);
        decal.setPosition(2, 2, -1.5f);
        decals.add(decal);
    }

    Vector3 dir = new Vector3();
    private boolean billboard = true;

    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);

        camera.update();
        for (int i = 0; i < decals.size; i++) {
            Decal decal = decals.get(i);
            if (billboard) {
                // billboarding for ortho cam :)
// dir.set(-camera.direction.x, -camera.direction.y, -camera.direction.z);
// decal.setRotation(dir, Vector3.Y);

                // billboarding for perspective cam
                decal.lookAt(camera.position, camera.up);
            }
            batch.add(decal);
        }
        batch.flush();
        logger.log();
    }

    @Override
    public void dispose () {
        batch.dispose();
    }
}

另外,如果有兴趣,Delvers 源代码:https://github.com/Interrupt/delverengine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多