Sceneform

Sceneform is a framework provided by Google’s ARCore development team to facilitate the development of ARCore (it has stopped updating at present). Sceneform can easily render realistic 3D scenes in AR and non-AR applications without learning OpenGL. It provides ArFragment to display scenes that can render virtual objects, as well as related functions such as model import, plane processing, material customization, gesture control and so on.

Directions for use

ARCore requires high performance and needs to be adapted to different types of cameras to improve the AR level, so not all mobile phones are supported. There is a list of supported models on the official website, and the Android version should be at least 24. Huawei’s new models are not supported due to huawei being blocked. In addition, the software required to run ARCore is Google Play Services for AR, which can be downloaded from the mobile app store. If not, Google Play is required.

Project configuration

1. Build. Gradle (Project) requires A Google Maven repository

allprojects {
    repositories{Google ()...Copy the code

2. Add ARCore and Sceneform dependencies in build.gradle(app)

Android {... defaultConfig {// Sceneform minimum Android ADK version is 24 (N)
        minSdkVersion 24... }// Sceneform libraries use language constructs from Java 8.
    // Add these compile options if targeting minSdkVersion < 26.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies{...// The version of arcore is not imported if only sceneform is used
    implementation 'com. Google. Ar: core: 1.5.0'

    // SceneForm library, 1.7.1 is the latest version but has the same function as 1.5, support through plug-in obj file import, 1.6 support GLTF format import
    implementation 'com. Google. Ar. Sceneform. Ux: sceneform - ux: 1.5.0'
    implementation 'com. Google. Ar. Sceneform: core: 1.5.0'
}
Copy the code

The introduction of ArFragment

Introduce ArFragment in XML files for pages that need TO use AR. ArFragment is Sceneform’s own Fragment for displaying virtual scenes. It contains apK request installation, permission request, leveling animation, flat display, gesture control, click events, etc.

<fragment android:name="com.google.ar.sceneform.ux.ArFragment"
    android:id="@+id/ux_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />. // You can add related tool components, such as back button, photo button, share button, etc.Copy the code

A few lines of code are a big step, and you can actually use it directly. When you go in, you will ask for camera permission, and you will ask to install Google Play Services for AR’s APK, and you will make leveling, but you can’t click to place the model because there is no import model and set up listening.

Model import

For example, if Scenefomr of version 1.5 or Sceneform of version 1.7 is used, the source files of the model in obJ, FBX, GLTF and other formats cannot be used directly. They need to be converted into SFB binary files before being introduced into the project. Sceneform provides a handy AS plugin for this.

1. Import the Google Sceneform Tools(Beta) plug-in

In Android Studio, open Plugins import:

  • windows : File > Settings > Plugins > MarketPlace
  • macOS : Android Studio > Preferences > Plugins

Create a sampleData folder in the app Directory. Right-click the app folder and choose New > Sample Data Directory. Create a directory named sampleData if there is no right click. Then copy all the files related to the model, such as MTL, bin, PNG, JPG, etc., into the SampleData folder

3. Gradle configuration

Add the following code to build. Gradle (app)

apply plugin: 'com.google.ar.sceneform.plugin'

sceneform.asset('sampledata/models/andy.obj'.// 'Source Asset Path' specified during import.
                'default'.// 'Material Path' specified during import.
                'sampledata/models/andy.sfa'.// '.sfa Output Path' specified during import.
                'src/main/res/raw/andy')      // '.sfb Output Path' specified during import.
Copy the code

4. Click on the build

Sfa and *.sfb files are generated in the corresponding directories after compilation. Sfa files are SFB documentation files. If you modify the corresponding configurations in SFA and then compile the SFB files, you can modify the contents of the generated SFB files

UI file writing

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(! checkIsSupportedDeviceOrFinish(this)) {
            return;
        }

        setContentView(R.layout.activity_ux);
        arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);

        / / create a Renderable
        ModelRenderable.builder()
                .setSource(this, R.raw.andy) // Read SFB files in raw materials by id. You can also read network or local files by Uri
                .build()
                .thenAccept(renderable -> andyRenderable = renderable)
                .exceptionally(
                        throwable -> {
                            Toast toast =
                                    Toast.makeText(this."Unable to load andy renderable",
                                            Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER, 0.0);
                            toast.show();
                            return null;
                        });
        // As the name implies, this listener is triggered only when you click on the plane
        arFragment.setOnTapArPlaneListener(
                (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
                    if (andyRenderable == null) {
                        return;
                    }

                    // Create the anchor point, determine the coordinates in space and attitude
                    Anchor anchor = hitResult.createAnchor();
                    AnchorNode anchorNode = new AnchorNode(anchor);
                    anchorNode.setParent(arFragment.getArSceneView().getScene());

                    // Create TransformableNode, which encapsulates gestures such as zoom, move, and rotate
                    TransformableNode andy = new TransformableNode(
                            arFragment.getTransformationSystem());
                    // Each point requires a parent node
                    andy.setParent(anchorNode);
                    andy.setRenderable(andyRenderable);
                    andy.select();
                });
    }
    // Android version check and OpenGL version check
    public static boolean checkIsSupportedDeviceOrFinish(final Activity activity) {
        if (Build.VERSION.SDK_INT < VERSION_CODES.N) {
            Log.e(TAG, "Sceneform requires Android N or later");
            Toast.makeText(activity, "Sceneform requires Android N or later", Toast.LENGTH_LONG)
                    .show();
            activity.finish();
            return false;
        }
        String openGlVersionString =
                ((ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE))
                        .getDeviceConfigurationInfo()
                        .getGlEsVersion();
        if (Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) {
            Log.e(TAG, "Sceneform requires OpenGL ES 3.0 later");
            Toast.makeText(activity, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG)
                    .show();
            activity.finish();
            return false;
        }
        return true;
    }
Copy the code

PS

This is the official Demo. Next I will explain some customizations.