ARCore 在白色背景下加載模型,無AR相機

一.不支持ARCore的手機如何加裝模型?

ARCore支持手機有限,相信慢慢會適配更多手機, 不支持ARCore的手機, 也可借助Sceneform來預覽3D體驗。
Sceneform SDK現(xiàn)在支持直接通過XML添加SceneView的功能,該功能可以構建3D場景而無需相機支持或ARCore支持。您還可以在自己的背景中渲染和放置3D模型。

二.下載模型

poly獲取我的免費模型。需要以下格式的文件-OBJ,F(xiàn)BX,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;
                });
    }

六添加節(jié)點

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

為了加載模型,我們使用Sceneform的API- ModelRenderable渲染3D模型。準備就緒后,將添加到中Scene。
渲染完成后,將創(chuàng)建一個空節(jié)點。不需要錨點,因為它不在AR場景中。為模型的節(jié)點設置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); //更改此節(jié)點的父節(jié)點。
        Vector3 localPosition = new Vector3(0f, 0f, -1f);
        Vector3 localScale = new Vector3(3f, 3f, 3f);
        cupCakeNode.setLocalPosition(localPosition); //設置此節(jié)點相對于其父節(jié)點的位置(本地空間)。
        cupCakeNode.setLocalScale(localScale); //設置此節(jié)點相對于其父節(jié)點(本地空間)的比例。
        cupCakeNode.setRenderable(model); //設置此節(jié)點要顯示模型
        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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容