美文网首页
利用ARCore Sceneform在android手机上实现简

利用ARCore Sceneform在android手机上实现简

作者: 不灭的钻石 | 来源:发表于2019-02-10 01:30 被阅读0次

需求说明

点击ARCore检测到平面,即可出现一个渲染的view

实现步骤

1.在主活动的xml布局中加入一个sceneform的fragment,作为呈现出ar效果的视图

首先要添加Sceneform的依赖,在build.gradle(Module:app)中的dependencies 最后添加

implementation "com.google.ar.sceneform.ux:sceneform-ux:1.5.0"

在mainfest中添加调用手机摄像头的权限:

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera.ar" android:required="true" />

在主活动的layout中添加以下代码以直接调用sceneform api中的fragment:

  <fragment
        android:id="@+id/ux_fragment"
        android:name="com.google.ar.sceneform.ux.ArFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/control"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintVertical_weight="9"/>

在MainActivity中可以直接调用这个fragment:

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

可以让这个fragment进行更新,这样就会实时根据手机摄像头拍摄的帧进行改变,同时有渲染物呈现在scene上的话也可以及时进行更新:

      arFragment.getArSceneView().getScene()
                .addOnUpdateListener((frameTime -> {
                    arFragment.onUpdate(frameTime);
                   // OnUpdate();
                }));

2.创建要被渲染素材
因为这份代码仅仅是为了了解sceneform的应用逻辑,所以我们简单地建立TextView,然后调用sceneform的api渲染出来即可.
接下来要做的是在drawble中添加一个"text_block":

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#E61976d2"/>
    <corners android:radius="10dp"/>
</shape>

然后在layout中创建文件"ar_test_view":

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/writeCard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/text_block"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="6dp"
    android:text="Hello Sceneform !"
    android:textAlignment="center" />

在MainActivity中进行声明:

private ViewRenderable testRenderable;

3.利用completableFuture实现对渲染线程的异步处理
渲染线程并不存在于主线程中,而是利用回调函数在需要的时候(比如点击一下屏幕)进行返回:

CompletableFuture<ViewRenderable> wordRender = ViewRenderable
               .builder()
               .setView(this,R.layout.ar_test_view)
               .build();

//这里可以添加多个线程进行处理
       CompletableFuture.allOf(
               wordRender
       ).handle((notUsed, throwable) -> {

           if (throwable!=null){
               return null;
           }
           try {
               testRenderable = wordRender.get();
           }catch (InterruptedException|ExecutionException ex){

           }
               return null;

       });

4.为ARCore检测到的平面加入监听器,监听是否有触碰,如果有就加入渲染视图

arFragment.setOnTapArPlaneListener(
                (HitResult hitResult, Plane plane, MotionEvent motionEvent)->{

                    Anchor anchor = hitResult.createAnchor();
                    AnchorNode anchorNode = new AnchorNode(anchor);
                    anchorNode.setParent(arFragment.getArSceneView().getScene());

                    TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
                    node.setParent(anchorNode);
                 
                    node.setRenderable(testRenderable);

             
                    node.select();



                });

相关文章

网友评论

      本文标题:利用ARCore Sceneform在android手机上实现简

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