美文网首页
ARCore 在白色背景下加载模型,无AR相机

ARCore 在白色背景下加载模型,无AR相机

作者: 橙果子 | 来源:发表于2020-04-30 14:32 被阅读0次

    一.不支持ARCore的手机如何加装模型?

    ARCore支持手机有限,相信慢慢会适配更多手机, 不支持ARCore的手机, 也可借助Sceneform来预览3D体验。
    Sceneform SDK现在支持直接通过XML添加SceneView的功能,该功能可以构建3D场景而无需相机支持或ARCore支持。您还可以在自己的背景中渲染和放置3D模型。

    二.下载模型

    poly获取我的免费模型。需要以下格式的文件-OBJ,FBX,glTF

    三.导入模型

    新建sampledata文件夹, 复制模型文件进去,并使用Google Sceneform Tools插件导入到Sceneform二进制文件, 生成sfb文件
    详细可查看ARCore初步使用

    四.建立SceneView

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <com.google.ar.sceneform.SceneView
            android:id="@+id/sceneView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    注意:可以自定义background颜色

    五.渲染加载模型

    /**
         * load the 3D model in the space
         *
         * @param parse - URI of the model, imported using Sceneform plugin
         */
        private void renderObject(Uri parse) {
    
            ModelRenderable.builder()
                    .setSource(this, parse)
                    .build()
                    .thenAccept(modelRenderable -> {
                        addNodeToScene(modelRenderable);
                    })
                    .exceptionally(throwable -> {
                        Toast toast = Toast.makeText(this, "报错==" + throwable.getMessage(), Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return null;
                    });
        }
    
    

    六添加节点

      /**
         * Adds a node to the current scene
         *
         * @param model - rendered model
         */
        private void addNodeToScene(ModelRenderable model) {
          cupCakeNode = new Node();
            cupCakeNode.setParent(scene); //更改此节点的父节点。
            Vector3 localPosition = new Vector3(0f, 0f, -1f);
            Vector3 localScale = new Vector3(3f, 3f, 3f);
            cupCakeNode.setLocalPosition(localPosition); //设置此节点相对于其父节点的位置(本地空间)。
            cupCakeNode.setLocalScale(localScale); //设置此节点相对于其父节点(本地空间)的比例。
            cupCakeNode.setRenderable(model); //设置此节点要显示模型
            scene.addChild(cupCakeNode);
        }
    

    为了加载模型,我们使用Sceneform的API- ModelRenderable渲染3D模型。准备就绪后,将添加到中Scene。
    渲染完成后,将创建一个空节点。不需要锚点,因为它不在AR场景中。为模型的节点设置localPosition和localScale属性。
    然后renderable,我们将设置为渲染模型并将其Node作为子级添加到Scene。

    package com.easyar.testfbxdemo;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import com.google.ar.core.exceptions.CameraNotAvailableException;
    import com.google.ar.sceneform.Node;
    import com.google.ar.sceneform.Scene;
    import com.google.ar.sceneform.SceneView;
    import com.google.ar.sceneform.math.Vector3;
    import com.google.ar.sceneform.rendering.ModelRenderable;
    
    /**
     * 没有AR相机
     * 通过 SceneView 加载模型
     */
    public class NotARCameraLoadModel extends AppCompatActivity {
    
        private Scene scene;
        private Node cupCakeNode;
        private SceneView sceneView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.notarcamera_loadmodel_layout);
            sceneView = findViewById(R.id.sceneView);
            scene = sceneView.getScene();
            renderObject(Uri.parse("andy.sfb"));
    
        }
    
        /**
         * load the 3D model in the space
         *
         * @param parse - URI of the model, imported using Sceneform plugin
         */
        private void renderObject(Uri parse) {
    
            ModelRenderable.builder()
                    .setSource(this, parse)
                    .build()
                    .thenAccept(modelRenderable -> {
                        addNodeToScene(modelRenderable);
                    })
                    .exceptionally(throwable -> {
                        Toast toast = Toast.makeText(this, "报错==" + throwable.getMessage(), Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return null;
                    });
        }
    
        /**
         * Adds a node to the current scene
         *
         * @param model - rendered model
         */
        private void addNodeToScene(ModelRenderable model) {
            cupCakeNode = new Node();
            cupCakeNode.setParent(scene); //更改此节点的父节点。
            Vector3 localPosition = new Vector3(0f, 0f, -1f);
            Vector3 localScale = new Vector3(3f, 3f, 3f);
            cupCakeNode.setLocalPosition(localPosition); //设置此节点相对于其父节点的位置(本地空间)。
            cupCakeNode.setLocalScale(localScale); //设置此节点相对于其父节点(本地空间)的比例。
            cupCakeNode.setRenderable(model); //设置此节点要显示模型
            scene.addChild(cupCakeNode);
        }
    
        protected void onPause() {
            super.onPause();
            sceneView.pause();
        }
    
        protected void onResume() {
            super.onResume();
            try {
                sceneView.resume();
            } catch (CameraNotAvailableException e) {
                e.printStackTrace();
            }
        }
    }
    

    源码地址

    预览效果


    image.png

    相关文章

      网友评论

          本文标题:ARCore 在白色背景下加载模型,无AR相机

          本文链接:https://www.haomeiwen.com/subject/dmpjwhtx.html