目录

应用开发

接下来向您介绍如何快速使用XRKit。由于XRKit代码实现复杂,本章以伪代码形式提供关键步骤的代码,详细实现请参见示例代码,您可根据需要,选择学习。

模型文件制作

在使用XRKit能力之前,您需要准备模型文件,用于在虚拟现实中展示,模型文件制作规范请参见模型文件规范说明。

运行前验证

检查当前设备是否安装了XRKit,若已经安装则正常运行。具体实现代码如下:

if (!XrKitFeatureFactory.isXrKitExist(getApplicationContext())) {
    Toast.makeText(this, "XRKit is not available ", Toast.LENGTH_SHORT).show();
    return false;
}

创建AR场景

使用XRKit特性组件工厂类XrKitFeatureFactory,实例化特性组件管理对象xrKitFeature并设置特性事件监听,特性事件监听用来获取XRKit服务端上报的必要特性事件。xrKitFeature可根据需求创建不同AR场景,本例创建的是AR展示场景(IArSceneView)。
try {
    if (xrKitFeature == null) {
        xrKitFeature = XrKitFeatureFactory.createXrKitFeature(getApplicationContext());
    }
    // Register a feature listener.
    xrKitFeature.setFeatureEventListener(this);
    // Create arSceneView.
    sceneView = xrKitFeature.createArSceneView(getApplicationContext());
} catch (XrKitUnavailableServiceApkTooOldException e) {
    Toast.makeText(this, "XRKit Service is Too Old, Please upgrade!", Toast.LENGTH_SHORT).show();
    return false;
}

绑定视图到应用布局

获取当前场景的视图,绑定视图到应用的UI布局当中。
layout.addView(sceneView.getView(), 0, new ConstraintLayout.LayoutParams(
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

选择模式

Ar展示场景(ArSceneView)有AR模式和3D模式两种模式,可根据需求选择AR模式或3D模式。

// AR模式
sceneView.setArMode(true);
// 3D模式
sceneView.setArMode(false);

设置场景

加载虚拟模型、设置3D场景的背景、设置AR场景下平面的可见性。

sceneView.loadModel(GONGCHENGSHI, "gongchengshi");
sceneView.setBackground(IMG_BLACK);
sceneView.setPlaneVisible(true);

在activity的onResume方法中调用sceneView的resume方法,启动sceneView场景。

protected void onResume() {
    Log.d(TAG, "activity resume.");
    super.onResume();
    if (sceneView != null) {
        sceneView.resume();
    }
}

至此完成AR场景的创建。

AR展示场景(ArSceneView)和ARFace场景(ArFaceView)在不同需求下创建的AR场景,在绑定视图到布局、模式选择则和场景设置中有所差异,具体实现请参见示例代码。