首页 > 解决方案 > 如何在 ARCore 中显示 .obj

问题描述

对于学校项目,我必须使用 ArCore 来显示我正在为其做项目的客户给我的 .obj。我首先尝试使用我在无许可证网站中找到的随机 .obj 并且效果很好,但是每当我尝试使用客户端的 .obj 时,它告诉我它无法加载而且我没有真的知道该怎么做了。

这是 logcat 中的错误:

2020-04-10 21:08:28.745 28542-29004/com.example.artest E/ModelRenderable: Unable to load Renderable registryId='sebastien.sfb'
java.util.concurrent.CompletionException: java.io.FileNotFoundException: sebastien.sfb (No such file or directory)
    at com.google.ar.sceneform.utilities.SceneformBufferUtils.inputStreamToByteBuffer(SourceFile:48)

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);

    createScene();
}

private void createScene() {
    mScene = arFragment.getArSceneView().getScene();

    ModelRenderable.builder()
            .setSource(this, Uri.parse("sebastien.sfb"))
            .build()
            .thenAccept(renderable -> onRenderableLoaded(renderable))
    .exceptionally(throwable -> {
        Log.i("Sceneform", "failed to load model");
        return null;
    });
}

void onRenderableLoaded(ModelRenderable model) {
   Node modelNode = new Node();
   modelNode.setRenderable(model);
   modelNode.setParent(mScene);
   modelNode.setLocalPosition(new Vector3(0, 0, 0));
   mScene.addChild(modelNode);
}

预先感谢您的帮助。

标签: android-studioaugmented-realityarcoresceneform

解决方案


I'm not posting this as an answer but I really do want to see if I can help but do not have enough reputation points yet to add comments. I don't know how much experience you have with ARCore and loading object file. I myself have only taken up Android coding two weeks ago and jumped right into the deep end writing an ARCore app. It was a steep learning curve but very exciting. I've learned a LOT. So, even though I have 25+ years of coding experience, I'm a newbie Kotlin/Android/ARCore coder.

From your question I take it that you have tested your code using the model you downloaded and your code works fine. I am also assuming that both models are in the same folder location and that for both models the sfa- and sfb files were created, correct?

I do see from the log file "sebastien.sfb (No such file or directory)". This could mean that the .sfb file was not created or when you try to import the file, you are referencing the wrong folder. Please check the following:

  1. In your build.gradle (app) file should be the code that defines the object file as an asset, similar to the code below. When compiling our code, this will build the .sfa and .sfb file for you. It is also worthwhile to delete the sebastian.sfa & sebastian.sfb files (if they already exist) to have them recreated.

    sceneform.asset('sampledata/sebastien.obj', 
                                        // 'Source Asset Path' specified during
                                        // import. This will typically be the
                                        // app/sampledata folder since resources in
                                        // this file is not compiled into your
                                        // application
    'default',                          // 'Material Path' specified during import.
    'sampledata/sebastien.sfa',         // '.sfa Output Path' specified during import.
    'src/main/res/raw/sebastien')       // '.sfb Output Path' specified during import.
    
  2. Typicall when I load the object I use code such as below. Note that I do not make use of Uri.parse but I rather use R.raw.sebastian which directly references the resource folder (res\raw) that the sebastian.sfb file is in.

    val sebastianStage = ModelRenderable.builder().setSource(this, R.raw.sebastian).build()
    

Another couple of questions:

  1. Since you are using an .obj file it will typically be accompanied by a .mtl file. The .obj file references the .mtl file. This can be seen when opening the .obj file in a text editor. Did you perhaps rename the .obj and .mtl file since the .obj file will still reference the old file name of the .mtl file. You also have to change the reference to the .mtl file in the .obj file
  2. Are you sure that the .obj file you were given is not corrupt. If you are using Windows 10, this is easy to check by using MicrosSoft 3D builder which is relatively small easy to use and free. (https://www.microsoft.com/en-za/p/3d-builder/9wzdncrfj3t6?activetab=pivot:overviewtab)

BTW A good source for free 3D models is https://poly.google.com/


推荐阅读