关于android:如何在现实场景中随心放置AR虚拟对象

1次阅读

共计 4478 个字符,预计需要花费 12 分钟才能阅读完成。

随着 AR 的倒退和电子设备的遍及,人们在生活中应用 AR 技术的门槛升高,比方对于不不便测量的物体应用 AR 测量,不便又精确;遇到生疏的路段应用 AR 导航,分明又便捷;网购时拿不准的物品应用 AR 购物,体验更真切。

想要让虚构物体和事实世界相交融,重要的一步就是将虚构对象精确搁置在事实场景中,当用户触摸电子屏幕上的任意中央时即可创立 AR 对象,达到良好的交互体验。

华为 HMS Core AR Engine 继续跟踪设施绝对于周围环境的地位和姿势变动轨迹,建设虚构数字世界和事实物理世界的对立几何空间,为您的利用提供虚实交融的交互根底平台。其中命中检测技术让用户可通过点击终端设备屏幕选中事实环境中的趣味点,终端设备屏幕上的趣味点映射为事实环境中的趣味点,并以趣味点为源收回一条射线连贯到摄像头所在位置,返回这条射线贯通的任何立体或特色点以及穿插地位在事实世界空间中的地位和姿势。命中检测与立体碰撞,取得碰撞点的地位及法向量,让用户能够自由选择环境中的物体或者与它们互动。

Demo

开发步骤

开发环境要求:

JDK 1.8.211 及以上。

装置 Android Studio 3.0 及以上:

minSdkVersion 26 及以上

targetSdkVersion 29(举荐)

compileSdkVersion 29(举荐)

Gradle 6.1.1 及以上(举荐)

在华为终端设备上的利用市场下载 AR Engine 服务端 APK(需在华为利用市场,搜寻“华为 AR Engine”)并装置到终端设备。

测试利用的设施:参见 AREngine 个性软硬件依赖表中环境 Mesh 反对设施列表。如果同时应用多个 HMS Core 的服务,则须要应用各个 Kit 对应的最大值。

开发筹备

  1. 在开发利用前须要在华为开发者联盟网站上注册成为开发者并实现实名认证,具体方法请参见帐号注册认证。
  2. 华为提供了 Maven 仓集成形式的 AR Engine SDK 包,在开始开发前,须要将 AR Engine SDK 集成到您的开发环境中。
  3. Android Studio 的代码库配置在 Gradle 插件 7.0 以下版本、7.0 版本和 7.1 及以上版本有所不同。请依据您以后的 Gradle 插件版本,抉择对应的配置过程。
  4. 以 7.0 为例:

关上 Android Studio 我的项目级“build.gradle”文件,增加 Maven 代码库。

在“buildscript > repositories”中配置 HMS Core SDK 的 Maven 仓地址。

buildscript {
        repositories {google()
            jcenter()
            maven {url "https://developer.huawei.com/repo/"}
        }
}

关上我的项目级“settings.gradle”文件,配置 HMS Core SDK 的 Maven 仓地址

dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
            repositories {
                    repositories {google()
                        jcenter()
                        maven {url "https://developer.huawei.com/repo/"}
                    }
                }
}
  1. 增加依赖 在“dependencies”中增加如下编译依赖:
dependencies {implementation 'com.huawei.hms:arenginesdk:{version}
}

开发步骤

  1. 首先创立 WorldRenderManager,这个类提供了与世界场景相干的渲染治理,包含标签渲染和虚构对象渲染治理。
public class WorldRenderManager implementsGLSurfaceView.Renderer{
    // 此办法构造函数传递上下文
    public WorldRenderManager(Activity activity, Context context) {
        mActivity = activity;
       mContext = context;
        …
}
// 此办法设置 ARSession,它将更新并获取 OnDrawFrame 中的最新数据。public void setArSession(ARSession arSession) {if (arSession == null) {LogUtil.error(TAG, "setSession error, arSession is null!");
            return;
        }
        mSession = arSession;
 }
// 设置 ARWorldTrackingConfig,获取配置模式。public void setArWorldTrackingConfig(ARWorldTrackingConfig arConfig) {if (arConfig == null) {LogUtil.error(TAG, "setArWorldTrackingConfig error, arConfig is null!");
            return;
        }
        mArWorldTrackingConfig = arConfig;
    }
// 实现 onDrawFrame 办法
@Override
public void onDrawFrame(GL10 unused) {mSession.setCameraTextureName(mTextureDisplay.getExternalTextureId());
ARFrame arFrame = mSession.update();
ARCamera arCamera = arFrame.getCamera();
…….
}
// 命中后果输入
private ARHitResult hitTest4Result(ARFrame frame, ARCamera camera, MotionEvent event) {
   ARHitResult hitResult = null;
   List<ARHitResult> hitTestResults = frame.hitTest(event);
// 确定命中点是否在立体多边形内。ARHitResult hitResultTemp = hitTestResults.get(i);
            if (hitResultTemp == null) {continue;}
ARTrackable trackable = hitResultTemp.getTrackable();
        // 确定点云是否被单击,以及点是否面向相机。boolean isPointHitJudge = trackable instanceof ARPoint
&& ((ARPoint) trackable).getOrientationMode() == ARPoint.OrientationMode.ESTIMATED_SURFACE_NORMAL;
// 优先选择立体上的点。if (isPlanHitJudge || isPointHitJudge) {
      hitResult = hitResultTemp;
      if (trackable instanceof ARPlane) {break;}
      }
return hitResult;
}
}
  1. 创立 WorldActivity,本 AR 示例介绍了如何应用 HUAWEI AR Engine 的世界 AR 场景。
public class WorldActivity extends BaseActivity {
    private ARSession mArSession;
private GLSurfaceView mSurfaceView;
private ARWorldTrackingConfig mConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {LogUtil.info(TAG, "onCreate");
       super.onCreate(savedInstanceState);
       setContentView(R.layout.world_java_activity_main);
 mWorldRenderManager = new WorldRenderManager(this, this);
mWorldRenderManager.setDisplayRotationManage(mDisplayRotationManager);
mWorldRenderManager.setQueuedSingleTaps(mQueuedSingleTaps)  
}
@Override
protected void onResume() {if (!PermissionManager.hasPermission(this)) {this.finish();
        }
        errorMessage = null;
        if (mArSession == null) {
            try {if (!arEngineAbilityCheck()) {finish();
                    return;
                }
                mArSession = new ARSession(this.getApplicationContext());
                mConfig = new ARWorldTrackingConfig(mArSession);
                refreshConfig(ARConfigBase.LIGHT_MODE_ENVIRONMENT_LIGHTING | ARConfigBase.LIGHT_MODE_ENVIRONMENT_TEXTURE);
            } catch (Exception capturedException) {setMessageWhenError(capturedException);
            }
            if (errorMessage != null) {stopArSession();
                return;
            }
}

@Override
    protected void onPause() {LogUtil.info(TAG, "onPause start.");
        super.onPause();
        if (mArSession != null) {mDisplayRotationManager.unregisterDisplayListener();
            mSurfaceView.onPause();
            mArSession.pause();}
        LogUtil.info(TAG, "onPause end.");
    }
@Override
    protected void onDestroy() {LogUtil.info(TAG, "onDestroy start.");
        if (mArSession != null) {mArSession.stop();
            mArSession = null;
        }
        if (mWorldRenderManager != null) {mWorldRenderManager.releaseARAnchor();
        }
        super.onDestroy();
        LogUtil.info(TAG, "onDestroy end.");
    }
…..
}

理解更多详情 >>

拜访华为开发者联盟官网
获取开发领导文档
华为挪动服务开源仓库地址:GitHub、Gitee

关注咱们,第一工夫理解 HMS Core 最新技术资讯~

正文完
 0