����λ�ã���ҳ > �����̳� > �̳� > ����6����ʵ��������������ʵ����ľ�׼����
��ǿ��ʵ��AR��������Ϊһ�ֽ�������Ϣ����ʵ�����ںϵĴ��¼������������õ��˿��ٷ�չ�����ڶ��Ӧ������չ�ֳ�����ص������������ڽ�����ҵ����ʦ����ͨ��������ʵ��������ֱ�۵ذ���ѧ�������������������ҵ��AR��������������ʷ�Ļ����������⵼���ȣ�Ϊ�ο��ṩ���ӳ����Ļ������顣 Ȼ��������Ӧ����˵��
��ǿ��ʵ��AR��������Ϊһ�ֽ�������Ϣ����ʵ�����ںϵĴ��¼������������õ��˿��ٷ�չ�����ڶ��Ӧ������չ�ֳ�����ص������������ڽ�����ҵ����ʦ����ͨ��������ʵ��������ֱ�۵ذ���ѧ�������������������ҵ��AR��������������ʷ�Ļ����������⵼���ȣ�Ϊ�ο��ṩ���ӳ����Ļ������顣
Ȼ��������Ӧ����˵��AR�����Ŀ���ʹ�þ������£�����Ҫ�߰��Ŀ����ɱ���רҵ�ļ����˲š����ڴˣ�HarmonyOS SDK AR������� ��AR Engine��Ϊ���Ӧ�ÿ������ṩ���Ƚ���AR����������˿����ɱ��ͼ����ż������⡣
�ڼ���AR Engine�����󣬿�����ֻ��6���������裬�Ϳ���ʵ�ֽ���������ڷ�����ʵ�����ƽ���ϣ�ʵ���������ʵ���ںϣ��ù��ܿ�Ӧ��������Ҿ߷��á�����չ����չ�ȳ�����Ϊ�û��ṩ��ʵ��ϵ������顣
AR�ڷ�ʵ�ֵ�ҵ��������Ҫ��Ϊ��Ӧ�á�ʶ��ƽ�沢չʾ�ͷ������������������֡�
��һ�������û���Ӧ�ã�Ӧ����Ҫ���û��������Ȩ�ޡ�����û�δͬ����Ȩ�����޷�ʹ�øù��ܡ�
�ڶ������У�AR Engineʶ��ƽ�沢չʾ���������AR Engine��ʼ��������ARFrame���󡢻�ȡƽ�桢����ƽ�沢��ʾԤ������Ȳ��衣
��������Ϊ�����������塣���û������Ļ��ͨ����ײ����ȡ��ʵ�����е���Ȥ�㣬������Ȥ���ϴ���ê�㣬����ʵ����ê��λ�û����������壬��������������ʾ��Ԥ�������ϡ�
��ʵ��AR����ڷŵľ��忪������֮ǰ����������Ҫ�ȴ���Native C++���̣�����ArkTs�ӿڣ�����������Ȩ����Ȩ��
������׼����������Ҫ����һ��UI���棬������ʾ���Ԥ�����棬����ʱ����ÿһ֡���ơ�
import { Logger } from '../utils/Logger';
import arEngineDemo from 'libentry.so';
import { resourceManager } from '@kit.LocalizationKit';
import { display } from '@kit.ArkUI';
[@Entry](https://my.oschina.net/u/4127701)
[@Component](https://my.oschina.net/u/3907912)
struct ArWorld {
private xcomponentId = 'ArWorld';
private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.All });
private resMgr: resourceManager.ResourceManager = getContext(this).resourceManager;
private interval: number = -1;
private isUpdate: boolean = true;
aboutToAppear() {
Logger.debug('aboutToAppear ' + this.xcomponentId);
arEngineDemo.init(this.resMgr);
arEngineDemo.start(this.xcomponentId);
display.on("foldStatusChange", (foldStatus: display.FoldStatus) => {
Logger.info('foldStatusChange display on ' + foldStatus);
if (foldStatus === display.FoldStatus.FOLD_STATUS_EXPANDED
|| foldStatus === display.FoldStatus.FOLD_STATUS_FOLDED) {
arEngineDemo.stop(this.xcomponentId);
arEngineDemo.init(this.resMgr);
// ����Native��start�ӿڣ�����ARSession��
arEngineDemo.start(this.xcomponentId);
arEngineDemo.show(this.xcomponentId);
}
})
}
aboutToDisappear() {
Logger.debug('aboutToDisappear ' + this.xcomponentId);
arEngineDemo.stop(this.xcomponentId);
}
onPageShow() {
this.isUpdate = true;
Logger.debug('onPageShow ' + this.xcomponentId);
arEngineDemo.show(this.xcomponentId);
}
onPageHide() {
Logger.debug('onPageHide ' + this.xcomponentId);
this.isUpdate = false;
arEngineDemo.hide(this.xcomponentId);
}
build() {
Column() {
XComponent({ id: this.xcomponentId, type: 'surface', libraryname: 'entry' })
.onLoad(() => {
Logger.debug('XComponent onLoad ' + this.xcomponentId);
this.interval = setInterval(() => {
if (this.isUpdate) {
// ����Native��update������AR Engineÿһ֡�ļ�����
arEngineDemo.update(this.xcomponentId);
}
}, 33); // ����֡��Ϊ30fps��ÿ33����ˢ��һ֡����
})
.width('100%');
.height('100%');
.onDestroy(() => {
Logger.debug('XComponent onDestroy ' + this.xcomponentId);
clearInterval(this.interval);
})
.backgroundColor(Color.White);
}
.justifyContent(FlexAlign.SpaceAround);
.alignItems(HorizontalAlign.Center);
.backgroundColor(Color.White);
.borderRadius(24);
.width('100%');
.height('100%');
}
}
������UI���������AR Engineͷ�ļ�������дCMakeLists.txt��
#include "ar/ar_engine_core.h"
find_library(
# Sets the name of the path variable.
arengine-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
libarengine_ndk.z.so
)
target_link_libraries(entry PUBLIC
${arengine-lib}
)
���ȣ�����AR�Ự��Ԥ���ߴ硣
// ����ѡ������һ��ӵ�к���Ĭ�����õ����ö���
AREngine_ARConfig *arConfig = nullptr;
HMS_AREngine_ARConfig_Create(arSession, &arConfig);
// ����ѡ������AREngine_ARSession�Ự��
HMS_AREngine_ARSession_Configure(arSession, arConfig);
// ����ѡ���ͷ�ָ�������ö�����ڴ�ռ䡣
HMS_AREngine_ARConfig_Destroy(arConfig);
// ����һ���µ�AREngine_ARFrame����
HMS_AREngine_ARFrame_Create(arSession, &arFrame);
// Ԥ�������ʵ�ʿ��ߣ���ʹ��xcomponent�����ʾ����ÿ��͸���xcomponent�Ŀ��͸ߣ������һ�£��ᵼ����ʾ���Ԥ��������
int32_t width = 1440;
int32_t height = 1080;
// ������ʾ�Ŀ��͸ߣ�������Ϊ��λ����
HMS_AREngine_ARSession_SetDisplayGeometry(arSession, displayRotation, width, height);
ͨ��openGL�ӿڻ�ȡ����ID��
//ͨ��openGL�ӿڻ�ȡ����ID.
GLuint textureId = 0;
glGenTextures(1, &textureId);
����openGL�������洢���Ԥ�������ݡ�
// ���ÿ����ڴ洢���Ԥ�������ݵ�openGL������
HMS_AREngine_ARSession_SetCameraGLTexture(arSession, textureId );
����HMS_AREngine_ARSession_Update�������µ�ǰAREngine_ARFrame����
// ��ȡ֡����AREngine_ARFrame��
HMS_AREngine_ARSession_Update(arSession, arFrame);
��ȡ�������ͼ����������ͶӰ�������ں������ơ�
// ����AREngine_ARFrame������Ի�ȡ�������AREngine_ARCamera��
AREngine_ARCamera *arCamera = nullptr;
HMS_AREngine_ARFrame_AcquireCamera(arSession, arFrame, &arCamera);
// ��ȡ����֡���������ͼ����
HMS_AREngine_ARCamera_GetViewMatrix(arSession, arCamera, glm::value_ptr(*viewMat), 16);
// ��ȡ���������ͼ���ϲ���Ⱦ�������ݵ�ͶӰ���󣬿������������ϵ���ü�����ϵת����Near (0.1) Far (100)��
HMS_AREngine_ARCamera_GetProjectionMatrix(arSession, arCamera, {0.1f, 100.f}, glm::value_ptr(*projectionMat), 16);
����HMS_AREngine_ARSession_GetAllTrackables������ȡƽ���б���
// ��ȡ��ǰ��⵽��ƽ���б���
AREngine_ARTrackableList *planeList = nullptr;
// ����һ���ɸ��ٶ����б���
HMS_AREngine_ARTrackableList_Create(arSession, &planeList);
// ��ȡ����ָ������ΪARENGINE_TRACKABLE_PLANE�Ŀɸ��ٶ��񼯺ϡ�
AREngine_ARTrackableType planeTrackedType = ARENGINE_TRACKABLE_PLANE;
HMS_AREngine_ARSession_GetAllTrackables(arSession, planeTrackedType, planeList);
int32_t planeListSize = 0;
// ��ȡ���б��еĿɸ��ٶ����������
HMS_AREngine_ARTrackableList_GetSize(arSession, planeList, &planeListSize);
mPlaneCount = planeListSize;
for (int i = 0; i < planeListSize; ++i) {
AREngine_ARTrackable *arTrackable = nullptr;
// �ӿɸ����б��л�ȡָ��index�Ķ���
HMS_AREngine_ARTrackableList_AcquireItem(arSession, planeList, i, &arTrackable);
AREngine_ARPlane *arPlane = reinterpret_cast(arTrackable);
// ��ȡ��ǰ�ɸ��ٶ���ĸ���״̬�����״̬Ϊ��ARENGINE_TRACKING_STATE_TRACKING���ɸ���״̬���Ž��л��ơ�
AREngine_ARTrackingState outTrackingState;
HMS_AREngine_ARTrackable_GetTrackingState(arSession, arTrackable, &outTrackingState);
AREngine_ARPlane *subsumePlane = nullptr;
// ��ȡƽ��ĸ�ƽ�棨һ��ƽ�汻��һ��ƽ��ϲ�ʱ���������ƽ�棩������޸�ƽ�淵��ΪNULL��
HMS_AREngine_ARPlane_AcquireSubsumedBy(arSession, arPlane, &subsumePlane);
if (subsumePlane != nullptr) {
HMS_AREngine_ARTrackable_Release(reinterpret_cast(subsumePlane));
// �����ǰƽ���и�ƽ�棬��ǰƽ�治����չʾ����������˫ƽ�档
continue;
}
// ����״̬Ϊ��ARENGINE_TRACKING_STATE_TRACKINGʱ�Ž��л��ơ�
if (AREngine_ARTrackingState::ARENGINE_TRACKING_STATE_TRACKING != outTrackingState) {
continue;
}
// ����ƽ����ơ�
}
HMS_AREngine_ARTrackableList_Destroy(planeList);
planeList = nullptr;
����HMS_AREngine_ARPlane_GetPolygon������ȡƽ��Ķ�ά�����������飬���ڻ���ƽ��߽硣
// ��ȡ��⵽ƽ��Ķ�ά���������С��
int32_t polygonLength = 0;
HMS_AREngine_ARPlane_GetPolygonSize(session, plane, &polygonLength);
// ��ȡ��⵽ƽ��Ķ�ά�������飬��ʽΪ[x1��z1��x2��z2��...]��
const int32_t verticesSize = polygonLength / 2;
std::vector raw_vertices(verticesSize);
HMS_AREngine_ARPlane_GetPolygon(session, plane, glm::value_ptr(raw_vertices.front()), polygonLength);
// �ֲ�����ϵ�������ꡣ
for (int32_t i = 0; i < verticesSize; ++i) {
vertices.emplace_back(raw_vertices[i].x, raw_vertices[i].y, 0.75f);
}
��ƽ��Ķ�ά��������ת������������ϵ��������ƽ�档
// ��ȡ��ƽ��ľֲ�����ϵ����������ϵת����λ����Ϣ��
AREngine_ARPose *scopedArPose = nullptr;
HMS_AREngine_ARPose_Create(session, nullptr, 0, &scopedArPose);
HMS_AREngine_ARPlane_GetCenterPose(session, plane, scopedArPose);
// ��λ������ת����4X4�ľ���outMatrixColMajor4x4Ϊ������飬���е����ݰ��������ȴ洢.
// �þ�����ֲ�����ϵ����������˷������Եõ��ֲ�����ϵ����������ϵ��ת����
HMS_AREngine_ARPose_GetMatrix(session, scopedArPose, glm::value_ptr(modelMat), 16);
HMS_AREngine_ARPose_Destroy(scopedArPose);
// ����������Ⱦƽ����������ݡ�
// ����������
for (int i = 1; i < verticesSize - 1; ++i) {
triangles.push_back(0);
triangles.push_back(i);
triangles.push_back(i + 1);
}
// ����ƽ���Χ�ߡ�
for (int i = 0; i < verticesSize; ++i) {
lines.push_back(i);
}
�û������Ļ�󣬻��ڵ���¼���ȡ��Ļ���ꡣ
// ����ͷ�ļ���native_interface_xcomponent.h
#include
float pixeLX= 0.0f;
float pixeLY= 0.0f;
int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &mTouchEvent);
if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
if (mTouchEvent.type == OH_NATIVEXCOMPONENT_DOWN) {
pixeLX= mTouchEvent.touchPoints[0].x;
pixeLY= mTouchEvent.touchPoints[0].y;
} else {
return;
}
}
����HMS_AREngine_ARFrame_HitTest����������ײ��⣬����������ײ������б��С�
// ����һ�����м���������б���arSessionΪ����AR���������д����ĻỰ����
AREngine_ARHitResultList *hitResultList = nullptr;
HMS_AREngine_ARHitResultList_Create(arSession, &hitResultList);
// ��ȡ���м���������б���arFrameΪ����AR���������д�����֡����pixeLX/pixeLYΪ��Ļ�����ꡣ
HMS_AREngine_ARFrame_HitTest(arSession, arFrame, pixeLX, pixeLY, hitResultList);
����HMS_AREngine_ARHitResultList_GetItem����������ײ������б�����ȡ���еĿɸ��ٶ���
// ��������������
AREngine_ARHitResult *arHit = nullptr;
HMS_AREngine_ARHitResult_Create(arSession, &arHit);
// ��ȡ��һ�����м��������
HMS_AREngine_ARHitResultList_GetItem(arSession, hitResultList, 0, arHit);
// ��ȡ�����еĿ�׷�ٶ���
AREngine_ARTrackable *arHitTrackable = nullptr;
HMS_AREngine_ARHitResult_AcquireTrackable(arSession, arHit, &arHitTrackable);
�ж���ײ����Ƿ������ƽ���ڲ���
AREngine_ARTrackableType ar_trackable_type = ARENGINE_TRACKABLE_INVALID;
HMS_AREngine_ARTrackable_GetType(arSession, arTrackable, &ar_trackable_type)
if (ARENGINE_TRACKABLE_PLANE == ar_trackable_type) {
AREngine_ARPose *arPose = nullptr;
HMS_AREngine_ARPose_Create(arSession, nullptr, 0, &arPose);
HMS_AREngine_ARHitResult_GetHitPose(arSession, arHit, arPose);
// �ж�λ���Ƿ�λ��ƽ��Ķ���η�Χ�ڡ�0��ʾ���ڷ�Χ�ڣ���0��ʾ�ڷ�Χ�ڡ�
HMS_AREngine_ARPlane_IsPoseInPolygon(mArSession, arPlane, arPose, &inPolygon)
HMS_AREngine_ARPose_Destroy(arPose);
if (!inPolygon) {
// ����ƽ���ڣ���������ǰƽ�档
continue;
}
}
����ײ���λ�ô���һ���µ�ê�㣬�����ڴ�ê���������ģ�͡�
// ����ײ����λ�ô���һ���µ�ê�㡣
AREngine_ARAnchor *anchor = nullptr;
HMS_AREngine_ARHitResult_AcquireNewAnchor(arSession, arHitResult, &anchor)
// �ж�ê��Ŀɸ���״̬
AREngine_ARTrackingState trackingState = ARENGINE_TRACKING_STATE_STOPPED;
HMS_AREngine_ARAnchor_GetTrackingState(arSession, anchor, &trackingState)
if (trackingState != ARENGINE_TRACKING_STATE_TRACKING) {
HMS_AREngine_ARAnchor_Release(anchor);
return;
}
����HMS_AREngine_ARAnchor_GetPose������ȡê��λ�ˣ������ڸ�λ�˻�������ģ�͡�
// ��ȡê���λ�ˡ�
AREngine_ARPose *pose = nullptr;
HMS_AREngine_ARPose_Create(arSession, nullptr, 0, &pose);
HMS_AREngine_ARAnchor_GetPose(arSession, anchor, pose);
// ��λ������ת����4X4�ľ���modelMat��
HMS_AREngine_ARPose_GetMatrix(arSession, pose, glm::value_ptr(modelMat), 16);
HMS_AREngine_ARPose_Destroy(pose);
// ��������ģ�͡�
�˽��������>>
���� AR Engine���˹���
��ȡ AR Engine����ָ���ĵ�
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������