如何用华为Scene Kit 8步实现AR放置应用

it2026-03-15  6

前言

AR放置类APP已经广泛应用于各行各业中,如家居行业用户可以用AR体验家具摆放效果;在零售行业消费者可以用AR提前试穿鞋服;在教育行业学生可以利用AR探索物体的虚拟3D模型,了解各种物体的内部构造。

那么如何才能快速开发一款AR放置APP呢?集成HUAWEI Scene Kit, 只需要8步就能实现。ARView是Scene Kit 中面向AR的场景化API,通过集成AR Engine的平面检测能力,结合图形引擎服务自身渲染引擎的图形能力,为开发者提供了在一般AR场景中加载、渲染3D素材的能力。

ARView功能简介

AR场景中加载、渲染3D素材。

可开关的平面辅助选取功能,屏幕中支持显示一个白色平面点阵的方式,辅助选取平面。

在平面放置素材后,可点击选取该素材物体。当物体处于红色被选取状态后,可进行移动、缩放、旋转。

AR放置应用开发

在使用ARView之前需要在Android工程中集成Scene Kit SDK,操作可以参见集成Scene Kit SDK

ARView继承自Android GLSurfaceView,并重写了相关生命周期方法,方便开发者调用。总共只需要8步即可实现完整的ARView应用。

Step1. 创建一个ARViewActivity,使其继承自Activity。添加一个Button按钮用于加载素材

public class ARViewActivity extends Activity { private ARView mARView; private Button mButton; private boolean isLoadResource = false; }

Step2. 将ARView添加到Layout布局中

<com.huawei.hms.scene.sdk.ARView android:id="@+id/ar_view" android:layout_width="match_parent" android:layout_height="match_parent"> </com.huawei.hms.scene.sdk.ARView>

【说明】ARView为保证效果暂不支持转屏与分屏操作,需要在Android Manifest文件中进行配置:

android:screenOrientation="portrait" android:resizeableActivity="false"

Step3. 重写onCreate方法,并获取ARView

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ar_view); mARView = findViewById(R.id.ar_view); mButton = findViewById(R.id.button); }

Step4. 可在onCreate方法中使用一个Switch按钮控制辅助显示平面是否打开。

Switch mSwitch = findViewById(R.id.show_plane_view); mSwitch.setChecked(true); mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mARView.enablePlaneDisplay(isChecked); } });

【说明】Switch按钮使用前请在Layout中添加。

<Switch android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/show_plane_view" android:layout_alignParentTop="true" android:layout_marginTop="15dp" android:layout_alignParentEnd="true" android:layout_marginEnd ="15dp" android:layout_gravity="end" android:text="@string/show_plane" android:theme="@style/AppTheme" tools:ignore="RelativeOverlap" />

Step5. 增加按钮回调方法。首次点击按钮加载素材,再次点击按钮清除素材。

public void onBtnClearResourceClicked(View view) { if (!isLoadResource) { mARView.loadAsset("ARView/scene.gltf"); isLoadResource = true; mButton.setText(R.string.btn_text_clear_resource); } else { mARView.clearResource(); mARView.loadAsset(""); isLoadResource = false; mButton.setText(R.string.btn_text_load); } }

【说明】onBtnSceneKitDemoClicked方法需在Button的布局属性onClick中注册。

Step6. 重写onPause方法,并调用ARView的onPause方法。

**@Override protected void onPause() { super.onPause(); mARView.onPause(); }**

Step7. 重写onResume方法,并调用ARView的onResume方法。

@Override protected void onResume() { super.onResume(); mARView.onResume(); }

Step8. 重写onDestroy方法,并调用ARView的destroy方法。

@Override protected void onDestroy() { super.onDestroy(); mARView.destroy(); }

Step9. 加载完素材后,可通过setInitialPose设置3D素材初始的缩放系数与旋转角度。【可选】

float[] scale = new float[] { 0.1f, 0.1f, 0.1f }; float[] rotation = new float[] { 0.707f, 0.0f, -0.707f, 0.0f }; mARView.setInitialPose(scale, rotation);

效果展示

按照以上8个步骤调用HUAWEI Scene Kit ARView接口,我们即可实现一个简单的AR放置应用。

如果你对实现方式感兴趣,可以查看Github源码:https://github.com/HMS-Core/hms-scene-demo

基于Scene Kit ARView的能力不仅仅可以用来开发AR放置应用,还可以帮助开发者实现很多有趣的功能, 例如:AR游戏、虚拟展厅、AR导航等。

欲了解更多详情,请参阅:

华为Scene Kit官网:https://developer.huawei.com/consumer/cn/hms/huawei-scenekit/

获取开发指导文档:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/dev-process-0000001054326746

参与开发者讨论请到Reddit社区:https://www.reddit.com/r/HMSCore/

下载demo和示例代码请到Github:https://github.com/HMS-Core/hms-scene-demo

解决集成问题请到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


原文链接:https://developer.huawei.com/consumer/cn/forum/topicview?tid=0202382479977390326&fid=18 作者:胡椒

华为开发者论坛 安卓开发 华为开发者论坛是一个为开发者提供信息传播、开发交流、技术分享的交流空间。开发者可以在此获取技术干货、华为源码开放、HMS最新活动等信息,欢迎大家来交流分享!
最新回复(0)