首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:谷歌灯丝GLTF/GLB型号未显示

Android:谷歌灯丝GLTF/GLB型号未显示
EN

Stack Overflow用户
提问于 2021-01-21 05:46:25
回答 1查看 574关注 0票数 1

我的问题是,我试图让SurfaceView显示一个3D模型,它只是整个应用程序布局的一小部分。该布局包括用于显示其他数据的多个视图;但是,一旦我创建了ModelViewerSurfaceView就消失了。当我使用1.9.9版本的Filament时,布局是显示的,但是严重的像素化,似乎使其他布局元素消失。使用v1.7.0时,其他元素不会消失,但不会显示任何内容。

我在Gradle文件中使用以下版本:

代码语言:javascript
复制
implementation 'com.google.android.filament:filament-android:1.7.0'
implementation 'com.google.android.filament:filament-utils-android:1.7.0'
implementation 'com.google.android.filament:gltfio-android:1.7.0'

我有一个模型复制到assets文件夹,就像我在它们的Kotlin示例中看到的那样:https://github.com/google/filament/tree/main/android/samples/sample-gltf-viewer

这是我在加载从Blender导出的自定义GLTF 2.0模型时所做的Java尝试:

代码语言:javascript
复制
import android.content.Context;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.view.Choreographer;
import android.view.SurfaceView;

import com.google.android.filament.Engine;
import com.google.android.filament.utils.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class Model implements Choreographer.FrameCallback {

    static {
        Utils.INSTANCE.init();
    }

    private final String TAG = Model.class.getSimpleName();
    private Context context;
    private SurfaceView surfaceView;
    private ModelViewer modelViewer;
    private Engine engine;
    private Choreographer choreographer;
    private final String modelFilename = "models/modelTest.glb";

    @Override
    public void doFrame(long currentTime) {
        choreographer.postFrameCallback(this);
        modelViewer.render(currentTime);
    }

    public Model(Context context, ConstraintLayout layout) {

        this.context = context;
        choreographer = Choreographer.getInstance();

        surfaceView = new SurfaceView(context);
        layout.addView(surfaceView);
        engine = Engine.create();
        modelViewer = new ModelViewer(surfaceView, engine, null);
        loadModel(modelFilename);
        choreographer.postFrameCallback(this);
    }

    private void loadModel(String filepath) {
        try {
            InputStream buffer = context.getAssets().open(filepath);
            byte[] bytes = new byte[buffer.available()];
            buffer.read(bytes);
            ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
            modelViewer.loadModelGlb(byteBuffer);
            modelViewer.transformToUnitCube(new Float3(0,0,0));
        }
        catch(IOException e) {

        }
    }

}

编辑1:

这是使用任何1.9.4以上版本时显示的内容。编舞循环使静态部分看起来像电视上的静态噪声。红色部分似乎是模型的一小部分。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-29 03:30:11

不久前,我通过复制ModelViewer类并使用新的构造函数创建一个自定义ModelViewer3D类修复了这个问题,这是一个很好的解决方案,因为与通常使用给定的ModelViewer相比,它为您提供了更多对选项的控制。

需要将uiHelper isOpaque字段设置为false以解决上述问题,并且为了获得表面视图的透明背景,渲染器需要重置clear选项(我为其创建了一个单独的函数,如下所示)。

代码语言:javascript
复制
     constructor(surfaceView: SurfaceView, engine: Engine = Engine.create(), manipulator: Manipulator? = null, isOpaque: Boolean) : this(engine) {
        cameraManipulator = manipulator ?: Manipulator.Builder()
                .targetPosition(kDefaultObjectPosition.x, kDefaultObjectPosition.y, kDefaultObjectPosition.z)
                .viewport(surfaceView.width, surfaceView.height)
                .build(Manipulator.Mode.ORBIT)

        this.surfaceView = surfaceView
        gestureDetector = GestureDetector(surfaceView, cameraManipulator)
        displayHelper = DisplayHelper(surfaceView.context)
        uiHelper.isOpaque = isOpaque
        uiHelper.renderCallback = SurfaceCallback()
        uiHelper.attachTo(surfaceView)
        addDetachListener(surfaceView)
    }

    fun makeBackgroundTransparent() {
        val options = renderer.clearOptions
        options.clear = true
        renderer.clearOptions = options
    }

我最终也使用了这个版本的库,因为它是当时的最新版本:

代码语言:javascript
复制
    implementation 'com.google.android.filament:filament-android:1.9.9'
    implementation 'com.google.android.filament:filament-utils-android:1.9.9'
    implementation 'com.google.android.filament:gltfio-android:1.9.9'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65818150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档