3D-модель Libgdx некорректно отображается при использовании AssetManager - PullRequest
0 голосов
/ 18 октября 2019

Я разработал приложение для Android, чтобы показать некоторые 3d модели. Часть 3D-моделей построена с использованием ModelBuilder, а часть из них экспортирована из Google Sketchup в формате fbx и преобразована в g3db для загрузки через AssetManager. Проблема заключается в использовании этих двух методов, загрузка моделей AssetManger была искажена. Но используя AssetManager для загрузки всех моделей, они отрисовываются просто отлично. Изображение слева - это загрузка моделей с использованием AssetManger, а справа - смесь с ModelBuilder, искаженная модель внизу.

enter image description here enter image description here

Это код, который я реализовал для загрузки 3D-моделей.

@Override
public void create () {
    modelBatch = new ModelBatch();
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(1f, 1f, 1f);
    cam.lookAt(0,0,0);
    cam.near = 1f;
    cam.far = 300f;
    cam.update();

    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);

    assets = new AssetManager();
    assets.load("data/cylindar.g3db", Model.class);
    loading = true;


}

private void doneLoading() {
    Model model1 = assets.get("data/cylindar.g3db", Model.class);
    ModelInstance modelInstance1 = new ModelInstance(model1); 
    modelInstance1.transform.setToTranslation(-5, -2, 1);
    instances.add(modelInstance1);

    Material material = new Material(ColorAttribute.createDiffuse(style.getLayerColor()));
    long attributes = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal;
    Model model2 = modelBuilder.createSphere(1, 1, 1, 48, 48, material, attributes);
    ModelInstance modelInstance2 = new ModelInstance(model2); 
    modelInstance2.transform.setToTranslation(-8, -2, -3);
    instances.add(modelInstance2);

    loading = false;
}

@Override
public void render () {
    if (loading && assets.update())
        doneLoading();
    camController.update();

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

    modelBatch.begin(cam);
    modelBatch.render(instances, environment);
    modelBatch.end();
}
...