This section provides only the key development steps. Click here to access the complete sample code.
Since version 5.1.0(18), the plane semantic recognition capability has been supported on a subset of phones and tablets. You can check whether a device supports the capability (ARENGINE_FEATURE_TYPE_SEMANTIC) by referring to Hardware Requirements.
Plane semantic information can be obtained using the ARPlane object, and the following are the plane-related APIs. For details about the APIs, please refer to AR Engine API Reference.
| API | Description |
|---|---|
| ARTrackable.getPose | Obtains the pose information of the tracked target. |
| ARTrackable.getAnchors | Obtains the anchor object bound to the input trackable object. |
| ARPose.getMatrix | Converts the pose data into a 4 × 4 matrix. |
| ARPlane.getPolygonXZ | Obtains the 2D vertex array of the detected plane. |
| ARPlane.getSubsumedBy | Obtains the parent plane of a plane (a parent plane is generated when a plane is merged with another one). |
| ARPlane.isPoseInExtents | Checks whether a given pose is within the boundary rectangle of the plane. |
| ARPlane.isPoseInPolygon | Checks whether a given pose is within the boundary polygon of the plane. |
AR Engine only outputs data for detected planes. To enhance observation, you can use either the Ark Graphics Platform (AGP) rendering engine or XComponent to draw the detected planes. For details about AGP, please refer to ArkGraphics 3D and AGP Engine.
For any AR app built with ArkTS, you must first create an AR session ARViewContext to manage the system status of AR Engine. Create an AR session ARViewContext by referring to Managing AR Sessions.
Before recognizing plane semantics, detect planes in the recognition environment. For details about how to detect planes in the environment, please refer to Detecting Planes in the Environment.
The following modules are required for recognizing plane semantics:
import { arEngine, ARView, arViewController } from '@kit.AREngine';
import {Camera, CustomGeometry, Geometry, Image, Material, MaterialType, Node, PrimitiveTopology,
Scene, SceneResourceFactory, Shader, ShaderMaterial} from '@kit.ArkGraphics3D';
import { BusinessError } from '@kit.BasicServicesKit'; Define the variable planeLabel to receive plane type label information.
let arSession: arEngine.ARSession; let planeLabel: arEngine.ARSemanticPlaneLabel;
First, initialize the AR session and AR scene. For details, please refer to Initializing the AR Session and AR Scene.
Set semanticMode to ARSemanticMode.PLANE to enable plane semantic recognition.
Display the recognized plane information on the device UI. Repeatedly call the corresponding API to update such information in real time.
@Builder
export function ARTargetBuilder() {
ARTarget()
}
// ...
@Component
struct ARTarget {
@State arContext?: arViewController.ARViewContext = undefined;
@State targetPlaneLabel: arEngine.ARSemanticPlaneLabel = planeLabel;
private intervalId: number = -1;
private delayInterval: number = 33;
private params: arEngine.ARConfig = { type: arEngine.ARType.WORLD };
// ...
build() {
NavDestination() {
RelativeContainer() {
if (this.arContext) {
ARView({ context: this.arContext })
.height('100%')
.width('100%')
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
Column() {
Text(`Label: ${convertSemanticLabel(this.targetPlaneLabel)}`)
.infoStyles()
}
.alignItems(HorizontalAlign.Center)
.alignRules({
bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
}
}
.onAppear(() => {
this.initARView();
this.intervalId = setInterval(async () => {
this.targetPlaneLabel = planeLabel;
// ...
}, this.delayInterval);
})
.onWillDisappear(() => {
// ...
})
.onShown(() => {
this.resumeARView();
})
.onHidden(() => {
this.pauseARView();
})
// ...
.hideTitleBar(true)
.hideBackButton(true)
.hideToolBar(true)
}
private pauseARView(): void {
// ...
}
private resumeARView(): void {
// ...
}
private initARView(): void {
Scene.load().then(async (scene) => {
let context = new arViewController.ARViewContext();
context.scene = scene;
context.callback = new ARViewCallbackImpl();
context.config = {
type: arEngine.ARType.WORLD,
planeFindingMode: arEngine.ARPlaneFindingMode.HORIZONTAL_AND_VERTICAL,
powerMode: this.params?.powerMode,
semanticMode: 3,
poseMode: this.params?.poseMode,
depthMode: this.params?.depthMode,
meshMode: this.params?.meshMode,
};
context.init().then(() => {
this.arContext = context;
// ...
});
// ...
});
}
} Call ARViewCallback and use its onFrameUpdate method to update frame data and display the detected plane type on the device UI.
Add the plane.label method to obtain the plane semantic information recognized in each frame.
class ARViewCallbackImpl extends arViewController.ARViewCallback {
// ...
onAnchorAdd(ctx: arViewController.ARViewContext, node: Node, anchor: arEngine.ARAnchor): void {
}
onAnchorUpdate(ctx: arViewController.ARViewContext, node: Node, anchor: arEngine.ARAnchor): void {
}
async onFrameUpdate(ctx: arViewController.ARViewContext, sysBootTs: number): Promise<void> {
if (!ctx.session) {
// ...
return;
}
arSession = ctx.session;
let frame = arSession.getFrame();
let camera = frame.getCamera();
if (!camera) {
// ...
} else {
// Update the frame data.
let trackables: arEngine.ARTrackable[] = arSession.getAllTrackables(arEngine.ARTrackableType.PLANE);
for (let i = 0; i < trackables.length; ++i) {
let plane: arEngine.ARPlane = trackables[i] as arEngine.ARPlane;
// ...
planeLabel = plane.label;
// ...
}
}
// ...
}
} Customize the getVertices method to obtain vertex data, generateMeshIndex to create indices, and generateMeshInput to create mesh data. For details, please refer to Customizing Methods for Plane Detection.
For details about arrayBufferFloat32ToNumber, please refer to Data Type Conversion.
For details about convertSemanticLabel, see the following information.
export function convertSemanticLabel(obj: number): string {
let res: string = '';
if (obj === 0) {
res = 'UNKNOWN';
} else if (obj === 1) {
res = 'WALL';
} else if (obj === 2) {
res = 'FLOOR';
} else if (obj === 3) {
res = 'SEAT';
} else if (obj === 4) {
res = 'TABLE';
} else if (obj === 5) {
res = 'CEILING';
} else if (obj === 6) {
res = 'DOOR';
} else if (obj === 7) {
res = 'WINDOW';
} else if (obj === 8) {
res = 'BED';
} else if (obj === 9) {
res = 'PLANE SPACE';
} else if (obj === 10) {
res = 'CUBE VOLUME';
} else if (obj === 11) {
res = 'CUBE SPACE';
}
return res;
}