This codelab guides you through changing the color, initial angle, and scaling of the AR logo by modifying the source code in the demo.

What is WorldAR

HUAWEI AR Engine is a platform for building augmented reality apps on Huawei smartphones. WorldAR is a subsystem of the HUAWEI AR Engine and allows your app to:

What You Will Create

In this codelab, you will create an app that is able to scan the surrounding environment using the camera, identify the plane in the environment, place a virtual object on the plane, and select and drag the object.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

Opening HwAREngineDemo Sample Program

  1. Click here to download HwAREngineDemo.zip from HUAWEI Developers, and decompress it to obtain HwAREngineDemo.
  2. Start HUAWEI DevEco Studio and choose to open an existing DevEco Studio project.
  3. Open the HwAREngineDemo directory and wait until the DevEco Studio completes project synchronization.

Updating AndroidManifest.xml

Set HUAWEI AR Engine to be Required and obtain the camera permission.

<!--Declare the camera permission.--> <uses-permission android:name="android.permission.CAMERA" /> <application ... <!--Declare that HUAWEI AR Engine is required.--> <meta-data android:name="com.huawei.ar.engine" android:value="required" /> </application>

Building and Running the Program

  1. The CodeLab must run on a physical device, and does not support Android Emulator. The device needs to be connected to the development computer via USB. (Enable USB debugging in Developer options, and grant the permission if the phone asks whether you wish to trust the connected computer.)
  2. You can run the following command to check whether the device is connected to the computer:
    adb devices
  3. This will connect to your devices and output the device serial number.

  4. In DevEco Studio, select and run HwAREngineDemo. Select your device as the deployment target and click OK to start the sample app on the device.
  5. Grant the requested permission on your device, which will allow the device to start the rear camera to scan the surrounding environment. After a plane is scanned, click the plane to add virtual objects to the real world. You can also click the added virtual object and drag it on the plane.

Modifying Virtual Object Rendering Factors

Add the interface for updating the scaling factor and rotation angle to WorldAR/VirtualObject.java and provide the updated values before rendering.
VirtualObject.java

public void updateScaleFactor(float scaleFactor){ // The scaling factor is set externally based on the 0.15f scaling. mScaleFactor = 0.15f * scaleFactor; } public void updateRotation(float angle) { // Set the rotation angle externally. mRotationAngle = angle; } public float[] getModelAnchorMatrix() { float[] modelMatrix = new float[MATRIX_SIZE]; if (mArAnchor != null) { mArAnchor.getPose().toMatrix(modelMatrix, 0); } else { Matrix.setIdentityM(modelMatrix, 0); } float[] rets = new float[MATRIX_SIZE]; Matrix.multiplyMM(rets, 0, modelMatrix, 0, mModelMatrix, 0); // Rotate the model around the y axis at a specified angle. Matrix.rotateM(rets, 0, mRotationAngle, 0f, 1f, 0f); // Scale the x, y, and z dimensions of the model according to the specified factor. if (mScaleFactor != 0) { Matrix.scaleM(rets, 0, mScaleFactor, mScaleFactor, mScaleFactor); } return rets; }

Adding SeekBars on the Interface

Two SeekBars are added to the main interface of the app to control the scaling and rotation parameters respectively. Add the following code to the res/layout/world_java_activity_main.xml file.
world_java_activity_main.xml

<SeekBar android:id="@+id/scaleSeekBar" android:layout_width="300dp" android:layout_height="40dp" android:layout_alignParentEnd="true" android:min="1" android:max="400" android:progress="150"/> <SeekBar android:id="@+id/rotationSeekBar" android:layout_width="300dp" android:layout_height="40dp" android:layout_below="@id/scaleSeekBar" android:layout_alignParentEnd="true" android:min="0" android:max="360" android:progress="180"/>

Setting Rotation and Scaling Factors

Obtain the corresponding SeekBar from WorldAR/rendering/WorldRenderManager.java and set it to the selected model.
WorldRenderManager.java

public WorldRenderManager(Activity activity, Context context) { mActivity = activity; mContext = context; mTextView = activity.findViewById(R.id.wordTextView); mSearchingTextView = activity.findViewById(R.id.searchingTextView); // Set the rotation and scaling factors. initSeekBar(activity); } private void initSeekBar(Activity activity) { mScaleSeekBar = activity.findViewById(R.id.scaleSeekBar); mScaleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (mSelectedObj != null) { mSelectedObj.updateScaleFactor(progress / (float) 150); } } ... }); mRotationSeekBar = activity.findViewById(R.id.rotationSeekBar); mRotationSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (mSelectedObj != null) { mSelectedObj.updateRotation(progress); } } ... }); }

Compiling and Running the Modified Program

Run the program in DevEco Studio and select your physical device. Move your device to scan the surrounding environment. After a plane is detected, touch the plane to add virtual objects to the real world. You can also select the virtual object and drag the SeekBar on the page to have the model zoomed in or out and rotated.

Well done. You have successfully completed this codelab and learned how to:

You can also check other demos in the sample project to learn more about the capabilities of HUAWEI AR Engine.

For more information, click the following links:

Code copied